diff options
Diffstat (limited to 'server/lib/activitypub/process')
-rw-r--r-- | server/lib/activitypub/process/misc.ts | 194 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-announce.ts | 19 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-create.ts | 127 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-like.ts | 10 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-undo.ts | 16 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-update.ts | 10 |
6 files changed, 54 insertions, 322 deletions
diff --git a/server/lib/activitypub/process/misc.ts b/server/lib/activitypub/process/misc.ts deleted file mode 100644 index 461619ea7..000000000 --- a/server/lib/activitypub/process/misc.ts +++ /dev/null | |||
@@ -1,194 +0,0 @@ | |||
1 | import * as magnetUtil from 'magnet-uri' | ||
2 | import { VideoTorrentObject } from '../../../../shared' | ||
3 | import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object' | ||
4 | import { VideoPrivacy } from '../../../../shared/models/videos' | ||
5 | import { isVideoFileInfoHashValid } from '../../../helpers/custom-validators/videos' | ||
6 | import { logger } from '../../../helpers/logger' | ||
7 | import { doRequest } from '../../../helpers/requests' | ||
8 | import { ACTIVITY_PUB, VIDEO_MIMETYPE_EXT } from '../../../initializers' | ||
9 | import { ActorModel } from '../../../models/activitypub/actor' | ||
10 | import { VideoModel } from '../../../models/video/video' | ||
11 | import { VideoChannelModel } from '../../../models/video/video-channel' | ||
12 | import { VideoCommentModel } from '../../../models/video/video-comment' | ||
13 | import { VideoShareModel } from '../../../models/video/video-share' | ||
14 | import { getOrCreateActorAndServerAndModel } from '../actor' | ||
15 | |||
16 | async function videoActivityObjectToDBAttributes ( | ||
17 | videoChannel: VideoChannelModel, | ||
18 | videoObject: VideoTorrentObject, | ||
19 | to: string[] = [], | ||
20 | cc: string[] = [] | ||
21 | ) { | ||
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 | |||
26 | const duration = videoObject.duration.replace(/[^\d]+/, '') | ||
27 | let language = null | ||
28 | if (videoObject.language) { | ||
29 | language = parseInt(videoObject.language.identifier, 10) | ||
30 | } | ||
31 | |||
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 | |||
47 | return { | ||
48 | name: videoObject.name, | ||
49 | uuid: videoObject.uuid, | ||
50 | url: videoObject.id, | ||
51 | category, | ||
52 | licence, | ||
53 | language, | ||
54 | description, | ||
55 | nsfw: videoObject.nsfw, | ||
56 | commentsEnabled: videoObject.commentsEnabled, | ||
57 | channelId: videoChannel.id, | ||
58 | duration: parseInt(duration, 10), | ||
59 | createdAt: new Date(videoObject.published), | ||
60 | // FIXME: updatedAt does not seems to be considered by Sequelize | ||
61 | updatedAt: new Date(videoObject.updated), | ||
62 | views: videoObject.views, | ||
63 | likes: 0, | ||
64 | dislikes: 0, | ||
65 | remote: true, | ||
66 | privacy | ||
67 | } | ||
68 | } | ||
69 | |||
70 | function videoFileActivityUrlToDBAttributes (videoCreated: VideoModel, videoObject: VideoTorrentObject) { | ||
71 | const mimeTypes = Object.keys(VIDEO_MIMETYPE_EXT) | ||
72 | const fileUrls = videoObject.url.filter(u => { | ||
73 | return mimeTypes.indexOf(u.mimeType) !== -1 && u.mimeType.startsWith('video/') | ||
74 | }) | ||
75 | |||
76 | if (fileUrls.length === 0) { | ||
77 | throw new Error('Cannot find video files for ' + videoCreated.url) | ||
78 | } | ||
79 | |||
80 | const attributes = [] | ||
81 | for (const fileUrl of fileUrls) { | ||
82 | // Fetch associated magnet uri | ||
83 | const magnet = videoObject.url.find(u => { | ||
84 | return u.mimeType === 'application/x-bittorrent;x-scheme-handler/magnet' && u.width === fileUrl.width | ||
85 | }) | ||
86 | |||
87 | if (!magnet) throw new Error('Cannot find associated magnet uri for file ' + fileUrl.url) | ||
88 | |||
89 | const parsed = magnetUtil.decode(magnet.url) | ||
90 | if (!parsed || isVideoFileInfoHashValid(parsed.infoHash) === false) throw new Error('Cannot parse magnet URI ' + magnet.url) | ||
91 | |||
92 | const attribute = { | ||
93 | extname: VIDEO_MIMETYPE_EXT[fileUrl.mimeType], | ||
94 | infoHash: parsed.infoHash, | ||
95 | resolution: fileUrl.width, | ||
96 | size: fileUrl.size, | ||
97 | videoId: videoCreated.id | ||
98 | } | ||
99 | attributes.push(attribute) | ||
100 | } | ||
101 | |||
102 | return attributes | ||
103 | } | ||
104 | |||
105 | async function videoCommentActivityObjectToDBAttributes (video: VideoModel, actor: ActorModel, comment: VideoCommentObject) { | ||
106 | let originCommentId: number = null | ||
107 | let inReplyToCommentId: number = null | ||
108 | |||
109 | // If this is not a reply to the video (thread), create or get the parent comment | ||
110 | if (video.url !== comment.inReplyTo) { | ||
111 | const [ parent ] = await addVideoComment(video, comment.inReplyTo) | ||
112 | if (!parent) { | ||
113 | logger.warn('Cannot fetch or get parent comment %s of comment %s.', comment.inReplyTo, comment.id) | ||
114 | return undefined | ||
115 | } | ||
116 | |||
117 | originCommentId = parent.originCommentId || parent.id | ||
118 | inReplyToCommentId = parent.id | ||
119 | } | ||
120 | |||
121 | return { | ||
122 | url: comment.url, | ||
123 | text: comment.content, | ||
124 | videoId: video.id, | ||
125 | accountId: actor.Account.id, | ||
126 | inReplyToCommentId, | ||
127 | originCommentId, | ||
128 | createdAt: new Date(comment.published), | ||
129 | updatedAt: new Date(comment.updated) | ||
130 | } | ||
131 | } | ||
132 | |||
133 | async function addVideoShares (instance: VideoModel, shareUrls: string[]) { | ||
134 | for (const shareUrl of shareUrls) { | ||
135 | // Fetch url | ||
136 | const { body } = await doRequest({ | ||
137 | uri: shareUrl, | ||
138 | json: true, | ||
139 | activityPub: true | ||
140 | }) | ||
141 | const actorUrl = body.actor | ||
142 | if (!actorUrl) continue | ||
143 | |||
144 | const actor = await getOrCreateActorAndServerAndModel(actorUrl) | ||
145 | |||
146 | const entry = { | ||
147 | actorId: actor.id, | ||
148 | videoId: instance.id | ||
149 | } | ||
150 | |||
151 | await VideoShareModel.findOrCreate({ | ||
152 | where: entry, | ||
153 | defaults: entry | ||
154 | }) | ||
155 | } | ||
156 | } | ||
157 | |||
158 | async function addVideoComments (instance: VideoModel, commentUrls: string[]) { | ||
159 | for (const commentUrl of commentUrls) { | ||
160 | await addVideoComment(instance, commentUrl) | ||
161 | } | ||
162 | } | ||
163 | |||
164 | async function addVideoComment (instance: VideoModel, commentUrl: string) { | ||
165 | // Fetch url | ||
166 | const { body } = await doRequest({ | ||
167 | uri: commentUrl, | ||
168 | json: true, | ||
169 | activityPub: true | ||
170 | }) | ||
171 | |||
172 | const actorUrl = body.attributedTo | ||
173 | if (!actorUrl) return [] | ||
174 | |||
175 | const actor = await getOrCreateActorAndServerAndModel(actorUrl) | ||
176 | const entry = await videoCommentActivityObjectToDBAttributes(instance, actor, body) | ||
177 | if (!entry) return [] | ||
178 | |||
179 | return VideoCommentModel.findOrCreate({ | ||
180 | where: { | ||
181 | url: body.id | ||
182 | }, | ||
183 | defaults: entry | ||
184 | }) | ||
185 | } | ||
186 | |||
187 | // --------------------------------------------------------------------------- | ||
188 | |||
189 | export { | ||
190 | videoFileActivityUrlToDBAttributes, | ||
191 | videoActivityObjectToDBAttributes, | ||
192 | addVideoShares, | ||
193 | addVideoComments | ||
194 | } | ||
diff --git a/server/lib/activitypub/process/process-announce.ts b/server/lib/activitypub/process/process-announce.ts index 9adb40e01..bf7d7879d 100644 --- a/server/lib/activitypub/process/process-announce.ts +++ b/server/lib/activitypub/process/process-announce.ts | |||
@@ -7,6 +7,7 @@ import { VideoModel } from '../../../models/video/video' | |||
7 | import { VideoShareModel } from '../../../models/video/video-share' | 7 | import { VideoShareModel } from '../../../models/video/video-share' |
8 | import { getOrCreateActorAndServerAndModel } from '../actor' | 8 | import { getOrCreateActorAndServerAndModel } from '../actor' |
9 | import { forwardActivity } from '../send/misc' | 9 | import { forwardActivity } from '../send/misc' |
10 | import { getOrCreateAccountAndVideoAndChannel } from '../videos' | ||
10 | import { processCreateActivity } from './process-create' | 11 | import { processCreateActivity } from './process-create' |
11 | 12 | ||
12 | async function processAnnounceActivity (activity: ActivityAnnounce) { | 13 | async function processAnnounceActivity (activity: ActivityAnnounce) { |
@@ -44,19 +45,19 @@ function processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnoun | |||
44 | return retryTransactionWrapper(shareVideo, options) | 45 | return retryTransactionWrapper(shareVideo, options) |
45 | } | 46 | } |
46 | 47 | ||
47 | function shareVideo (actorAnnouncer: ActorModel, activity: ActivityAnnounce) { | 48 | async function shareVideo (actorAnnouncer: ActorModel, activity: ActivityAnnounce) { |
48 | const announced = activity.object | 49 | const announced = activity.object |
50 | let video: VideoModel | ||
51 | |||
52 | if (typeof announced === 'string') { | ||
53 | const res = await getOrCreateAccountAndVideoAndChannel(announced) | ||
54 | video = res.video | ||
55 | } else { | ||
56 | video = await processCreateActivity(announced) | ||
57 | } | ||
49 | 58 | ||
50 | return sequelizeTypescript.transaction(async t => { | 59 | return sequelizeTypescript.transaction(async t => { |
51 | // Add share entry | 60 | // Add share entry |
52 | let video: VideoModel | ||
53 | |||
54 | if (typeof announced === 'string') { | ||
55 | video = await VideoModel.loadByUrlAndPopulateAccount(announced) | ||
56 | if (!video) throw new Error('Unknown video to share ' + announced) | ||
57 | } else { | ||
58 | video = await processCreateActivity(announced) | ||
59 | } | ||
60 | 61 | ||
61 | const share = { | 62 | const share = { |
62 | actorId: actorAnnouncer.id, | 63 | actorId: actorAnnouncer.id, |
diff --git a/server/lib/activitypub/process/process-create.ts b/server/lib/activitypub/process/process-create.ts index e65b257c0..08d61996a 100644 --- a/server/lib/activitypub/process/process-create.ts +++ b/server/lib/activitypub/process/process-create.ts | |||
@@ -8,15 +8,13 @@ import { logger } from '../../../helpers/logger' | |||
8 | import { sequelizeTypescript } from '../../../initializers' | 8 | import { sequelizeTypescript } from '../../../initializers' |
9 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' | 9 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' |
10 | import { ActorModel } from '../../../models/activitypub/actor' | 10 | import { ActorModel } from '../../../models/activitypub/actor' |
11 | import { TagModel } from '../../../models/video/tag' | ||
12 | import { VideoModel } from '../../../models/video/video' | 11 | import { VideoModel } from '../../../models/video/video' |
13 | import { VideoAbuseModel } from '../../../models/video/video-abuse' | 12 | import { VideoAbuseModel } from '../../../models/video/video-abuse' |
14 | import { VideoCommentModel } from '../../../models/video/video-comment' | 13 | import { VideoCommentModel } from '../../../models/video/video-comment' |
15 | import { VideoFileModel } from '../../../models/video/video-file' | ||
16 | import { getOrCreateActorAndServerAndModel } from '../actor' | 14 | import { getOrCreateActorAndServerAndModel } from '../actor' |
17 | import { forwardActivity, getActorsInvolvedInVideo } from '../send/misc' | 15 | import { forwardActivity, getActorsInvolvedInVideo } from '../send/misc' |
18 | import { generateThumbnailFromUrl } from '../videos' | 16 | import { addVideoComments, resolveThread } from '../video-comments' |
19 | import { addVideoComments, addVideoShares, videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc' | 17 | import { addVideoShares, getOrCreateAccountAndVideoAndChannel } from '../videos' |
20 | 18 | ||
21 | async function processCreateActivity (activity: ActivityCreate) { | 19 | async function processCreateActivity (activity: ActivityCreate) { |
22 | const activityObject = activity.object | 20 | const activityObject = activity.object |
@@ -53,17 +51,7 @@ async function processCreateVideo ( | |||
53 | ) { | 51 | ) { |
54 | const videoToCreateData = activity.object as VideoTorrentObject | 52 | const videoToCreateData = activity.object as VideoTorrentObject |
55 | 53 | ||
56 | const channel = videoToCreateData.attributedTo.find(a => a.type === 'Group') | 54 | const { video } = await getOrCreateAccountAndVideoAndChannel(videoToCreateData, actor) |
57 | if (!channel) throw new Error('Cannot find associated video channel to video ' + videoToCreateData.url) | ||
58 | |||
59 | const channelActor = await getOrCreateActorAndServerAndModel(channel.id) | ||
60 | |||
61 | const options = { | ||
62 | arguments: [ actor, activity, videoToCreateData, channelActor ], | ||
63 | errorMessage: 'Cannot insert the remote video with many retries.' | ||
64 | } | ||
65 | |||
66 | const video = await retryTransactionWrapper(createRemoteVideo, options) | ||
67 | 55 | ||
68 | // Process outside the transaction because we could fetch remote data | 56 | // Process outside the transaction because we could fetch remote data |
69 | if (videoToCreateData.likes && Array.isArray(videoToCreateData.likes.orderedItems)) { | 57 | if (videoToCreateData.likes && Array.isArray(videoToCreateData.likes.orderedItems)) { |
@@ -89,48 +77,6 @@ async function processCreateVideo ( | |||
89 | return video | 77 | return video |
90 | } | 78 | } |
91 | 79 | ||
92 | function createRemoteVideo ( | ||
93 | account: ActorModel, | ||
94 | activity: ActivityCreate, | ||
95 | videoToCreateData: VideoTorrentObject, | ||
96 | channelActor: ActorModel | ||
97 | ) { | ||
98 | logger.debug('Adding remote video %s.', videoToCreateData.id) | ||
99 | |||
100 | return sequelizeTypescript.transaction(async t => { | ||
101 | const sequelizeOptions = { | ||
102 | transaction: t | ||
103 | } | ||
104 | const videoFromDatabase = await VideoModel.loadByUUIDOrURL(videoToCreateData.uuid, videoToCreateData.id, t) | ||
105 | if (videoFromDatabase) return videoFromDatabase | ||
106 | |||
107 | const videoData = await videoActivityObjectToDBAttributes(channelActor.VideoChannel, videoToCreateData, activity.to, activity.cc) | ||
108 | const video = VideoModel.build(videoData) | ||
109 | |||
110 | // Don't block on request | ||
111 | generateThumbnailFromUrl(video, videoToCreateData.icon) | ||
112 | .catch(err => logger.warn('Cannot generate thumbnail of %s.', videoToCreateData.id, err)) | ||
113 | |||
114 | const videoCreated = await video.save(sequelizeOptions) | ||
115 | |||
116 | const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoCreated, videoToCreateData) | ||
117 | if (videoFileAttributes.length === 0) { | ||
118 | throw new Error('Cannot find valid files for video %s ' + videoToCreateData.url) | ||
119 | } | ||
120 | |||
121 | const tasks: Bluebird<any>[] = videoFileAttributes.map(f => VideoFileModel.create(f, { transaction: t })) | ||
122 | await Promise.all(tasks) | ||
123 | |||
124 | const tags = videoToCreateData.tag.map(t => t.name) | ||
125 | const tagInstances = await TagModel.findOrCreateTags(tags, t) | ||
126 | await videoCreated.$set('Tags', tagInstances, sequelizeOptions) | ||
127 | |||
128 | logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid) | ||
129 | |||
130 | return videoCreated | ||
131 | }) | ||
132 | } | ||
133 | |||
134 | async function createRates (actorUrls: string[], video: VideoModel, rate: VideoRateType) { | 80 | async function createRates (actorUrls: string[], video: VideoModel, rate: VideoRateType) { |
135 | let rateCounts = 0 | 81 | let rateCounts = 0 |
136 | const tasks: Bluebird<any>[] = [] | 82 | const tasks: Bluebird<any>[] = [] |
@@ -167,16 +113,15 @@ async function processCreateDislike (byActor: ActorModel, activity: ActivityCrea | |||
167 | return retryTransactionWrapper(createVideoDislike, options) | 113 | return retryTransactionWrapper(createVideoDislike, options) |
168 | } | 114 | } |
169 | 115 | ||
170 | function createVideoDislike (byActor: ActorModel, activity: ActivityCreate) { | 116 | async function createVideoDislike (byActor: ActorModel, activity: ActivityCreate) { |
171 | const dislike = activity.object as DislikeObject | 117 | const dislike = activity.object as DislikeObject |
172 | const byAccount = byActor.Account | 118 | const byAccount = byActor.Account |
173 | 119 | ||
174 | if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url) | 120 | if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url) |
175 | 121 | ||
176 | return sequelizeTypescript.transaction(async t => { | 122 | const { video } = await getOrCreateAccountAndVideoAndChannel(dislike.object) |
177 | const video = await VideoModel.loadByUrlAndPopulateAccount(dislike.object, t) | ||
178 | if (!video) throw new Error('Unknown video ' + dislike.object) | ||
179 | 123 | ||
124 | return sequelizeTypescript.transaction(async t => { | ||
180 | const rate = { | 125 | const rate = { |
181 | type: 'dislike' as 'dislike', | 126 | type: 'dislike' as 'dislike', |
182 | videoId: video.id, | 127 | videoId: video.id, |
@@ -200,9 +145,7 @@ function createVideoDislike (byActor: ActorModel, activity: ActivityCreate) { | |||
200 | async function processCreateView (byActor: ActorModel, activity: ActivityCreate) { | 145 | async function processCreateView (byActor: ActorModel, activity: ActivityCreate) { |
201 | const view = activity.object as ViewObject | 146 | const view = activity.object as ViewObject |
202 | 147 | ||
203 | const video = await VideoModel.loadByUrlAndPopulateAccount(view.object) | 148 | const { video } = await getOrCreateAccountAndVideoAndChannel(view.object) |
204 | |||
205 | if (!video) throw new Error('Unknown video ' + view.object) | ||
206 | 149 | ||
207 | const account = await ActorModel.loadByUrl(view.actor) | 150 | const account = await ActorModel.loadByUrl(view.actor) |
208 | if (!account) throw new Error('Unknown account ' + view.actor) | 151 | if (!account) throw new Error('Unknown account ' + view.actor) |
@@ -225,19 +168,15 @@ function processCreateVideoAbuse (actor: ActorModel, videoAbuseToCreateData: Vid | |||
225 | return retryTransactionWrapper(addRemoteVideoAbuse, options) | 168 | return retryTransactionWrapper(addRemoteVideoAbuse, options) |
226 | } | 169 | } |
227 | 170 | ||
228 | function addRemoteVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) { | 171 | async function addRemoteVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) { |
229 | logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object) | 172 | logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object) |
230 | 173 | ||
231 | const account = actor.Account | 174 | const account = actor.Account |
232 | if (!account) throw new Error('Cannot create dislike with the non account actor ' + actor.url) | 175 | if (!account) throw new Error('Cannot create dislike with the non account actor ' + actor.url) |
233 | 176 | ||
234 | return sequelizeTypescript.transaction(async t => { | 177 | const { video } = await getOrCreateAccountAndVideoAndChannel(videoAbuseToCreateData.object) |
235 | const video = await VideoModel.loadByUrlAndPopulateAccount(videoAbuseToCreateData.object, t) | ||
236 | if (!video) { | ||
237 | logger.warn('Unknown video %s for remote video abuse.', videoAbuseToCreateData.object) | ||
238 | return undefined | ||
239 | } | ||
240 | 178 | ||
179 | return sequelizeTypescript.transaction(async t => { | ||
241 | const videoAbuseData = { | 180 | const videoAbuseData = { |
242 | reporterAccountId: account.id, | 181 | reporterAccountId: account.id, |
243 | reason: videoAbuseToCreateData.content, | 182 | reason: videoAbuseToCreateData.content, |
@@ -259,41 +198,33 @@ function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreat | |||
259 | return retryTransactionWrapper(createVideoComment, options) | 198 | return retryTransactionWrapper(createVideoComment, options) |
260 | } | 199 | } |
261 | 200 | ||
262 | function createVideoComment (byActor: ActorModel, activity: ActivityCreate) { | 201 | async function createVideoComment (byActor: ActorModel, activity: ActivityCreate) { |
263 | const comment = activity.object as VideoCommentObject | 202 | const comment = activity.object as VideoCommentObject |
264 | const byAccount = byActor.Account | 203 | const byAccount = byActor.Account |
265 | 204 | ||
266 | if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url) | 205 | if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url) |
267 | 206 | ||
207 | const { video, parents } = await resolveThread(comment.inReplyTo) | ||
208 | |||
268 | return sequelizeTypescript.transaction(async t => { | 209 | return sequelizeTypescript.transaction(async t => { |
269 | let video = await VideoModel.loadByUrlAndPopulateAccount(comment.inReplyTo, t) | 210 | let originCommentId = null |
270 | let objectToCreate | 211 | let inReplyToCommentId = null |
212 | |||
213 | if (parents.length !== 0) { | ||
214 | const parent = parents[0] | ||
215 | |||
216 | originCommentId = parent.getThreadId() | ||
217 | inReplyToCommentId = parent.id | ||
218 | } | ||
271 | 219 | ||
272 | // This is a new thread | 220 | // This is a new thread |
273 | if (video) { | 221 | const objectToCreate = { |
274 | objectToCreate = { | 222 | url: comment.id, |
275 | url: comment.id, | 223 | text: comment.content, |
276 | text: comment.content, | 224 | originCommentId, |
277 | originCommentId: null, | 225 | inReplyToCommentId, |
278 | inReplyToComment: null, | 226 | videoId: video.id, |
279 | videoId: video.id, | 227 | accountId: byAccount.id |
280 | accountId: byAccount.id | ||
281 | } | ||
282 | } else { | ||
283 | const inReplyToComment = await VideoCommentModel.loadByUrl(comment.inReplyTo, t) | ||
284 | if (!inReplyToComment) throw new Error('Unknown replied comment ' + comment.inReplyTo) | ||
285 | |||
286 | video = await VideoModel.loadAndPopulateAccount(inReplyToComment.videoId) | ||
287 | |||
288 | const originCommentId = inReplyToComment.originCommentId || inReplyToComment.id | ||
289 | objectToCreate = { | ||
290 | url: comment.id, | ||
291 | text: comment.content, | ||
292 | originCommentId, | ||
293 | inReplyToCommentId: inReplyToComment.id, | ||
294 | videoId: video.id, | ||
295 | accountId: byAccount.id | ||
296 | } | ||
297 | } | 228 | } |
298 | 229 | ||
299 | const options = { | 230 | const options = { |
diff --git a/server/lib/activitypub/process/process-like.ts b/server/lib/activitypub/process/process-like.ts index 77fadabe1..0d161b126 100644 --- a/server/lib/activitypub/process/process-like.ts +++ b/server/lib/activitypub/process/process-like.ts | |||
@@ -3,9 +3,9 @@ import { retryTransactionWrapper } from '../../../helpers/database-utils' | |||
3 | import { sequelizeTypescript } from '../../../initializers' | 3 | import { sequelizeTypescript } from '../../../initializers' |
4 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' | 4 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' |
5 | import { ActorModel } from '../../../models/activitypub/actor' | 5 | import { ActorModel } from '../../../models/activitypub/actor' |
6 | import { VideoModel } from '../../../models/video/video' | ||
7 | import { getOrCreateActorAndServerAndModel } from '../actor' | 6 | import { getOrCreateActorAndServerAndModel } from '../actor' |
8 | import { forwardActivity } from '../send/misc' | 7 | import { forwardActivity } from '../send/misc' |
8 | import { getOrCreateAccountAndVideoAndChannel } from '../videos' | ||
9 | 9 | ||
10 | async function processLikeActivity (activity: ActivityLike) { | 10 | async function processLikeActivity (activity: ActivityLike) { |
11 | const actor = await getOrCreateActorAndServerAndModel(activity.actor) | 11 | const actor = await getOrCreateActorAndServerAndModel(activity.actor) |
@@ -30,17 +30,15 @@ async function processLikeVideo (actor: ActorModel, activity: ActivityLike) { | |||
30 | return retryTransactionWrapper(createVideoLike, options) | 30 | return retryTransactionWrapper(createVideoLike, options) |
31 | } | 31 | } |
32 | 32 | ||
33 | function createVideoLike (byActor: ActorModel, activity: ActivityLike) { | 33 | async function createVideoLike (byActor: ActorModel, activity: ActivityLike) { |
34 | const videoUrl = activity.object | 34 | const videoUrl = activity.object |
35 | 35 | ||
36 | const byAccount = byActor.Account | 36 | const byAccount = byActor.Account |
37 | if (!byAccount) throw new Error('Cannot create like with the non account actor ' + byActor.url) | 37 | if (!byAccount) throw new Error('Cannot create like with the non account actor ' + byActor.url) |
38 | 38 | ||
39 | return sequelizeTypescript.transaction(async t => { | 39 | const { video } = await getOrCreateAccountAndVideoAndChannel(videoUrl) |
40 | const video = await VideoModel.loadByUrlAndPopulateAccount(videoUrl) | ||
41 | |||
42 | if (!video) throw new Error('Unknown video ' + videoUrl) | ||
43 | 40 | ||
41 | return sequelizeTypescript.transaction(async t => { | ||
44 | const rate = { | 42 | const rate = { |
45 | type: 'like' as 'like', | 43 | type: 'like' as 'like', |
46 | videoId: video.id, | 44 | videoId: video.id, |
diff --git a/server/lib/activitypub/process/process-undo.ts b/server/lib/activitypub/process/process-undo.ts index 9cad59233..5a770bb97 100644 --- a/server/lib/activitypub/process/process-undo.ts +++ b/server/lib/activitypub/process/process-undo.ts | |||
@@ -7,8 +7,8 @@ import { AccountModel } from '../../../models/account/account' | |||
7 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' | 7 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' |
8 | import { ActorModel } from '../../../models/activitypub/actor' | 8 | import { ActorModel } from '../../../models/activitypub/actor' |
9 | import { ActorFollowModel } from '../../../models/activitypub/actor-follow' | 9 | import { ActorFollowModel } from '../../../models/activitypub/actor-follow' |
10 | import { VideoModel } from '../../../models/video/video' | ||
11 | import { forwardActivity } from '../send/misc' | 10 | import { forwardActivity } from '../send/misc' |
11 | import { getOrCreateAccountAndVideoAndChannel } from '../videos' | ||
12 | 12 | ||
13 | async function processUndoActivity (activity: ActivityUndo) { | 13 | async function processUndoActivity (activity: ActivityUndo) { |
14 | const activityToUndo = activity.object | 14 | const activityToUndo = activity.object |
@@ -43,16 +43,15 @@ function processUndoLike (actorUrl: string, activity: ActivityUndo) { | |||
43 | return retryTransactionWrapper(undoLike, options) | 43 | return retryTransactionWrapper(undoLike, options) |
44 | } | 44 | } |
45 | 45 | ||
46 | function undoLike (actorUrl: string, activity: ActivityUndo) { | 46 | async function undoLike (actorUrl: string, activity: ActivityUndo) { |
47 | const likeActivity = activity.object as ActivityLike | 47 | const likeActivity = activity.object as ActivityLike |
48 | 48 | ||
49 | const { video } = await getOrCreateAccountAndVideoAndChannel(likeActivity.object) | ||
50 | |||
49 | return sequelizeTypescript.transaction(async t => { | 51 | return sequelizeTypescript.transaction(async t => { |
50 | const byAccount = await AccountModel.loadByUrl(actorUrl, t) | 52 | const byAccount = await AccountModel.loadByUrl(actorUrl, t) |
51 | if (!byAccount) throw new Error('Unknown account ' + actorUrl) | 53 | if (!byAccount) throw new Error('Unknown account ' + actorUrl) |
52 | 54 | ||
53 | const video = await VideoModel.loadByUrlAndPopulateAccount(likeActivity.object, t) | ||
54 | if (!video) throw new Error('Unknown video ' + likeActivity.actor) | ||
55 | |||
56 | const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t) | 55 | const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t) |
57 | if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) | 56 | if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) |
58 | 57 | ||
@@ -76,16 +75,15 @@ function processUndoDislike (actorUrl: string, activity: ActivityUndo) { | |||
76 | return retryTransactionWrapper(undoDislike, options) | 75 | return retryTransactionWrapper(undoDislike, options) |
77 | } | 76 | } |
78 | 77 | ||
79 | function undoDislike (actorUrl: string, activity: ActivityUndo) { | 78 | async function undoDislike (actorUrl: string, activity: ActivityUndo) { |
80 | const dislike = activity.object.object as DislikeObject | 79 | const dislike = activity.object.object as DislikeObject |
81 | 80 | ||
81 | const { video } = await getOrCreateAccountAndVideoAndChannel(dislike.object) | ||
82 | |||
82 | return sequelizeTypescript.transaction(async t => { | 83 | return sequelizeTypescript.transaction(async t => { |
83 | const byAccount = await AccountModel.loadByUrl(actorUrl, t) | 84 | const byAccount = await AccountModel.loadByUrl(actorUrl, t) |
84 | if (!byAccount) throw new Error('Unknown account ' + actorUrl) | 85 | if (!byAccount) throw new Error('Unknown account ' + actorUrl) |
85 | 86 | ||
86 | const video = await VideoModel.loadByUrlAndPopulateAccount(dislike.object, t) | ||
87 | if (!video) throw new Error('Unknown video ' + dislike.actor) | ||
88 | |||
89 | const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t) | 87 | const rate = await AccountVideoRateModel.load(byAccount.id, video.id, t) |
90 | if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) | 88 | if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) |
91 | 89 | ||
diff --git a/server/lib/activitypub/process/process-update.ts b/server/lib/activitypub/process/process-update.ts index 2c094f7ca..a5431c76b 100644 --- a/server/lib/activitypub/process/process-update.ts +++ b/server/lib/activitypub/process/process-update.ts | |||
@@ -9,10 +9,9 @@ import { sequelizeTypescript } from '../../../initializers' | |||
9 | import { AccountModel } from '../../../models/account/account' | 9 | import { AccountModel } from '../../../models/account/account' |
10 | import { ActorModel } from '../../../models/activitypub/actor' | 10 | import { ActorModel } from '../../../models/activitypub/actor' |
11 | import { TagModel } from '../../../models/video/tag' | 11 | import { TagModel } from '../../../models/video/tag' |
12 | import { VideoModel } from '../../../models/video/video' | ||
13 | import { VideoFileModel } from '../../../models/video/video-file' | 12 | import { VideoFileModel } from '../../../models/video/video-file' |
14 | import { fetchAvatarIfExists, getOrCreateActorAndServerAndModel, updateActorAvatarInstance, updateActorInstance } from '../actor' | 13 | import { fetchAvatarIfExists, getOrCreateActorAndServerAndModel, updateActorAvatarInstance, updateActorInstance } from '../actor' |
15 | import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc' | 14 | import { getOrCreateAccountAndVideoAndChannel, videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from '../videos' |
16 | 15 | ||
17 | async function processUpdateActivity (activity: ActivityUpdate) { | 16 | async function processUpdateActivity (activity: ActivityUpdate) { |
18 | const actor = await getOrCreateActorAndServerAndModel(activity.actor) | 17 | const actor = await getOrCreateActorAndServerAndModel(activity.actor) |
@@ -46,8 +45,10 @@ function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate) { | |||
46 | async function updateRemoteVideo (actor: ActorModel, activity: ActivityUpdate) { | 45 | async function updateRemoteVideo (actor: ActorModel, activity: ActivityUpdate) { |
47 | const videoAttributesToUpdate = activity.object as VideoTorrentObject | 46 | const videoAttributesToUpdate = activity.object as VideoTorrentObject |
48 | 47 | ||
48 | const res = await getOrCreateAccountAndVideoAndChannel(videoAttributesToUpdate.id) | ||
49 | |||
49 | logger.debug('Updating remote video "%s".', videoAttributesToUpdate.uuid) | 50 | logger.debug('Updating remote video "%s".', videoAttributesToUpdate.uuid) |
50 | let videoInstance: VideoModel | 51 | let videoInstance = res.video |
51 | let videoFieldsSave: any | 52 | let videoFieldsSave: any |
52 | 53 | ||
53 | try { | 54 | try { |
@@ -56,9 +57,6 @@ async function updateRemoteVideo (actor: ActorModel, activity: ActivityUpdate) { | |||
56 | transaction: t | 57 | transaction: t |
57 | } | 58 | } |
58 | 59 | ||
59 | const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(videoAttributesToUpdate.id, t) | ||
60 | if (!videoInstance) throw new Error('Video ' + videoAttributesToUpdate.id + ' not found.') | ||
61 | |||
62 | videoFieldsSave = videoInstance.toJSON() | 60 | videoFieldsSave = videoInstance.toJSON() |
63 | 61 | ||
64 | const videoChannel = videoInstance.VideoChannel | 62 | const videoChannel = videoInstance.VideoChannel |