]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/accounts.ts
Fix channel sync right check
[github/Chocobozzz/PeerTube.git] / server / controllers / api / accounts.ts
CommitLineData
41fb13c3 1import express from 'express'
d6886027 2import { pickCommonVideoQuery } from '@server/helpers/query'
4beda9e1 3import { ActorFollowModel } from '@server/models/actor/actor-follow'
8054669f 4import { getServerActor } from '@server/models/application/application'
2760b454 5import { guessAdditionalAttributesFromQuery } from '@server/models/video/formatter/video-format-utils'
d4d9bbc6 6import { VideoChannelSyncModel } from '@server/models/video/video-channel-sync'
8054669f 7import { buildNSFWFilter, getCountVideos, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
e1c55031 8import { getFormattedObjects } from '../../helpers/utils'
8054669f 9import { JobQueue } from '../../lib/job-queue'
1fd61899 10import { Hooks } from '../../lib/plugins/hooks'
48dce1c9 11import {
7ad9b984 12 asyncMiddleware,
22834691 13 authenticate,
7ad9b984 14 commonVideosFiltersValidator,
48dce1c9
C
15 optionalAuthenticate,
16 paginationValidator,
17 setDefaultPagination,
418d092a 18 setDefaultSort,
8054669f 19 setDefaultVideosSort,
c100a614 20 videoPlaylistsSortValidator,
22834691
C
21 videoRatesSortValidator,
22 videoRatingValidator
48dce1c9 23} from '../../middlewares'
c100a614
YB
24import {
25 accountNameWithHostGetValidator,
4beda9e1 26 accountsFollowersSortValidator,
c100a614 27 accountsSortValidator,
22834691 28 ensureAuthUserOwnsAccountValidator,
d4d9bbc6 29 ensureCanManageChannelOrAccount,
a1587156 30 videoChannelsSortValidator,
8054669f 31 videoChannelStatsValidator,
2a491182 32 videoChannelSyncsSortValidator,
8054669f 33 videosSortValidator
c100a614 34} from '../../middlewares/validators'
8054669f 35import { commonVideoPlaylistFiltersValidator, videoPlaylistsSearchValidator } from '../../middlewares/validators/videos/video-playlists'
265ba139 36import { AccountModel } from '../../models/account/account'
c100a614 37import { AccountVideoRateModel } from '../../models/account/account-video-rate'
0626e7af 38import { VideoModel } from '../../models/video/video'
48dce1c9 39import { VideoChannelModel } from '../../models/video/video-channel'
418d092a 40import { VideoPlaylistModel } from '../../models/video/video-playlist'
265ba139
C
41
42const accountsRouter = express.Router()
43
44accountsRouter.get('/',
45 paginationValidator,
46 accountsSortValidator,
1174a847 47 setDefaultSort,
f05a1c30 48 setDefaultPagination,
265ba139
C
49 asyncMiddleware(listAccounts)
50)
51
ad9e39fb 52accountsRouter.get('/:accountName',
418d092a 53 asyncMiddleware(accountNameWithHostGetValidator),
265ba139
C
54 getAccount
55)
56
ad9e39fb 57accountsRouter.get('/:accountName/videos',
418d092a 58 asyncMiddleware(accountNameWithHostGetValidator),
0626e7af
C
59 paginationValidator,
60 videosSortValidator,
8054669f 61 setDefaultVideosSort,
0626e7af
C
62 setDefaultPagination,
63 optionalAuthenticate,
d525fc39 64 commonVideosFiltersValidator,
48dce1c9
C
65 asyncMiddleware(listAccountVideos)
66)
67
ad9e39fb 68accountsRouter.get('/:accountName/video-channels',
418d092a 69 asyncMiddleware(accountNameWithHostGetValidator),
747c5628 70 videoChannelStatsValidator,
91b66319
C
71 paginationValidator,
72 videoChannelsSortValidator,
73 setDefaultSort,
74 setDefaultPagination,
418d092a
C
75 asyncMiddleware(listAccountChannels)
76)
77
2a491182
F
78accountsRouter.get('/:accountName/video-channel-syncs',
79 authenticate,
80 asyncMiddleware(accountNameWithHostGetValidator),
d4d9bbc6 81 ensureCanManageChannelOrAccount,
2a491182
F
82 paginationValidator,
83 videoChannelSyncsSortValidator,
84 setDefaultSort,
85 setDefaultPagination,
86 asyncMiddleware(listAccountChannelsSync)
87)
88
418d092a
C
89accountsRouter.get('/:accountName/video-playlists',
90 optionalAuthenticate,
91 asyncMiddleware(accountNameWithHostGetValidator),
92 paginationValidator,
93 videoPlaylistsSortValidator,
94 setDefaultSort,
95 setDefaultPagination,
df0b219d 96 commonVideoPlaylistFiltersValidator,
c06af501 97 videoPlaylistsSearchValidator,
418d092a 98 asyncMiddleware(listAccountPlaylists)
48dce1c9
C
99)
100
c100a614
YB
101accountsRouter.get('/:accountName/ratings',
102 authenticate,
103 asyncMiddleware(accountNameWithHostGetValidator),
104 ensureAuthUserOwnsAccountValidator,
105 paginationValidator,
106 videoRatesSortValidator,
107 setDefaultSort,
108 setDefaultPagination,
109 videoRatingValidator,
110 asyncMiddleware(listAccountRatings)
111)
112
4beda9e1
C
113accountsRouter.get('/:accountName/followers',
114 authenticate,
115 asyncMiddleware(accountNameWithHostGetValidator),
116 ensureAuthUserOwnsAccountValidator,
117 paginationValidator,
118 accountsFollowersSortValidator,
119 setDefaultSort,
120 setDefaultPagination,
121 asyncMiddleware(listAccountFollowers)
122)
123
265ba139
C
124// ---------------------------------------------------------------------------
125
126export {
127 accountsRouter
128}
129
130// ---------------------------------------------------------------------------
131
418d092a 132function getAccount (req: express.Request, res: express.Response) {
dae86118 133 const account = res.locals.account
0626e7af 134
744d0eca 135 if (account.isOutdated()) {
bd911b54 136 JobQueue.Instance.createJobAsync({ type: 'activitypub-refresher', payload: { type: 'actor', url: account.Actor.url } })
744d0eca
C
137 }
138
0626e7af 139 return res.json(account.toFormattedJSON())
265ba139
C
140}
141
418d092a 142async function listAccounts (req: express.Request, res: express.Response) {
265ba139
C
143 const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort)
144
145 return res.json(getFormattedObjects(resultList.data, resultList.total))
146}
0626e7af 147
418d092a 148async function listAccountChannels (req: express.Request, res: express.Response) {
91b66319
C
149 const options = {
150 accountId: res.locals.account.id,
151 start: req.query.start,
152 count: req.query.count,
747c5628 153 sort: req.query.sort,
4f5d0459
RK
154 withStats: req.query.withStats,
155 search: req.query.search
91b66319
C
156 }
157
4beda9e1 158 const resultList = await VideoChannelModel.listByAccountForAPI(options)
48dce1c9
C
159
160 return res.json(getFormattedObjects(resultList.data, resultList.total))
161}
162
2a491182
F
163async function listAccountChannelsSync (req: express.Request, res: express.Response) {
164 const options = {
165 accountId: res.locals.account.id,
166 start: req.query.start,
167 count: req.query.count,
168 sort: req.query.sort,
169 search: req.query.search
170 }
171
172 const resultList = await VideoChannelSyncModel.listByAccountForAPI(options)
173
174 return res.json(getFormattedObjects(resultList.data, resultList.total))
175}
176
418d092a
C
177async function listAccountPlaylists (req: express.Request, res: express.Response) {
178 const serverActor = await getServerActor()
179
180 // Allow users to see their private/unlisted video playlists
6b0c3c7c 181 let listMyPlaylists = false
dae86118 182 if (res.locals.oauth && res.locals.oauth.token.User.Account.id === res.locals.account.id) {
6b0c3c7c 183 listMyPlaylists = true
418d092a
C
184 }
185
186 const resultList = await VideoPlaylistModel.listForApi({
c06af501 187 search: req.query.search,
418d092a
C
188 followerActorId: serverActor.id,
189 start: req.query.start,
190 count: req.query.count,
191 sort: req.query.sort,
192 accountId: res.locals.account.id,
6b0c3c7c 193 listMyPlaylists,
df0b219d 194 type: req.query.playlistType
418d092a
C
195 })
196
197 return res.json(getFormattedObjects(resultList.data, resultList.total))
198}
199
200async function listAccountVideos (req: express.Request, res: express.Response) {
2760b454
C
201 const serverActor = await getServerActor()
202
dae86118 203 const account = res.locals.account
2760b454
C
204
205 const displayOnlyForFollower = isUserAbleToSearchRemoteURI(res)
206 ? null
207 : {
208 actorId: serverActor.id,
209 orLocalVideos: true
210 }
211
fe987656 212 const countVideos = getCountVideos(req)
d6886027 213 const query = pickCommonVideoQuery(req.query)
0626e7af 214
1bfc07e4 215 const apiOptions = await Hooks.wrapObject({
d6886027
C
216 ...query,
217
2760b454 218 displayOnlyForFollower,
1fd61899 219 nsfw: buildNSFWFilter(res, query.nsfw),
1cd3facc 220 accountId: account.id,
fe987656 221 user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
d6886027 222 countVideos
38267c0c 223 }, 'filter:api.accounts.videos.list.params')
1bfc07e4 224
225 const resultList = await Hooks.wrapPromiseFun(
226 VideoModel.listForApi,
227 apiOptions,
38267c0c 228 'filter:api.accounts.videos.list.result'
1bfc07e4 229 )
0626e7af 230
2760b454 231 return res.json(getFormattedObjects(resultList.data, resultList.total, guessAdditionalAttributesFromQuery(query)))
0626e7af 232}
c100a614
YB
233
234async function listAccountRatings (req: express.Request, res: express.Response) {
235 const account = res.locals.account
236
237 const resultList = await AccountVideoRateModel.listByAccountForApi({
238 accountId: account.id,
239 start: req.query.start,
240 count: req.query.count,
241 sort: req.query.sort,
242 type: req.query.rating
243 })
d0800f76 244 return res.json(getFormattedObjects(resultList.data, resultList.total))
c100a614 245}
4beda9e1
C
246
247async function listAccountFollowers (req: express.Request, res: express.Response) {
248 const account = res.locals.account
249
250 const channels = await VideoChannelModel.listAllByAccount(account.id)
251 const actorIds = [ account.actorId ].concat(channels.map(c => c.actorId))
252
253 const resultList = await ActorFollowModel.listFollowersForApi({
254 actorIds,
255 start: req.query.start,
256 count: req.query.count,
257 sort: req.query.sort,
258 search: req.query.search,
906f46d0 259 state: 'accepted'
4beda9e1
C
260 })
261
262 return res.json(getFormattedObjects(resultList.data, resultList.total))
263}