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