]>
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' |
50d6de9c | 10 | import { getOrCreateActorAndServerAndModel } from '../actor' |
83e6519b | 11 | import { addVideoComment, resolveThread } from '../video-comments' |
1297eb5d | 12 | import { getOrCreateVideoAndAccountAndChannel } from '../videos' |
9588d4f4 | 13 | import { forwardActivity, forwardVideoRelatedActivity } from '../send/utils' |
6b616860 | 14 | import { Redis } from '../../redis' |
c48e82b5 | 15 | import { createCacheFile } from '../cache-file' |
e4f97bab | 16 | |
0d0e8dd0 | 17 | async function processCreateActivity (activity: ActivityCreate) { |
e4f97bab C |
18 | const activityObject = activity.object |
19 | const activityType = activityObject.type | |
50d6de9c | 20 | const actor = await getOrCreateActorAndServerAndModel(activity.actor) |
e4f97bab | 21 | |
40ff5707 | 22 | if (activityType === 'View') { |
50d6de9c | 23 | return processCreateView(actor, activity) |
0032ebe9 | 24 | } else if (activityType === 'Dislike') { |
90d4bb81 | 25 | return retryTransactionWrapper(processCreateDislike, actor, activity) |
50d6de9c | 26 | } else if (activityType === 'Video') { |
f6eebcb3 | 27 | return processCreateVideo(activity) |
8e13fa7d | 28 | } else if (activityType === 'Flag') { |
90d4bb81 | 29 | return retryTransactionWrapper(processCreateVideoAbuse, actor, activityObject as VideoAbuseObject) |
6d852470 | 30 | } else if (activityType === 'Note') { |
90d4bb81 | 31 | return retryTransactionWrapper(processCreateVideoComment, actor, activity) |
c48e82b5 C |
32 | } else if (activityType === 'CacheFile') { |
33 | return retryTransactionWrapper(processCacheFile, actor, activity) | |
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 | ||
1297eb5d | 51 | const { video } = await getOrCreateVideoAndAccountAndChannel(videoToCreateData) |
50d6de9c | 52 | |
50d6de9c C |
53 | return video |
54 | } | |
55 | ||
50d6de9c | 56 | async function processCreateDislike (byActor: ActorModel, activity: ActivityCreate) { |
63c93323 | 57 | const dislike = activity.object as DislikeObject |
50d6de9c C |
58 | const byAccount = byActor.Account |
59 | ||
60 | if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url) | |
0032ebe9 | 61 | |
1297eb5d | 62 | const { video } = await getOrCreateVideoAndAccountAndChannel(dislike.object) |
0032ebe9 | 63 | |
2ccaeeb3 | 64 | return sequelizeTypescript.transaction(async t => { |
0032ebe9 C |
65 | const rate = { |
66 | type: 'dislike' as 'dislike', | |
67 | videoId: video.id, | |
68 | accountId: byAccount.id | |
69 | } | |
3fd3ab2d | 70 | const [ , created ] = await AccountVideoRateModel.findOrCreate({ |
0032ebe9 | 71 | where: rate, |
63c93323 C |
72 | defaults: rate, |
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 | ||
1297eb5d | 89 | const { video } = await getOrCreateVideoAndAccountAndChannel(view.object) |
40ff5707 | 90 | |
7bc29171 C |
91 | const actor = await ActorModel.loadByUrl(view.actor) |
92 | if (!actor) throw new Error('Unknown actor ' + view.actor) | |
40ff5707 | 93 | |
6b616860 | 94 | await Redis.Instance.addVideoView(video.id) |
40ff5707 | 95 | |
63c93323 C |
96 | if (video.isOwned()) { |
97 | // Don't resend the activity to the sender | |
6d852470 | 98 | const exceptions = [ byActor ] |
63c93323 C |
99 | await forwardActivity(activity, undefined, exceptions) |
100 | } | |
40ff5707 C |
101 | } |
102 | ||
c48e82b5 C |
103 | async function processCacheFile (byActor: ActorModel, activity: ActivityCreate) { |
104 | const cacheFile = activity.object as CacheFileObject | |
105 | ||
106 | const { video } = await getOrCreateVideoAndAccountAndChannel(cacheFile.object) | |
107 | ||
108 | await createCacheFile(cacheFile, video, byActor) | |
109 | ||
110 | if (video.isOwned()) { | |
111 | // Don't resend the activity to the sender | |
112 | const exceptions = [ byActor ] | |
113 | await forwardActivity(activity, undefined, exceptions) | |
114 | } | |
115 | } | |
116 | ||
90d4bb81 | 117 | async function processCreateVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) { |
8e13fa7d C |
118 | logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object) |
119 | ||
50d6de9c C |
120 | const account = actor.Account |
121 | if (!account) throw new Error('Cannot create dislike with the non account actor ' + actor.url) | |
122 | ||
1297eb5d | 123 | const { video } = await getOrCreateVideoAndAccountAndChannel(videoAbuseToCreateData.object) |
8e13fa7d | 124 | |
2ccaeeb3 | 125 | return sequelizeTypescript.transaction(async t => { |
8e13fa7d C |
126 | const videoAbuseData = { |
127 | reporterAccountId: account.id, | |
128 | reason: videoAbuseToCreateData.content, | |
268eebed C |
129 | videoId: video.id, |
130 | state: VideoAbuseState.PENDING | |
8e13fa7d C |
131 | } |
132 | ||
c48e82b5 | 133 | await VideoAbuseModel.create(videoAbuseData, { transaction: t }) |
8e13fa7d C |
134 | |
135 | logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object) | |
136 | }) | |
137 | } | |
6d852470 | 138 | |
90d4bb81 | 139 | async function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) { |
83e6519b | 140 | const commentObject = activity.object as VideoCommentObject |
6d852470 C |
141 | const byAccount = byActor.Account |
142 | ||
143 | if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url) | |
144 | ||
83e6519b | 145 | const { video } = await resolveThread(commentObject.inReplyTo) |
2ccaeeb3 | 146 | |
83e6519b | 147 | const { created } = await addVideoComment(video, commentObject.id) |
93ef8a9d | 148 | |
83e6519b C |
149 | if (video.isOwned() && created === true) { |
150 | // Don't resend the activity to the sender | |
151 | const exceptions = [ byActor ] | |
93ef8a9d | 152 | |
83e6519b C |
153 | await forwardVideoRelatedActivity(activity, undefined, exceptions, video) |
154 | } | |
6d852470 | 155 | } |