aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/feeds/shared/video-feed-utils.ts
blob: 3175cea59c02a8b12cbfe885c4ffc37ace7d320a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { mdToOneLinePlainText, toSafeHtml } from '@server/helpers/markdown'
import { CONFIG } from '@server/initializers/config'
import { WEBSERVER } from '@server/initializers/constants'
import { getServerActor } from '@server/models/application/application'
import { getCategoryLabel } from '@server/models/video/formatter/video-format-utils'
import { DisplayOnlyForFollowerOptions } from '@server/models/video/sql/video'
import { VideoModel } from '@server/models/video/video'
import { MThumbnail, MUserDefault } from '@server/types/models'
import { VideoInclude } from '@shared/models'

export async function getVideosForFeeds (options: {
  sort: string
  nsfw: boolean
  isLocal: boolean
  include: VideoInclude

  accountId?: number
  videoChannelId?: number
  displayOnlyForFollower?: DisplayOnlyForFollowerOptions
  user?: MUserDefault
}) {
  const server = await getServerActor()

  const { data } = await VideoModel.listForApi({
    start: 0,
    count: CONFIG.FEEDS.VIDEOS.COUNT,
    displayOnlyForFollower: {
      actorId: server.id,
      orLocalVideos: true
    },
    hasFiles: true,
    countVideos: false,

    ...options
  })

  return data
}

export function getCommonVideoFeedAttributes (video: VideoModel) {
  const localLink = WEBSERVER.URL + video.getWatchStaticPath()

  const thumbnailModels: MThumbnail[] = []
  if (video.hasPreview()) thumbnailModels.push(video.getPreview())
  thumbnailModels.push(video.getMiniature())

  return {
    title: video.name,
    link: localLink,
    description: mdToOneLinePlainText(video.getTruncatedDescription()),
    content: toSafeHtml(video.description),

    date: video.publishedAt,
    nsfw: video.nsfw,

    category: video.category
      ? [ { name: getCategoryLabel(video.category) } ]
      : undefined,

    thumbnails: thumbnailModels.map(t => ({
      url: WEBSERVER.URL + t.getLocalStaticPath(),
      width: t.width,
      height: t.height
    }))
  }
}