aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/users
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-08-16 15:25:20 +0200
committerChocobozzz <me@florianbigard.com>2018-08-27 09:41:54 +0200
commit06a05d5f4784a7cbb27aa1188385b5679845dad8 (patch)
treeac197f3ed0768529456225ad76c912f22bc55e29 /server/controllers/api/users
parent4bda2e47bbc937c401ddcf14c1be53c70481a294 (diff)
downloadPeerTube-06a05d5f4784a7cbb27aa1188385b5679845dad8.tar.gz
PeerTube-06a05d5f4784a7cbb27aa1188385b5679845dad8.tar.zst
PeerTube-06a05d5f4784a7cbb27aa1188385b5679845dad8.zip
Add subscriptions endpoints to REST API
Diffstat (limited to 'server/controllers/api/users')
-rw-r--r--server/controllers/api/users/index.ts3
-rw-r--r--server/controllers/api/users/me.ts114
2 files changed, 107 insertions, 10 deletions
diff --git a/server/controllers/api/users/index.ts b/server/controllers/api/users/index.ts
index 105244ddd..608d439ac 100644
--- a/server/controllers/api/users/index.ts
+++ b/server/controllers/api/users/index.ts
@@ -29,7 +29,6 @@ import { usersAskResetPasswordValidator, usersBlockingValidator, usersResetPassw
29import { UserModel } from '../../../models/account/user' 29import { UserModel } from '../../../models/account/user'
30import { OAuthTokenModel } from '../../../models/oauth/oauth-token' 30import { OAuthTokenModel } from '../../../models/oauth/oauth-token'
31import { auditLoggerFactory, UserAuditView } from '../../../helpers/audit-logger' 31import { auditLoggerFactory, UserAuditView } from '../../../helpers/audit-logger'
32import { videosRouter } from '../videos'
33import { meRouter } from './me' 32import { meRouter } from './me'
34 33
35const auditLogger = auditLoggerFactory('users') 34const auditLogger = auditLoggerFactory('users')
@@ -41,7 +40,7 @@ const loginRateLimiter = new RateLimit({
41}) 40})
42 41
43const usersRouter = express.Router() 42const usersRouter = express.Router()
44videosRouter.use('/', meRouter) 43usersRouter.use('/', meRouter)
45 44
46usersRouter.get('/', 45usersRouter.get('/',
47 authenticate, 46 authenticate,
diff --git a/server/controllers/api/users/me.ts b/server/controllers/api/users/me.ts
index 1e096a35d..403842163 100644
--- a/server/controllers/api/users/me.ts
+++ b/server/controllers/api/users/me.ts
@@ -7,23 +7,35 @@ import { sendUpdateActor } from '../../../lib/activitypub/send'
7import { 7import {
8 asyncMiddleware, 8 asyncMiddleware,
9 authenticate, 9 authenticate,
10 commonVideosFiltersValidator,
10 paginationValidator, 11 paginationValidator,
11 setDefaultPagination, 12 setDefaultPagination,
12 setDefaultSort, 13 setDefaultSort,
14 userSubscriptionAddValidator,
15 userSubscriptionRemoveValidator,
13 usersUpdateMeValidator, 16 usersUpdateMeValidator,
14 usersVideoRatingValidator 17 usersVideoRatingValidator
15} from '../../../middlewares' 18} from '../../../middlewares'
16import { deleteMeValidator, videoImportsSortValidator, videosSortValidator } from '../../../middlewares/validators' 19import {
20 deleteMeValidator,
21 userSubscriptionsSortValidator,
22 videoImportsSortValidator,
23 videosSortValidator
24} from '../../../middlewares/validators'
17import { AccountVideoRateModel } from '../../../models/account/account-video-rate' 25import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
18import { UserModel } from '../../../models/account/user' 26import { UserModel } from '../../../models/account/user'
19import { VideoModel } from '../../../models/video/video' 27import { VideoModel } from '../../../models/video/video'
20import { VideoSortField } from '../../../../client/src/app/shared/video/sort-field.type' 28import { VideoSortField } from '../../../../client/src/app/shared/video/sort-field.type'
21import { createReqFiles } from '../../../helpers/express-utils' 29import { buildNSFWFilter, createReqFiles } from '../../../helpers/express-utils'
22import { UserVideoQuota } from '../../../../shared/models/users/user-video-quota.model' 30import { UserVideoQuota } from '../../../../shared/models/users/user-video-quota.model'
23import { updateAvatarValidator } from '../../../middlewares/validators/avatar' 31import { updateAvatarValidator } from '../../../middlewares/validators/avatar'
24import { updateActorAvatarFile } from '../../../lib/avatar' 32import { updateActorAvatarFile } from '../../../lib/avatar'
25import { auditLoggerFactory, UserAuditView } from '../../../helpers/audit-logger' 33import { auditLoggerFactory, UserAuditView } from '../../../helpers/audit-logger'
26import { VideoImportModel } from '../../../models/video/video-import' 34import { VideoImportModel } from '../../../models/video/video-import'
35import { VideoFilter } from '../../../../shared/models/videos/video-query.type'
36import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
37import { JobQueue } from '../../../lib/job-queue'
38import { logger } from '../../../helpers/logger'
27 39
28const auditLogger = auditLoggerFactory('users-me') 40const auditLogger = auditLoggerFactory('users-me')
29 41
@@ -83,6 +95,40 @@ meRouter.post('/me/avatar/pick',
83 asyncMiddleware(updateMyAvatar) 95 asyncMiddleware(updateMyAvatar)
84) 96)
85 97
98// ##### Subscriptions part #####
99
100meRouter.get('/me/subscriptions',
101 authenticate,
102 paginationValidator,
103 userSubscriptionsSortValidator,
104 setDefaultSort,
105 setDefaultPagination,
106 asyncMiddleware(getUserSubscriptions)
107)
108
109meRouter.post('/me/subscriptions',
110 authenticate,
111 userSubscriptionAddValidator,
112 asyncMiddleware(addUserSubscription)
113)
114
115meRouter.delete('/me/subscriptions/:uri',
116 authenticate,
117 userSubscriptionRemoveValidator,
118 asyncMiddleware(deleteUserSubscription)
119)
120
121meRouter.get('/me/subscriptions/videos',
122 authenticate,
123 authenticate,
124 paginationValidator,
125 videosSortValidator,
126 setDefaultSort,
127 setDefaultPagination,
128 commonVideosFiltersValidator,
129 asyncMiddleware(getUserSubscriptionVideos)
130)
131
86// --------------------------------------------------------------------------- 132// ---------------------------------------------------------------------------
87 133
88export { 134export {
@@ -91,6 +137,62 @@ export {
91 137
92// --------------------------------------------------------------------------- 138// ---------------------------------------------------------------------------
93 139
140async function addUserSubscription (req: express.Request, res: express.Response) {
141 const user = res.locals.oauth.token.User as UserModel
142 const [ name, host ] = req.body.uri.split('@')
143
144 const payload = {
145 name,
146 host,
147 followerActorId: user.Account.Actor.id
148 }
149
150 JobQueue.Instance.createJob({ type: 'activitypub-follow', payload })
151 .catch(err => logger.error('Cannot create follow job for subscription %s.', req.body.uri, err))
152
153 return res.status(204).end()
154}
155
156async function deleteUserSubscription (req: express.Request, res: express.Response) {
157 const subscription: ActorFollowModel = res.locals.subscription
158
159 await sequelizeTypescript.transaction(async t => {
160 return subscription.destroy({ transaction: t })
161 })
162
163 return res.type('json').status(204).end()
164}
165
166async function getUserSubscriptions (req: express.Request, res: express.Response) {
167 const user = res.locals.oauth.token.User as UserModel
168 const actorId = user.Account.Actor.id
169
170 const resultList = await ActorFollowModel.listSubscriptionsForApi(actorId, req.query.start, req.query.count, req.query.sort)
171
172 return res.json(getFormattedObjects(resultList.data, resultList.total))
173}
174
175async function getUserSubscriptionVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
176 const user = res.locals.oauth.token.User as UserModel
177 const resultList = await VideoModel.listForApi({
178 start: req.query.start,
179 count: req.query.count,
180 sort: req.query.sort,
181 includeLocalVideos: false,
182 categoryOneOf: req.query.categoryOneOf,
183 licenceOneOf: req.query.licenceOneOf,
184 languageOneOf: req.query.languageOneOf,
185 tagsOneOf: req.query.tagsOneOf,
186 tagsAllOf: req.query.tagsAllOf,
187 nsfw: buildNSFWFilter(res, req.query.nsfw),
188 filter: req.query.filter as VideoFilter,
189 withFiles: false,
190 actorId: user.Account.Actor.id
191 })
192
193 return res.json(getFormattedObjects(resultList.data, resultList.total))
194}
195
94async function getUserVideos (req: express.Request, res: express.Response, next: express.NextFunction) { 196async function getUserVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
95 const user = res.locals.oauth.token.User as UserModel 197 const user = res.locals.oauth.token.User as UserModel
96 const resultList = await VideoModel.listUserVideosForApi( 198 const resultList = await VideoModel.listUserVideosForApi(
@@ -150,7 +252,7 @@ async function getUserVideoRating (req: express.Request, res: express.Response,
150 videoId, 252 videoId,
151 rating 253 rating
152 } 254 }
153 res.json(json) 255 return res.json(json)
154} 256}
155 257
156async function deleteMe (req: express.Request, res: express.Response) { 258async function deleteMe (req: express.Request, res: express.Response) {
@@ -207,9 +309,5 @@ async function updateMyAvatar (req: express.Request, res: express.Response, next
207 oldUserAuditView 309 oldUserAuditView
208 ) 310 )
209 311
210 return res 312 return res.json({ avatar: avatar.toFormattedJSON() })
211 .json({
212 avatar: avatar.toFormattedJSON()
213 })
214 .end()
215} 313}