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