aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/process/process-create.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-11-23 14:19:55 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-11-27 19:40:53 +0100
commit0032ebe94aa83fab761c7de3ceb6210ac4532824 (patch)
tree3ea407d7ea6de4c7f7bc66caba7e23c0cc4036e3 /server/lib/activitypub/process/process-create.ts
parentd52eb8f656242c7e34afdb2dee681861fb9bce35 (diff)
downloadPeerTube-0032ebe94aa83fab761c7de3ceb6210ac4532824.tar.gz
PeerTube-0032ebe94aa83fab761c7de3ceb6210ac4532824.tar.zst
PeerTube-0032ebe94aa83fab761c7de3ceb6210ac4532824.zip
Federate likes/dislikes
Diffstat (limited to 'server/lib/activitypub/process/process-create.ts')
-rw-r--r--server/lib/activitypub/process/process-create.ts35
1 files changed, 34 insertions, 1 deletions
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'
5import { database as db } from '../../../initializers' 5import { database as db } from '../../../initializers'
6import { AccountInstance } from '../../../models/account/account-interface' 6import { AccountInstance } from '../../../models/account/account-interface'
7import { getOrCreateAccountAndServer } from '../account' 7import { getOrCreateAccountAndServer } from '../account'
8import { sendCreateViewToVideoFollowers } from '../send/send-create' 8import { sendCreateDislikeToVideoFollowers, sendCreateViewToVideoFollowers } from '../send/send-create'
9import { getVideoChannelActivityPubUrl } from '../url' 9import { getVideoChannelActivityPubUrl } from '../url'
10import { videoChannelActivityObjectToDBAttributes } from './misc' 10import { videoChannelActivityObjectToDBAttributes } from './misc'
11import { DislikeObject } from '../../../../shared/models/activitypub/objects/dislike-object'
11 12
12async function processCreateActivity (activity: ActivityCreate) { 13async 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
40async 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
49function 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
37async function processCreateView (view: ViewObject) { 70async function processCreateView (view: ViewObject) {
38 const video = await db.Video.loadByUrlAndPopulateAccount(view.object) 71 const video = await db.Video.loadByUrlAndPopulateAccount(view.object)
39 72