aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/feeds/shared
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/feeds/shared')
-rw-r--r--server/controllers/feeds/shared/common-feed-utils.ts145
-rw-r--r--server/controllers/feeds/shared/index.ts2
-rw-r--r--server/controllers/feeds/shared/video-feed-utils.ts66
3 files changed, 213 insertions, 0 deletions
diff --git a/server/controllers/feeds/shared/common-feed-utils.ts b/server/controllers/feeds/shared/common-feed-utils.ts
new file mode 100644
index 000000000..375c2814b
--- /dev/null
+++ b/server/controllers/feeds/shared/common-feed-utils.ts
@@ -0,0 +1,145 @@
1import express from 'express'
2import { Feed } from '@peertube/feed'
3import { CustomTag, CustomXMLNS, Person } from '@peertube/feed/lib/typings'
4import { mdToOneLinePlainText } from '@server/helpers/markdown'
5import { CONFIG } from '@server/initializers/config'
6import { WEBSERVER } from '@server/initializers/constants'
7import { UserModel } from '@server/models/user/user'
8import { MAccountDefault, MChannelBannerAccountDefault, MUser, MVideoFullLight } from '@server/types/models'
9import { pick } from '@shared/core-utils'
10import { ActorImageType } from '@shared/models'
11
12export function initFeed (parameters: {
13 name: string
14 description: string
15 imageUrl: string
16 isPodcast: boolean
17 link?: string
18 locked?: { isLocked: boolean, email: string }
19 author?: {
20 name: string
21 link: string
22 imageUrl: string
23 }
24 person?: Person[]
25 resourceType?: 'videos' | 'video-comments'
26 queryString?: string
27 medium?: string
28 stunServers?: string[]
29 trackers?: string[]
30 customXMLNS?: CustomXMLNS[]
31 customTags?: CustomTag[]
32}) {
33 const webserverUrl = WEBSERVER.URL
34 const { name, description, link, imageUrl, isPodcast, resourceType, queryString, medium } = parameters
35
36 return new Feed({
37 title: name,
38 description: mdToOneLinePlainText(description),
39 // updated: TODO: somehowGetLatestUpdate, // optional, default = today
40 id: link || webserverUrl,
41 link: link || webserverUrl,
42 image: imageUrl,
43 favicon: webserverUrl + '/client/assets/images/favicon.png',
44 copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` +
45 ` and potential licenses granted by each content's rightholder.`,
46 generator: `Toraifōsu`, // ^.~
47 medium: medium || 'video',
48 feedLinks: {
49 json: `${webserverUrl}/feeds/${resourceType}.json${queryString}`,
50 atom: `${webserverUrl}/feeds/${resourceType}.atom${queryString}`,
51 rss: isPodcast
52 ? `${webserverUrl}/feeds/podcast/videos.xml${queryString}`
53 : `${webserverUrl}/feeds/${resourceType}.xml${queryString}`
54 },
55
56 ...pick(parameters, [ 'stunServers', 'trackers', 'customXMLNS', 'customTags', 'author', 'person', 'locked' ])
57 })
58}
59
60export function sendFeed (feed: Feed, req: express.Request, res: express.Response) {
61 const format = req.params.format
62
63 if (format === 'atom' || format === 'atom1') {
64 return res.send(feed.atom1()).end()
65 }
66
67 if (format === 'json' || format === 'json1') {
68 return res.send(feed.json1()).end()
69 }
70
71 if (format === 'rss' || format === 'rss2') {
72 return res.send(feed.rss2()).end()
73 }
74
75 // We're in the ambiguous '.xml' case and we look at the format query parameter
76 if (req.query.format === 'atom' || req.query.format === 'atom1') {
77 return res.send(feed.atom1()).end()
78 }
79
80 return res.send(feed.rss2()).end()
81}
82
83export async function buildFeedMetadata (options: {
84 videoChannel?: MChannelBannerAccountDefault
85 account?: MAccountDefault
86 video?: MVideoFullLight
87}) {
88 const { video, videoChannel, account } = options
89
90 let imageUrl = WEBSERVER.URL + '/client/assets/images/icons/icon-96x96.png'
91 let accountImageUrl: string
92 let name: string
93 let userName: string
94 let description: string
95 let email: string
96 let link: string
97 let accountLink: string
98 let user: MUser
99
100 if (videoChannel) {
101 name = videoChannel.getDisplayName()
102 description = videoChannel.description
103 link = videoChannel.getClientUrl()
104 accountLink = videoChannel.Account.getClientUrl()
105
106 if (videoChannel.Actor.hasImage(ActorImageType.AVATAR)) {
107 imageUrl = WEBSERVER.URL + videoChannel.Actor.Avatars[0].getStaticPath()
108 }
109
110 if (videoChannel.Account.Actor.hasImage(ActorImageType.AVATAR)) {
111 accountImageUrl = WEBSERVER.URL + videoChannel.Account.Actor.Avatars[0].getStaticPath()
112 }
113
114 user = await UserModel.loadById(videoChannel.Account.userId)
115 userName = videoChannel.Account.getDisplayName()
116 } else if (account) {
117 name = account.getDisplayName()
118 description = account.description
119 link = account.getClientUrl()
120 accountLink = link
121
122 if (account.Actor.hasImage(ActorImageType.AVATAR)) {
123 imageUrl = WEBSERVER.URL + account.Actor.Avatars[0].getStaticPath()
124 accountImageUrl = imageUrl
125 }
126
127 user = await UserModel.loadById(account.userId)
128 } else if (video) {
129 name = video.name
130 description = video.description
131 link = video.url
132 } else {
133 name = CONFIG.INSTANCE.NAME
134 description = CONFIG.INSTANCE.DESCRIPTION
135 link = WEBSERVER.URL
136 }
137
138 // If the user is local, has a verified email address, and allows it to be publicly displayed
139 // Return it so the owner can prove ownership of their feed
140 if (user && !user.pluginAuth && user.emailVerified && user.emailPublic) {
141 email = user.email
142 }
143
144 return { name, userName, description, imageUrl, accountImageUrl, email, link, accountLink }
145}
diff --git a/server/controllers/feeds/shared/index.ts b/server/controllers/feeds/shared/index.ts
new file mode 100644
index 000000000..0136c8477
--- /dev/null
+++ b/server/controllers/feeds/shared/index.ts
@@ -0,0 +1,2 @@
1export * from './video-feed-utils'
2export * from './common-feed-utils'
diff --git a/server/controllers/feeds/shared/video-feed-utils.ts b/server/controllers/feeds/shared/video-feed-utils.ts
new file mode 100644
index 000000000..3175cea59
--- /dev/null
+++ b/server/controllers/feeds/shared/video-feed-utils.ts
@@ -0,0 +1,66 @@
1import { mdToOneLinePlainText, toSafeHtml } from '@server/helpers/markdown'
2import { CONFIG } from '@server/initializers/config'
3import { WEBSERVER } from '@server/initializers/constants'
4import { getServerActor } from '@server/models/application/application'
5import { getCategoryLabel } from '@server/models/video/formatter/video-format-utils'
6import { DisplayOnlyForFollowerOptions } from '@server/models/video/sql/video'
7import { VideoModel } from '@server/models/video/video'
8import { MThumbnail, MUserDefault } from '@server/types/models'
9import { VideoInclude } from '@shared/models'
10
11export async function getVideosForFeeds (options: {
12 sort: string
13 nsfw: boolean
14 isLocal: boolean
15 include: VideoInclude
16
17 accountId?: number
18 videoChannelId?: number
19 displayOnlyForFollower?: DisplayOnlyForFollowerOptions
20 user?: MUserDefault
21}) {
22 const server = await getServerActor()
23
24 const { data } = await VideoModel.listForApi({
25 start: 0,
26 count: CONFIG.FEEDS.VIDEOS.COUNT,
27 displayOnlyForFollower: {
28 actorId: server.id,
29 orLocalVideos: true
30 },
31 hasFiles: true,
32 countVideos: false,
33
34 ...options
35 })
36
37 return data
38}
39
40export function getCommonVideoFeedAttributes (video: VideoModel) {
41 const localLink = WEBSERVER.URL + video.getWatchStaticPath()
42
43 const thumbnailModels: MThumbnail[] = []
44 if (video.hasPreview()) thumbnailModels.push(video.getPreview())
45 thumbnailModels.push(video.getMiniature())
46
47 return {
48 title: video.name,
49 link: localLink,
50 description: mdToOneLinePlainText(video.getTruncatedDescription()),
51 content: toSafeHtml(video.description),
52
53 date: video.publishedAt,
54 nsfw: video.nsfw,
55
56 category: video.category
57 ? [ { name: getCategoryLabel(video.category) } ]
58 : undefined,
59
60 thumbnails: thumbnailModels.map(t => ({
61 url: WEBSERVER.URL + t.getLocalStaticPath(),
62 width: t.width,
63 height: t.height
64 }))
65 }
66}