diff options
Diffstat (limited to 'server/controllers')
-rw-r--r-- | server/controllers/activitypub/client.ts | 36 | ||||
-rw-r--r-- | server/controllers/activitypub/inbox.ts | 6 | ||||
-rw-r--r-- | server/controllers/api/videos/rate.ts | 17 |
3 files changed, 46 insertions, 13 deletions
diff --git a/server/controllers/activitypub/client.ts b/server/controllers/activitypub/client.ts index 433186179..ffbf1ba19 100644 --- a/server/controllers/activitypub/client.ts +++ b/server/controllers/activitypub/client.ts | |||
@@ -3,17 +3,22 @@ import * as express from 'express' | |||
3 | import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos' | 3 | import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos' |
4 | import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub' | 4 | import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub' |
5 | import { CONFIG, ROUTE_CACHE_LIFETIME } from '../../initializers' | 5 | import { CONFIG, ROUTE_CACHE_LIFETIME } from '../../initializers' |
6 | import { buildAnnounceWithVideoAudience } from '../../lib/activitypub/send' | 6 | import { buildAnnounceWithVideoAudience, buildDislikeActivity, buildLikeActivity } from '../../lib/activitypub/send' |
7 | import { audiencify, getAudience } from '../../lib/activitypub/audience' | 7 | import { audiencify, getAudience } from '../../lib/activitypub/audience' |
8 | import { buildCreateActivity } from '../../lib/activitypub/send/send-create' | 8 | import { buildCreateActivity } from '../../lib/activitypub/send/send-create' |
9 | import { | 9 | import { |
10 | asyncMiddleware, | 10 | asyncMiddleware, |
11 | videosShareValidator, | ||
11 | executeIfActivityPub, | 12 | executeIfActivityPub, |
12 | localAccountValidator, | 13 | localAccountValidator, |
13 | localVideoChannelValidator, | 14 | localVideoChannelValidator, |
14 | videosCustomGetValidator | 15 | videosCustomGetValidator |
15 | } from '../../middlewares' | 16 | } from '../../middlewares' |
16 | import { videoCommentGetValidator, videosGetValidator, videosShareValidator } from '../../middlewares/validators' | 17 | import { |
18 | getAccountVideoRateValidator, | ||
19 | videoCommentGetValidator, | ||
20 | videosGetValidator | ||
21 | } from '../../middlewares/validators' | ||
17 | import { AccountModel } from '../../models/account/account' | 22 | import { AccountModel } from '../../models/account/account' |
18 | import { ActorModel } from '../../models/activitypub/actor' | 23 | import { ActorModel } from '../../models/activitypub/actor' |
19 | import { ActorFollowModel } from '../../models/activitypub/actor-follow' | 24 | import { ActorFollowModel } from '../../models/activitypub/actor-follow' |
@@ -25,6 +30,7 @@ import { cacheRoute } from '../../middlewares/cache' | |||
25 | import { activityPubResponse } from './utils' | 30 | import { activityPubResponse } from './utils' |
26 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' | 31 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' |
27 | import { | 32 | import { |
33 | getRateUrl, | ||
28 | getVideoCommentsActivityPubUrl, | 34 | getVideoCommentsActivityPubUrl, |
29 | getVideoDislikesActivityPubUrl, | 35 | getVideoDislikesActivityPubUrl, |
30 | getVideoLikesActivityPubUrl, | 36 | getVideoLikesActivityPubUrl, |
@@ -48,6 +54,14 @@ activityPubClientRouter.get('/accounts?/:name/following', | |||
48 | executeIfActivityPub(asyncMiddleware(localAccountValidator)), | 54 | executeIfActivityPub(asyncMiddleware(localAccountValidator)), |
49 | executeIfActivityPub(asyncMiddleware(accountFollowingController)) | 55 | executeIfActivityPub(asyncMiddleware(accountFollowingController)) |
50 | ) | 56 | ) |
57 | activityPubClientRouter.get('/accounts?/:name/likes/:videoId', | ||
58 | executeIfActivityPub(asyncMiddleware(getAccountVideoRateValidator('like'))), | ||
59 | executeIfActivityPub(getAccountVideoRate('like')) | ||
60 | ) | ||
61 | activityPubClientRouter.get('/accounts?/:name/dislikes/:videoId', | ||
62 | executeIfActivityPub(asyncMiddleware(getAccountVideoRateValidator('dislike'))), | ||
63 | executeIfActivityPub(getAccountVideoRate('dislike')) | ||
64 | ) | ||
51 | 65 | ||
52 | activityPubClientRouter.get('/videos/watch/:id', | 66 | activityPubClientRouter.get('/videos/watch/:id', |
53 | executeIfActivityPub(asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS))), | 67 | executeIfActivityPub(asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS))), |
@@ -62,7 +76,7 @@ activityPubClientRouter.get('/videos/watch/:id/announces', | |||
62 | executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))), | 76 | executeIfActivityPub(asyncMiddleware(videosCustomGetValidator('only-video'))), |
63 | executeIfActivityPub(asyncMiddleware(videoAnnouncesController)) | 77 | executeIfActivityPub(asyncMiddleware(videoAnnouncesController)) |
64 | ) | 78 | ) |
65 | activityPubClientRouter.get('/videos/watch/:id/announces/:accountId', | 79 | activityPubClientRouter.get('/videos/watch/:id/announces/:actorId', |
66 | executeIfActivityPub(asyncMiddleware(videosShareValidator)), | 80 | executeIfActivityPub(asyncMiddleware(videosShareValidator)), |
67 | executeIfActivityPub(asyncMiddleware(videoAnnounceController)) | 81 | executeIfActivityPub(asyncMiddleware(videoAnnounceController)) |
68 | ) | 82 | ) |
@@ -133,6 +147,20 @@ async function accountFollowingController (req: express.Request, res: express.Re | |||
133 | return activityPubResponse(activityPubContextify(activityPubResult), res) | 147 | return activityPubResponse(activityPubContextify(activityPubResult), res) |
134 | } | 148 | } |
135 | 149 | ||
150 | function getAccountVideoRate (rateType: VideoRateType) { | ||
151 | return (req: express.Request, res: express.Response) => { | ||
152 | const accountVideoRate: AccountVideoRateModel = res.locals.accountVideoRate | ||
153 | |||
154 | const byActor = accountVideoRate.Account.Actor | ||
155 | const url = getRateUrl(rateType, byActor, accountVideoRate.Video) | ||
156 | const APObject = rateType === 'like' | ||
157 | ? buildLikeActivity(url, byActor, accountVideoRate.Video) | ||
158 | : buildCreateActivity(url, byActor, buildDislikeActivity(url, byActor, accountVideoRate.Video)) | ||
159 | |||
160 | return activityPubResponse(activityPubContextify(APObject), res) | ||
161 | } | ||
162 | } | ||
163 | |||
136 | async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) { | 164 | async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) { |
137 | const video: VideoModel = res.locals.video | 165 | const video: VideoModel = res.locals.video |
138 | 166 | ||
@@ -276,7 +304,7 @@ function videoRates (req: express.Request, rateType: VideoRateType, video: Video | |||
276 | const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count) | 304 | const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count) |
277 | return { | 305 | return { |
278 | total: result.count, | 306 | total: result.count, |
279 | data: result.rows.map(r => r.Account.Actor.url) | 307 | data: result.rows.map(r => r.url) |
280 | } | 308 | } |
281 | } | 309 | } |
282 | return activityPubCollectionPagination(url, handler, req.query.page) | 310 | return activityPubCollectionPagination(url, handler, req.query.page) |
diff --git a/server/controllers/activitypub/inbox.ts b/server/controllers/activitypub/inbox.ts index 738d155eb..f0e65015b 100644 --- a/server/controllers/activitypub/inbox.ts +++ b/server/controllers/activitypub/inbox.ts | |||
@@ -43,11 +43,13 @@ export { | |||
43 | // --------------------------------------------------------------------------- | 43 | // --------------------------------------------------------------------------- |
44 | 44 | ||
45 | const inboxQueue = queue<{ activities: Activity[], signatureActor?: ActorModel, inboxActor?: ActorModel }, Error>((task, cb) => { | 45 | const inboxQueue = queue<{ activities: Activity[], signatureActor?: ActorModel, inboxActor?: ActorModel }, Error>((task, cb) => { |
46 | processActivities(task.activities, task.signatureActor, task.inboxActor) | 46 | const options = { signatureActor: task.signatureActor, inboxActor: task.inboxActor } |
47 | |||
48 | processActivities(task.activities, options) | ||
47 | .then(() => cb()) | 49 | .then(() => cb()) |
48 | }) | 50 | }) |
49 | 51 | ||
50 | function inboxController (req: express.Request, res: express.Response, next: express.NextFunction) { | 52 | function inboxController (req: express.Request, res: express.Response) { |
51 | const rootActivity: RootActivity = req.body | 53 | const rootActivity: RootActivity = req.body |
52 | let activities: Activity[] = [] | 54 | let activities: Activity[] = [] |
53 | 55 | ||
diff --git a/server/controllers/api/videos/rate.ts b/server/controllers/api/videos/rate.ts index dc322bb0c..53952a0a2 100644 --- a/server/controllers/api/videos/rate.ts +++ b/server/controllers/api/videos/rate.ts | |||
@@ -2,8 +2,8 @@ import * as express from 'express' | |||
2 | import { UserVideoRateUpdate } from '../../../../shared' | 2 | import { UserVideoRateUpdate } from '../../../../shared' |
3 | import { logger } from '../../../helpers/logger' | 3 | import { logger } from '../../../helpers/logger' |
4 | import { sequelizeTypescript, VIDEO_RATE_TYPES } from '../../../initializers' | 4 | import { sequelizeTypescript, VIDEO_RATE_TYPES } from '../../../initializers' |
5 | import { sendVideoRateChange } from '../../../lib/activitypub' | 5 | import { getRateUrl, sendVideoRateChange } from '../../../lib/activitypub' |
6 | import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoRateValidator } from '../../../middlewares' | 6 | import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoUpdateRateValidator } from '../../../middlewares' |
7 | import { AccountModel } from '../../../models/account/account' | 7 | import { AccountModel } from '../../../models/account/account' |
8 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' | 8 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' |
9 | import { VideoModel } from '../../../models/video/video' | 9 | import { VideoModel } from '../../../models/video/video' |
@@ -12,7 +12,7 @@ const rateVideoRouter = express.Router() | |||
12 | 12 | ||
13 | rateVideoRouter.put('/:id/rate', | 13 | rateVideoRouter.put('/:id/rate', |
14 | authenticate, | 14 | authenticate, |
15 | asyncMiddleware(videoRateValidator), | 15 | asyncMiddleware(videoUpdateRateValidator), |
16 | asyncRetryTransactionMiddleware(rateVideo) | 16 | asyncRetryTransactionMiddleware(rateVideo) |
17 | ) | 17 | ) |
18 | 18 | ||
@@ -28,11 +28,12 @@ async function rateVideo (req: express.Request, res: express.Response) { | |||
28 | const body: UserVideoRateUpdate = req.body | 28 | const body: UserVideoRateUpdate = req.body |
29 | const rateType = body.rating | 29 | const rateType = body.rating |
30 | const videoInstance: VideoModel = res.locals.video | 30 | const videoInstance: VideoModel = res.locals.video |
31 | const userAccount: AccountModel = res.locals.oauth.token.User.Account | ||
31 | 32 | ||
32 | await sequelizeTypescript.transaction(async t => { | 33 | await sequelizeTypescript.transaction(async t => { |
33 | const sequelizeOptions = { transaction: t } | 34 | const sequelizeOptions = { transaction: t } |
34 | 35 | ||
35 | const accountInstance = await AccountModel.load(res.locals.oauth.token.User.Account.id, t) | 36 | const accountInstance = await AccountModel.load(userAccount.id, t) |
36 | const previousRate = await AccountVideoRateModel.load(accountInstance.id, videoInstance.id, t) | 37 | const previousRate = await AccountVideoRateModel.load(accountInstance.id, videoInstance.id, t) |
37 | 38 | ||
38 | let likesToIncrement = 0 | 39 | let likesToIncrement = 0 |
@@ -44,20 +45,22 @@ async function rateVideo (req: express.Request, res: express.Response) { | |||
44 | // There was a previous rate, update it | 45 | // There was a previous rate, update it |
45 | if (previousRate) { | 46 | if (previousRate) { |
46 | // We will remove the previous rate, so we will need to update the video count attribute | 47 | // We will remove the previous rate, so we will need to update the video count attribute |
47 | if (previousRate.type === VIDEO_RATE_TYPES.LIKE) likesToIncrement-- | 48 | if (previousRate.type === 'like') likesToIncrement-- |
48 | else if (previousRate.type === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement-- | 49 | else if (previousRate.type === 'dislike') dislikesToIncrement-- |
49 | 50 | ||
50 | if (rateType === 'none') { // Destroy previous rate | 51 | if (rateType === 'none') { // Destroy previous rate |
51 | await previousRate.destroy(sequelizeOptions) | 52 | await previousRate.destroy(sequelizeOptions) |
52 | } else { // Update previous rate | 53 | } else { // Update previous rate |
53 | previousRate.type = rateType | 54 | previousRate.type = rateType |
55 | previousRate.url = getRateUrl(rateType, userAccount.Actor, videoInstance) | ||
54 | await previousRate.save(sequelizeOptions) | 56 | await previousRate.save(sequelizeOptions) |
55 | } | 57 | } |
56 | } else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate | 58 | } else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate |
57 | const query = { | 59 | const query = { |
58 | accountId: accountInstance.id, | 60 | accountId: accountInstance.id, |
59 | videoId: videoInstance.id, | 61 | videoId: videoInstance.id, |
60 | type: rateType | 62 | type: rateType, |
63 | url: getRateUrl(rateType, userAccount.Actor, videoInstance) | ||
61 | } | 64 | } |
62 | 65 | ||
63 | await AccountVideoRateModel.create(query, sequelizeOptions) | 66 | await AccountVideoRateModel.create(query, sequelizeOptions) |