aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/process
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-11-24 13:41:10 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-11-27 19:40:53 +0100
commit63c93323ecdeaa4b6183d75dd3f13469e1ef3ebd (patch)
tree3ee4b351025d4b19f6e880754df44fa7605b683d /server/lib/activitypub/process
parentd4f1e94c89336255537b0b82913591f00e716201 (diff)
downloadPeerTube-63c93323ecdeaa4b6183d75dd3f13469e1ef3ebd.tar.gz
PeerTube-63c93323ecdeaa4b6183d75dd3f13469e1ef3ebd.tar.zst
PeerTube-63c93323ecdeaa4b6183d75dd3f13469e1ef3ebd.zip
Correctly forward like/dislikes and undo
Diffstat (limited to 'server/lib/activitypub/process')
-rw-r--r--server/lib/activitypub/process/process-add.ts2
-rw-r--r--server/lib/activitypub/process/process-create.ts42
-rw-r--r--server/lib/activitypub/process/process-like.ts25
-rw-r--r--server/lib/activitypub/process/process-undo.ts43
4 files changed, 71 insertions, 41 deletions
diff --git a/server/lib/activitypub/process/process-add.ts b/server/lib/activitypub/process/process-add.ts
index 433e68eb6..98280b9f0 100644
--- a/server/lib/activitypub/process/process-add.ts
+++ b/server/lib/activitypub/process/process-add.ts
@@ -76,7 +76,7 @@ function addRemoteVideo (account: AccountInstance,
76 if (videoChannel.Account.id !== account.id) throw new Error('Video channel is not owned by this account.') 76 if (videoChannel.Account.id !== account.id) throw new Error('Video channel is not owned by this account.')
77 77
78 const videoFromDatabase = await db.Video.loadByUUIDOrURL(videoToCreateData.uuid, videoToCreateData.id, t) 78 const videoFromDatabase = await db.Video.loadByUUIDOrURL(videoToCreateData.uuid, videoToCreateData.id, t)
79 if (videoFromDatabase) throw new Error('Video with this UUID/Url already exists.') 79 if (videoFromDatabase) return videoFromDatabase
80 80
81 const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoToCreateData, activity.to, activity.cc) 81 const videoData = await videoActivityObjectToDBAttributes(videoChannel, videoToCreateData, activity.to, activity.cc)
82 const video = db.Video.build(videoData) 82 const video = db.Video.build(videoData)
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)
diff --git a/server/lib/activitypub/process/process-like.ts b/server/lib/activitypub/process/process-like.ts
index d77b30f24..0347f95be 100644
--- a/server/lib/activitypub/process/process-like.ts
+++ b/server/lib/activitypub/process/process-like.ts
@@ -1,14 +1,14 @@
1import { ActivityLike } from '../../../../shared/models/activitypub/activity' 1import { ActivityLike } from '../../../../shared/models/activitypub/activity'
2import { retryTransactionWrapper } from '../../../helpers/database-utils'
2import { database as db } from '../../../initializers' 3import { database as db } from '../../../initializers'
3import { AccountInstance } from '../../../models/account/account-interface' 4import { AccountInstance } from '../../../models/account/account-interface'
4import { getOrCreateAccountAndServer } from '../account' 5import { getOrCreateAccountAndServer } from '../account'
5import { sendLikeToVideoFollowers } from '../send/send-like' 6import { forwardActivity } from '../send/misc'
6import { retryTransactionWrapper } from '../../../helpers/database-utils'
7 7
8async function processLikeActivity (activity: ActivityLike) { 8async function processLikeActivity (activity: ActivityLike) {
9 const account = await getOrCreateAccountAndServer(activity.actor) 9 const account = await getOrCreateAccountAndServer(activity.actor)
10 10
11 return processLikeVideo(account, activity.object) 11 return processLikeVideo(account, activity)
12} 12}
13 13
14// --------------------------------------------------------------------------- 14// ---------------------------------------------------------------------------
@@ -19,16 +19,18 @@ export {
19 19
20// --------------------------------------------------------------------------- 20// ---------------------------------------------------------------------------
21 21
22async function processLikeVideo (byAccount: AccountInstance, videoUrl: string) { 22async function processLikeVideo (byAccount: AccountInstance, activity: ActivityLike) {
23 const options = { 23 const options = {
24 arguments: [ byAccount, videoUrl ], 24 arguments: [ byAccount, activity ],
25 errorMessage: 'Cannot like the video with many retries.' 25 errorMessage: 'Cannot like the video with many retries.'
26 } 26 }
27 27
28 return retryTransactionWrapper(createVideoLike, options) 28 return retryTransactionWrapper(createVideoLike, options)
29} 29}
30 30
31function createVideoLike (byAccount: AccountInstance, videoUrl: string) { 31function createVideoLike (byAccount: AccountInstance, activity: ActivityLike) {
32 const videoUrl = activity.object
33
32 return db.sequelize.transaction(async t => { 34 return db.sequelize.transaction(async t => {
33 const video = await db.Video.loadByUrlAndPopulateAccount(videoUrl) 35 const video = await db.Video.loadByUrlAndPopulateAccount(videoUrl)
34 36
@@ -41,10 +43,15 @@ function createVideoLike (byAccount: AccountInstance, videoUrl: string) {
41 } 43 }
42 const [ , created ] = await db.AccountVideoRate.findOrCreate({ 44 const [ , created ] = await db.AccountVideoRate.findOrCreate({
43 where: rate, 45 where: rate,
44 defaults: rate 46 defaults: rate,
47 transaction: t
45 }) 48 })
46 await video.increment('likes') 49 await video.increment('likes', { transaction: t })
47 50
48 if (video.isOwned() && created === true) await sendLikeToVideoFollowers(byAccount, video, undefined) 51 if (video.isOwned() && created === true) {
52 // Don't resend the activity to the sender
53 const exceptions = [ byAccount ]
54 await forwardActivity(activity, t, exceptions)
55 }
49 }) 56 })
50} 57}
diff --git a/server/lib/activitypub/process/process-undo.ts b/server/lib/activitypub/process/process-undo.ts
index 9fe066c01..cc221045f 100644
--- a/server/lib/activitypub/process/process-undo.ts
+++ b/server/lib/activitypub/process/process-undo.ts
@@ -3,16 +3,15 @@ import { DislikeObject } from '../../../../shared/models/activitypub/objects/dis
3import { retryTransactionWrapper } from '../../../helpers/database-utils' 3import { retryTransactionWrapper } from '../../../helpers/database-utils'
4import { logger } from '../../../helpers/logger' 4import { logger } from '../../../helpers/logger'
5import { database as db } from '../../../initializers' 5import { database as db } from '../../../initializers'
6import { sendUndoDislikeToVideoFollowers } from '../index' 6import { forwardActivity } from '../send/misc'
7import { sendUndoLikeToVideoFollowers } from '../send/send-undo'
8 7
9async function processUndoActivity (activity: ActivityUndo) { 8async function processUndoActivity (activity: ActivityUndo) {
10 const activityToUndo = activity.object 9 const activityToUndo = activity.object
11 10
12 if (activityToUndo.type === 'Like') { 11 if (activityToUndo.type === 'Like') {
13 return processUndoLike(activity.actor, activityToUndo) 12 return processUndoLike(activity.actor, activity)
14 } else if (activityToUndo.type === 'Create' && activityToUndo.object.type === 'Dislike') { 13 } else if (activityToUndo.type === 'Create' && activityToUndo.object.type === 'Dislike') {
15 return processUndoDislike(activity.actor, activityToUndo.object) 14 return processUndoDislike(activity.actor, activity)
16 } else if (activityToUndo.type === 'Follow') { 15 } else if (activityToUndo.type === 'Follow') {
17 return processUndoFollow(activity.actor, activityToUndo) 16 return processUndoFollow(activity.actor, activityToUndo)
18 } 17 }
@@ -30,57 +29,69 @@ export {
30 29
31// --------------------------------------------------------------------------- 30// ---------------------------------------------------------------------------
32 31
33function processUndoLike (actor: string, likeActivity: ActivityLike) { 32function processUndoLike (actor: string, activity: ActivityUndo) {
34 const options = { 33 const options = {
35 arguments: [ actor, likeActivity ], 34 arguments: [ actor, activity ],
36 errorMessage: 'Cannot undo like with many retries.' 35 errorMessage: 'Cannot undo like with many retries.'
37 } 36 }
38 37
39 return retryTransactionWrapper(undoLike, options) 38 return retryTransactionWrapper(undoLike, options)
40} 39}
41 40
42function undoLike (actor: string, likeActivity: ActivityLike) { 41function undoLike (actor: string, activity: ActivityUndo) {
42 const likeActivity = activity.object as ActivityLike
43
43 return db.sequelize.transaction(async t => { 44 return db.sequelize.transaction(async t => {
44 const byAccount = await db.Account.loadByUrl(actor, t) 45 const byAccount = await db.Account.loadByUrl(actor, t)
45 if (!byAccount) throw new Error('Unknown account ' + actor) 46 if (!byAccount) throw new Error('Unknown account ' + actor)
46 47
47 const video = await db.Video.loadByUrlAndPopulateAccount(likeActivity.object) 48 const video = await db.Video.loadByUrlAndPopulateAccount(likeActivity.object, t)
48 if (!video) throw new Error('Unknown video ' + likeActivity.actor) 49 if (!video) throw new Error('Unknown video ' + likeActivity.actor)
49 50
50 const rate = await db.AccountVideoRate.load(byAccount.id, video.id, t) 51 const rate = await db.AccountVideoRate.load(byAccount.id, video.id, t)
51 if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) 52 if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`)
52 53
53 await rate.destroy({ transaction: t }) 54 await rate.destroy({ transaction: t })
54 await video.decrement('likes') 55 await video.decrement('likes', { transaction: t })
55 56
56 if (video.isOwned()) await sendUndoLikeToVideoFollowers(byAccount, video, t) 57 if (video.isOwned()) {
58 // Don't resend the activity to the sender
59 const exceptions = [ byAccount ]
60 await forwardActivity(activity, t, exceptions)
61 }
57 }) 62 })
58} 63}
59 64
60function processUndoDislike (actor: string, dislikeCreateActivity: DislikeObject) { 65function processUndoDislike (actor: string, activity: ActivityUndo) {
61 const options = { 66 const options = {
62 arguments: [ actor, dislikeCreateActivity ], 67 arguments: [ actor, activity ],
63 errorMessage: 'Cannot undo dislike with many retries.' 68 errorMessage: 'Cannot undo dislike with many retries.'
64 } 69 }
65 70
66 return retryTransactionWrapper(undoDislike, options) 71 return retryTransactionWrapper(undoDislike, options)
67} 72}
68 73
69function undoDislike (actor: string, dislike: DislikeObject) { 74function undoDislike (actor: string, activity: ActivityUndo) {
75 const dislike = activity.object.object as DislikeObject
76
70 return db.sequelize.transaction(async t => { 77 return db.sequelize.transaction(async t => {
71 const byAccount = await db.Account.loadByUrl(actor, t) 78 const byAccount = await db.Account.loadByUrl(actor, t)
72 if (!byAccount) throw new Error('Unknown account ' + actor) 79 if (!byAccount) throw new Error('Unknown account ' + actor)
73 80
74 const video = await db.Video.loadByUrlAndPopulateAccount(dislike.object) 81 const video = await db.Video.loadByUrlAndPopulateAccount(dislike.object, t)
75 if (!video) throw new Error('Unknown video ' + dislike.actor) 82 if (!video) throw new Error('Unknown video ' + dislike.actor)
76 83
77 const rate = await db.AccountVideoRate.load(byAccount.id, video.id, t) 84 const rate = await db.AccountVideoRate.load(byAccount.id, video.id, t)
78 if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`) 85 if (!rate) throw new Error(`Unknown rate by account ${byAccount.id} for video ${video.id}.`)
79 86
80 await rate.destroy({ transaction: t }) 87 await rate.destroy({ transaction: t })
81 await video.decrement('dislikes') 88 await video.decrement('dislikes', { transaction: t })
82 89
83 if (video.isOwned()) await sendUndoDislikeToVideoFollowers(byAccount, video, t) 90 if (video.isOwned()) {
91 // Don't resend the activity to the sender
92 const exceptions = [ byAccount ]
93 await forwardActivity(activity, t, exceptions)
94 }
84 }) 95 })
85} 96}
86 97