]>
Commit | Line | Data |
---|---|---|
268eebed | 1 | import { ActivityCreate, 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' |
6d852470 | 10 | import { VideoCommentModel } from '../../../models/video/video-comment' |
50d6de9c | 11 | import { getOrCreateActorAndServerAndModel } from '../actor' |
7acee6f1 C |
12 | import { resolveThread } from '../video-comments' |
13 | import { getOrCreateAccountAndVideoAndChannel } from '../videos' | |
9588d4f4 | 14 | import { forwardActivity, forwardVideoRelatedActivity } from '../send/utils' |
e4f97bab | 15 | |
0d0e8dd0 | 16 | async function processCreateActivity (activity: ActivityCreate) { |
e4f97bab C |
17 | const activityObject = activity.object |
18 | const activityType = activityObject.type | |
50d6de9c | 19 | const actor = await getOrCreateActorAndServerAndModel(activity.actor) |
e4f97bab | 20 | |
40ff5707 | 21 | if (activityType === 'View') { |
50d6de9c | 22 | return processCreateView(actor, activity) |
0032ebe9 | 23 | } else if (activityType === 'Dislike') { |
90d4bb81 | 24 | return retryTransactionWrapper(processCreateDislike, actor, activity) |
50d6de9c | 25 | } else if (activityType === 'Video') { |
f6eebcb3 | 26 | return processCreateVideo(activity) |
8e13fa7d | 27 | } else if (activityType === 'Flag') { |
90d4bb81 | 28 | return retryTransactionWrapper(processCreateVideoAbuse, actor, activityObject as VideoAbuseObject) |
6d852470 | 29 | } else if (activityType === 'Note') { |
90d4bb81 | 30 | return retryTransactionWrapper(processCreateVideoComment, actor, activity) |
e4f97bab C |
31 | } |
32 | ||
33 | logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) | |
0d0e8dd0 | 34 | return Promise.resolve(undefined) |
e4f97bab C |
35 | } |
36 | ||
37 | // --------------------------------------------------------------------------- | |
38 | ||
39 | export { | |
40 | processCreateActivity | |
41 | } | |
42 | ||
43 | // --------------------------------------------------------------------------- | |
44 | ||
f6eebcb3 | 45 | async function processCreateVideo (activity: ActivityCreate) { |
50d6de9c C |
46 | const videoToCreateData = activity.object as VideoTorrentObject |
47 | ||
f6eebcb3 | 48 | const { video } = await getOrCreateAccountAndVideoAndChannel(videoToCreateData) |
50d6de9c | 49 | |
50d6de9c C |
50 | return video |
51 | } | |
52 | ||
50d6de9c | 53 | async function processCreateDislike (byActor: ActorModel, activity: ActivityCreate) { |
63c93323 | 54 | const dislike = activity.object as DislikeObject |
50d6de9c C |
55 | const byAccount = byActor.Account |
56 | ||
57 | if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url) | |
0032ebe9 | 58 | |
2ccaeeb3 | 59 | const { video } = await getOrCreateAccountAndVideoAndChannel(dislike.object) |
0032ebe9 | 60 | |
2ccaeeb3 | 61 | return sequelizeTypescript.transaction(async t => { |
0032ebe9 C |
62 | const rate = { |
63 | type: 'dislike' as 'dislike', | |
64 | videoId: video.id, | |
65 | accountId: byAccount.id | |
66 | } | |
3fd3ab2d | 67 | const [ , created ] = await AccountVideoRateModel.findOrCreate({ |
0032ebe9 | 68 | where: rate, |
63c93323 C |
69 | defaults: rate, |
70 | transaction: t | |
0032ebe9 | 71 | }) |
f00984c0 | 72 | if (created === true) await video.increment('dislikes', { transaction: t }) |
0032ebe9 | 73 | |
63c93323 C |
74 | if (video.isOwned() && created === true) { |
75 | // Don't resend the activity to the sender | |
50d6de9c | 76 | const exceptions = [ byActor ] |
9588d4f4 C |
77 | |
78 | await forwardVideoRelatedActivity(activity, t, exceptions, video) | |
63c93323 | 79 | } |
0032ebe9 C |
80 | }) |
81 | } | |
82 | ||
6d852470 | 83 | async function processCreateView (byActor: ActorModel, activity: ActivityCreate) { |
63c93323 C |
84 | const view = activity.object as ViewObject |
85 | ||
2ccaeeb3 | 86 | const { video } = await getOrCreateAccountAndVideoAndChannel(view.object) |
40ff5707 | 87 | |
7bc29171 C |
88 | const actor = await ActorModel.loadByUrl(view.actor) |
89 | if (!actor) throw new Error('Unknown actor ' + view.actor) | |
40ff5707 C |
90 | |
91 | await video.increment('views') | |
92 | ||
63c93323 C |
93 | if (video.isOwned()) { |
94 | // Don't resend the activity to the sender | |
6d852470 | 95 | const exceptions = [ byActor ] |
63c93323 C |
96 | await forwardActivity(activity, undefined, exceptions) |
97 | } | |
40ff5707 C |
98 | } |
99 | ||
90d4bb81 | 100 | async function processCreateVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) { |
8e13fa7d C |
101 | logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object) |
102 | ||
50d6de9c C |
103 | const account = actor.Account |
104 | if (!account) throw new Error('Cannot create dislike with the non account actor ' + actor.url) | |
105 | ||
2ccaeeb3 | 106 | const { video } = await getOrCreateAccountAndVideoAndChannel(videoAbuseToCreateData.object) |
8e13fa7d | 107 | |
2ccaeeb3 | 108 | return sequelizeTypescript.transaction(async t => { |
8e13fa7d C |
109 | const videoAbuseData = { |
110 | reporterAccountId: account.id, | |
111 | reason: videoAbuseToCreateData.content, | |
268eebed C |
112 | videoId: video.id, |
113 | state: VideoAbuseState.PENDING | |
8e13fa7d C |
114 | } |
115 | ||
3fd3ab2d | 116 | await VideoAbuseModel.create(videoAbuseData) |
8e13fa7d C |
117 | |
118 | logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object) | |
119 | }) | |
120 | } | |
6d852470 | 121 | |
90d4bb81 | 122 | async function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) { |
6d852470 C |
123 | const comment = activity.object as VideoCommentObject |
124 | const byAccount = byActor.Account | |
125 | ||
126 | if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url) | |
127 | ||
2ccaeeb3 C |
128 | const { video, parents } = await resolveThread(comment.inReplyTo) |
129 | ||
6d852470 | 130 | return sequelizeTypescript.transaction(async t => { |
2ccaeeb3 C |
131 | let originCommentId = null |
132 | let inReplyToCommentId = null | |
133 | ||
134 | if (parents.length !== 0) { | |
135 | const parent = parents[0] | |
136 | ||
137 | originCommentId = parent.getThreadId() | |
138 | inReplyToCommentId = parent.id | |
139 | } | |
6d852470 C |
140 | |
141 | // This is a new thread | |
2ccaeeb3 C |
142 | const objectToCreate = { |
143 | url: comment.id, | |
144 | text: comment.content, | |
145 | originCommentId, | |
146 | inReplyToCommentId, | |
147 | videoId: video.id, | |
148 | accountId: byAccount.id | |
ea44f375 C |
149 | } |
150 | ||
93ef8a9d C |
151 | const options = { |
152 | where: { | |
153 | url: objectToCreate.url | |
154 | }, | |
155 | defaults: objectToCreate, | |
156 | transaction: t | |
157 | } | |
158 | const [ ,created ] = await VideoCommentModel.findOrCreate(options) | |
159 | ||
160 | if (video.isOwned() && created === true) { | |
ea44f375 C |
161 | // Don't resend the activity to the sender |
162 | const exceptions = [ byActor ] | |
93ef8a9d | 163 | |
9588d4f4 | 164 | await forwardVideoRelatedActivity(activity, t, exceptions, video) |
ea44f375 | 165 | } |
6d852470 C |
166 | }) |
167 | } |