video-feed-utils.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { mdToOneLinePlainText, toSafeHtml } from '@server/helpers/markdown'
  2. import { CONFIG } from '@server/initializers/config'
  3. import { WEBSERVER } from '@server/initializers/constants'
  4. import { getServerActor } from '@server/models/application/application'
  5. import { getCategoryLabel } from '@server/models/video/formatter/video-format-utils'
  6. import { DisplayOnlyForFollowerOptions } from '@server/models/video/sql/video'
  7. import { VideoModel } from '@server/models/video/video'
  8. import { MThumbnail, MUserDefault } from '@server/types/models'
  9. import { VideoInclude } from '@shared/models'
  10. export async function getVideosForFeeds (options: {
  11. sort: string
  12. nsfw: boolean
  13. isLocal: boolean
  14. include: VideoInclude
  15. accountId?: number
  16. videoChannelId?: number
  17. displayOnlyForFollower?: DisplayOnlyForFollowerOptions
  18. user?: MUserDefault
  19. }) {
  20. const server = await getServerActor()
  21. const { data } = await VideoModel.listForApi({
  22. start: 0,
  23. count: CONFIG.FEEDS.VIDEOS.COUNT,
  24. displayOnlyForFollower: {
  25. actorId: server.id,
  26. orLocalVideos: true
  27. },
  28. hasFiles: true,
  29. countVideos: false,
  30. ...options
  31. })
  32. return data
  33. }
  34. export function getCommonVideoFeedAttributes (video: VideoModel) {
  35. const localLink = WEBSERVER.URL + video.getWatchStaticPath()
  36. const thumbnailModels: MThumbnail[] = []
  37. if (video.hasPreview()) thumbnailModels.push(video.getPreview())
  38. thumbnailModels.push(video.getMiniature())
  39. return {
  40. title: video.name,
  41. link: localLink,
  42. description: mdToOneLinePlainText(video.getTruncatedDescription()),
  43. content: toSafeHtml(video.description),
  44. date: video.publishedAt,
  45. nsfw: video.nsfw,
  46. category: video.category
  47. ? [ { name: getCategoryLabel(video.category) } ]
  48. : undefined,
  49. thumbnails: thumbnailModels.map(t => ({
  50. url: WEBSERVER.URL + t.getLocalStaticPath(),
  51. width: t.width,
  52. height: t.height
  53. }))
  54. }
  55. }