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