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