diff options
Diffstat (limited to 'server/lib/activitypub/process/process-create.ts')
-rw-r--r-- | server/lib/activitypub/process/process-create.ts | 122 |
1 files changed, 33 insertions, 89 deletions
diff --git a/server/lib/activitypub/process/process-create.ts b/server/lib/activitypub/process/process-create.ts index cefe89db0..5f4d793a5 100644 --- a/server/lib/activitypub/process/process-create.ts +++ b/server/lib/activitypub/process/process-create.ts | |||
@@ -1,34 +1,44 @@ | |||
1 | import { ActivityCreate, CacheFileObject, VideoAbuseState, VideoTorrentObject } from '../../../../shared' | 1 | import { ActivityCreate, CacheFileObject, VideoTorrentObject } from '../../../../shared' |
2 | import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects' | ||
3 | import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object' | 2 | import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object' |
4 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | 3 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
5 | import { logger } from '../../../helpers/logger' | 4 | import { logger } from '../../../helpers/logger' |
6 | import { sequelizeTypescript } from '../../../initializers' | 5 | import { sequelizeTypescript } from '../../../initializers' |
7 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' | ||
8 | import { ActorModel } from '../../../models/activitypub/actor' | 6 | import { ActorModel } from '../../../models/activitypub/actor' |
9 | import { VideoAbuseModel } from '../../../models/video/video-abuse' | ||
10 | import { addVideoComment, resolveThread } from '../video-comments' | 7 | import { addVideoComment, resolveThread } from '../video-comments' |
11 | import { getOrCreateVideoAndAccountAndChannel } from '../videos' | 8 | import { getOrCreateVideoAndAccountAndChannel } from '../videos' |
12 | import { forwardVideoRelatedActivity } from '../send/utils' | 9 | import { forwardVideoRelatedActivity } from '../send/utils' |
13 | import { Redis } from '../../redis' | ||
14 | import { createOrUpdateCacheFile } from '../cache-file' | 10 | import { createOrUpdateCacheFile } from '../cache-file' |
11 | import { Notifier } from '../../notifier' | ||
12 | import { processViewActivity } from './process-view' | ||
13 | import { processDislikeActivity } from './process-dislike' | ||
14 | import { processFlagActivity } from './process-flag' | ||
15 | 15 | ||
16 | async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) { | 16 | async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) { |
17 | const activityObject = activity.object | 17 | const activityObject = activity.object |
18 | const activityType = activityObject.type | 18 | const activityType = activityObject.type |
19 | 19 | ||
20 | if (activityType === 'View') { | 20 | if (activityType === 'View') { |
21 | return processCreateView(byActor, activity) | 21 | return processViewActivity(activity, byActor) |
22 | } else if (activityType === 'Dislike') { | 22 | } |
23 | return retryTransactionWrapper(processCreateDislike, byActor, activity) | 23 | |
24 | } else if (activityType === 'Video') { | 24 | if (activityType === 'Dislike') { |
25 | return retryTransactionWrapper(processDislikeActivity, activity, byActor) | ||
26 | } | ||
27 | |||
28 | if (activityType === 'Flag') { | ||
29 | return retryTransactionWrapper(processFlagActivity, activity, byActor) | ||
30 | } | ||
31 | |||
32 | if (activityType === 'Video') { | ||
25 | return processCreateVideo(activity) | 33 | return processCreateVideo(activity) |
26 | } else if (activityType === 'Flag') { | 34 | } |
27 | return retryTransactionWrapper(processCreateVideoAbuse, byActor, activityObject as VideoAbuseObject) | 35 | |
28 | } else if (activityType === 'Note') { | 36 | if (activityType === 'Note') { |
29 | return retryTransactionWrapper(processCreateVideoComment, byActor, activity) | 37 | return retryTransactionWrapper(processCreateVideoComment, activity, byActor) |
30 | } else if (activityType === 'CacheFile') { | 38 | } |
31 | return retryTransactionWrapper(processCacheFile, byActor, activity) | 39 | |
40 | if (activityType === 'CacheFile') { | ||
41 | return retryTransactionWrapper(processCacheFile, activity, byActor) | ||
32 | } | 42 | } |
33 | 43 | ||
34 | logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) | 44 | logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) |
@@ -46,60 +56,14 @@ export { | |||
46 | async function processCreateVideo (activity: ActivityCreate) { | 56 | async function processCreateVideo (activity: ActivityCreate) { |
47 | const videoToCreateData = activity.object as VideoTorrentObject | 57 | const videoToCreateData = activity.object as VideoTorrentObject |
48 | 58 | ||
49 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData }) | 59 | const { video, created } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData }) |
50 | |||
51 | return video | ||
52 | } | ||
53 | |||
54 | async function processCreateDislike (byActor: ActorModel, activity: ActivityCreate) { | ||
55 | const dislike = activity.object as DislikeObject | ||
56 | const byAccount = byActor.Account | ||
57 | |||
58 | if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url) | ||
59 | |||
60 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislike.object }) | ||
61 | |||
62 | return sequelizeTypescript.transaction(async t => { | ||
63 | const rate = { | ||
64 | type: 'dislike' as 'dislike', | ||
65 | videoId: video.id, | ||
66 | accountId: byAccount.id | ||
67 | } | ||
68 | const [ , created ] = await AccountVideoRateModel.findOrCreate({ | ||
69 | where: rate, | ||
70 | defaults: rate, | ||
71 | transaction: t | ||
72 | }) | ||
73 | if (created === true) await video.increment('dislikes', { transaction: t }) | ||
74 | |||
75 | if (video.isOwned() && created === true) { | ||
76 | // Don't resend the activity to the sender | ||
77 | const exceptions = [ byActor ] | ||
78 | |||
79 | await forwardVideoRelatedActivity(activity, t, exceptions, video) | ||
80 | } | ||
81 | }) | ||
82 | } | ||
83 | |||
84 | async function processCreateView (byActor: ActorModel, activity: ActivityCreate) { | ||
85 | const view = activity.object as ViewObject | ||
86 | |||
87 | const options = { | ||
88 | videoObject: view.object, | ||
89 | fetchType: 'only-video' as 'only-video' | ||
90 | } | ||
91 | const { video } = await getOrCreateVideoAndAccountAndChannel(options) | ||
92 | 60 | ||
93 | await Redis.Instance.addVideoView(video.id) | 61 | if (created) Notifier.Instance.notifyOnNewVideo(video) |
94 | 62 | ||
95 | if (video.isOwned()) { | 63 | return video |
96 | // Don't resend the activity to the sender | ||
97 | const exceptions = [ byActor ] | ||
98 | await forwardVideoRelatedActivity(activity, undefined, exceptions, video) | ||
99 | } | ||
100 | } | 64 | } |
101 | 65 | ||
102 | async function processCacheFile (byActor: ActorModel, activity: ActivityCreate) { | 66 | async function processCacheFile (activity: ActivityCreate, byActor: ActorModel) { |
103 | const cacheFile = activity.object as CacheFileObject | 67 | const cacheFile = activity.object as CacheFileObject |
104 | 68 | ||
105 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object }) | 69 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object }) |
@@ -115,29 +79,7 @@ async function processCacheFile (byActor: ActorModel, activity: ActivityCreate) | |||
115 | } | 79 | } |
116 | } | 80 | } |
117 | 81 | ||
118 | async function processCreateVideoAbuse (byActor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) { | 82 | async function processCreateVideoComment (activity: ActivityCreate, byActor: ActorModel) { |
119 | logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object) | ||
120 | |||
121 | const account = byActor.Account | ||
122 | if (!account) throw new Error('Cannot create dislike with the non account actor ' + byActor.url) | ||
123 | |||
124 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoAbuseToCreateData.object }) | ||
125 | |||
126 | return sequelizeTypescript.transaction(async t => { | ||
127 | const videoAbuseData = { | ||
128 | reporterAccountId: account.id, | ||
129 | reason: videoAbuseToCreateData.content, | ||
130 | videoId: video.id, | ||
131 | state: VideoAbuseState.PENDING | ||
132 | } | ||
133 | |||
134 | await VideoAbuseModel.create(videoAbuseData, { transaction: t }) | ||
135 | |||
136 | logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object) | ||
137 | }) | ||
138 | } | ||
139 | |||
140 | async function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) { | ||
141 | const commentObject = activity.object as VideoCommentObject | 83 | const commentObject = activity.object as VideoCommentObject |
142 | const byAccount = byActor.Account | 84 | const byAccount = byActor.Account |
143 | 85 | ||
@@ -145,7 +87,7 @@ async function processCreateVideoComment (byActor: ActorModel, activity: Activit | |||
145 | 87 | ||
146 | const { video } = await resolveThread(commentObject.inReplyTo) | 88 | const { video } = await resolveThread(commentObject.inReplyTo) |
147 | 89 | ||
148 | const { created } = await addVideoComment(video, commentObject.id) | 90 | const { comment, created } = await addVideoComment(video, commentObject.id) |
149 | 91 | ||
150 | if (video.isOwned() && created === true) { | 92 | if (video.isOwned() && created === true) { |
151 | // Don't resend the activity to the sender | 93 | // Don't resend the activity to the sender |
@@ -153,4 +95,6 @@ async function processCreateVideoComment (byActor: ActorModel, activity: Activit | |||
153 | 95 | ||
154 | await forwardVideoRelatedActivity(activity, undefined, exceptions, video) | 96 | await forwardVideoRelatedActivity(activity, undefined, exceptions, video) |
155 | } | 97 | } |
98 | |||
99 | if (created === true) Notifier.Instance.notifyOnNewComment(comment) | ||
156 | } | 100 | } |