Add ability to forbid followers
[oweals/peertube.git] / server / controllers / api / accounts.ts
1 import * as express from 'express'
2 import { getFormattedObjects, getServerActor } from '../../helpers/utils'
3 import {
4   asyncMiddleware,
5   commonVideosFiltersValidator,
6   optionalAuthenticate,
7   paginationValidator,
8   setDefaultPagination,
9   setDefaultSort,
10   videoPlaylistsSortValidator
11 } from '../../middlewares'
12 import { accountNameWithHostGetValidator, accountsSortValidator, videosSortValidator } from '../../middlewares/validators'
13 import { AccountModel } from '../../models/account/account'
14 import { VideoModel } from '../../models/video/video'
15 import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
16 import { VideoChannelModel } from '../../models/video/video-channel'
17 import { JobQueue } from '../../lib/job-queue'
18 import { logger } from '../../helpers/logger'
19 import { VideoPlaylistModel } from '../../models/video/video-playlist'
20 import { commonVideoPlaylistFiltersValidator } from '../../middlewares/validators/videos/video-playlists'
21
22 const accountsRouter = express.Router()
23
24 accountsRouter.get('/',
25   paginationValidator,
26   accountsSortValidator,
27   setDefaultSort,
28   setDefaultPagination,
29   asyncMiddleware(listAccounts)
30 )
31
32 accountsRouter.get('/:accountName',
33   asyncMiddleware(accountNameWithHostGetValidator),
34   getAccount
35 )
36
37 accountsRouter.get('/:accountName/videos',
38   asyncMiddleware(accountNameWithHostGetValidator),
39   paginationValidator,
40   videosSortValidator,
41   setDefaultSort,
42   setDefaultPagination,
43   optionalAuthenticate,
44   commonVideosFiltersValidator,
45   asyncMiddleware(listAccountVideos)
46 )
47
48 accountsRouter.get('/:accountName/video-channels',
49   asyncMiddleware(accountNameWithHostGetValidator),
50   asyncMiddleware(listAccountChannels)
51 )
52
53 accountsRouter.get('/:accountName/video-playlists',
54   optionalAuthenticate,
55   asyncMiddleware(accountNameWithHostGetValidator),
56   paginationValidator,
57   videoPlaylistsSortValidator,
58   setDefaultSort,
59   setDefaultPagination,
60   commonVideoPlaylistFiltersValidator,
61   asyncMiddleware(listAccountPlaylists)
62 )
63
64 // ---------------------------------------------------------------------------
65
66 export {
67   accountsRouter
68 }
69
70 // ---------------------------------------------------------------------------
71
72 function getAccount (req: express.Request, res: express.Response) {
73   const account = res.locals.account
74
75   if (account.isOutdated()) {
76     JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } })
77             .catch(err => logger.error('Cannot create AP refresher job for actor %s.', account.Actor.url, { err }))
78   }
79
80   return res.json(account.toFormattedJSON())
81 }
82
83 async function listAccounts (req: express.Request, res: express.Response) {
84   const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
85
86   return res.json(getFormattedObjects(resultList.data, resultList.total))
87 }
88
89 async function listAccountChannels (req: express.Request, res: express.Response) {
90   const resultList = await VideoChannelModel.listByAccount(res.locals.account.id)
91
92   return res.json(getFormattedObjects(resultList.data, resultList.total))
93 }
94
95 async function listAccountPlaylists (req: express.Request, res: express.Response) {
96   const serverActor = await getServerActor()
97
98   // Allow users to see their private/unlisted video playlists
99   let privateAndUnlisted = false
100   if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
101     privateAndUnlisted = true
102   }
103
104   const resultList = await VideoPlaylistModel.listForApi({
105     followerActorId: serverActor.id,
106     start: req.query.start,
107     count: req.query.count,
108     sort: req.query.sort,
109     accountId: res.locals.account.id,
110     privateAndUnlisted,
111     type: req.query.playlistType
112   })
113
114   return res.json(getFormattedObjects(resultList.data, resultList.total))
115 }
116
117 async function listAccountVideos (req: express.Request, res: express.Response) {
118   const account = res.locals.account
119   const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
120
121   const resultList = await VideoModel.listForApi({
122     followerActorId,
123     start: req.query.start,
124     count: req.query.count,
125     sort: req.query.sort,
126     includeLocalVideos: true,
127     categoryOneOf: req.query.categoryOneOf,
128     licenceOneOf: req.query.licenceOneOf,
129     languageOneOf: req.query.languageOneOf,
130     tagsOneOf: req.query.tagsOneOf,
131     tagsAllOf: req.query.tagsAllOf,
132     filter: req.query.filter,
133     nsfw: buildNSFWFilter(res, req.query.nsfw),
134     withFiles: false,
135     accountId: account.id,
136     user: res.locals.oauth ? res.locals.oauth.token.User : undefined
137   })
138
139   return res.json(getFormattedObjects(resultList.data, resultList.total))
140 }