]>
Commit | Line | Data |
---|---|---|
1 | import { ActivityCreate, CacheFileObject, VideoTorrentObject } from '../../../../shared' | |
2 | import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object' | |
3 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | |
4 | import { logger } from '../../../helpers/logger' | |
5 | import { sequelizeTypescript } from '../../../initializers' | |
6 | import { resolveThread } from '../video-comments' | |
7 | import { getOrCreateVideoAndAccountAndChannel } from '../videos' | |
8 | import { forwardVideoRelatedActivity } from '../send/utils' | |
9 | import { createOrUpdateCacheFile } from '../cache-file' | |
10 | import { Notifier } from '../../notifier' | |
11 | import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object' | |
12 | import { createOrUpdateVideoPlaylist } from '../playlist' | |
13 | import { APProcessorOptions } from '../../../typings/activitypub-processor.model' | |
14 | import { MActorSignature, MCommentOwnerVideo, MVideoAccountAllFiles } from '../../../typings/models' | |
15 | ||
16 | async function processCreateActivity (options: APProcessorOptions<ActivityCreate>) { | |
17 | const { activity, byActor } = options | |
18 | ||
19 | // Only notify if it is not from a fetcher job | |
20 | const notify = options.fromFetch !== true | |
21 | const activityObject = activity.object | |
22 | const activityType = activityObject.type | |
23 | ||
24 | if (activityType === 'Video') { | |
25 | return processCreateVideo(activity, notify) | |
26 | } | |
27 | ||
28 | if (activityType === 'Note') { | |
29 | return retryTransactionWrapper(processCreateVideoComment, activity, byActor, notify) | |
30 | } | |
31 | ||
32 | if (activityType === 'CacheFile') { | |
33 | return retryTransactionWrapper(processCreateCacheFile, activity, byActor) | |
34 | } | |
35 | ||
36 | if (activityType === 'Playlist') { | |
37 | return retryTransactionWrapper(processCreatePlaylist, activity, byActor) | |
38 | } | |
39 | ||
40 | logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) | |
41 | return Promise.resolve(undefined) | |
42 | } | |
43 | ||
44 | // --------------------------------------------------------------------------- | |
45 | ||
46 | export { | |
47 | processCreateActivity | |
48 | } | |
49 | ||
50 | // --------------------------------------------------------------------------- | |
51 | ||
52 | async function processCreateVideo (activity: ActivityCreate, notify: boolean) { | |
53 | const videoToCreateData = activity.object as VideoTorrentObject | |
54 | ||
55 | const { video, created } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData }) | |
56 | ||
57 | if (created && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video) | |
58 | ||
59 | return video | |
60 | } | |
61 | ||
62 | async function processCreateCacheFile (activity: ActivityCreate, byActor: MActorSignature) { | |
63 | const cacheFile = activity.object as CacheFileObject | |
64 | ||
65 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object }) | |
66 | ||
67 | await sequelizeTypescript.transaction(async t => { | |
68 | return createOrUpdateCacheFile(cacheFile, video, byActor, t) | |
69 | }) | |
70 | ||
71 | if (video.isOwned()) { | |
72 | // Don't resend the activity to the sender | |
73 | const exceptions = [ byActor ] | |
74 | await forwardVideoRelatedActivity(activity, undefined, exceptions, video) | |
75 | } | |
76 | } | |
77 | ||
78 | async function processCreateVideoComment (activity: ActivityCreate, byActor: MActorSignature, notify: boolean) { | |
79 | const commentObject = activity.object as VideoCommentObject | |
80 | const byAccount = byActor.Account | |
81 | ||
82 | if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url) | |
83 | ||
84 | let video: MVideoAccountAllFiles | |
85 | let created: boolean | |
86 | let comment: MCommentOwnerVideo | |
87 | try { | |
88 | const resolveThreadResult = await resolveThread({ url: commentObject.id, isVideo: false }) | |
89 | video = resolveThreadResult.video | |
90 | created = resolveThreadResult.commentCreated | |
91 | comment = resolveThreadResult.comment | |
92 | } catch (err) { | |
93 | logger.debug( | |
94 | 'Cannot process video comment because we could not resolve thread %s. Maybe it was not a video thread, so skip it.', | |
95 | commentObject.inReplyTo, | |
96 | { err } | |
97 | ) | |
98 | return | |
99 | } | |
100 | ||
101 | if (video.isOwned() && created === true) { | |
102 | // Don't resend the activity to the sender | |
103 | const exceptions = [ byActor ] | |
104 | ||
105 | await forwardVideoRelatedActivity(activity, undefined, exceptions, video) | |
106 | } | |
107 | ||
108 | if (created && notify) Notifier.Instance.notifyOnNewComment(comment) | |
109 | } | |
110 | ||
111 | async function processCreatePlaylist (activity: ActivityCreate, byActor: MActorSignature) { | |
112 | const playlistObject = activity.object as PlaylistObject | |
113 | const byAccount = byActor.Account | |
114 | ||
115 | if (!byAccount) throw new Error('Cannot create video playlist with the non account actor ' + byActor.url) | |
116 | ||
117 | await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to) | |
118 | } |