]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/misc.ts
Begin to add avatar to actors
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / misc.ts
CommitLineData
0d0e8dd0 1import * as magnetUtil from 'magnet-uri'
54141398 2import { VideoTorrentObject } from '../../../../shared'
da854ddd 3import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
3fd3ab2d 4import { VideoPrivacy } from '../../../../shared/models/videos'
54141398 5import { isVideoFileInfoHashValid } from '../../../helpers/custom-validators/videos'
da854ddd
C
6import { logger } from '../../../helpers/logger'
7import { doRequest } from '../../../helpers/requests'
3fd3ab2d 8import { ACTIVITY_PUB, VIDEO_MIMETYPE_EXT } from '../../../initializers'
da854ddd 9import { ActorModel } from '../../../models/activitypub/actor'
3fd3ab2d
C
10import { VideoModel } from '../../../models/video/video'
11import { VideoChannelModel } from '../../../models/video/video-channel'
da854ddd 12import { VideoCommentModel } from '../../../models/video/video-comment'
3fd3ab2d 13import { VideoShareModel } from '../../../models/video/video-share'
50d6de9c 14import { getOrCreateActorAndServerAndModel } from '../actor'
0d0e8dd0 15
571389d4 16async function videoActivityObjectToDBAttributes (
3fd3ab2d 17 videoChannel: VideoChannelModel,
9a27cdc2
C
18 videoObject: VideoTorrentObject,
19 to: string[] = [],
20 cc: string[] = []
571389d4 21) {
9a27cdc2
C
22 let privacy = VideoPrivacy.PRIVATE
23 if (to.indexOf(ACTIVITY_PUB.PUBLIC) !== -1) privacy = VideoPrivacy.PUBLIC
24 else if (cc.indexOf(ACTIVITY_PUB.PUBLIC) !== -1) privacy = VideoPrivacy.UNLISTED
25
0d0e8dd0 26 const duration = videoObject.duration.replace(/[^\d]+/, '')
40ff5707
C
27 let language = null
28 if (videoObject.language) {
29 language = parseInt(videoObject.language.identifier, 10)
30 }
31
f595d394
C
32 let category = null
33 if (videoObject.category) {
34 category = parseInt(videoObject.category.identifier, 10)
35 }
36
37 let licence = null
38 if (videoObject.licence) {
39 licence = parseInt(videoObject.licence.identifier, 10)
40 }
41
42 let description = null
43 if (videoObject.content) {
44 description = videoObject.content
45 }
46
3fd3ab2d 47 return {
0d0e8dd0
C
48 name: videoObject.name,
49 uuid: videoObject.uuid,
50 url: videoObject.id,
f595d394
C
51 category,
52 licence,
40ff5707 53 language,
f595d394 54 description,
0d0e8dd0 55 nsfw: videoObject.nsfw,
0d0e8dd0
C
56 channelId: videoChannel.id,
57 duration: parseInt(duration, 10),
efc32059 58 createdAt: new Date(videoObject.published),
0d0e8dd0 59 // FIXME: updatedAt does not seems to be considered by Sequelize
efc32059 60 updatedAt: new Date(videoObject.updated),
0d0e8dd0
C
61 views: videoObject.views,
62 likes: 0,
63 dislikes: 0,
0d0e8dd0 64 remote: true,
9a27cdc2 65 privacy
0d0e8dd0 66 }
0d0e8dd0
C
67}
68
3fd3ab2d 69function videoFileActivityUrlToDBAttributes (videoCreated: VideoModel, videoObject: VideoTorrentObject) {
20494f12
C
70 const mimeTypes = Object.keys(VIDEO_MIMETYPE_EXT)
71 const fileUrls = videoObject.url.filter(u => {
72 return mimeTypes.indexOf(u.mimeType) !== -1 && u.mimeType.startsWith('video/')
73 })
74
75 if (fileUrls.length === 0) {
76 throw new Error('Cannot find video files for ' + videoCreated.url)
77 }
0d0e8dd0 78
3fd3ab2d 79 const attributes = []
20494f12 80 for (const fileUrl of fileUrls) {
0d0e8dd0 81 // Fetch associated magnet uri
20494f12
C
82 const magnet = videoObject.url.find(u => {
83 return u.mimeType === 'application/x-bittorrent;x-scheme-handler/magnet' && u.width === fileUrl.width
84 })
85
86 if (!magnet) throw new Error('Cannot find associated magnet uri for file ' + fileUrl.url)
0d0e8dd0
C
87
88 const parsed = magnetUtil.decode(magnet.url)
89 if (!parsed || isVideoFileInfoHashValid(parsed.infoHash) === false) throw new Error('Cannot parse magnet URI ' + magnet.url)
90
91 const attribute = {
20494f12 92 extname: VIDEO_MIMETYPE_EXT[fileUrl.mimeType],
0d0e8dd0 93 infoHash: parsed.infoHash,
20494f12
C
94 resolution: fileUrl.width,
95 size: fileUrl.size,
0d0e8dd0
C
96 videoId: videoCreated.id
97 }
98 attributes.push(attribute)
99 }
100
101 return attributes
102}
103
da854ddd
C
104async function videoCommentActivityObjectToDBAttributes (video: VideoModel, actor: ActorModel, comment: VideoCommentObject) {
105 let originCommentId: number = null
106 let inReplyToCommentId: number = null
107
108 // If this is not a reply to the video (thread), create or get the parent comment
109 if (video.url !== comment.inReplyTo) {
110 const [ parent ] = await addVideoComment(video, comment.inReplyTo)
111 if (!parent) {
112 logger.warn('Cannot fetch or get parent comment %s of comment %s.', comment.inReplyTo, comment.id)
113 return undefined
114 }
115
116 originCommentId = parent.originCommentId || parent.id
117 inReplyToCommentId = parent.id
118 }
119
120 return {
121 url: comment.url,
122 text: comment.content,
123 videoId: video.id,
124 accountId: actor.Account.id,
125 inReplyToCommentId,
126 originCommentId,
127 createdAt: new Date(comment.published),
128 updatedAt: new Date(comment.updated)
129 }
130}
131
132async function addVideoShares (instance: VideoModel, shareUrls: string[]) {
133 for (const shareUrl of shareUrls) {
4e50b6a1 134 // Fetch url
da854ddd
C
135 const { body } = await doRequest({
136 uri: shareUrl,
137 json: true,
138 activityPub: true
4e50b6a1 139 })
da854ddd 140 const actorUrl = body.actor
50d6de9c 141 if (!actorUrl) continue
4e50b6a1 142
50d6de9c 143 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
4e50b6a1
C
144
145 const entry = {
50d6de9c 146 actorId: actor.id,
4e50b6a1
C
147 videoId: instance.id
148 }
149
3fd3ab2d 150 await VideoShareModel.findOrCreate({
4e50b6a1
C
151 where: entry,
152 defaults: entry
153 })
154 }
155}
156
da854ddd
C
157async function addVideoComments (instance: VideoModel, commentUrls: string[]) {
158 for (const commentUrl of commentUrls) {
159 await addVideoComment(instance, commentUrl)
160 }
161}
162
163async function addVideoComment (instance: VideoModel, commentUrl: string) {
164 // Fetch url
165 const { body } = await doRequest({
166 uri: commentUrl,
167 json: true,
168 activityPub: true
169 })
170
171 const actorUrl = body.attributedTo
172 if (!actorUrl) return []
173
174 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
175 const entry = await videoCommentActivityObjectToDBAttributes(instance, actor, body)
176 if (!entry) return []
177
178 return VideoCommentModel.findOrCreate({
179 where: {
180 url: body.id
181 },
182 defaults: entry
183 })
184}
185
0d0e8dd0
C
186// ---------------------------------------------------------------------------
187
188export {
189 videoFileActivityUrlToDBAttributes,
20494f12 190 videoActivityObjectToDBAttributes,
da854ddd
C
191 addVideoShares,
192 addVideoComments
0d0e8dd0 193}