diff options
Diffstat (limited to 'server/lib/activitypub/process')
-rw-r--r-- | server/lib/activitypub/process/index.ts | 1 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-create.ts | 35 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-like.ts | 50 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-undo.ts | 99 | ||||
-rw-r--r-- | server/lib/activitypub/process/process.ts | 6 |
5 files changed, 177 insertions, 14 deletions
diff --git a/server/lib/activitypub/process/index.ts b/server/lib/activitypub/process/index.ts index c68312053..e25c261cc 100644 --- a/server/lib/activitypub/process/index.ts +++ b/server/lib/activitypub/process/index.ts | |||
@@ -5,5 +5,6 @@ export * from './process-announce' | |||
5 | export * from './process-create' | 5 | export * from './process-create' |
6 | export * from './process-delete' | 6 | export * from './process-delete' |
7 | export * from './process-follow' | 7 | export * from './process-follow' |
8 | export * from './process-like' | ||
8 | export * from './process-undo' | 9 | export * from './process-undo' |
9 | export * from './process-update' | 10 | export * from './process-update' |
diff --git a/server/lib/activitypub/process/process-create.ts b/server/lib/activitypub/process/process-create.ts index 1777733a0..147bbd132 100644 --- a/server/lib/activitypub/process/process-create.ts +++ b/server/lib/activitypub/process/process-create.ts | |||
@@ -5,9 +5,10 @@ import { logger, retryTransactionWrapper } from '../../../helpers' | |||
5 | import { database as db } from '../../../initializers' | 5 | import { database as db } from '../../../initializers' |
6 | import { AccountInstance } from '../../../models/account/account-interface' | 6 | import { AccountInstance } from '../../../models/account/account-interface' |
7 | import { getOrCreateAccountAndServer } from '../account' | 7 | import { getOrCreateAccountAndServer } from '../account' |
8 | import { sendCreateViewToVideoFollowers } from '../send/send-create' | 8 | import { sendCreateDislikeToVideoFollowers, sendCreateViewToVideoFollowers } from '../send/send-create' |
9 | import { getVideoChannelActivityPubUrl } from '../url' | 9 | import { getVideoChannelActivityPubUrl } from '../url' |
10 | import { videoChannelActivityObjectToDBAttributes } from './misc' | 10 | import { videoChannelActivityObjectToDBAttributes } from './misc' |
11 | import { DislikeObject } from '../../../../shared/models/activitypub/objects/dislike-object' | ||
11 | 12 | ||
12 | async function processCreateActivity (activity: ActivityCreate) { | 13 | async function processCreateActivity (activity: ActivityCreate) { |
13 | const activityObject = activity.object | 14 | const activityObject = activity.object |
@@ -16,6 +17,8 @@ async function processCreateActivity (activity: ActivityCreate) { | |||
16 | 17 | ||
17 | if (activityType === 'View') { | 18 | if (activityType === 'View') { |
18 | return processCreateView(activityObject as ViewObject) | 19 | return processCreateView(activityObject as ViewObject) |
20 | } else if (activityType === 'Dislike') { | ||
21 | return processCreateDislike(account, activityObject as DislikeObject) | ||
19 | } else if (activityType === 'VideoChannel') { | 22 | } else if (activityType === 'VideoChannel') { |
20 | return processCreateVideoChannel(account, activityObject as VideoChannelObject) | 23 | return processCreateVideoChannel(account, activityObject as VideoChannelObject) |
21 | } else if (activityType === 'Flag') { | 24 | } else if (activityType === 'Flag') { |
@@ -34,6 +37,36 @@ export { | |||
34 | 37 | ||
35 | // --------------------------------------------------------------------------- | 38 | // --------------------------------------------------------------------------- |
36 | 39 | ||
40 | async function processCreateDislike (byAccount: AccountInstance, dislike: DislikeObject) { | ||
41 | const options = { | ||
42 | arguments: [ byAccount, dislike ], | ||
43 | errorMessage: 'Cannot dislike the video with many retries.' | ||
44 | } | ||
45 | |||
46 | return retryTransactionWrapper(createVideoDislike, options) | ||
47 | } | ||
48 | |||
49 | function createVideoDislike (byAccount: AccountInstance, dislike: DislikeObject) { | ||
50 | return db.sequelize.transaction(async t => { | ||
51 | const video = await db.Video.loadByUrlAndPopulateAccount(dislike.object) | ||
52 | |||
53 | if (!video) throw new Error('Unknown video ' + dislike.object) | ||
54 | |||
55 | const rate = { | ||
56 | type: 'dislike' as 'dislike', | ||
57 | videoId: video.id, | ||
58 | accountId: byAccount.id | ||
59 | } | ||
60 | const [ , created ] = await db.AccountVideoRate.findOrCreate({ | ||
61 | where: rate, | ||
62 | defaults: rate | ||
63 | }) | ||
64 | await video.increment('dislikes') | ||
65 | |||
66 | if (video.isOwned() && created === true) await sendCreateDislikeToVideoFollowers(byAccount, video, undefined) | ||
67 | }) | ||
68 | } | ||
69 | |||
37 | async function processCreateView (view: ViewObject) { | 70 | async function processCreateView (view: ViewObject) { |
38 | const video = await db.Video.loadByUrlAndPopulateAccount(view.object) | 71 | const video = await db.Video.loadByUrlAndPopulateAccount(view.object) |
39 | 72 | ||
diff --git a/server/lib/activitypub/process/process-like.ts b/server/lib/activitypub/process/process-like.ts new file mode 100644 index 000000000..d77b30f24 --- /dev/null +++ b/server/lib/activitypub/process/process-like.ts | |||
@@ -0,0 +1,50 @@ | |||
1 | import { ActivityLike } from '../../../../shared/models/activitypub/activity' | ||
2 | import { database as db } from '../../../initializers' | ||
3 | import { AccountInstance } from '../../../models/account/account-interface' | ||
4 | import { getOrCreateAccountAndServer } from '../account' | ||
5 | import { sendLikeToVideoFollowers } from '../send/send-like' | ||
6 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | ||
7 | |||
8 | async function processLikeActivity (activity: ActivityLike) { | ||
9 | const account = await getOrCreateAccountAndServer(activity.actor) | ||
10 | |||
11 | return processLikeVideo(account, activity.object) | ||
12 | } | ||
13 | |||
14 | // --------------------------------------------------------------------------- | ||
15 | |||
16 | export { | ||
17 | processLikeActivity | ||
18 | } | ||
19 | |||
20 | // --------------------------------------------------------------------------- | ||
21 | |||
22 | async function processLikeVideo (byAccount: AccountInstance, videoUrl: string) { | ||
23 | const options = { | ||
24 | arguments: [ byAccount, videoUrl ], | ||
25 | errorMessage: 'Cannot like the video with many retries.' | ||
26 | } | ||
27 | |||
28 | return retryTransactionWrapper(createVideoLike, options) | ||
29 | } | ||
30 | |||
31 | function createVideoLike (byAccount: AccountInstance, videoUrl: string) { | ||
32 | return db.sequelize.transaction(async t => { | ||
33 | const video = await db.Video.loadByUrlAndPopulateAccount(videoUrl) | ||
34 | |||
35 | if (!video) throw new Error('Unknown video ' + videoUrl) | ||
36 | |||
37 | const rate = { | ||
38 | type: 'like' as 'like', | ||
39 | videoId: video.id, | ||
40 | accountId: byAccount.id | ||
41 | } | ||
42 | const [ , created ] = await db.AccountVideoRate.findOrCreate({ | ||
43 | where: rate, | ||
44 | defaults: rate | ||
45 | }) | ||
46 | await video.increment('likes') | ||
47 | |||
48 | if (video.isOwned() && created === true) await sendLikeToVideoFollowers(byAccount, video, undefined) | ||
49 | }) | ||
50 | } | ||
diff --git a/server/lib/activitypub/process/process-undo.ts b/server/lib/activitypub/process/process-undo.ts index 610b800fb..caa835714 100644 --- a/server/lib/activitypub/process/process-undo.ts +++ b/server/lib/activitypub/process/process-undo.ts | |||
@@ -1,20 +1,20 @@ | |||
1 | import { ActivityUndo } from '../../../../shared/models/activitypub/activity' | 1 | import { ActivityCreate, ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub/activity' |
2 | import { logger } from '../../../helpers/logger' | 2 | import { logger } from '../../../helpers/logger' |
3 | import { database as db } from '../../../initializers' | 3 | import { database as db } from '../../../initializers' |
4 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | ||
5 | import { DislikeObject } from '../../../../shared/models/activitypub/objects/dislike-object' | ||
6 | import { sendUndoLikeToVideoFollowers } from '../send/send-undo' | ||
7 | import { sendUndoDislikeToVideoFollowers } from '../index' | ||
4 | 8 | ||
5 | async function processUndoActivity (activity: ActivityUndo) { | 9 | async function processUndoActivity (activity: ActivityUndo) { |
6 | const activityToUndo = activity.object | 10 | const activityToUndo = activity.object |
7 | 11 | ||
8 | if (activityToUndo.type === 'Follow') { | 12 | if (activityToUndo.type === 'Like') { |
9 | const follower = await db.Account.loadByUrl(activity.actor) | 13 | return processUndoLike(activity.actor, activityToUndo) |
10 | const following = await db.Account.loadByUrl(activityToUndo.object) | 14 | } else if (activityToUndo.type === 'Create' && activityToUndo.object.type === 'Dislike') { |
11 | const accountFollow = await db.AccountFollow.loadByAccountAndTarget(follower.id, following.id) | 15 | return processUndoDislike(activity.actor, activityToUndo.object) |
12 | 16 | } else if (activityToUndo.type === 'Follow') { | |
13 | if (!accountFollow) throw new Error(`'Unknown account follow ${follower.id} -> ${following.id}.`) | 17 | return processUndoFollow(activity.actor, activityToUndo) |
14 | |||
15 | await accountFollow.destroy() | ||
16 | |||
17 | return undefined | ||
18 | } | 18 | } |
19 | 19 | ||
20 | logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id }) | 20 | logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id }) |
@@ -29,3 +29,80 @@ export { | |||
29 | } | 29 | } |
30 | 30 | ||
31 | // --------------------------------------------------------------------------- | 31 | // --------------------------------------------------------------------------- |
32 | |||
33 | function processUndoLike (actor: string, likeActivity: ActivityLike) { | ||
34 | const options = { | ||
35 | arguments: [ actor, likeActivity ], | ||
36 | errorMessage: 'Cannot undo like with many retries.' | ||
37 | } | ||
38 | |||
39 | return retryTransactionWrapper(undoLike, options) | ||
40 | } | ||
41 | |||
42 | function undoLike (actor: string, likeActivity: ActivityLike) { | ||
43 | return db.sequelize.transaction(async t => { | ||
44 | const byAccount = await db.Account.loadByUrl(actor, t) | ||
45 | if (!byAccount) throw new Error('Unknown account ' + actor) | ||
46 | |||
47 | const video = await db.Video.loadByUrlAndPopulateAccount(likeActivity.object) | ||
48 | if (!video) throw new Error('Unknown video ' + likeActivity.actor) | ||
49 | |||
50 | 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 | |||
53 | await rate.destroy({ transaction: t }) | ||
54 | await video.decrement('likes') | ||
55 | |||
56 | if (video.isOwned()) await sendUndoLikeToVideoFollowers(byAccount, video, t) | ||
57 | }) | ||
58 | } | ||
59 | |||
60 | function processUndoDislike (actor: string, dislikeCreateActivity: DislikeObject) { | ||
61 | const options = { | ||
62 | arguments: [ actor, dislikeCreateActivity ], | ||
63 | errorMessage: 'Cannot undo dislike with many retries.' | ||
64 | } | ||
65 | |||
66 | return retryTransactionWrapper(undoDislike, options) | ||
67 | } | ||
68 | |||
69 | function undoDislike (actor: string, dislike: DislikeObject) { | ||
70 | return db.sequelize.transaction(async t => { | ||
71 | const byAccount = await db.Account.loadByUrl(actor, t) | ||
72 | if (!byAccount) throw new Error('Unknown account ' + actor) | ||
73 | |||
74 | const video = await db.Video.loadByUrlAndPopulateAccount(dislike.object) | ||
75 | if (!video) throw new Error('Unknown video ' + dislike.actor) | ||
76 | |||
77 | 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}.`) | ||
79 | |||
80 | await rate.destroy({ transaction: t }) | ||
81 | await video.decrement('dislikes') | ||
82 | |||
83 | if (video.isOwned()) await sendUndoDislikeToVideoFollowers(byAccount, video, t) | ||
84 | }) | ||
85 | } | ||
86 | |||
87 | function processUndoFollow (actor: string, followActivity: ActivityFollow) { | ||
88 | const options = { | ||
89 | arguments: [ actor, followActivity ], | ||
90 | errorMessage: 'Cannot undo follow with many retries.' | ||
91 | } | ||
92 | |||
93 | return retryTransactionWrapper(undoFollow, options) | ||
94 | } | ||
95 | |||
96 | function undoFollow (actor: string, followActivity: ActivityFollow) { | ||
97 | return db.sequelize.transaction(async t => { | ||
98 | const follower = await db.Account.loadByUrl(actor, t) | ||
99 | const following = await db.Account.loadByUrl(followActivity.object, t) | ||
100 | const accountFollow = await db.AccountFollow.loadByAccountAndTarget(follower.id, following.id, t) | ||
101 | |||
102 | if (!accountFollow) throw new Error(`'Unknown account follow ${follower.id} -> ${following.id}.`) | ||
103 | |||
104 | await accountFollow.destroy({ transaction: t }) | ||
105 | |||
106 | return undefined | ||
107 | }) | ||
108 | } | ||
diff --git a/server/lib/activitypub/process/process.ts b/server/lib/activitypub/process/process.ts index 613597341..942bce0e6 100644 --- a/server/lib/activitypub/process/process.ts +++ b/server/lib/activitypub/process/process.ts | |||
@@ -1,4 +1,5 @@ | |||
1 | import { Activity, ActivityType } from '../../../../shared/models/activitypub/activity' | 1 | import { Activity, ActivityType } from '../../../../shared/models/activitypub/activity' |
2 | import { logger } from '../../../helpers/logger' | ||
2 | import { AccountInstance } from '../../../models/account/account-interface' | 3 | import { AccountInstance } from '../../../models/account/account-interface' |
3 | import { processAcceptActivity } from './process-accept' | 4 | import { processAcceptActivity } from './process-accept' |
4 | import { processAddActivity } from './process-add' | 5 | import { processAddActivity } from './process-add' |
@@ -6,9 +7,9 @@ import { processAnnounceActivity } from './process-announce' | |||
6 | import { processCreateActivity } from './process-create' | 7 | import { processCreateActivity } from './process-create' |
7 | import { processDeleteActivity } from './process-delete' | 8 | import { processDeleteActivity } from './process-delete' |
8 | import { processFollowActivity } from './process-follow' | 9 | import { processFollowActivity } from './process-follow' |
10 | import { processLikeActivity } from './process-like' | ||
9 | import { processUndoActivity } from './process-undo' | 11 | import { processUndoActivity } from './process-undo' |
10 | import { processUpdateActivity } from './process-update' | 12 | import { processUpdateActivity } from './process-update' |
11 | import { logger } from '../../../helpers/logger' | ||
12 | 13 | ||
13 | const processActivity: { [ P in ActivityType ]: (activity: Activity, inboxAccount?: AccountInstance) => Promise<any> } = { | 14 | const processActivity: { [ P in ActivityType ]: (activity: Activity, inboxAccount?: AccountInstance) => Promise<any> } = { |
14 | Create: processCreateActivity, | 15 | Create: processCreateActivity, |
@@ -18,7 +19,8 @@ const processActivity: { [ P in ActivityType ]: (activity: Activity, inboxAccoun | |||
18 | Follow: processFollowActivity, | 19 | Follow: processFollowActivity, |
19 | Accept: processAcceptActivity, | 20 | Accept: processAcceptActivity, |
20 | Announce: processAnnounceActivity, | 21 | Announce: processAnnounceActivity, |
21 | Undo: processUndoActivity | 22 | Undo: processUndoActivity, |
23 | Like: processLikeActivity | ||
22 | } | 24 | } |
23 | 25 | ||
24 | async function processActivities (activities: Activity[], inboxAccount?: AccountInstance) { | 26 | async function processActivities (activities: Activity[], inboxAccount?: AccountInstance) { |