video_view.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. const { form, button, div, h2, p, section, input, label, br, a, video: videoHyperaxe } = require("../server/node_modules/hyperaxe");
  2. const moment = require("../server/node_modules/moment");
  3. const { template, i18n } = require('./main_views');
  4. const { config } = require('../server/SSB_server.js');
  5. const userId = config.keys.id
  6. const getFilteredVideos = (filter, videos, userId) => {
  7. const now = Date.now();
  8. let filtered =
  9. filter === 'mine' ? videos.filter(v => v.author === userId) :
  10. filter === 'recent' ? videos.filter(v => new Date(v.createdAt).getTime() >= now - 86400000) :
  11. filter === 'top' ? [...videos].sort((a, b) => {
  12. const sumA = Object.values(a.opinions || {}).reduce((s, n) => s + (n || 0), 0);
  13. const sumB = Object.values(b.opinions || {}).reduce((s, n) => s + (n || 0), 0);
  14. return sumB - sumA;
  15. }) :
  16. videos;
  17. return filtered.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
  18. };
  19. const renderVideoActions = (filter, video) => {
  20. return filter === 'mine' ? div({ class: "video-actions" },
  21. form({ method: "GET", action: `/videos/edit/${encodeURIComponent(video.key)}` },
  22. button({ class: "update-btn", type: "submit" }, i18n.videoUpdateButton)
  23. ),
  24. form({ method: "POST", action: `/videos/delete/${encodeURIComponent(video.key)}` },
  25. button({ class: "delete-btn", type: "submit" }, i18n.videoDeleteButton)
  26. )
  27. ) : null;
  28. };
  29. const renderVideoList = (filteredVideos, filter) => {
  30. return filteredVideos.length > 0
  31. ? filteredVideos.map(video =>
  32. div({ class: "video-item" },
  33. renderVideoActions(filter, video),
  34. form({ method: "GET", action: `/videos/${encodeURIComponent(video.key)}` },
  35. button({ type: "submit", class: "filter-btn" }, i18n.viewDetails)),
  36. br,
  37. video.url
  38. ? div({ class: "video-container" },
  39. videoHyperaxe({
  40. controls: true,
  41. src: `/blob/${encodeURIComponent(video.url)}`,
  42. type: video.mimeType,
  43. preload: 'metadata',
  44. width: '640',
  45. height: '360'
  46. })
  47. )
  48. : p(i18n.videoNoFile),
  49. p(`${i18n.videoCreatedAt}: ${moment(video.createdAt).format('YYYY/MM/DD HH:mm:ss')}`),
  50. p(`${i18n.videoAuthor}: `, a({ href: `/author/${encodeURIComponent(video.author)}` }, video.author)),
  51. video.title?.trim() ? h2(video.title) : null,
  52. video.description?.trim() ? p(video.description) : null,
  53. video.tags?.length
  54. ? div(video.tags.map(tag =>
  55. a({ href: `/search?query=%23${encodeURIComponent(tag)}`, class: "tag-link", style: "margin-right: 0.8em; margin-bottom: 0.5em;" }, `#${tag}`)
  56. ))
  57. : null,
  58. div({ class: "voting-buttons" },
  59. ['interesting','necessary','funny','disgusting','sensible',
  60. 'propaganda','adultOnly','boring','confusing','inspiring','spam']
  61. .map(category =>
  62. form({ method: "POST", action: `/videos/opinions/${encodeURIComponent(video.key)}/${category}` },
  63. button({ class: "vote-btn" },
  64. `${i18n[`vote${category.charAt(0).toUpperCase() + category.slice(1)}`]} [${video.opinions?.[category] || 0}]`
  65. )
  66. )
  67. )
  68. )
  69. )
  70. )
  71. : div(i18n.noVideos);
  72. };
  73. const renderVideoForm = (filter, videoId, videoToEdit) => {
  74. return div({ class: "div-center video-form" },
  75. form({
  76. action: filter === 'edit' ? `/videos/update/${encodeURIComponent(videoId)}` : "/videos/create",
  77. method: "POST", enctype: "multipart/form-data"
  78. },
  79. label(i18n.videoFileLabel), br(),
  80. input({ type: "file", name: "video", required: filter !== "edit" }), br(), br(),
  81. label(i18n.videoTagsLabel), br(),
  82. input({ type: "text", name: "tags", placeholder: i18n.videoTagsPlaceholder, value: videoToEdit?.tags?.join(', ') || '' }), br(), br(),
  83. label(i18n.videoTitleLabel), br(),
  84. input({ type: "text", name: "title", placeholder: i18n.videoTitlePlaceholder, value: videoToEdit?.title || '' }), br(), br(),
  85. label(i18n.videoDescriptionLabel), br(),
  86. input({ type: "text", name: "description", placeholder: i18n.videoDescriptionPlaceholder, value: videoToEdit?.description || '' }), br(), br(),
  87. button({ type: "submit" }, filter === 'edit' ? i18n.videoUpdateButton : i18n.videoCreateButton)
  88. )
  89. );
  90. };
  91. exports.videoView = async (videos, filter, videoId) => {
  92. const title = filter === 'mine' ? i18n.videoMineSectionTitle :
  93. filter === 'create' ? i18n.videoCreateSectionTitle :
  94. filter === 'edit' ? i18n.videoUpdateSectionTitle :
  95. filter === 'recent' ? i18n.videoRecentSectionTitle :
  96. filter === 'top' ? i18n.videoTopSectionTitle :
  97. i18n.videoAllSectionTitle;
  98. const filteredVideos = getFilteredVideos(filter, videos, userId);
  99. const videoToEdit = videos.find(v => v.key === videoId);
  100. return template(
  101. title,
  102. section(
  103. div({ class: "tags-header" },
  104. h2(title),
  105. p(i18n.videoDescription)
  106. ),
  107. div({ class: "filters" },
  108. form({ method: "GET", action: "/videos" },
  109. ["all", "mine", "recent", "top"].map(f =>
  110. button({
  111. type: "submit", name: "filter", value: f,
  112. class: filter === f ? "filter-btn active" : "filter-btn"
  113. },
  114. i18n[`videoFilter${f.charAt(0).toUpperCase() + f.slice(1)}`]
  115. )
  116. ),
  117. button({ type: "submit", name: "filter", value: "create", class: "create-button" },
  118. i18n.videoCreateButton)
  119. )
  120. )
  121. ),
  122. section(
  123. (filter === 'create' || filter === 'edit')
  124. ? renderVideoForm(filter, videoId, videoToEdit)
  125. : renderVideoList(filteredVideos, filter)
  126. )
  127. );
  128. };
  129. exports.singleVideoView = async (video, filter) => {
  130. const isAuthor = video.author === userId;
  131. const hasOpinions = Object.keys(video.opinions || {}).length > 0;
  132. return template(
  133. i18n.videoTitle,
  134. section(
  135. div({ class: "filters" },
  136. form({ method: "GET", action: "/videos" },
  137. button({ type: "submit", name: "filter", value: "all", class: filter === 'all' ? 'filter-btn active' : 'filter-btn' }, i18n.videoFilterAll),
  138. button({ type: "submit", name: "filter", value: "mine", class: filter === 'mine' ? 'filter-btn active' : 'filter-btn' }, i18n.videoFilterMine),
  139. button({ type: "submit", name: "filter", value: "recent", class: filter === 'recent' ? 'filter-btn active' : 'filter-btn' }, i18n.videoFilterRecent),
  140. button({ type: "submit", name: "filter", value: "top", class: filter === 'top' ? 'filter-btn active' : 'filter-btn' }, i18n.videoFilterTop),
  141. button({ type: "submit", name: "filter", value: "create", class: "create-button" }, i18n.videoCreateButton)
  142. )
  143. ),
  144. div({ class: "tags-header" },
  145. h2(video.title),
  146. p(video.description),
  147. video.url
  148. ? div({ class: "video-container" },
  149. videoHyperaxe({
  150. controls: true,
  151. src: `/blob/${encodeURIComponent(video.url)}`,
  152. type: video.mimeType,
  153. preload: 'metadata',
  154. width: '640',
  155. height: '360'
  156. })
  157. )
  158. : p(i18n.videoNoFile),
  159. p(`${i18n.videoCreatedAt}: ${moment(video.createdAt).format('YYYY/MM/DD HH:mm:ss')}`),
  160. p(`${i18n.videoAuthor}: `, a({ href: `/author/${encodeURIComponent(video.author)}` }, video.author)),
  161. video.tags?.length
  162. ? div(video.tags.map(tag =>
  163. a({ href: `/search?query=%23${encodeURIComponent(tag)}`, class: "tag-link", style: "margin-right: 0.8em; margin-bottom: 0.5em;" }, `#${tag}`)
  164. ))
  165. : null
  166. ),
  167. isAuthor ? div({ class: "video-actions" },
  168. !hasOpinions
  169. ? form({ method: "GET", action: `/videos/edit/${encodeURIComponent(video.key)}` },
  170. button({ class: "update-btn", type: "submit" }, i18n.videoUpdateButton)
  171. )
  172. : null,
  173. form({ method: "POST", action: `/videos/delete/${encodeURIComponent(video.key)}` },
  174. button({ class: "delete-btn", type: "submit" }, i18n.videoDeleteButton)
  175. )
  176. ) : null,
  177. div({ class: "voting-buttons" },
  178. ['interesting', 'necessary', 'funny', 'disgusting', 'sensible', 'propaganda', 'adultOnly', 'boring', 'confusing', 'inspiring', 'spam'].map(category =>
  179. form({ method: "POST", action: `/videos/opinions/${encodeURIComponent(video.key)}/${category}` },
  180. button({ class: "vote-btn" }, `${i18n[`vote${category.charAt(0).toUpperCase() + category.slice(1)}`]} [${video.opinions?.[category] || 0}]`)
  181. )
  182. )
  183. )
  184. )
  185. );
  186. };