1 import express from 'express'
2 import { Feed } from '@peertube/feed'
3 import { CustomTag, CustomXMLNS, Person } from '@peertube/feed/lib/typings'
4 import { mdToOneLinePlainText } from '@server/helpers/markdown'
5 import { CONFIG } from '@server/initializers/config'
6 import { WEBSERVER } from '@server/initializers/constants'
7 import { UserModel } from '@server/models/user/user'
8 import { MAccountDefault, MChannelBannerAccountDefault, MUser, MVideoFullLight } from '@server/types/models'
9 import { pick } from '@shared/core-utils'
10 import { ActorImageType } from '@shared/models'
12 export function initFeed (parameters: {
18 locked?: { isLocked: boolean, email: string }
25 resourceType?: 'videos' | 'video-comments'
28 stunServers?: string[]
30 customXMLNS?: CustomXMLNS[]
31 customTags?: CustomTag[]
33 const webserverUrl = WEBSERVER.URL
34 const { name, description, link, imageUrl, isPodcast, resourceType, queryString, medium } = parameters
38 description: mdToOneLinePlainText(description),
39 // updated: TODO: somehowGetLatestUpdate, // optional, default = today
40 id: link || webserverUrl,
41 link: link || webserverUrl,
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',
49 json: `${webserverUrl}/feeds/${resourceType}.json${queryString}`,
50 atom: `${webserverUrl}/feeds/${resourceType}.atom${queryString}`,
52 ? `${webserverUrl}/feeds/podcast/videos.xml${queryString}`
53 : `${webserverUrl}/feeds/${resourceType}.xml${queryString}`
56 ...pick(parameters, [ 'stunServers', 'trackers', 'customXMLNS', 'customTags', 'author', 'person', 'locked' ])
60 export function sendFeed (feed: Feed, req: express.Request, res: express.Response) {
61 const format = req.params.format
63 if (format === 'atom' || format === 'atom1') {
64 return res.send(feed.atom1()).end()
67 if (format === 'json' || format === 'json1') {
68 return res.send(feed.json1()).end()
71 if (format === 'rss' || format === 'rss2') {
72 return res.send(feed.rss2()).end()
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()
80 return res.send(feed.rss2()).end()
83 export async function buildFeedMetadata (options: {
84 videoChannel?: MChannelBannerAccountDefault
85 account?: MAccountDefault
86 video?: MVideoFullLight
88 const { video, videoChannel, account } = options
90 let imageUrl = WEBSERVER.URL + '/client/assets/images/icons/icon-96x96.png'
91 let accountImageUrl: string
94 let description: string
97 let accountLink: string
101 name = videoChannel.getDisplayName()
102 description = videoChannel.description
103 link = videoChannel.getClientUrl()
104 accountLink = videoChannel.Account.getClientUrl()
106 if (videoChannel.Actor.hasImage(ActorImageType.AVATAR)) {
107 imageUrl = WEBSERVER.URL + videoChannel.Actor.Avatars[0].getStaticPath()
110 if (videoChannel.Account.Actor.hasImage(ActorImageType.AVATAR)) {
111 accountImageUrl = WEBSERVER.URL + videoChannel.Account.Actor.Avatars[0].getStaticPath()
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()
122 if (account.Actor.hasImage(ActorImageType.AVATAR)) {
123 imageUrl = WEBSERVER.URL + account.Actor.Avatars[0].getStaticPath()
124 accountImageUrl = imageUrl
127 user = await UserModel.loadById(account.userId)
130 description = video.description
133 name = CONFIG.INSTANCE.NAME
134 description = CONFIG.INSTANCE.DESCRIPTION
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) {
144 return { name, userName, description, imageUrl, accountImageUrl, email, link, accountLink }