aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/process/process-create.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/activitypub/process/process-create.ts')
-rw-r--r--server/lib/activitypub/process/process-create.ts42
1 files changed, 27 insertions, 15 deletions
diff --git a/server/lib/activitypub/process/process-create.ts b/server/lib/activitypub/process/process-create.ts
index 147bbd132..1f982598b 100644
--- a/server/lib/activitypub/process/process-create.ts
+++ b/server/lib/activitypub/process/process-create.ts
@@ -1,14 +1,14 @@
1import { ActivityCreate, VideoChannelObject } from '../../../../shared' 1import { ActivityCreate, VideoChannelObject } from '../../../../shared'
2import { DislikeObject } from '../../../../shared/models/activitypub/objects/dislike-object'
2import { VideoAbuseObject } from '../../../../shared/models/activitypub/objects/video-abuse-object' 3import { VideoAbuseObject } from '../../../../shared/models/activitypub/objects/video-abuse-object'
3import { ViewObject } from '../../../../shared/models/activitypub/objects/view-object' 4import { ViewObject } from '../../../../shared/models/activitypub/objects/view-object'
4import { logger, retryTransactionWrapper } from '../../../helpers' 5import { logger, retryTransactionWrapper } from '../../../helpers'
5import { database as db } from '../../../initializers' 6import { database as db } from '../../../initializers'
6import { AccountInstance } from '../../../models/account/account-interface' 7import { AccountInstance } from '../../../models/account/account-interface'
7import { getOrCreateAccountAndServer } from '../account' 8import { getOrCreateAccountAndServer } from '../account'
8import { sendCreateDislikeToVideoFollowers, sendCreateViewToVideoFollowers } from '../send/send-create' 9import { forwardActivity } from '../send/misc'
9import { getVideoChannelActivityPubUrl } from '../url' 10import { getVideoChannelActivityPubUrl } from '../url'
10import { videoChannelActivityObjectToDBAttributes } from './misc' 11import { videoChannelActivityObjectToDBAttributes } from './misc'
11import { DislikeObject } from '../../../../shared/models/activitypub/objects/dislike-object'
12 12
13async function processCreateActivity (activity: ActivityCreate) { 13async function processCreateActivity (activity: ActivityCreate) {
14 const activityObject = activity.object 14 const activityObject = activity.object
@@ -16,9 +16,9 @@ async function processCreateActivity (activity: ActivityCreate) {
16 const account = await getOrCreateAccountAndServer(activity.actor) 16 const account = await getOrCreateAccountAndServer(activity.actor)
17 17
18 if (activityType === 'View') { 18 if (activityType === 'View') {
19 return processCreateView(activityObject as ViewObject) 19 return processCreateView(account, activity)
20 } else if (activityType === 'Dislike') { 20 } else if (activityType === 'Dislike') {
21 return processCreateDislike(account, activityObject as DislikeObject) 21 return processCreateDislike(account, activity)
22 } else if (activityType === 'VideoChannel') { 22 } else if (activityType === 'VideoChannel') {
23 return processCreateVideoChannel(account, activityObject as VideoChannelObject) 23 return processCreateVideoChannel(account, activityObject as VideoChannelObject)
24 } else if (activityType === 'Flag') { 24 } else if (activityType === 'Flag') {
@@ -37,19 +37,20 @@ export {
37 37
38// --------------------------------------------------------------------------- 38// ---------------------------------------------------------------------------
39 39
40async function processCreateDislike (byAccount: AccountInstance, dislike: DislikeObject) { 40async function processCreateDislike (byAccount: AccountInstance, activity: ActivityCreate) {
41 const options = { 41 const options = {
42 arguments: [ byAccount, dislike ], 42 arguments: [ byAccount, activity ],
43 errorMessage: 'Cannot dislike the video with many retries.' 43 errorMessage: 'Cannot dislike the video with many retries.'
44 } 44 }
45 45
46 return retryTransactionWrapper(createVideoDislike, options) 46 return retryTransactionWrapper(createVideoDislike, options)
47} 47}
48 48
49function createVideoDislike (byAccount: AccountInstance, dislike: DislikeObject) { 49function createVideoDislike (byAccount: AccountInstance, activity: ActivityCreate) {
50 return db.sequelize.transaction(async t => { 50 const dislike = activity.object as DislikeObject
51 const video = await db.Video.loadByUrlAndPopulateAccount(dislike.object)
52 51
52 return db.sequelize.transaction(async t => {
53 const video = await db.Video.loadByUrlAndPopulateAccount(dislike.object, t)
53 if (!video) throw new Error('Unknown video ' + dislike.object) 54 if (!video) throw new Error('Unknown video ' + dislike.object)
54 55
55 const rate = { 56 const rate = {
@@ -59,15 +60,22 @@ function createVideoDislike (byAccount: AccountInstance, dislike: DislikeObject)
59 } 60 }
60 const [ , created ] = await db.AccountVideoRate.findOrCreate({ 61 const [ , created ] = await db.AccountVideoRate.findOrCreate({
61 where: rate, 62 where: rate,
62 defaults: rate 63 defaults: rate,
64 transaction: t
63 }) 65 })
64 await video.increment('dislikes') 66 await video.increment('dislikes', { transaction: t })
65 67
66 if (video.isOwned() && created === true) await sendCreateDislikeToVideoFollowers(byAccount, video, undefined) 68 if (video.isOwned() && created === true) {
69 // Don't resend the activity to the sender
70 const exceptions = [ byAccount ]
71 await forwardActivity(activity, t, exceptions)
72 }
67 }) 73 })
68} 74}
69 75
70async function processCreateView (view: ViewObject) { 76async function processCreateView (byAccount: AccountInstance, activity: ActivityCreate) {
77 const view = activity.object as ViewObject
78
71 const video = await db.Video.loadByUrlAndPopulateAccount(view.object) 79 const video = await db.Video.loadByUrlAndPopulateAccount(view.object)
72 80
73 if (!video) throw new Error('Unknown video ' + view.object) 81 if (!video) throw new Error('Unknown video ' + view.object)
@@ -77,7 +85,11 @@ async function processCreateView (view: ViewObject) {
77 85
78 await video.increment('views') 86 await video.increment('views')
79 87
80 if (video.isOwned()) await sendCreateViewToVideoFollowers(account, video, undefined) 88 if (video.isOwned()) {
89 // Don't resend the activity to the sender
90 const exceptions = [ byAccount ]
91 await forwardActivity(activity, undefined, exceptions)
92 }
81} 93}
82 94
83function processCreateVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) { 95function processCreateVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) {
@@ -94,7 +106,7 @@ function addRemoteVideoChannel (account: AccountInstance, videoChannelToCreateDa
94 106
95 return db.sequelize.transaction(async t => { 107 return db.sequelize.transaction(async t => {
96 let videoChannel = await db.VideoChannel.loadByUUIDOrUrl(videoChannelToCreateData.uuid, videoChannelToCreateData.id, t) 108 let videoChannel = await db.VideoChannel.loadByUUIDOrUrl(videoChannelToCreateData.uuid, videoChannelToCreateData.id, t)
97 if (videoChannel) throw new Error('Video channel with this URL/UUID already exists.') 109 if (videoChannel) return videoChannel
98 110
99 const videoChannelData = videoChannelActivityObjectToDBAttributes(videoChannelToCreateData, account) 111 const videoChannelData = videoChannelActivityObjectToDBAttributes(videoChannelToCreateData, account)
100 videoChannel = db.VideoChannel.build(videoChannelData) 112 videoChannel = db.VideoChannel.build(videoChannelData)