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