]>
Commit | Line | Data |
---|---|---|
c48e82b5 | 1 | import { ActivityCreate, CacheFileObject, VideoAbuseState, VideoTorrentObject } from '../../../../shared' |
3fd3ab2d | 2 | import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects' |
6d852470 | 3 | import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object' |
da854ddd C |
4 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
5 | import { logger } from '../../../helpers/logger' | |
3fd3ab2d | 6 | import { sequelizeTypescript } from '../../../initializers' |
3fd3ab2d | 7 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' |
50d6de9c | 8 | import { ActorModel } from '../../../models/activitypub/actor' |
3fd3ab2d | 9 | import { VideoAbuseModel } from '../../../models/video/video-abuse' |
83e6519b | 10 | import { addVideoComment, resolveThread } from '../video-comments' |
1297eb5d | 11 | import { getOrCreateVideoAndAccountAndChannel } from '../videos' |
9588d4f4 | 12 | import { forwardActivity, forwardVideoRelatedActivity } from '../send/utils' |
6b616860 | 13 | import { Redis } from '../../redis' |
c48e82b5 | 14 | import { createCacheFile } from '../cache-file' |
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') { |
e587e0ec | 21 | return processCreateView(byActor, activity) |
0032ebe9 | 22 | } else if (activityType === 'Dislike') { |
e587e0ec | 23 | return retryTransactionWrapper(processCreateDislike, byActor, activity) |
50d6de9c | 24 | } else if (activityType === 'Video') { |
f6eebcb3 | 25 | return processCreateVideo(activity) |
8e13fa7d | 26 | } else if (activityType === 'Flag') { |
e587e0ec | 27 | return retryTransactionWrapper(processCreateVideoAbuse, byActor, activityObject as VideoAbuseObject) |
6d852470 | 28 | } else if (activityType === 'Note') { |
e587e0ec | 29 | return retryTransactionWrapper(processCreateVideoComment, byActor, activity) |
c48e82b5 | 30 | } else if (activityType === 'CacheFile') { |
e587e0ec | 31 | return retryTransactionWrapper(processCacheFile, byActor, activity) |
e4f97bab C |
32 | } |
33 | ||
34 | logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) | |
0d0e8dd0 | 35 | return Promise.resolve(undefined) |
e4f97bab C |
36 | } |
37 | ||
38 | // --------------------------------------------------------------------------- | |
39 | ||
40 | export { | |
41 | processCreateActivity | |
42 | } | |
43 | ||
44 | // --------------------------------------------------------------------------- | |
45 | ||
f6eebcb3 | 46 | async function processCreateVideo (activity: ActivityCreate) { |
50d6de9c C |
47 | const videoToCreateData = activity.object as VideoTorrentObject |
48 | ||
4157cdb1 | 49 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData }) |
50d6de9c | 50 | |
50d6de9c C |
51 | return video |
52 | } | |
53 | ||
50d6de9c | 54 | async function processCreateDislike (byActor: ActorModel, activity: ActivityCreate) { |
63c93323 | 55 | const dislike = activity.object as DislikeObject |
50d6de9c C |
56 | const byAccount = byActor.Account |
57 | ||
58 | if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url) | |
0032ebe9 | 59 | |
4157cdb1 | 60 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislike.object }) |
0032ebe9 | 61 | |
2ccaeeb3 | 62 | return sequelizeTypescript.transaction(async t => { |
0032ebe9 C |
63 | const rate = { |
64 | type: 'dislike' as 'dislike', | |
65 | videoId: video.id, | |
66 | accountId: byAccount.id | |
67 | } | |
3fd3ab2d | 68 | const [ , created ] = await AccountVideoRateModel.findOrCreate({ |
0032ebe9 | 69 | where: rate, |
63c93323 C |
70 | defaults: rate, |
71 | transaction: t | |
0032ebe9 | 72 | }) |
f00984c0 | 73 | if (created === true) await video.increment('dislikes', { transaction: t }) |
0032ebe9 | 74 | |
63c93323 C |
75 | if (video.isOwned() && created === true) { |
76 | // Don't resend the activity to the sender | |
50d6de9c | 77 | const exceptions = [ byActor ] |
9588d4f4 C |
78 | |
79 | await forwardVideoRelatedActivity(activity, t, exceptions, video) | |
63c93323 | 80 | } |
0032ebe9 C |
81 | }) |
82 | } | |
83 | ||
6d852470 | 84 | async function processCreateView (byActor: ActorModel, activity: ActivityCreate) { |
63c93323 C |
85 | const view = activity.object as ViewObject |
86 | ||
d4defe07 C |
87 | const options = { |
88 | videoObject: view.object, | |
89 | fetchType: 'only-video' as 'only-video' | |
90 | } | |
91 | const { video } = await getOrCreateVideoAndAccountAndChannel(options) | |
40ff5707 | 92 | |
6b616860 | 93 | await Redis.Instance.addVideoView(video.id) |
40ff5707 | 94 | |
63c93323 C |
95 | if (video.isOwned()) { |
96 | // Don't resend the activity to the sender | |
6d852470 | 97 | const exceptions = [ byActor ] |
63c93323 C |
98 | await forwardActivity(activity, undefined, exceptions) |
99 | } | |
40ff5707 C |
100 | } |
101 | ||
c48e82b5 C |
102 | async function processCacheFile (byActor: ActorModel, activity: ActivityCreate) { |
103 | const cacheFile = activity.object as CacheFileObject | |
104 | ||
4157cdb1 | 105 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object }) |
c48e82b5 C |
106 | |
107 | await createCacheFile(cacheFile, video, byActor) | |
108 | ||
109 | if (video.isOwned()) { | |
110 | // Don't resend the activity to the sender | |
111 | const exceptions = [ byActor ] | |
112 | await forwardActivity(activity, undefined, exceptions) | |
113 | } | |
114 | } | |
115 | ||
e587e0ec | 116 | async function processCreateVideoAbuse (byActor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) { |
8e13fa7d C |
117 | logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object) |
118 | ||
e587e0ec C |
119 | const account = byActor.Account |
120 | if (!account) throw new Error('Cannot create dislike with the non account actor ' + byActor.url) | |
50d6de9c | 121 | |
4157cdb1 | 122 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoAbuseToCreateData.object }) |
8e13fa7d | 123 | |
2ccaeeb3 | 124 | return sequelizeTypescript.transaction(async t => { |
8e13fa7d C |
125 | const videoAbuseData = { |
126 | reporterAccountId: account.id, | |
127 | reason: videoAbuseToCreateData.content, | |
268eebed C |
128 | videoId: video.id, |
129 | state: VideoAbuseState.PENDING | |
8e13fa7d C |
130 | } |
131 | ||
c48e82b5 | 132 | await VideoAbuseModel.create(videoAbuseData, { transaction: t }) |
8e13fa7d C |
133 | |
134 | logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object) | |
135 | }) | |
136 | } | |
6d852470 | 137 | |
90d4bb81 | 138 | async function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) { |
83e6519b | 139 | const commentObject = activity.object as VideoCommentObject |
6d852470 C |
140 | const byAccount = byActor.Account |
141 | ||
142 | if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url) | |
143 | ||
83e6519b | 144 | const { video } = await resolveThread(commentObject.inReplyTo) |
2ccaeeb3 | 145 | |
83e6519b | 146 | const { created } = await addVideoComment(video, commentObject.id) |
93ef8a9d | 147 | |
83e6519b C |
148 | if (video.isOwned() && created === true) { |
149 | // Don't resend the activity to the sender | |
150 | const exceptions = [ byActor ] | |
93ef8a9d | 151 | |
83e6519b C |
152 | await forwardVideoRelatedActivity(activity, undefined, exceptions, video) |
153 | } | |
6d852470 | 154 | } |