aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-01-15 09:46:46 +0100
committerChocobozzz <me@florianbigard.com>2018-01-15 09:47:19 +0100
commit7006bc6378eb0d480bc5b60f2c18876f0bb2a97d (patch)
tree1d914b217a7374ed6315ef2f4d38e921689d3705
parent304016c52ba275a293a2c8ee3287e569b398dc68 (diff)
downloadPeerTube-7006bc6378eb0d480bc5b60f2c18876f0bb2a97d.tar.gz
PeerTube-7006bc6378eb0d480bc5b60f2c18876f0bb2a97d.tar.zst
PeerTube-7006bc6378eb0d480bc5b60f2c18876f0bb2a97d.zip
Fix actor followers/following counts
-rw-r--r--server/controllers/activitypub/client.ts63
-rw-r--r--server/helpers/requests.ts6
-rw-r--r--server/lib/activitypub/actor.ts9
-rw-r--r--server/tests/api/server/follows.ts2
4 files changed, 54 insertions, 26 deletions
diff --git a/server/controllers/activitypub/client.ts b/server/controllers/activitypub/client.ts
index ec3f72b64..ba659984f 100644
--- a/server/controllers/activitypub/client.ts
+++ b/server/controllers/activitypub/client.ts
@@ -10,6 +10,7 @@ import { asyncMiddleware, executeIfActivityPub, localAccountValidator } from '..
10import { videoChannelsGetValidator, videosGetValidator, videosShareValidator } from '../../middlewares/validators' 10import { videoChannelsGetValidator, videosGetValidator, videosShareValidator } from '../../middlewares/validators'
11import { videoCommentGetValidator } from '../../middlewares/validators/video-comments' 11import { videoCommentGetValidator } from '../../middlewares/validators/video-comments'
12import { AccountModel } from '../../models/account/account' 12import { AccountModel } from '../../models/account/account'
13import { ActorModel } from '../../models/activitypub/actor'
13import { ActorFollowModel } from '../../models/activitypub/actor-follow' 14import { ActorFollowModel } from '../../models/activitypub/actor-follow'
14import { VideoModel } from '../../models/video/video' 15import { VideoModel } from '../../models/video/video'
15import { VideoChannelModel } from '../../models/video/video-channel' 16import { VideoChannelModel } from '../../models/video/video-channel'
@@ -22,13 +23,11 @@ activityPubClientRouter.get('/accounts?/:name',
22 executeIfActivityPub(asyncMiddleware(localAccountValidator)), 23 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
23 executeIfActivityPub(accountController) 24 executeIfActivityPub(accountController)
24) 25)
25 26activityPubClientRouter.get('/accounts?/:name/followers',
26activityPubClientRouter.get('/accounts/:name/followers',
27 executeIfActivityPub(asyncMiddleware(localAccountValidator)), 27 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
28 executeIfActivityPub(asyncMiddleware(accountFollowersController)) 28 executeIfActivityPub(asyncMiddleware(accountFollowersController))
29) 29)
30 30activityPubClientRouter.get('/accounts?/:name/following',
31activityPubClientRouter.get('/accounts/:name/following',
32 executeIfActivityPub(asyncMiddleware(localAccountValidator)), 31 executeIfActivityPub(asyncMiddleware(localAccountValidator)),
33 executeIfActivityPub(asyncMiddleware(accountFollowingController)) 32 executeIfActivityPub(asyncMiddleware(accountFollowingController))
34) 33)
@@ -37,12 +36,10 @@ activityPubClientRouter.get('/videos/watch/:id',
37 executeIfActivityPub(asyncMiddleware(videosGetValidator)), 36 executeIfActivityPub(asyncMiddleware(videosGetValidator)),
38 executeIfActivityPub(asyncMiddleware(videoController)) 37 executeIfActivityPub(asyncMiddleware(videoController))
39) 38)
40
41activityPubClientRouter.get('/videos/watch/:id/announces/:accountId', 39activityPubClientRouter.get('/videos/watch/:id/announces/:accountId',
42 executeIfActivityPub(asyncMiddleware(videosShareValidator)), 40 executeIfActivityPub(asyncMiddleware(videosShareValidator)),
43 executeIfActivityPub(asyncMiddleware(videoAnnounceController)) 41 executeIfActivityPub(asyncMiddleware(videoAnnounceController))
44) 42)
45
46activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId', 43activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
47 executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)), 44 executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)),
48 executeIfActivityPub(asyncMiddleware(videoCommentController)) 45 executeIfActivityPub(asyncMiddleware(videoCommentController))
@@ -52,6 +49,14 @@ activityPubClientRouter.get('/video-channels/:id',
52 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)), 49 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
53 executeIfActivityPub(asyncMiddleware(videoChannelController)) 50 executeIfActivityPub(asyncMiddleware(videoChannelController))
54) 51)
52activityPubClientRouter.get('/video-channels/:id/followers',
53 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
54 executeIfActivityPub(asyncMiddleware(videoChannelFollowersController))
55)
56activityPubClientRouter.get('/video-channels/:id/following',
57 executeIfActivityPub(asyncMiddleware(videoChannelsGetValidator)),
58 executeIfActivityPub(asyncMiddleware(videoChannelFollowingController))
59)
55 60
56// --------------------------------------------------------------------------- 61// ---------------------------------------------------------------------------
57 62
@@ -70,24 +75,14 @@ function accountController (req: express.Request, res: express.Response, next: e
70 75
71async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) { 76async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
72 const account: AccountModel = res.locals.account 77 const account: AccountModel = res.locals.account
73 78 const activityPubResult = await actorFollowers(req, account.Actor)
74 const page = req.query.page || 1
75 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
76
77 const result = await ActorFollowModel.listAcceptedFollowerUrlsForApi([ account.Actor.id ], undefined, start, count)
78 const activityPubResult = activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
79 79
80 return res.json(activityPubResult) 80 return res.json(activityPubResult)
81} 81}
82 82
83async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) { 83async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
84 const account: AccountModel = res.locals.account 84 const account: AccountModel = res.locals.account
85 85 const activityPubResult = await actorFollowing(req, account.Actor)
86 const page = req.query.page || 1
87 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
88
89 const result = await ActorFollowModel.listAcceptedFollowingUrlsForApi([ account.Actor.id ], undefined, start, count)
90 const activityPubResult = activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
91 86
92 return res.json(activityPubResult) 87 return res.json(activityPubResult)
93} 88}
@@ -115,9 +110,41 @@ async function videoChannelController (req: express.Request, res: express.Respon
115 return res.json(videoChannel.toActivityPubObject()) 110 return res.json(videoChannel.toActivityPubObject())
116} 111}
117 112
113async function videoChannelFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) {
114 const videoChannel: VideoChannelModel = res.locals.videoChannel
115 const activityPubResult = await actorFollowers(req, videoChannel.Actor)
116
117 return res.json(activityPubResult)
118}
119
120async function videoChannelFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) {
121 const videoChannel: VideoChannelModel = res.locals.videoChannel
122 const activityPubResult = await actorFollowing(req, videoChannel.Actor)
123
124 return res.json(activityPubResult)
125}
126
118async function videoCommentController (req: express.Request, res: express.Response, next: express.NextFunction) { 127async function videoCommentController (req: express.Request, res: express.Response, next: express.NextFunction) {
119 const videoComment: VideoCommentModel = res.locals.videoComment 128 const videoComment: VideoCommentModel = res.locals.videoComment
120 129
121 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined) 130 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
122 return res.json(videoComment.toActivityPubObject(threadParentComments)) 131 return res.json(videoComment.toActivityPubObject(threadParentComments))
123} 132}
133
134// ---------------------------------------------------------------------------
135
136async function actorFollowing (req: express.Request, actor: ActorModel) {
137 const page = req.query.page || 1
138 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
139
140 const result = await ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
141 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
142}
143
144async function actorFollowers (req: express.Request, actor: ActorModel) {
145 const page = req.query.page || 1
146 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
147
148 const result = await ActorFollowModel.listAcceptedFollowerUrlsForApi([ actor.id ], undefined, start, count)
149 return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result)
150}
diff --git a/server/helpers/requests.ts b/server/helpers/requests.ts
index ce185a2c0..ac3559c58 100644
--- a/server/helpers/requests.ts
+++ b/server/helpers/requests.ts
@@ -1,9 +1,13 @@
1import * as Promise from 'bluebird' 1import * as Promise from 'bluebird'
2import { createWriteStream } from 'fs' 2import { createWriteStream } from 'fs'
3import { RequestResponse } from 'request'
3import * as request from 'request' 4import * as request from 'request'
4import { ACTIVITY_PUB } from '../initializers' 5import { ACTIVITY_PUB } from '../initializers'
6import Bluebird = require('bluebird')
5 7
6function doRequest (requestOptions: request.CoreOptions & request.UriOptions & { activityPub?: boolean }) { 8function doRequest (
9 requestOptions: request.CoreOptions & request.UriOptions & { activityPub?: boolean }
10): Bluebird<{ response: RequestResponse, body: any }> {
7 if (requestOptions.activityPub === true) { 11 if (requestOptions.activityPub === true) {
8 if (!Array.isArray(requestOptions.headers)) requestOptions.headers = {} 12 if (!Array.isArray(requestOptions.headers)) requestOptions.headers = {}
9 requestOptions.headers['accept'] = ACTIVITY_PUB.ACCEPT_HEADER 13 requestOptions.headers['accept'] = ACTIVITY_PUB.ACCEPT_HEADER
diff --git a/server/lib/activitypub/actor.ts b/server/lib/activitypub/actor.ts
index b3fb75421..2e0f3cfc2 100644
--- a/server/lib/activitypub/actor.ts
+++ b/server/lib/activitypub/actor.ts
@@ -132,15 +132,13 @@ async function fetchActorTotalItems (url: string) {
132 activityPub: true 132 activityPub: true
133 } 133 }
134 134
135 let requestResult
136 try { 135 try {
137 requestResult = await doRequest(options) 136 const { body } = await doRequest(options)
137 return body.totalItems ? body.totalItems : 0
138 } catch (err) { 138 } catch (err) {
139 logger.warn('Cannot fetch remote actor count %s.', url, err) 139 logger.warn('Cannot fetch remote actor count %s.', url, err)
140 return undefined 140 return 0
141 } 141 }
142
143 return requestResult.totalItems ? requestResult.totalItems : 0
144} 142}
145 143
146async function fetchAvatarIfExists (actorJSON: ActivityPubActor) { 144async function fetchAvatarIfExists (actorJSON: ActivityPubActor) {
@@ -314,7 +312,6 @@ async function refreshActorIfNeeded (actor: ActorModel) {
314 if (result === undefined) throw new Error('Cannot fetch remote actor in refresh actor.') 312 if (result === undefined) throw new Error('Cannot fetch remote actor in refresh actor.')
315 313
316 return sequelizeTypescript.transaction(async t => { 314 return sequelizeTypescript.transaction(async t => {
317 logger.info('coucou', result.actor.toJSON())
318 updateInstanceWithAnother(actor, result.actor) 315 updateInstanceWithAnother(actor, result.actor)
319 316
320 if (result.avatarName !== undefined) { 317 if (result.avatarName !== undefined) {
diff --git a/server/tests/api/server/follows.ts b/server/tests/api/server/follows.ts
index d16e98bc1..fad58e840 100644
--- a/server/tests/api/server/follows.ts
+++ b/server/tests/api/server/follows.ts
@@ -256,7 +256,7 @@ describe('Test follows', function () {
256 await expectAccountFollows(servers[1].url, 'peertube@localhost:9001', 0, 1) 256 await expectAccountFollows(servers[1].url, 'peertube@localhost:9001', 0, 1)
257 await expectAccountFollows(servers[1].url, 'peertube@localhost:9002', 1, 0) 257 await expectAccountFollows(servers[1].url, 'peertube@localhost:9002', 1, 0)
258 258
259 await expectAccountFollows(servers[2].url, 'peertube@localhost:9001', 0, 1) 259 await expectAccountFollows(servers[2].url, 'peertube@localhost:9001', 0, 2)
260 await expectAccountFollows(servers[2].url, 'peertube@localhost:9003', 1, 0) 260 await expectAccountFollows(servers[2].url, 'peertube@localhost:9003', 1, 0)
261 }) 261 })
262 262