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