]>
Commit | Line | Data |
---|---|---|
49af5ac8 | 1 | import { isBlockedByServerOrAccount } from '@server/lib/blocklist' |
696d83fd | 2 | import { isRedundancyAccepted } from '@server/lib/redundancy' |
b2111066 C |
3 | import { VideoModel } from '@server/models/video/video' |
4 | import { ActivityCreate, CacheFileObject, PlaylistObject, VideoCommentObject, VideoObject, WatchActionObject } from '@shared/models' | |
da854ddd C |
5 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
6 | import { logger } from '../../../helpers/logger' | |
80fdaf06 | 7 | import { sequelizeTypescript } from '../../../initializers/database' |
26d6bf65 C |
8 | import { APProcessorOptions } from '../../../types/activitypub-processor.model' |
9 | import { MActorSignature, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../../types/models' | |
696d83fd C |
10 | import { Notifier } from '../../notifier' |
11 | import { createOrUpdateCacheFile } from '../cache-file' | |
b2111066 | 12 | import { createOrUpdateLocalVideoViewer } from '../local-video-viewer' |
49af5ac8 | 13 | import { createOrUpdateVideoPlaylist } from '../playlists' |
57e4e1c1 | 14 | import { forwardVideoRelatedActivity } from '../send/shared/send-utils' |
696d83fd | 15 | import { resolveThread } from '../video-comments' |
304a84d5 | 16 | import { getOrCreateAPVideo } from '../videos' |
e4f97bab | 17 | |
1198edf4 C |
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 | |
e4f97bab C |
23 | const activityObject = activity.object |
24 | const activityType = activityObject.type | |
25 | ||
848f499d | 26 | if (activityType === 'Video') { |
1198edf4 | 27 | return processCreateVideo(activity, notify) |
848f499d C |
28 | } |
29 | ||
30 | if (activityType === 'Note') { | |
a2f99b54 C |
31 | // Comments will be fetched from videos |
32 | if (options.fromFetch) return | |
33 | ||
1198edf4 | 34 | return retryTransactionWrapper(processCreateVideoComment, activity, byActor, notify) |
848f499d C |
35 | } |
36 | ||
b2111066 C |
37 | if (activityType === 'WatchAction') { |
38 | return retryTransactionWrapper(processCreateWatchAction, activity) | |
39 | } | |
40 | ||
848f499d | 41 | if (activityType === 'CacheFile') { |
418d092a C |
42 | return retryTransactionWrapper(processCreateCacheFile, activity, byActor) |
43 | } | |
44 | ||
45 | if (activityType === 'Playlist') { | |
46 | return retryTransactionWrapper(processCreatePlaylist, activity, byActor) | |
e4f97bab C |
47 | } |
48 | ||
49 | logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) | |
0d0e8dd0 | 50 | return Promise.resolve(undefined) |
e4f97bab C |
51 | } |
52 | ||
53 | // --------------------------------------------------------------------------- | |
54 | ||
55 | export { | |
56 | processCreateActivity | |
57 | } | |
58 | ||
59 | // --------------------------------------------------------------------------- | |
60 | ||
1198edf4 | 61 | async function processCreateVideo (activity: ActivityCreate, notify: boolean) { |
de6310b2 | 62 | const videoToCreateData = activity.object as VideoObject |
50d6de9c | 63 | |
57e4e1c1 | 64 | const syncParam = { rates: false, shares: false, comments: false, thumbnail: true, refreshVideo: false } |
304a84d5 | 65 | const { video, created } = await getOrCreateAPVideo({ videoObject: videoToCreateData, syncParam }) |
cef534ed | 66 | |
1198edf4 | 67 | if (created && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video) |
50d6de9c | 68 | |
50d6de9c C |
69 | return video |
70 | } | |
71 | ||
453e83ea | 72 | async function processCreateCacheFile (activity: ActivityCreate, byActor: MActorSignature) { |
8c9e7875 C |
73 | if (await isRedundancyAccepted(activity, byActor) !== true) return |
74 | ||
c48e82b5 C |
75 | const cacheFile = activity.object as CacheFileObject |
76 | ||
304a84d5 | 77 | const { video } = await getOrCreateAPVideo({ videoObject: cacheFile.object }) |
c48e82b5 | 78 | |
e5565833 | 79 | await sequelizeTypescript.transaction(async t => { |
b88a4596 | 80 | return createOrUpdateCacheFile(cacheFile, video, byActor, t) |
e5565833 | 81 | }) |
c48e82b5 C |
82 | |
83 | if (video.isOwned()) { | |
84 | // Don't resend the activity to the sender | |
85 | const exceptions = [ byActor ] | |
e5565833 | 86 | await forwardVideoRelatedActivity(activity, undefined, exceptions, video) |
c48e82b5 C |
87 | } |
88 | } | |
89 | ||
b2111066 C |
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 | ||
453e83ea | 103 | async function processCreateVideoComment (activity: ActivityCreate, byActor: MActorSignature, notify: boolean) { |
83e6519b | 104 | const commentObject = activity.object as VideoCommentObject |
6d852470 C |
105 | const byAccount = byActor.Account |
106 | ||
107 | if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url) | |
108 | ||
0283eaac | 109 | let video: MVideoAccountLightBlacklistAllFiles |
6b9c966f | 110 | let created: boolean |
453e83ea | 111 | let comment: MCommentOwnerVideo |
ee79b60e | 112 | try { |
6b9c966f | 113 | const resolveThreadResult = await resolveThread({ url: commentObject.id, isVideo: false }) |
403c69c5 | 114 | |
ee79b60e | 115 | video = resolveThreadResult.video |
6b9c966f C |
116 | created = resolveThreadResult.commentCreated |
117 | comment = resolveThreadResult.comment | |
ee79b60e C |
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 | ) | |
7d14d4d2 | 124 | return |
ee79b60e | 125 | } |
2ccaeeb3 | 126 | |
7a4fd56c | 127 | // Try to not forward unwanted comments on our videos |
403c69c5 C |
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 | } | |
696d83fd | 133 | |
403c69c5 C |
134 | if (created === true) { |
135 | // Don't resend the activity to the sender | |
136 | const exceptions = [ byActor ] | |
93ef8a9d | 137 | |
403c69c5 C |
138 | await forwardVideoRelatedActivity(activity, undefined, exceptions, video) |
139 | } | |
83e6519b | 140 | } |
cef534ed | 141 | |
1198edf4 | 142 | if (created && notify) Notifier.Instance.notifyOnNewComment(comment) |
6d852470 | 143 | } |
418d092a | 144 | |
453e83ea | 145 | async function processCreatePlaylist (activity: ActivityCreate, byActor: MActorSignature) { |
418d092a C |
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 | ||
37a44fc9 | 151 | await createOrUpdateVideoPlaylist(playlistObject, activity.to) |
418d092a | 152 | } |