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