]>
Commit | Line | Data |
---|---|---|
1 | import { ActivityCreate, CacheFileObject, VideoTorrentObject } from '../../../../shared' | |
2 | import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object' | |
3 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | |
4 | import { logger } from '../../../helpers/logger' | |
5 | import { sequelizeTypescript } from '../../../initializers' | |
6 | import { ActorModel } from '../../../models/activitypub/actor' | |
7 | import { addVideoComment, resolveThread } from '../video-comments' | |
8 | import { getOrCreateVideoAndAccountAndChannel } from '../videos' | |
9 | import { forwardVideoRelatedActivity } from '../send/utils' | |
10 | import { createOrUpdateCacheFile } from '../cache-file' | |
11 | import { Notifier } from '../../notifier' | |
12 | import { processViewActivity } from './process-view' | |
13 | import { processDislikeActivity } from './process-dislike' | |
14 | import { processFlagActivity } from './process-flag' | |
15 | ||
16 | async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) { | |
17 | const activityObject = activity.object | |
18 | const activityType = activityObject.type | |
19 | ||
20 | if (activityType === 'View') { | |
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') { | |
33 | return processCreateVideo(activity) | |
34 | } | |
35 | ||
36 | if (activityType === 'Note') { | |
37 | return retryTransactionWrapper(processCreateVideoComment, activity, byActor) | |
38 | } | |
39 | ||
40 | if (activityType === 'CacheFile') { | |
41 | return retryTransactionWrapper(processCacheFile, activity, byActor) | |
42 | } | |
43 | ||
44 | logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) | |
45 | return Promise.resolve(undefined) | |
46 | } | |
47 | ||
48 | // --------------------------------------------------------------------------- | |
49 | ||
50 | export { | |
51 | processCreateActivity | |
52 | } | |
53 | ||
54 | // --------------------------------------------------------------------------- | |
55 | ||
56 | async function processCreateVideo (activity: ActivityCreate) { | |
57 | const videoToCreateData = activity.object as VideoTorrentObject | |
58 | ||
59 | const { video, created } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData }) | |
60 | ||
61 | if (created) Notifier.Instance.notifyOnNewVideo(video) | |
62 | ||
63 | return video | |
64 | } | |
65 | ||
66 | async function processCacheFile (activity: ActivityCreate, byActor: ActorModel) { | |
67 | const cacheFile = activity.object as CacheFileObject | |
68 | ||
69 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object }) | |
70 | ||
71 | await sequelizeTypescript.transaction(async t => { | |
72 | return createOrUpdateCacheFile(cacheFile, video, byActor, t) | |
73 | }) | |
74 | ||
75 | if (video.isOwned()) { | |
76 | // Don't resend the activity to the sender | |
77 | const exceptions = [ byActor ] | |
78 | await forwardVideoRelatedActivity(activity, undefined, exceptions, video) | |
79 | } | |
80 | } | |
81 | ||
82 | async function processCreateVideoComment (activity: ActivityCreate, byActor: ActorModel) { | |
83 | const commentObject = activity.object as VideoCommentObject | |
84 | const byAccount = byActor.Account | |
85 | ||
86 | if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url) | |
87 | ||
88 | const { video } = await resolveThread(commentObject.inReplyTo) | |
89 | ||
90 | const { comment, created } = await addVideoComment(video, commentObject.id) | |
91 | ||
92 | if (video.isOwned() && created === true) { | |
93 | // Don't resend the activity to the sender | |
94 | const exceptions = [ byActor ] | |
95 | ||
96 | await forwardVideoRelatedActivity(activity, undefined, exceptions, video) | |
97 | } | |
98 | ||
99 | if (created === true) Notifier.Instance.notifyOnNewComment(comment) | |
100 | } |