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