]>
Commit | Line | Data |
---|---|---|
848f499d | 1 | import { ActivityCreate, CacheFileObject, VideoTorrentObject } from '../../../../shared' |
6d852470 | 2 | import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object' |
da854ddd C |
3 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
4 | import { logger } from '../../../helpers/logger' | |
3fd3ab2d | 5 | import { sequelizeTypescript } from '../../../initializers' |
50d6de9c | 6 | import { ActorModel } from '../../../models/activitypub/actor' |
83e6519b | 7 | import { addVideoComment, resolveThread } from '../video-comments' |
1297eb5d | 8 | import { getOrCreateVideoAndAccountAndChannel } from '../videos' |
cfaf819c | 9 | import { forwardVideoRelatedActivity } from '../send/utils' |
b88a4596 | 10 | import { createOrUpdateCacheFile } from '../cache-file' |
cef534ed | 11 | import { Notifier } from '../../notifier' |
418d092a C |
12 | import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object' |
13 | import { createOrUpdateVideoPlaylist } from '../playlist' | |
ee79b60e | 14 | import { VideoModel } from '../../../models/video/video' |
e4f97bab | 15 | |
e587e0ec | 16 | async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) { |
e4f97bab C |
17 | const activityObject = activity.object |
18 | const activityType = activityObject.type | |
19 | ||
848f499d | 20 | if (activityType === 'Video') { |
f6eebcb3 | 21 | return processCreateVideo(activity) |
848f499d C |
22 | } |
23 | ||
24 | if (activityType === 'Note') { | |
25 | return retryTransactionWrapper(processCreateVideoComment, activity, byActor) | |
26 | } | |
27 | ||
28 | if (activityType === 'CacheFile') { | |
418d092a C |
29 | return retryTransactionWrapper(processCreateCacheFile, activity, byActor) |
30 | } | |
31 | ||
32 | if (activityType === 'Playlist') { | |
33 | return retryTransactionWrapper(processCreatePlaylist, activity, byActor) | |
e4f97bab C |
34 | } |
35 | ||
36 | logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) | |
0d0e8dd0 | 37 | return Promise.resolve(undefined) |
e4f97bab C |
38 | } |
39 | ||
40 | // --------------------------------------------------------------------------- | |
41 | ||
42 | export { | |
43 | processCreateActivity | |
44 | } | |
45 | ||
46 | // --------------------------------------------------------------------------- | |
47 | ||
f6eebcb3 | 48 | async function processCreateVideo (activity: ActivityCreate) { |
50d6de9c C |
49 | const videoToCreateData = activity.object as VideoTorrentObject |
50 | ||
6691c522 | 51 | const { video, created, autoBlacklisted } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData }) |
cef534ed | 52 | |
6691c522 | 53 | if (created && !autoBlacklisted) Notifier.Instance.notifyOnNewVideo(video) |
50d6de9c | 54 | |
50d6de9c C |
55 | return video |
56 | } | |
57 | ||
418d092a | 58 | async function processCreateCacheFile (activity: ActivityCreate, byActor: ActorModel) { |
c48e82b5 C |
59 | const cacheFile = activity.object as CacheFileObject |
60 | ||
4157cdb1 | 61 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object }) |
c48e82b5 | 62 | |
e5565833 | 63 | await sequelizeTypescript.transaction(async t => { |
b88a4596 | 64 | return createOrUpdateCacheFile(cacheFile, video, byActor, t) |
e5565833 | 65 | }) |
c48e82b5 C |
66 | |
67 | if (video.isOwned()) { | |
68 | // Don't resend the activity to the sender | |
69 | const exceptions = [ byActor ] | |
e5565833 | 70 | await forwardVideoRelatedActivity(activity, undefined, exceptions, video) |
c48e82b5 C |
71 | } |
72 | } | |
73 | ||
848f499d | 74 | async function processCreateVideoComment (activity: ActivityCreate, byActor: ActorModel) { |
83e6519b | 75 | const commentObject = activity.object as VideoCommentObject |
6d852470 C |
76 | const byAccount = byActor.Account |
77 | ||
78 | if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url) | |
79 | ||
ee79b60e C |
80 | let video: VideoModel |
81 | try { | |
82 | const resolveThreadResult = await resolveThread(commentObject.inReplyTo) | |
83 | video = resolveThreadResult.video | |
84 | } catch (err) { | |
85 | logger.debug( | |
86 | 'Cannot process video comment because we could not resolve thread %s. Maybe it was not a video thread, so skip it.', | |
87 | commentObject.inReplyTo, | |
88 | { err } | |
89 | ) | |
7d14d4d2 | 90 | return |
ee79b60e | 91 | } |
2ccaeeb3 | 92 | |
cef534ed | 93 | const { comment, created } = await addVideoComment(video, commentObject.id) |
93ef8a9d | 94 | |
83e6519b C |
95 | if (video.isOwned() && created === true) { |
96 | // Don't resend the activity to the sender | |
97 | const exceptions = [ byActor ] | |
93ef8a9d | 98 | |
83e6519b C |
99 | await forwardVideoRelatedActivity(activity, undefined, exceptions, video) |
100 | } | |
cef534ed C |
101 | |
102 | if (created === true) Notifier.Instance.notifyOnNewComment(comment) | |
6d852470 | 103 | } |
418d092a C |
104 | |
105 | async function processCreatePlaylist (activity: ActivityCreate, byActor: ActorModel) { | |
106 | const playlistObject = activity.object as PlaylistObject | |
107 | const byAccount = byActor.Account | |
108 | ||
109 | if (!byAccount) throw new Error('Cannot create video playlist with the non account actor ' + byActor.url) | |
110 | ||
111 | await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to) | |
112 | } |