search_view.js 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. const { form, button, div, h2, p, section, input, label, select, option, img, audio: audioHyperaxe, video: videoHyperaxe, table, hr, hd, br, td, tr, th, a, span } = require("../server/node_modules/hyperaxe");
  2. const { template, i18n, userLink} = require('./main_views');
  3. const moment = require("../server/node_modules/moment");
  4. const { renderTextWithStyles } = require('../backend/renderTextWithStyles');
  5. const { renderUrl } = require('../backend/renderUrl');
  6. const { sanitizeHtml } = require('../backend/sanitizeHtml');
  7. const { config } = require("../server/SSB_server.js");
  8. const userId = config.keys.id;
  9. const decodeMaybe = (s) => {
  10. try { return decodeURIComponent(String(s || '')); } catch { return String(s || ''); }
  11. };
  12. const rewriteHashtagLinks = (html) => {
  13. const s = String(html || '');
  14. return s.replace(/href=(["'])(?:https?:\/\/[^"']+)?\/hashtag\/([^"'?#]+)([^"']*)\1/g, (m, q, tag, rest) => {
  15. const t = decodeMaybe(tag).replace(/^#/, '').trim().toLowerCase();
  16. const href = `/search?query=%23${encodeURIComponent(t)}`;
  17. return `href=${q}${href}${q}`;
  18. });
  19. };
  20. const searchView = ({ messages = [], blobs = {}, query = "", type = "", types = [], hashtag = null, results = {}, resultCount = "10" }) => {
  21. const searchInput = input({
  22. id: "search_query",
  23. name: "query",
  24. required: false,
  25. type: "search",
  26. value: query,
  27. placeholder: i18n.searchPlaceholder
  28. });
  29. searchInput.setAttribute("minlength", 3);
  30. const contentTypes = [
  31. "post", "about", "curriculum", "tribe", "market", "transfer", "feed", "votes",
  32. "report", "task", "event", "bookmark", "image", "audio", "video", "document", "torrent",
  33. "bankWallet", "bankClaim", "project", "job", "forum", "vote", "contact", "pub", "map", "shop", "shopProduct", "chat", "pad", "all"
  34. ];
  35. const filterSelect = select(
  36. {
  37. id: "search-type",
  38. name: "type",
  39. class: "input-select",
  40. style: "position:relative; z-index:10;"
  41. },
  42. contentTypes.map(type =>
  43. option({
  44. value: type === 'all' ? "" : type,
  45. selected: (types.length === 0 && type === 'all') || types.includes(type)
  46. }, i18n[type + "Label"] || type.toUpperCase())
  47. )
  48. );
  49. const resultsPerPageSelect = select(
  50. {
  51. id: "results-per-page",
  52. name: "resultsPerPage",
  53. class: "input-select",
  54. style: "position:relative; z-index:10;margin-left:10px;"
  55. },
  56. option({ value: "100", selected: resultCount === "100" ? "selected" : undefined }, "100"),
  57. option({ value: "50", selected: resultCount === "50" ? "selected" : undefined }, "50"),
  58. option({ value: "10", selected: resultCount === "10" ? "selected" : undefined }, "10"),
  59. option({ value: "all", selected: resultCount === "all" ? "selected" : undefined }, i18n.allTypesLabel)
  60. );
  61. const getViewDetailsActionForSearch = (type, contentId, content) => {
  62. switch (type) {
  63. case 'votes': return `/votes/${encodeURIComponent(contentId)}`;
  64. case 'transfer': return `/transfers/${encodeURIComponent(contentId)}`;
  65. case 'tribe': return `/tribe/${encodeURIComponent(contentId)}`;
  66. case 'about': return content && (content.about || content.author) ? `/inhabitant/${encodeURIComponent(content.about || content.author)}` : '#';
  67. case 'curriculum': return content && content.author ? `/inhabitant/${encodeURIComponent(content.author)}` : '#';
  68. case 'image': return `/images/${encodeURIComponent(contentId)}`;
  69. case 'audio': return `/audios/${encodeURIComponent(contentId)}`;
  70. case 'video': return `/videos/${encodeURIComponent(contentId)}`;
  71. case 'document': return `/documents/${encodeURIComponent(contentId)}`;
  72. case 'bookmark': return `/bookmarks/${encodeURIComponent(contentId)}`;
  73. case 'event': return `/events/${encodeURIComponent(contentId)}`;
  74. case 'task': return `/tasks/${encodeURIComponent(contentId)}`;
  75. case 'post': return `/thread/${encodeURIComponent(contentId)}#${encodeURIComponent(contentId)}`;
  76. case 'market': return `/market/${encodeURIComponent(contentId)}`;
  77. case 'report': return `/reports/${encodeURIComponent(contentId)}`;
  78. case 'project': return `/projects/${encodeURIComponent(contentId)}`;
  79. case 'job': return `/jobs/${encodeURIComponent(contentId)}`;
  80. case 'forum': return `/forum/${encodeURIComponent(contentId)}`;
  81. case 'vote': return content && content.vote && content.vote.link ? `/thread/${encodeURIComponent(content.vote.link)}#${encodeURIComponent(content.vote.link)}` : '#';
  82. case 'contact': return content && content.contact ? `/author/${encodeURIComponent(content.contact)}` : '#';
  83. case 'pub': return '#';
  84. case 'bankWallet': return `/banking`;
  85. case 'bankClaim': return `/banking`;
  86. case 'map': return `/maps/${encodeURIComponent(contentId)}`;
  87. case 'shop': return `/shops/${encodeURIComponent(contentId)}`;
  88. case 'shopProduct': return `/shops/product/${encodeURIComponent(contentId)}`;
  89. case 'chat': return `/chats/${encodeURIComponent(contentId)}`;
  90. case 'pad': return `/pads/${encodeURIComponent(contentId)}`;
  91. case 'torrent': return `/torrents/${encodeURIComponent(contentId)}`;
  92. case 'gameScore': return content && content.game ? `/games/${encodeURIComponent(content.game)}` : '/games';
  93. default: return '#';
  94. }
  95. };
  96. let hasDocument = false;
  97. const blobImg = (value) => {
  98. if (!value) return null;
  99. const s = String(value).trim().replace(/&/g, '&');
  100. const m = s.match(/!\[[^\]]*\]\(\s*(&[^)\s]+\.sha256)\s*\)/);
  101. const src = m ? m[1] : s;
  102. return src.startsWith('&') ? img({ src: `/blob/${encodeURIComponent(src)}`, class: 'search-result-image' }) : null;
  103. };
  104. const renderContentHtml = (content) => {
  105. switch (content.type) {
  106. case 'post':
  107. return div({ class: 'search-post' },
  108. content.contentWarning ? h2({ class: 'card-field' }, span({ class: 'card-value' }, content.contentWarning)) : null,
  109. content.text ? div({ class: 'card-field' }, span({ class: 'card-value', innerHTML: sanitizeHtml(content.text) })) : null
  110. );
  111. case 'about':
  112. return div({ class: 'search-about' },
  113. content.name ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.name + ':'), span({ class: 'card-value' }, content.name)) : null,
  114. content.description ? div({ class: 'card-field card-field-stacked' }, span({ class: 'card-label' }, i18n.description + ':'), span({ class: 'card-value' }, content.description)) : null,
  115. content.image ? img({ src: `/image/64/${encodeURIComponent(content.image)}` }) : null
  116. );
  117. case 'feed': {
  118. const rawText = typeof content.text === 'string' ? content.text.trim() : '';
  119. const htmlText = rawText ? rewriteHashtagLinks(renderTextWithStyles(rawText)) : '';
  120. const refeedsNum = Number(content.refeeds || 0) || 0;
  121. return div({ class: 'search-feed' },
  122. rawText ? div({ class: 'card-field' }, span({ class: 'card-value', innerHTML: sanitizeHtml(htmlText) })) : null,
  123. refeedsNum > 0
  124. ? h2({ class: 'card-field' },
  125. span({ class: 'card-label' }, i18n.tribeFeedRefeeds + ':'),
  126. span({ class: 'card-value' }, String(refeedsNum))
  127. )
  128. : null
  129. );
  130. }
  131. case 'event': {
  132. const rawEvDesc = content.description || '';
  133. const blobInEvDesc = rawEvDesc.match(/!\[[^\]]*\]\((&[^)\s]+\.sha256)\)/)?.[1] || null;
  134. const cleanEvDesc = rawEvDesc.replace(/!\[[^\]]*\]\(&[^)]+\.sha256\)/g, '').trim();
  135. return div({ class: 'search-event' },
  136. content.title ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.eventTitleLabel + ':'), span({ class: 'card-value' }, content.title)) : null,
  137. cleanEvDesc ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.searchDescription + ':'), span({ class: 'card-value' }, cleanEvDesc)) : null,
  138. blobImg(content.image || blobInEvDesc),
  139. content.date ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.eventDate + ':'), span({ class: 'card-value' }, new Date(content.date).toLocaleString())) : null,
  140. content.location ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.eventLocation + ':'), span({ class: 'card-value' }, content.location)) : null,
  141. content.isPublic ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.eventPrivacyLabel + ':'), span({ class: 'card-value' }, content.isPublic)) : null,
  142. content.status ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.eventStatus + ':'), span({ class: 'card-value' }, content.status)) : null,
  143. content.eventUrl ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.eventUrlLabel + ':'), span({ class: 'card-value' }, a({ href: content.eventUrl, target: '_blank' }, content.eventUrl))) : null,
  144. content.price ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.eventPrice + ':'), span({ class: 'card-value' }, content.price)) : null,
  145. content.tags && content.tags.length
  146. ? div({ class: 'card-tags' }, content.tags.map(tag =>
  147. a({ href: `/search?query=%23${encodeURIComponent(tag)}`, class: 'tag-link' }, `#${tag}`)
  148. ))
  149. : null
  150. );
  151. }
  152. case 'votes':
  153. const votesList = content.votes && typeof content.votes === 'object'
  154. ? Object.entries(content.votes).map(([option, count]) => ({ option, count }))
  155. : [];
  156. return div({ class: 'search-vote' },
  157. br(),
  158. content.question ? div({ class: 'card-field' },
  159. span({ class: 'card-label' }, i18n.voteQuestionLabel + ':' ),
  160. span({ class: 'card-value' }, content.question)
  161. ) : null,
  162. content.status ? div({ class: 'card-field' },
  163. span({ class: 'card-label' }, i18n.voteStatus + ':' ),
  164. span({ class: 'card-value' }, content.status)
  165. ) : null,
  166. content.deadline ? div({ class: 'card-field' },
  167. span({ class: 'card-label' }, i18n.voteDeadline + ':' ),
  168. span({ class: 'card-value' }, content.deadline ? new Date(content.deadline).toLocaleString() : '')
  169. ) : null,
  170. div({ class: 'card-field' },
  171. span({ class: 'card-label' }, i18n.voteTotalVotes + ':' ),
  172. span({ class: 'card-value' }, content.totalVotes !== undefined ? content.totalVotes : '0')
  173. ),
  174. br(),
  175. votesList.length > 0 ? div({ class: 'card-votes' },
  176. table(
  177. tr(...votesList.map(({ option }) => th(i18n[option] || option))),
  178. tr(...votesList.map(({ count }) => td(count)))
  179. )
  180. ) : null
  181. );
  182. case 'tribe':
  183. return div({ class: 'search-tribe' },
  184. content.title ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.title + ':'), span({ class: 'card-value' }, content.title)) : null,
  185. (() => { const s = String(content.image || '').trim().replace(/&/g, '&'); const m = s.match(/!\[[^\]]*\]\(\s*(&[^)\s]+\.sha256)\s*\)/); const src = m ? m[1] : s; return src.startsWith('&') ? img({ src: `/blob/${encodeURIComponent(src)}`, class: 'feed-image' }) : img({ src: '/assets/images/default-tribe.png', class: 'feed-image' }); })(),
  186. content.description ? div({ class: 'card-field card-field-stacked' }, span({ class: 'card-label' }, i18n.searchDescription + ':'), span({ class: 'card-value' }, ...renderUrl(content.description))) : null,
  187. content.location ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.tribeLocationLabel + ':'), span({ class: 'card-value' }, ...renderUrl(content.location))) : null,
  188. div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.tribeIsAnonymousLabel + ':'), span({ class: 'card-value' }, content.isAnonymous ? i18n.tribePrivate : i18n.tribePublic)),
  189. content.inviteMode ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.tribeModeLabel + ':'), span({ class: 'card-value' }, String(content.inviteMode).toUpperCase())) : null,
  190. div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.tribeLARPLabel + ':'), span({ class: 'card-value' }, content.isLARP ? i18n.tribeYes : i18n.tribeNo)),
  191. Array.isArray(content.members)
  192. ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.tribeMembersCount + ':'), span({ class: 'card-value' }, String(content.members.length)))
  193. : null,
  194. content.tags && content.tags.length
  195. ? div({ class: 'card-tags' }, content.tags.map(tag =>
  196. a({ href: `/search?query=%23${encodeURIComponent(tag)}`, class: 'tag-link' }, `#${tag}`)
  197. ))
  198. : null
  199. );
  200. case 'audio':
  201. return content.url ? div({ class: 'search-audio' },
  202. content.title ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.audioTitleLabel + ':'), span({ class: 'card-value' }, content.title)) : null,
  203. content.description ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.audioDescriptionLabel + ':'), span({ class: 'card-value' }, content.description)) : null,
  204. br(),
  205. audioHyperaxe({ controls: true, src: `/blob/${encodeURIComponent(content.url)}`, type: content.mimeType, preload: 'metadata' }),
  206. br(),
  207. content.tags && content.tags.length
  208. ? div({ class: 'card-tags' }, content.tags.map(tag =>
  209. a({ href: `/search?query=%23${encodeURIComponent(tag)}`, class: 'tag-link' }, `#${tag}`)
  210. ))
  211. : null
  212. ) : null;
  213. case 'image':
  214. return content.url ? div({ class: 'search-image' },
  215. content.title ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.imageTitleLabel + ':'), span({ class: 'card-value' }, content.title)) : null,
  216. content.description ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.imageDescriptionLabel + ':'), span({ class: 'card-value' }, content.description)) : null,
  217. content.meme ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.trendingCategory + ':'), span({ class: 'card-value' }, i18n.meme)) : null,
  218. br(),
  219. img({ src: `/blob/${encodeURIComponent(content.url)}` }),
  220. br(),
  221. content.tags && content.tags.length
  222. ? div({ class: 'card-tags' }, content.tags.map(tag =>
  223. a({ href: `/search?query=%23${encodeURIComponent(tag)}`, class: 'tag-link' }, `#${tag}`)
  224. ))
  225. : null
  226. ) : null;
  227. case 'video':
  228. return content.url ? div({ class: 'search-video' },
  229. content.title ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.videoTitleLabel + ':'), span({ class: 'card-value' }, content.title)) : null,
  230. content.description ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.videoDescriptionLabel + ':'), span({ class: 'card-value' }, content.description)) : null,
  231. br(),
  232. videoHyperaxe({ controls: true, src: `/blob/${encodeURIComponent(content.url)}`, type: content.mimeType || 'video/mp4', width: '640', height: '360' }),
  233. br(),
  234. content.tags && content.tags.length
  235. ? div({ class: 'card-tags' }, content.tags.map(tag =>
  236. a({ href: `/search?query=%23${encodeURIComponent(tag)}`, class: 'tag-link' }, `#${tag}`)
  237. ))
  238. : null
  239. ) : null;
  240. case 'document':
  241. return div({ class: 'search-document' },
  242. content.title ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.documentTitleLabel + ':'), span({ class: 'card-value' }, content.title)) : null,
  243. br(),
  244. content.description ? div({ class: 'card-field' }, span({ class: 'card-value' }, content.description)) : null,
  245. br(),
  246. div({
  247. id: `pdf-container-${content.key || content.url}`,
  248. class: 'pdf-viewer-container',
  249. 'data-pdf-url': `/blob/${encodeURIComponent(content.url)}`
  250. }),
  251. br(),
  252. content.tags && content.tags.length
  253. ? div({ class: 'card-tags' }, content.tags.map(tag =>
  254. a({ href: `/search?query=%23${encodeURIComponent(tag)}`, class: 'tag-link' }, `#${tag}`)
  255. ))
  256. : null
  257. );
  258. case 'torrent':
  259. return div({ class: 'search-torrent' },
  260. content.title ? div({ class: 'card-field' }, span({ class: 'card-label' }, (i18n.torrentTitleLabel || i18n.title) + ':'), span({ class: 'card-value' }, content.title)) : null,
  261. content.description ? div({ class: 'card-field' }, span({ class: 'card-label' }, (i18n.torrentDescriptionLabel || i18n.searchDescription) + ':'), span({ class: 'card-value' }, content.description)) : null,
  262. content.size ? div({ class: 'card-field' }, span({ class: 'card-label' }, (i18n.torrentSizeLabel || 'Size') + ':'), span({ class: 'card-value' }, String(content.size))) : null,
  263. content.tags && content.tags.length
  264. ? div({ class: 'card-tags' }, content.tags.map(tag =>
  265. a({ href: `/search?query=%23${encodeURIComponent(tag)}`, class: 'tag-link' }, `#${tag}`)
  266. ))
  267. : null
  268. );
  269. case 'market':
  270. return div({ class: 'search-market' },
  271. content.title ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.title + ':'), span({ class: 'card-value' }, content.title)) : null,
  272. content.description ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.searchDescription + ':'), span({ class: 'card-value' }, content.description)) : null,
  273. content.item_type ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.marketItemType + ':'), span({ class: 'card-value' }, content.item_type.toUpperCase())) : null,
  274. content.status ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.marketItemCondition + ':'), span({ class: 'card-value' }, content.status)) : null,
  275. content.deadline ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.marketItemDeadline + ':'), span({ class: 'card-value' }, new Date(content.deadline).toLocaleString())) : null,
  276. br(),
  277. blobImg(content.image),
  278. br(),
  279. content.seller ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.marketItemSeller + ':'), span({ class: 'card-value' }, userLink(content.seller))) : null,
  280. content.stock ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.marketItemStock + ':'), span({ class: 'card-value' }, content.stock || 'N/A')) : null,
  281. content.price ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.searchPriceLabel + ':'), span({ class: 'card-value' }, `${content.price} ECO`)) : null,
  282. content.condition ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.searchDescription + ':'), span({ class: 'card-value' }, content.condition)) : null,
  283. content.includesShipping ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.marketItemIncludesShipping + ':'), span({ class: 'card-value' }, `${content.includesShipping ? i18n.YESLabel : i18n.NOLabel}`)) : null,
  284. content.auctions_poll && content.auctions_poll.length > 0
  285. ? div({ class: 'auction-info' },
  286. p(i18n.marketAuctionBids),
  287. table({ class: 'auction-bid-table' },
  288. tr(
  289. th(i18n.marketAuctionBidTime),
  290. th(i18n.marketAuctionUser),
  291. th(i18n.marketAuctionBidAmount)
  292. ),
  293. content.auctions_poll.map(bid => {
  294. const [userId, bidAmount, bidTime] = bid.split(':');
  295. return tr(
  296. td(moment(bidTime).format('YYYY-MM-DD HH:mm:ss')),
  297. td(a({ href: `/author/${encodeURIComponent(userId)}` }, userId)),
  298. td(`${parseFloat(bidAmount).toFixed(6)} ECO`)
  299. );
  300. })
  301. )
  302. )
  303. : null,
  304. content.tags && content.tags.length
  305. ? div({ class: 'card-tags' }, content.tags.map(tag =>
  306. a({ href: `/search?query=%23${encodeURIComponent(tag)}`, class: 'tag-link' }, `#${tag}`)
  307. ))
  308. : null
  309. );
  310. case 'bookmark':
  311. return div({ class: 'search-bookmark' },
  312. content.url ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.bookmarkUrlLabel + ':'), span({ class: 'card-value' }, a({ href: content.url, target: '_blank' }, content.url))) : null,
  313. content.description ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.searchDescription + ':'), span({ class: 'card-value' }, content.description)) : null,
  314. content.category ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.bookmarkCategory + ':'), span({ class: 'card-value' }, content.category)) : null,
  315. content.lastVisit ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.bookmarkLastVisit + ':'), span({ class: 'card-value' }, new Date(content.lastVisit).toLocaleString())) : null,
  316. content.tags && content.tags.length
  317. ? div({ class: 'card-tags' }, content.tags.map(tag =>
  318. a({ href: `/search?query=%23${encodeURIComponent(tag)}`, class: 'tag-link' }, `#${tag}`)
  319. ))
  320. : null
  321. );
  322. case 'task':
  323. return div({ class: 'search-task' },
  324. content.title ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.taskTitleLabel + ':'), span({ class: 'card-value' }, content.title)) : null,
  325. content.description ? div({ class: 'card-field', style: 'display:flex;flex-direction:column;' }, span({ class: 'card-label' }, i18n.searchDescription + ':'), p({ class: 'card-value', style: 'white-space:pre-wrap;margin-top:4px;' }, content.description)) : null,
  326. content.location ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.searchLocationLabel + ':'), span({ class: 'card-value' }, content.location)) : null,
  327. content.status ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.searchStatusLabel + ':'), span({ class: 'card-value' }, content.status)) : null,
  328. content.priority ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.searchPriorityLabel + ':'), span({ class: 'card-value' }, content.priority)) : null,
  329. typeof content.isPublic === 'boolean' ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.searchIsPublicLabel + ':'), span({ class: 'card-value' }, content.isPublic ? i18n.YESLabel : i18n.NOLabel)) : null,
  330. content.startTime ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.taskStartTimeLabel + ':'), span({ class: 'card-value' }, new Date(content.startTime).toLocaleString())) : null,
  331. content.endTime ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.taskEndTimeLabel + ':'), span({ class: 'card-value' }, new Date(content.endTime).toLocaleString())) : null,
  332. Array.isArray(content.assignees) ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.taskAssignees + ':'), span({ class: 'card-value' }, content.assignees.length)) : null,
  333. content.tags && content.tags.length
  334. ? div({ class: 'card-tags' }, content.tags.map(tag =>
  335. a({ href: `/search?query=%23${encodeURIComponent(tag)}`, class: 'tag-link' }, `#${tag}`)
  336. ))
  337. : null
  338. );
  339. case 'report':
  340. return div({ class: 'search-report' },
  341. content.title ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.reportsTitleLabel + ':'), span({ class: 'card-value' }, content.title)) : null,
  342. content.status ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.searchStatusLabel + ':'), span({ class: 'card-value' }, content.status)) : null,
  343. content.severity ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.reportsSeverity + ':'), span({ class: 'card-value' }, content.severity)) : null,
  344. content.category ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.searchCategoryLabel + ':'), span({ class: 'card-value' }, content.category)) : null,
  345. content.description ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.searchDescription + ':'), span({ class: 'card-value' }, content.description)) : null,
  346. br(),
  347. content.image ? img({ src: `/blob/${encodeURIComponent(content.image)}` }) : null,
  348. br(),
  349. typeof content.confirmations === 'number' ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.reportsConfirmations + ':'), span({ class: 'card-value' }, content.confirmations)) : null,
  350. content.tags && content.tags.length
  351. ? div({ class: 'card-tags' }, content.tags.map(tag =>
  352. a({ href: `/search?query=%23${encodeURIComponent(tag)}`, class: 'tag-link' }, `#${tag}`)
  353. ))
  354. : null
  355. );
  356. case 'transfer':
  357. return div({ class: 'search-transfer' },
  358. content.concept ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.transfersConcept + ':'), span({ class: 'card-value' }, content.concept)) : null,
  359. content.deadline ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.transfersDeadline + ':'), span({ class: 'card-value' }, content.deadline)) : null,
  360. content.status ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.transfersStatus + ':'), span({ class: 'card-value' }, content.status)) : null,
  361. content.amount ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.transfersAmount + ':'), span({ class: 'card-value' }, content.amount)) : null,
  362. br(),
  363. content.from ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.transfersFrom + ':'), span({ class: 'card-value' }, userLink(content.from))) : null,
  364. content.to ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.transfersTo + ':'), span({ class: 'card-value' }, userLink(content.to))) : null,
  365. br(),
  366. content.confirmedBy && content.confirmedBy.length
  367. ? h2({ class: 'card-field' }, span({ class: 'card-label' }, i18n.transfersConfirmations + ':'), span({ class: 'card-value' }, content.confirmedBy.length))
  368. : null,
  369. content.tags && content.tags.length
  370. ? div({ class: 'card-tags' }, content.tags.map(tag =>
  371. a({ href: `/search?query=%23${encodeURIComponent(tag)}`, class: 'tag-link' }, `#${tag}`)
  372. ))
  373. : null
  374. );
  375. case 'curriculum':
  376. return div({ class: 'search-curriculum' },
  377. content.name ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.cvNameLabel + ':'), span({ class: 'card-value' }, content.name)) : null,
  378. content.description ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.cvDescriptionLabel + ':'), span({ class: 'card-value' }, content.description)) : null,
  379. content.photo ? img({ src: `/blob/${encodeURIComponent(content.photo)}`, class: 'curriculum-photo' }) : null,
  380. content.location ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.cvLocationLabel + ':'), span({ class: 'card-value' }, content.location)) : null,
  381. content.status ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.cvStatusLabel + ':'), span({ class: 'card-value' }, content.status)) : null,
  382. content.preferences ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.cvPreferencesLabel + ':'), span({ class: 'card-value' }, content.preferences)) : null,
  383. Array.isArray(content.personalSkills) && content.personalSkills.length
  384. ? div({ class: 'card-field' }, content.personalSkills.map(skill =>
  385. a({ href: `/search?query=%23${encodeURIComponent(skill)}`, class: 'tag-link' }, `#${skill}`)
  386. )) : null,
  387. Array.isArray(content.personalExperiences) && content.personalExperiences.length
  388. ? div({ class: 'card-field' }, content.personalExperiences.map(exp => p(exp))) : null,
  389. Array.isArray(content.oasisExperiences) && content.oasisExperiences.length
  390. ? div({ class: 'card-field' }, content.oasisExperiences.map(exp => p(exp))) : null,
  391. Array.isArray(content.oasisSkills) && content.oasisSkills.length
  392. ? div({ class: 'card-field' }, content.oasisSkills.map(skill => p(skill))) : null,
  393. Array.isArray(content.educationExperiences) && content.educationExperiences.length
  394. ? div({ class: 'card-field' }, content.educationExperiences.map(exp => p(exp))) : null,
  395. Array.isArray(content.educationalSkills) && content.educationalSkills.length
  396. ? div({ class: 'card-field' }, content.educationalSkills.map(skill =>
  397. a({ href: `/search?query=%23${encodeURIComponent(skill)}`, class: 'tag-link' }, `#${skill}`)
  398. )) : null,
  399. Array.isArray(content.languages) && content.languages.length
  400. ? div({ class: 'card-field' }, content.languages.map(lang => p(lang))) : null,
  401. Array.isArray(content.professionalExperiences) && content.professionalExperiences.length
  402. ? div({ class: 'card-field' }, content.professionalExperiences.map(exp => p(exp))) : null,
  403. Array.isArray(content.professionalSkills) && content.professionalSkills.length
  404. ? div({ class: 'card-field' }, content.professionalSkills.map(skill => p(skill))) : null
  405. );
  406. case 'bankWallet':
  407. return div({ class: 'search-bank-wallet' },
  408. content.address ? div({ class: 'card-field' },
  409. span({ class: 'card-label' }, i18n.bankWalletConnected + ':' ),
  410. span({ class: 'card-value' }, content.address)
  411. ) : null
  412. );
  413. case 'bankClaim':
  414. return div({ class: 'search-bank-claim' },
  415. div({ class: 'card-field' },
  416. span({ class: 'card-label' }, i18n.bankUbiReceived + ':' ),
  417. span({ class: 'card-value' }, `${Number(content.amount || 0).toFixed(6)} ECO`)
  418. ),
  419. content.epochId ? div({ class: 'card-field' },
  420. span({ class: 'card-label' }, i18n.bankEpochShort + ':' ),
  421. span({ class: 'card-value' }, content.epochId)
  422. ) : null,
  423. content.allocationId ? div({ class: 'card-field' },
  424. span({ class: 'card-label' }, i18n.bankAllocId + ':' ),
  425. span({ class: 'card-value' }, content.allocationId)
  426. ) : null,
  427. content.txid ? div({ class: 'card-field' },
  428. span({ class: 'card-label' }, i18n.bankTx + ':' ),
  429. a({ href: `https://ecoin.03c8.net/blockexplorer/search?q=${content.txid}`, target: '_blank' }, content.txid)
  430. ) : null
  431. );
  432. case 'job':
  433. return div({ class: 'search-job' },
  434. content.title ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.title + ':'), span({ class: 'card-value' }, content.title)) : null,
  435. content.salary ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.jobSalary + ':'), span({ class: 'card-value' }, `${content.salary} ECO`)) : null,
  436. content.status ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.jobStatus + ':'), span({ class: 'card-value' }, content.status.toUpperCase())) : null,
  437. content.job_type ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.jobType + ':'), span({ class: 'card-value' }, content.job_type.toUpperCase())) : null,
  438. content.location ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.jobLocation + ':'), span({ class: 'card-value' }, String(content.location).toUpperCase())) : null,
  439. content.vacants ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.jobVacants + ':'), span({ class: 'card-value' }, content.vacants)) : null,
  440. Array.isArray(content.subscribers) ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.jobSubscribers + ':'), span({ class: 'card-value' }, `${content.subscribers.length}`)) : null
  441. );
  442. case 'forum':
  443. return div({ class: 'search-forum' },
  444. content.title ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.title + ':'), span({ class: 'card-value' }, content.title)) : null,
  445. content.category ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.searchCategoryLabel + ':'), span({ class: 'card-value' }, content.category)) : null,
  446. content.text ? div({ class: 'card-field card-field-stacked' }, span({ class: 'card-label' }, i18n.searchDescription + ':'), span({ class: 'card-value' }, content.text)) : null
  447. );
  448. case 'vote':
  449. return div({ class: 'search-vote-link' },
  450. content.vote && content.vote.link ? p(a({ href: `/thread/${encodeURIComponent(content.vote.link)}#${encodeURIComponent(content.vote.link)}`, class: 'activityVotePost' }, content.vote.link)) : null
  451. );
  452. case 'contact':
  453. return div({ class: 'search-contact' },
  454. content.contact ? p(a({ href: `/author/${encodeURIComponent(content.contact)}`, class: 'activitySpreadInhabitant2' }, content.contact)) : null
  455. );
  456. case 'pub':
  457. return div({ class: 'search-pub' },
  458. content.address && content.address.key ? p(a({ href: `/author/${encodeURIComponent(content.address.key)}`, class: 'activitySpreadInhabitant2' }, content.address.key)) : null
  459. );
  460. case 'shop':
  461. return div({ class: 'search-shop' },
  462. content.title ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.title + ':'), span({ class: 'card-value' }, content.title)) : null,
  463. blobImg(content.image),
  464. content.shortDescription ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.searchDescription + ':'), span({ class: 'card-value' }, content.shortDescription)) : null,
  465. content.visibility ? div({ class: 'card-field' }, span({ class: 'card-label' }, (i18n.shopStatus || 'STATUS') + ':'), span({ class: 'card-value' }, content.visibility)) : null,
  466. content.location ? div({ class: 'card-field' }, span({ class: 'card-label' }, (i18n.searchLocationLabel || 'LOCATION') + ':'), span({ class: 'card-value' }, content.location)) : null,
  467. content.tags && content.tags.length
  468. ? div({ class: 'card-tags' }, content.tags.map(tag => a({ href: `/search?query=%23${encodeURIComponent(tag)}`, class: 'tag-link' }, `#${tag}`)))
  469. : null
  470. );
  471. case 'shopProduct':
  472. return div({ class: 'search-shop-product' },
  473. content.title ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.title + ':'), span({ class: 'card-value' }, content.title)) : null,
  474. blobImg(content.image),
  475. content.description ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.searchDescription + ':'), span({ class: 'card-value' }, content.description)) : null,
  476. content.price ? div({ class: 'card-field' }, span({ class: 'card-label' }, (i18n.searchPriceLabel || 'PRICE') + ':'), span({ class: 'card-value' }, `${content.price} ECO`)) : null,
  477. content.stock !== undefined ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.marketItemStock + ':'), span({ class: 'card-value' }, content.stock)) : null
  478. );
  479. case 'chat': {
  480. const chatInviteOnly = content.status === 'INVITE-ONLY' && content.author !== userId && !(Array.isArray(content.members) && content.members.includes(userId));
  481. if (chatInviteOnly) return div({ class: 'search-chat' },
  482. content.title ? div({ class: 'card-field' }, span({ class: 'card-label' }, (i18n.chatsTitle || 'Chat') + ':'), span({ class: 'card-value' }, content.title)) : null,
  483. div({ class: 'card-field' }, span({ class: 'card-label' }, (i18n.chatStatus || 'STATUS') + ':'), span({ class: 'card-value' }, i18n.chatStatusInviteOnly || 'INVITE-ONLY'))
  484. );
  485. return div({ class: 'search-chat' },
  486. content.title ? div({ class: 'card-field' }, span({ class: 'card-label' }, (i18n.chatsTitle || 'Chat') + ':'), span({ class: 'card-value' }, content.title)) : null,
  487. content.description ? div({ class: 'card-field' }, span({ class: 'card-label' }, (i18n.chatDescription || 'Description') + ':'), span({ class: 'card-value' }, content.description)) : null,
  488. content.category ? div({ class: 'card-field' }, span({ class: 'card-label' }, (i18n.chatCategoryLabel || 'Category') + ':'), span({ class: 'card-value' }, content.category)) : null,
  489. content.status ? div({ class: 'card-field' }, span({ class: 'card-label' }, (i18n.chatStatus || 'STATUS') + ':'), span({ class: 'card-value' }, content.status)) : null
  490. );
  491. }
  492. case 'pad': {
  493. const padInviteOnly = content.status === 'INVITE-ONLY' && content.author !== userId && !(Array.isArray(content.members) && content.members.includes(userId));
  494. if (padInviteOnly) return div({ class: 'search-pad' },
  495. content.title ? div({ class: 'card-field' }, span({ class: 'card-label' }, (i18n.padTitle || 'Pad') + ':'), span({ class: 'card-value' }, content.title)) : null,
  496. div({ class: 'card-field' }, span({ class: 'card-label' }, (i18n.padStatusLabel || 'Status') + ':'), span({ class: 'card-value' }, i18n.padStatusInviteOnly || 'INVITE-ONLY'))
  497. );
  498. return div({ class: 'search-pad' },
  499. content.title ? div({ class: 'card-field' }, span({ class: 'card-label' }, (i18n.padTitle || 'Pad') + ':'), span({ class: 'card-value' }, content.title)) : null,
  500. content.status ? div({ class: 'card-field' }, span({ class: 'card-label' }, (i18n.padStatusLabel || 'Status') + ':'), span({ class: 'card-value' }, content.status)) : null,
  501. content.deadline ? div({ class: 'card-field' }, span({ class: 'card-label' }, (i18n.padDeadlineLabel || 'Deadline') + ':'), span({ class: 'card-value' }, content.deadline)) : null
  502. );
  503. }
  504. case 'gameScore':
  505. return div({ class: 'search-game' },
  506. content.game ? div({ class: 'game-row' },
  507. img({ src: `/game-assets/${content.game}/thumbnail.svg`, alt: content.game, class: 'game-scoring-thumb', loading: 'lazy' }),
  508. div({ class: 'game-row-body' },
  509. h2({ class: 'game-card-title' }, content.game.charAt(0).toUpperCase() + content.game.slice(1)),
  510. content.score !== undefined ? div({ class: 'card-field' }, span({ class: 'card-label' }, (i18n.gamesHallScore || 'Score') + ':'), span({ class: 'card-value' }, String(content.score))) : null,
  511. a({ href: `/games/${encodeURIComponent(content.game)}`, class: 'filter-btn' }, i18n.gamesPlayButton || 'PLAY!')
  512. )
  513. ) : null
  514. );
  515. case 'map':
  516. return div({ class: 'search-map' },
  517. content.title ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.title + ':'), span({ class: 'card-value' }, content.title)) : null,
  518. content.description ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.mapDescriptionLabel + ':'), span({ class: 'card-value' }, content.description)) : null,
  519. content.lat && content.lng ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.mapLocationTitle + ':'), span({ class: 'card-value' }, `${content.lat}, ${content.lng}`)) : null,
  520. content.mapType ? div({ class: 'card-field' }, span({ class: 'card-label' }, i18n.mapTypeLabel + ':'), span({ class: 'card-value' }, content.mapType)) : null,
  521. content.tags && content.tags.length
  522. ? div({ class: 'card-tags' }, content.tags.map(tag =>
  523. a({ href: `/search?query=%23${encodeURIComponent(tag)}`, class: 'tag-link' }, `#${tag}`)
  524. ))
  525. : null
  526. );
  527. default:
  528. return div({ class: 'styled-text', innerHTML: sanitizeHtml(renderTextWithStyles(content.text || content.description || content.title || '[no content]')) });
  529. }
  530. };
  531. const resultSection = Object.entries(results).length > 0
  532. ? Object.entries(results).map(([key, msgs]) =>
  533. div(
  534. { class: "search-result-group" },
  535. h2(i18n[key + "Label"] || key.toUpperCase()),
  536. ...msgs.map((msg) => {
  537. const content = msg.value.content || {};
  538. const created = new Date(msg.timestamp).toLocaleString();
  539. if (content.type === 'document') hasDocument = true;
  540. const contentHtml = renderContentHtml(content);
  541. let author;
  542. let authorUrl = '#';
  543. if (content.type === 'market') {
  544. author = content.seller || i18n.anonymous || "Anonymous";
  545. authorUrl = `/author/${encodeURIComponent(content.seller)}`;
  546. } else if (content.type === 'event') {
  547. author = content.organizer || i18n.anonymous || "Anonymous";
  548. authorUrl = `/author/${encodeURIComponent(content.organizer)}`;
  549. } else if (content.type === 'transfer') {
  550. author = content.from || i18n.anonymous || "Anonymous";
  551. authorUrl = `/author/${encodeURIComponent(content.from)}`;
  552. } else if (content.type === 'post' || content.type === 'about') {
  553. author = msg.value.author || i18n.anonymous || "Anonymous";
  554. authorUrl = `/author/${encodeURIComponent(msg.value.author)}`;
  555. } else if (content.type === 'report') {
  556. author = content.author || i18n.anonymous || "Anonymous";
  557. authorUrl = `/author/${encodeURIComponent(content.author || 'Anonymous')}`;
  558. } else if (content.type === 'votes') {
  559. author = content.createdBy || i18n.anonymous || "Anonymous";
  560. authorUrl = `/author/${encodeURIComponent(content.createdBy || 'Anonymous')}`;
  561. } else if (content.type === 'shop' || content.type === 'shopProduct' || content.type === 'chat' || content.type === 'gameScore') {
  562. author = content.author || content.player || msg.value.author || i18n.anonymous || "Anonymous";
  563. authorUrl = `/author/${encodeURIComponent(content.author || content.player || msg.value.author || 'Anonymous')}`;
  564. } else {
  565. author = content.author;
  566. authorUrl = `/author/${encodeURIComponent(content.author || 'Anonymous')}`;
  567. }
  568. const contentId = msg.key;
  569. const detailsButton = form({ method: "GET", action: getViewDetailsActionForSearch(content.type, contentId, content) },
  570. button({ type: "submit", class: "filter-btn" }, i18n.viewDetails)
  571. );
  572. return div({ class: 'result-item' }, [
  573. detailsButton,
  574. br(),
  575. contentHtml,
  576. author
  577. ? p({ class: 'card-footer' },
  578. span({ class: 'date-link' }, `${created} ${i18n.performed} `),
  579. (authorUrl && authorUrl !== '#' && authorUrl.startsWith('/author/'))
  580. ? userLink(decodeURIComponent(authorUrl.replace(/^\/author\//, '')))
  581. : a({ href: authorUrl, class: 'user-link' }, `${author}`)
  582. ): null,
  583. ]);
  584. })
  585. )
  586. )
  587. : div({ class: 'no-results' }, p(i18n.noResultsFound));
  588. let html = template(
  589. hashtag ? `#${hashtag}` : i18n.searchTitle,
  590. section(
  591. div({ class: "tags-header" },
  592. h2(hashtag ? `#${hashtag}` : i18n.searchTitle),
  593. p(hashtag ? i18n.hashtagDescription : i18n.searchDescriptionLabel)
  594. ),
  595. form(
  596. { action: "/search", method: "POST", class: "search-form" },
  597. div({ class: "search-filters-row" },
  598. table({ class: "search-filters-table" },
  599. tr(
  600. td({ class: 'card-label' }, label({ for: "search_from" }, i18n.searchFromLabel || "From")),
  601. td(input({ id: "search_from", type: "datetime-local", name: "from" }))
  602. ),
  603. tr(
  604. td({ class: 'card-label' }, label({ for: "search_to" }, i18n.searchToLabel || "To")),
  605. td(input({ id: "search_to", type: "datetime-local", name: "to" }))
  606. ),
  607. tr(
  608. td({ class: 'card-label' }, label({ for: "search_inhabitant" }, "Oasis ID")),
  609. td(input({ id: "search_inhabitant", type: "text", name: "inhabitant", placeholder: "@...=.ed25519", pattern: "@[A-Za-z0-9+/_\\-]{43}=\\.ed25519", maxlength: 56, class: "search-oasis-id" }))
  610. )
  611. ),
  612. table({ class: "search-filters-table" },
  613. tr(
  614. td({ class: 'card-label' }, label({ for: "search_query" }, i18n.searchQueryLabel || "Query")),
  615. td(searchInput)
  616. ),
  617. tr(
  618. td({ class: 'card-label' }, label({ for: "results-per-page" }, i18n.searchPerPageLabel || "Results per page")),
  619. td(resultsPerPageSelect)
  620. )
  621. )
  622. ),
  623. div({ class: "search-submit-row" },
  624. button({ type: "submit" }, i18n.searchSubmit)
  625. )
  626. )
  627. ),
  628. section(resultSection)
  629. );
  630. if (hasDocument) {
  631. html += `
  632. <script type="module" src="/js/pdf.min.mjs"></script>
  633. <script src="/js/pdf-viewer.js"></script>
  634. `;
  635. }
  636. return html;
  637. };
  638. exports.searchView = searchView;