From b014b6b9c7cb68d09c52b44046afe486c0736426 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 10 Oct 2018 09:43:53 +0200 Subject: Add ability to search on followers/following --- server/controllers/api/server/follows.ts | 16 +++++- server/models/activitypub/actor-follow.ts | 90 +++++++++++++++++++------------ server/tests/api/server/follows.ts | 40 +++++++++++++- server/tests/utils/server/follows.ts | 6 ++- 4 files changed, 112 insertions(+), 40 deletions(-) (limited to 'server') diff --git a/server/controllers/api/server/follows.ts b/server/controllers/api/server/follows.ts index d62400e42..9fa6c34ba 100644 --- a/server/controllers/api/server/follows.ts +++ b/server/controllers/api/server/follows.ts @@ -61,14 +61,26 @@ export { async function listFollowing (req: express.Request, res: express.Response, next: express.NextFunction) { const serverActor = await getServerActor() - const resultList = await ActorFollowModel.listFollowingForApi(serverActor.id, req.query.start, req.query.count, req.query.sort) + const resultList = await ActorFollowModel.listFollowingForApi( + serverActor.id, + req.query.start, + req.query.count, + req.query.sort, + req.query.search + ) return res.json(getFormattedObjects(resultList.data, resultList.total)) } async function listFollowers (req: express.Request, res: express.Response, next: express.NextFunction) { const serverActor = await getServerActor() - const resultList = await ActorFollowModel.listFollowersForApi(serverActor.id, req.query.start, req.query.count, req.query.sort) + const resultList = await ActorFollowModel.listFollowersForApi( + serverActor.id, + req.query.start, + req.query.count, + req.query.sort, + req.query.search + ) return res.json(getFormattedObjects(resultList.data, resultList.total)) } diff --git a/server/models/activitypub/actor-follow.ts b/server/models/activitypub/actor-follow.ts index 27bb43dae..3373355ef 100644 --- a/server/models/activitypub/actor-follow.ts +++ b/server/models/activitypub/actor-follow.ts @@ -280,7 +280,7 @@ export class ActorFollowModel extends Model { return ActorFollowModel.findAll(query) } - static listFollowingForApi (id: number, start: number, count: number, sort: string) { + static listFollowingForApi (id: number, start: number, count: number, sort: string, search?: string) { const query = { distinct: true, offset: start, @@ -299,7 +299,17 @@ export class ActorFollowModel extends Model { model: ActorModel, as: 'ActorFollowing', required: true, - include: [ ServerModel ] + include: [ + { + model: ServerModel, + required: true, + where: search ? { + host: { + [Sequelize.Op.iLike]: '%' + search + '%' + } + } : undefined + } + ] } ] } @@ -313,6 +323,49 @@ export class ActorFollowModel extends Model { }) } + static listFollowersForApi (id: number, start: number, count: number, sort: string, search?: string) { + const query = { + distinct: true, + offset: start, + limit: count, + order: getSort(sort), + include: [ + { + model: ActorModel, + required: true, + as: 'ActorFollower', + include: [ + { + model: ServerModel, + required: true, + where: search ? { + host: { + [ Sequelize.Op.iLike ]: '%' + search + '%' + } + } : undefined + } + ] + }, + { + model: ActorModel, + as: 'ActorFollowing', + required: true, + where: { + id + } + } + ] + } + + return ActorFollowModel.findAndCountAll(query) + .then(({ rows, count }) => { + return { + data: rows, + total: count + } + }) + } + static listSubscriptionsForApi (id: number, start: number, count: number, sort: string) { const query = { attributes: [], @@ -370,39 +423,6 @@ export class ActorFollowModel extends Model { }) } - static listFollowersForApi (id: number, start: number, count: number, sort: string) { - const query = { - distinct: true, - offset: start, - limit: count, - order: getSort(sort), - include: [ - { - model: ActorModel, - required: true, - as: 'ActorFollower', - include: [ ServerModel ] - }, - { - model: ActorModel, - as: 'ActorFollowing', - required: true, - where: { - id - } - } - ] - } - - return ActorFollowModel.findAndCountAll(query) - .then(({ rows, count }) => { - return { - data: rows, - total: count - } - }) - } - static listAcceptedFollowerUrlsForApi (actorIds: number[], t: Sequelize.Transaction, start?: number, count?: number) { return ActorFollowModel.createListAcceptedFollowForApiQuery('followers', actorIds, t, start, count) } diff --git a/server/tests/api/server/follows.ts b/server/tests/api/server/follows.ts index 310c291bf..e80e93e7f 100644 --- a/server/tests/api/server/follows.ts +++ b/server/tests/api/server/follows.ts @@ -93,7 +93,26 @@ describe('Test follows', function () { expect(server3Follow.state).to.equal('accepted') }) - it('Should have 0 followings on server 1 and 2', async function () { + it('Should search followings on server 1', async function () { + { + const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 1, 'createdAt', ':9002') + const follows = res.body.data + + expect(res.body.total).to.equal(1) + expect(follows.length).to.equal(1) + expect(follows[ 0 ].following.host).to.equal('localhost:9002') + } + + { + const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 1, 'createdAt', 'bla') + const follows = res.body.data + + expect(res.body.total).to.equal(0) + expect(follows.length).to.equal(0) + } + }) + + it('Should have 0 followings on server 2 and 3', async function () { for (const server of [ servers[1], servers[2] ]) { const res = await getFollowingListPaginationAndSort(server.url, 0, 5, 'createdAt') const follows = res.body.data @@ -116,6 +135,25 @@ describe('Test follows', function () { } }) + it('Should search followers on server 2', async function () { + { + const res = await getFollowersListPaginationAndSort(servers[ 2 ].url, 0, 5, 'createdAt', '9001') + const follows = res.body.data + + expect(res.body.total).to.equal(1) + expect(follows.length).to.equal(1) + expect(follows[ 0 ].following.host).to.equal('localhost:9003') + } + + { + const res = await getFollowersListPaginationAndSort(servers[ 2 ].url, 0, 5, 'createdAt', 'bla') + const follows = res.body.data + + expect(res.body.total).to.equal(0) + expect(follows.length).to.equal(0) + } + }) + it('Should have 0 followers on server 1', async function () { const res = await getFollowersListPaginationAndSort(servers[0].url, 0, 5, 'createdAt') const follows = res.body.data diff --git a/server/tests/utils/server/follows.ts b/server/tests/utils/server/follows.ts index 8a65a958b..7741757a6 100644 --- a/server/tests/utils/server/follows.ts +++ b/server/tests/utils/server/follows.ts @@ -2,7 +2,7 @@ import * as request from 'supertest' import { ServerInfo } from './servers' import { waitJobs } from './jobs' -function getFollowersListPaginationAndSort (url: string, start: number, count: number, sort: string) { +function getFollowersListPaginationAndSort (url: string, start: number, count: number, sort: string, search?: string) { const path = '/api/v1/server/followers' return request(url) @@ -10,12 +10,13 @@ function getFollowersListPaginationAndSort (url: string, start: number, count: n .query({ start }) .query({ count }) .query({ sort }) + .query({ search }) .set('Accept', 'application/json') .expect(200) .expect('Content-Type', /json/) } -function getFollowingListPaginationAndSort (url: string, start: number, count: number, sort: string) { +function getFollowingListPaginationAndSort (url: string, start: number, count: number, sort: string, search?: string) { const path = '/api/v1/server/following' return request(url) @@ -23,6 +24,7 @@ function getFollowingListPaginationAndSort (url: string, start: number, count: n .query({ start }) .query({ count }) .query({ sort }) + .query({ search }) .set('Accept', 'application/json') .expect(200) .expect('Content-Type', /json/) -- cgit v1.2.3