]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/accounts.ts
Fix embed on mastodon
[github/Chocobozzz/PeerTube.git] / server / controllers / api / accounts.ts
CommitLineData
265ba139 1import * as express from 'express'
418d092a 2import { getFormattedObjects, getServerActor } from '../../helpers/utils'
48dce1c9 3import {
7ad9b984 4 asyncMiddleware,
22834691 5 authenticate,
7ad9b984 6 commonVideosFiltersValidator,
48dce1c9
C
7 optionalAuthenticate,
8 paginationValidator,
9 setDefaultPagination,
418d092a 10 setDefaultSort,
c100a614 11 videoPlaylistsSortValidator,
22834691
C
12 videoRatesSortValidator,
13 videoRatingValidator
48dce1c9 14} from '../../middlewares'
c100a614
YB
15import {
16 accountNameWithHostGetValidator,
17 accountsSortValidator,
22834691 18 ensureAuthUserOwnsAccountValidator,
91b66319
C
19 videosSortValidator,
20 videoChannelsSortValidator
c100a614 21} from '../../middlewares/validators'
265ba139 22import { AccountModel } from '../../models/account/account'
c100a614 23import { AccountVideoRateModel } from '../../models/account/account-video-rate'
0626e7af 24import { VideoModel } from '../../models/video/video'
fe987656 25import { buildNSFWFilter, isUserAbleToSearchRemoteURI, getCountVideos } from '../../helpers/express-utils'
48dce1c9 26import { VideoChannelModel } from '../../models/video/video-channel'
744d0eca
C
27import { JobQueue } from '../../lib/job-queue'
28import { logger } from '../../helpers/logger'
418d092a 29import { VideoPlaylistModel } from '../../models/video/video-playlist'
c06af501
RK
30import {
31 commonVideoPlaylistFiltersValidator,
32 videoPlaylistsSearchValidator
33} from '../../middlewares/validators/videos/video-playlists'
265ba139
C
34
35const accountsRouter = express.Router()
36
37accountsRouter.get('/',
38 paginationValidator,
39 accountsSortValidator,
1174a847 40 setDefaultSort,
f05a1c30 41 setDefaultPagination,
265ba139
C
42 asyncMiddleware(listAccounts)
43)
44
ad9e39fb 45accountsRouter.get('/:accountName',
418d092a 46 asyncMiddleware(accountNameWithHostGetValidator),
265ba139
C
47 getAccount
48)
49
ad9e39fb 50accountsRouter.get('/:accountName/videos',
418d092a 51 asyncMiddleware(accountNameWithHostGetValidator),
0626e7af
C
52 paginationValidator,
53 videosSortValidator,
54 setDefaultSort,
55 setDefaultPagination,
56 optionalAuthenticate,
d525fc39 57 commonVideosFiltersValidator,
48dce1c9
C
58 asyncMiddleware(listAccountVideos)
59)
60
ad9e39fb 61accountsRouter.get('/:accountName/video-channels',
418d092a 62 asyncMiddleware(accountNameWithHostGetValidator),
91b66319
C
63 paginationValidator,
64 videoChannelsSortValidator,
65 setDefaultSort,
66 setDefaultPagination,
418d092a
C
67 asyncMiddleware(listAccountChannels)
68)
69
70accountsRouter.get('/:accountName/video-playlists',
71 optionalAuthenticate,
72 asyncMiddleware(accountNameWithHostGetValidator),
73 paginationValidator,
74 videoPlaylistsSortValidator,
75 setDefaultSort,
76 setDefaultPagination,
df0b219d 77 commonVideoPlaylistFiltersValidator,
c06af501 78 videoPlaylistsSearchValidator,
418d092a 79 asyncMiddleware(listAccountPlaylists)
48dce1c9
C
80)
81
c100a614
YB
82accountsRouter.get('/:accountName/ratings',
83 authenticate,
84 asyncMiddleware(accountNameWithHostGetValidator),
85 ensureAuthUserOwnsAccountValidator,
86 paginationValidator,
87 videoRatesSortValidator,
88 setDefaultSort,
89 setDefaultPagination,
90 videoRatingValidator,
91 asyncMiddleware(listAccountRatings)
92)
93
265ba139
C
94// ---------------------------------------------------------------------------
95
96export {
97 accountsRouter
98}
99
100// ---------------------------------------------------------------------------
101
418d092a 102function getAccount (req: express.Request, res: express.Response) {
dae86118 103 const account = res.locals.account
0626e7af 104
744d0eca
C
105 if (account.isOutdated()) {
106 JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } })
107 .catch(err => logger.error('Cannot create AP refresher job for actor %s.', account.Actor.url, { err }))
108 }
109
0626e7af 110 return res.json(account.toFormattedJSON())
265ba139
C
111}
112
418d092a 113async function listAccounts (req: express.Request, res: express.Response) {
265ba139
C
114 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
115
116 return res.json(getFormattedObjects(resultList.data, resultList.total))
117}
0626e7af 118
418d092a 119async function listAccountChannels (req: express.Request, res: express.Response) {
91b66319
C
120 const options = {
121 accountId: res.locals.account.id,
122 start: req.query.start,
123 count: req.query.count,
bc01017b 124 sort: req.query.sort
91b66319
C
125 }
126
127 const resultList = await VideoChannelModel.listByAccount(options)
48dce1c9
C
128
129 return res.json(getFormattedObjects(resultList.data, resultList.total))
130}
131
418d092a
C
132async function listAccountPlaylists (req: express.Request, res: express.Response) {
133 const serverActor = await getServerActor()
134
135 // Allow users to see their private/unlisted video playlists
6b0c3c7c 136 let listMyPlaylists = false
dae86118 137 if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
6b0c3c7c 138 listMyPlaylists = true
418d092a
C
139 }
140
141 const resultList = await VideoPlaylistModel.listForApi({
c06af501 142 search: req.query.search,
418d092a
C
143 followerActorId: serverActor.id,
144 start: req.query.start,
145 count: req.query.count,
146 sort: req.query.sort,
147 accountId: res.locals.account.id,
6b0c3c7c 148 listMyPlaylists,
df0b219d 149 type: req.query.playlistType
418d092a
C
150 })
151
152 return res.json(getFormattedObjects(resultList.data, resultList.total))
153}
154
155async function listAccountVideos (req: express.Request, res: express.Response) {
dae86118 156 const account = res.locals.account
4e74e803 157 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
fe987656 158 const countVideos = getCountVideos(req)
0626e7af 159
48dce1c9 160 const resultList = await VideoModel.listForApi({
4e74e803 161 followerActorId,
48dce1c9
C
162 start: req.query.start,
163 count: req.query.count,
164 sort: req.query.sort,
8a19bee1 165 includeLocalVideos: true,
d525fc39
C
166 categoryOneOf: req.query.categoryOneOf,
167 licenceOneOf: req.query.licenceOneOf,
168 languageOneOf: req.query.languageOneOf,
169 tagsOneOf: req.query.tagsOneOf,
170 tagsAllOf: req.query.tagsAllOf,
1cd3facc 171 filter: req.query.filter,
d525fc39 172 nsfw: buildNSFWFilter(res, req.query.nsfw),
48dce1c9 173 withFiles: false,
1cd3facc 174 accountId: account.id,
fe987656
C
175 user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
176 countVideos
48dce1c9 177 })
0626e7af
C
178
179 return res.json(getFormattedObjects(resultList.data, resultList.total))
180}
c100a614
YB
181
182async function listAccountRatings (req: express.Request, res: express.Response) {
183 const account = res.locals.account
184
185 const resultList = await AccountVideoRateModel.listByAccountForApi({
186 accountId: account.id,
187 start: req.query.start,
188 count: req.query.count,
189 sort: req.query.sort,
190 type: req.query.rating
191 })
192 return res.json(getFormattedObjects(resultList.rows, resultList.count))
193}