]>
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' |
848f499d C |
12 | import { processViewActivity } from './process-view' |
13 | import { processDislikeActivity } from './process-dislike' | |
14 | import { processFlagActivity } from './process-flag' | |
e4f97bab | 15 | |
e587e0ec | 16 | async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) { |
e4f97bab C |
17 | const activityObject = activity.object |
18 | const activityType = activityObject.type | |
19 | ||
40ff5707 | 20 | if (activityType === 'View') { |
848f499d C |
21 | return processViewActivity(activity, byActor) |
22 | } | |
23 | ||
24 | if (activityType === 'Dislike') { | |
25 | return retryTransactionWrapper(processDislikeActivity, activity, byActor) | |
26 | } | |
27 | ||
28 | if (activityType === 'Flag') { | |
29 | return retryTransactionWrapper(processFlagActivity, activity, byActor) | |
30 | } | |
31 | ||
32 | if (activityType === 'Video') { | |
f6eebcb3 | 33 | return processCreateVideo(activity) |
848f499d C |
34 | } |
35 | ||
36 | if (activityType === 'Note') { | |
37 | return retryTransactionWrapper(processCreateVideoComment, activity, byActor) | |
38 | } | |
39 | ||
40 | if (activityType === 'CacheFile') { | |
41 | return retryTransactionWrapper(processCacheFile, activity, byActor) | |
e4f97bab C |
42 | } |
43 | ||
44 | logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) | |
0d0e8dd0 | 45 | return Promise.resolve(undefined) |
e4f97bab C |
46 | } |
47 | ||
48 | // --------------------------------------------------------------------------- | |
49 | ||
50 | export { | |
51 | processCreateActivity | |
52 | } | |
53 | ||
54 | // --------------------------------------------------------------------------- | |
55 | ||
f6eebcb3 | 56 | async function processCreateVideo (activity: ActivityCreate) { |
50d6de9c C |
57 | const videoToCreateData = activity.object as VideoTorrentObject |
58 | ||
cef534ed C |
59 | const { video, created } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData }) |
60 | ||
61 | if (created) Notifier.Instance.notifyOnNewVideo(video) | |
50d6de9c | 62 | |
50d6de9c C |
63 | return video |
64 | } | |
65 | ||
848f499d | 66 | async function processCacheFile (activity: ActivityCreate, byActor: ActorModel) { |
c48e82b5 C |
67 | const cacheFile = activity.object as CacheFileObject |
68 | ||
4157cdb1 | 69 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object }) |
c48e82b5 | 70 | |
e5565833 | 71 | await sequelizeTypescript.transaction(async t => { |
b88a4596 | 72 | return createOrUpdateCacheFile(cacheFile, video, byActor, t) |
e5565833 | 73 | }) |
c48e82b5 C |
74 | |
75 | if (video.isOwned()) { | |
76 | // Don't resend the activity to the sender | |
77 | const exceptions = [ byActor ] | |
e5565833 | 78 | await forwardVideoRelatedActivity(activity, undefined, exceptions, video) |
c48e82b5 C |
79 | } |
80 | } | |
81 | ||
848f499d | 82 | async function processCreateVideoComment (activity: ActivityCreate, byActor: ActorModel) { |
83e6519b | 83 | const commentObject = activity.object as VideoCommentObject |
6d852470 C |
84 | const byAccount = byActor.Account |
85 | ||
86 | if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url) | |
87 | ||
83e6519b | 88 | const { video } = await resolveThread(commentObject.inReplyTo) |
2ccaeeb3 | 89 | |
cef534ed | 90 | const { comment, created } = await addVideoComment(video, commentObject.id) |
93ef8a9d | 91 | |
83e6519b C |
92 | if (video.isOwned() && created === true) { |
93 | // Don't resend the activity to the sender | |
94 | const exceptions = [ byActor ] | |
93ef8a9d | 95 | |
83e6519b C |
96 | await forwardVideoRelatedActivity(activity, undefined, exceptions, video) |
97 | } | |
cef534ed C |
98 | |
99 | if (created === true) Notifier.Instance.notifyOnNewComment(comment) | |
6d852470 | 100 | } |