diff options
author | Chocobozzz <me@florianbigard.com> | 2019-02-11 11:52:34 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-02-11 11:52:34 +0100 |
commit | 88108880bbdba473cfe36ecbebc1c3c4f972e102 (patch) | |
tree | b242efb3b4f0d7e49d88f2d1f2063b5b3b0489c0 /server/controllers/api/users | |
parent | 53a94c7cfa8368da4cd248d65df8346905938f0c (diff) | |
parent | 9b712a2017e4ab3cf12cd6bd58278905520159d0 (diff) | |
download | PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.tar.gz PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.tar.zst PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.zip |
Merge branch 'develop' into pr/1217
Diffstat (limited to 'server/controllers/api/users')
-rw-r--r-- | server/controllers/api/users/index.ts | 33 | ||||
-rw-r--r-- | server/controllers/api/users/me.ts | 165 | ||||
-rw-r--r-- | server/controllers/api/users/my-blocklist.ts | 125 | ||||
-rw-r--r-- | server/controllers/api/users/my-history.ts | 57 | ||||
-rw-r--r-- | server/controllers/api/users/my-notifications.ts | 108 | ||||
-rw-r--r-- | server/controllers/api/users/my-subscriptions.ts | 170 |
6 files changed, 491 insertions, 167 deletions
diff --git a/server/controllers/api/users/index.ts b/server/controllers/api/users/index.ts index 0b0081520..e3533a7f6 100644 --- a/server/controllers/api/users/index.ts +++ b/server/controllers/api/users/index.ts | |||
@@ -37,6 +37,11 @@ import { UserModel } from '../../../models/account/user' | |||
37 | import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '../../../helpers/audit-logger' | 37 | import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '../../../helpers/audit-logger' |
38 | import { meRouter } from './me' | 38 | import { meRouter } from './me' |
39 | import { deleteUserToken } from '../../../lib/oauth-model' | 39 | import { deleteUserToken } from '../../../lib/oauth-model' |
40 | import { myBlocklistRouter } from './my-blocklist' | ||
41 | import { myVideosHistoryRouter } from './my-history' | ||
42 | import { myNotificationsRouter } from './my-notifications' | ||
43 | import { Notifier } from '../../../lib/notifier' | ||
44 | import { mySubscriptionsRouter } from './my-subscriptions' | ||
40 | 45 | ||
41 | const auditLogger = auditLoggerFactory('users') | 46 | const auditLogger = auditLoggerFactory('users') |
42 | 47 | ||
@@ -53,6 +58,10 @@ const askSendEmailLimiter = new RateLimit({ | |||
53 | }) | 58 | }) |
54 | 59 | ||
55 | const usersRouter = express.Router() | 60 | const usersRouter = express.Router() |
61 | usersRouter.use('/', myNotificationsRouter) | ||
62 | usersRouter.use('/', mySubscriptionsRouter) | ||
63 | usersRouter.use('/', myBlocklistRouter) | ||
64 | usersRouter.use('/', myVideosHistoryRouter) | ||
56 | usersRouter.use('/', meRouter) | 65 | usersRouter.use('/', meRouter) |
57 | 66 | ||
58 | usersRouter.get('/autocomplete', | 67 | usersRouter.get('/autocomplete', |
@@ -207,6 +216,8 @@ async function registerUser (req: express.Request, res: express.Response) { | |||
207 | await sendVerifyUserEmail(user) | 216 | await sendVerifyUserEmail(user) |
208 | } | 217 | } |
209 | 218 | ||
219 | Notifier.Instance.notifyOnNewUserRegistration(user) | ||
220 | |||
210 | return res.type('json').status(204).end() | 221 | return res.type('json').status(204).end() |
211 | } | 222 | } |
212 | 223 | ||
@@ -218,7 +229,7 @@ async function unblockUser (req: express.Request, res: express.Response, next: e | |||
218 | return res.status(204).end() | 229 | return res.status(204).end() |
219 | } | 230 | } |
220 | 231 | ||
221 | async function blockUser (req: express.Request, res: express.Response, next: express.NextFunction) { | 232 | async function blockUser (req: express.Request, res: express.Response) { |
222 | const user: UserModel = res.locals.user | 233 | const user: UserModel = res.locals.user |
223 | const reason = req.body.reason | 234 | const reason = req.body.reason |
224 | 235 | ||
@@ -227,23 +238,23 @@ async function blockUser (req: express.Request, res: express.Response, next: exp | |||
227 | return res.status(204).end() | 238 | return res.status(204).end() |
228 | } | 239 | } |
229 | 240 | ||
230 | function getUser (req: express.Request, res: express.Response, next: express.NextFunction) { | 241 | function getUser (req: express.Request, res: express.Response) { |
231 | return res.json((res.locals.user as UserModel).toFormattedJSON()) | 242 | return res.json((res.locals.user as UserModel).toFormattedJSON()) |
232 | } | 243 | } |
233 | 244 | ||
234 | async function autocompleteUsers (req: express.Request, res: express.Response, next: express.NextFunction) { | 245 | async function autocompleteUsers (req: express.Request, res: express.Response) { |
235 | const resultList = await UserModel.autoComplete(req.query.search as string) | 246 | const resultList = await UserModel.autoComplete(req.query.search as string) |
236 | 247 | ||
237 | return res.json(resultList) | 248 | return res.json(resultList) |
238 | } | 249 | } |
239 | 250 | ||
240 | async function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) { | 251 | async function listUsers (req: express.Request, res: express.Response) { |
241 | const resultList = await UserModel.listForApi(req.query.start, req.query.count, req.query.sort) | 252 | const resultList = await UserModel.listForApi(req.query.start, req.query.count, req.query.sort, req.query.search) |
242 | 253 | ||
243 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | 254 | return res.json(getFormattedObjects(resultList.data, resultList.total)) |
244 | } | 255 | } |
245 | 256 | ||
246 | async function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) { | 257 | async function removeUser (req: express.Request, res: express.Response) { |
247 | const user: UserModel = res.locals.user | 258 | const user: UserModel = res.locals.user |
248 | 259 | ||
249 | await user.destroy() | 260 | await user.destroy() |
@@ -253,13 +264,15 @@ async function removeUser (req: express.Request, res: express.Response, next: ex | |||
253 | return res.sendStatus(204) | 264 | return res.sendStatus(204) |
254 | } | 265 | } |
255 | 266 | ||
256 | async function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) { | 267 | async function updateUser (req: express.Request, res: express.Response) { |
257 | const body: UserUpdate = req.body | 268 | const body: UserUpdate = req.body |
258 | const userToUpdate = res.locals.user as UserModel | 269 | const userToUpdate = res.locals.user as UserModel |
259 | const oldUserAuditView = new UserAuditView(userToUpdate.toFormattedJSON()) | 270 | const oldUserAuditView = new UserAuditView(userToUpdate.toFormattedJSON()) |
260 | const roleChanged = body.role !== undefined && body.role !== userToUpdate.role | 271 | const roleChanged = body.role !== undefined && body.role !== userToUpdate.role |
261 | 272 | ||
273 | if (body.password !== undefined) userToUpdate.password = body.password | ||
262 | if (body.email !== undefined) userToUpdate.email = body.email | 274 | if (body.email !== undefined) userToUpdate.email = body.email |
275 | if (body.emailVerified !== undefined) userToUpdate.emailVerified = body.emailVerified | ||
263 | if (body.videoQuota !== undefined) userToUpdate.videoQuota = body.videoQuota | 276 | if (body.videoQuota !== undefined) userToUpdate.videoQuota = body.videoQuota |
264 | if (body.videoQuotaDaily !== undefined) userToUpdate.videoQuotaDaily = body.videoQuotaDaily | 277 | if (body.videoQuotaDaily !== undefined) userToUpdate.videoQuotaDaily = body.videoQuotaDaily |
265 | if (body.role !== undefined) userToUpdate.role = body.role | 278 | if (body.role !== undefined) userToUpdate.role = body.role |
@@ -267,11 +280,11 @@ async function updateUser (req: express.Request, res: express.Response, next: ex | |||
267 | const user = await userToUpdate.save() | 280 | const user = await userToUpdate.save() |
268 | 281 | ||
269 | // Destroy user token to refresh rights | 282 | // Destroy user token to refresh rights |
270 | if (roleChanged) await deleteUserToken(userToUpdate.id) | 283 | if (roleChanged || body.password !== undefined) await deleteUserToken(userToUpdate.id) |
271 | 284 | ||
272 | auditLogger.update(getAuditIdFromRes(res), new UserAuditView(user.toFormattedJSON()), oldUserAuditView) | 285 | auditLogger.update(getAuditIdFromRes(res), new UserAuditView(user.toFormattedJSON()), oldUserAuditView) |
273 | 286 | ||
274 | // Don't need to send this update to followers, these attributes are not propagated | 287 | // Don't need to send this update to followers, these attributes are not federated |
275 | 288 | ||
276 | return res.sendStatus(204) | 289 | return res.sendStatus(204) |
277 | } | 290 | } |
@@ -281,7 +294,7 @@ async function askResetUserPassword (req: express.Request, res: express.Response | |||
281 | 294 | ||
282 | const verificationString = await Redis.Instance.setResetPasswordVerificationString(user.id) | 295 | const verificationString = await Redis.Instance.setResetPasswordVerificationString(user.id) |
283 | const url = CONFIG.WEBSERVER.URL + '/reset-password?userId=' + user.id + '&verificationString=' + verificationString | 296 | const url = CONFIG.WEBSERVER.URL + '/reset-password?userId=' + user.id + '&verificationString=' + verificationString |
284 | await Emailer.Instance.addForgetPasswordEmailJob(user.email, url) | 297 | await Emailer.Instance.addPasswordResetEmailJob(user.email, url) |
285 | 298 | ||
286 | return res.status(204).end() | 299 | return res.status(204).end() |
287 | } | 300 | } |
diff --git a/server/controllers/api/users/me.ts b/server/controllers/api/users/me.ts index 591ec6b25..d5e154869 100644 --- a/server/controllers/api/users/me.ts +++ b/server/controllers/api/users/me.ts | |||
@@ -2,47 +2,34 @@ import * as express from 'express' | |||
2 | import 'multer' | 2 | import 'multer' |
3 | import { UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../../shared' | 3 | import { UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../../shared' |
4 | import { getFormattedObjects } from '../../../helpers/utils' | 4 | import { getFormattedObjects } from '../../../helpers/utils' |
5 | import { CONFIG, IMAGE_MIMETYPE_EXT, sequelizeTypescript } from '../../../initializers' | 5 | import { CONFIG, MIMETYPES, sequelizeTypescript } from '../../../initializers' |
6 | import { sendUpdateActor } from '../../../lib/activitypub/send' | 6 | import { sendUpdateActor } from '../../../lib/activitypub/send' |
7 | import { | 7 | import { |
8 | asyncMiddleware, | 8 | asyncMiddleware, |
9 | asyncRetryTransactionMiddleware, | 9 | asyncRetryTransactionMiddleware, |
10 | authenticate, | 10 | authenticate, |
11 | commonVideosFiltersValidator, | ||
12 | paginationValidator, | 11 | paginationValidator, |
13 | setDefaultPagination, | 12 | setDefaultPagination, |
14 | setDefaultSort, | 13 | setDefaultSort, |
15 | userSubscriptionAddValidator, | ||
16 | userSubscriptionGetValidator, | ||
17 | usersUpdateMeValidator, | 14 | usersUpdateMeValidator, |
18 | usersVideoRatingValidator | 15 | usersVideoRatingValidator |
19 | } from '../../../middlewares' | 16 | } from '../../../middlewares' |
20 | import { | 17 | import { deleteMeValidator, videoImportsSortValidator, videosSortValidator } from '../../../middlewares/validators' |
21 | areSubscriptionsExistValidator, | ||
22 | deleteMeValidator, | ||
23 | userSubscriptionsSortValidator, | ||
24 | videoImportsSortValidator, | ||
25 | videosSortValidator | ||
26 | } from '../../../middlewares/validators' | ||
27 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' | 18 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' |
28 | import { UserModel } from '../../../models/account/user' | 19 | import { UserModel } from '../../../models/account/user' |
29 | import { VideoModel } from '../../../models/video/video' | 20 | import { VideoModel } from '../../../models/video/video' |
30 | import { VideoSortField } from '../../../../client/src/app/shared/video/sort-field.type' | 21 | import { VideoSortField } from '../../../../client/src/app/shared/video/sort-field.type' |
31 | import { buildNSFWFilter, createReqFiles } from '../../../helpers/express-utils' | 22 | import { createReqFiles } from '../../../helpers/express-utils' |
32 | import { UserVideoQuota } from '../../../../shared/models/users/user-video-quota.model' | 23 | import { UserVideoQuota } from '../../../../shared/models/users/user-video-quota.model' |
33 | import { updateAvatarValidator } from '../../../middlewares/validators/avatar' | 24 | import { updateAvatarValidator } from '../../../middlewares/validators/avatar' |
34 | import { updateActorAvatarFile } from '../../../lib/avatar' | 25 | import { updateActorAvatarFile } from '../../../lib/avatar' |
35 | import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '../../../helpers/audit-logger' | 26 | import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '../../../helpers/audit-logger' |
36 | import { VideoImportModel } from '../../../models/video/video-import' | 27 | import { VideoImportModel } from '../../../models/video/video-import' |
37 | import { VideoFilter } from '../../../../shared/models/videos/video-query.type' | ||
38 | import { ActorFollowModel } from '../../../models/activitypub/actor-follow' | ||
39 | import { JobQueue } from '../../../lib/job-queue' | ||
40 | import { logger } from '../../../helpers/logger' | ||
41 | import { AccountModel } from '../../../models/account/account' | 28 | import { AccountModel } from '../../../models/account/account' |
42 | 29 | ||
43 | const auditLogger = auditLoggerFactory('users-me') | 30 | const auditLogger = auditLoggerFactory('users-me') |
44 | 31 | ||
45 | const reqAvatarFile = createReqFiles([ 'avatarfile' ], IMAGE_MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.AVATARS_DIR }) | 32 | const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.TMP_DIR }) |
46 | 33 | ||
47 | const meRouter = express.Router() | 34 | const meRouter = express.Router() |
48 | 35 | ||
@@ -98,51 +85,6 @@ meRouter.post('/me/avatar/pick', | |||
98 | asyncRetryTransactionMiddleware(updateMyAvatar) | 85 | asyncRetryTransactionMiddleware(updateMyAvatar) |
99 | ) | 86 | ) |
100 | 87 | ||
101 | // ##### Subscriptions part ##### | ||
102 | |||
103 | meRouter.get('/me/subscriptions/videos', | ||
104 | authenticate, | ||
105 | paginationValidator, | ||
106 | videosSortValidator, | ||
107 | setDefaultSort, | ||
108 | setDefaultPagination, | ||
109 | commonVideosFiltersValidator, | ||
110 | asyncMiddleware(getUserSubscriptionVideos) | ||
111 | ) | ||
112 | |||
113 | meRouter.get('/me/subscriptions/exist', | ||
114 | authenticate, | ||
115 | areSubscriptionsExistValidator, | ||
116 | asyncMiddleware(areSubscriptionsExist) | ||
117 | ) | ||
118 | |||
119 | meRouter.get('/me/subscriptions', | ||
120 | authenticate, | ||
121 | paginationValidator, | ||
122 | userSubscriptionsSortValidator, | ||
123 | setDefaultSort, | ||
124 | setDefaultPagination, | ||
125 | asyncMiddleware(getUserSubscriptions) | ||
126 | ) | ||
127 | |||
128 | meRouter.post('/me/subscriptions', | ||
129 | authenticate, | ||
130 | userSubscriptionAddValidator, | ||
131 | asyncMiddleware(addUserSubscription) | ||
132 | ) | ||
133 | |||
134 | meRouter.get('/me/subscriptions/:uri', | ||
135 | authenticate, | ||
136 | userSubscriptionGetValidator, | ||
137 | getUserSubscription | ||
138 | ) | ||
139 | |||
140 | meRouter.delete('/me/subscriptions/:uri', | ||
141 | authenticate, | ||
142 | userSubscriptionGetValidator, | ||
143 | asyncRetryTransactionMiddleware(deleteUserSubscription) | ||
144 | ) | ||
145 | |||
146 | // --------------------------------------------------------------------------- | 88 | // --------------------------------------------------------------------------- |
147 | 89 | ||
148 | export { | 90 | export { |
@@ -151,99 +93,6 @@ export { | |||
151 | 93 | ||
152 | // --------------------------------------------------------------------------- | 94 | // --------------------------------------------------------------------------- |
153 | 95 | ||
154 | async function areSubscriptionsExist (req: express.Request, res: express.Response) { | ||
155 | const uris = req.query.uris as string[] | ||
156 | const user = res.locals.oauth.token.User as UserModel | ||
157 | |||
158 | const handles = uris.map(u => { | ||
159 | let [ name, host ] = u.split('@') | ||
160 | if (host === CONFIG.WEBSERVER.HOST) host = null | ||
161 | |||
162 | return { name, host, uri: u } | ||
163 | }) | ||
164 | |||
165 | const results = await ActorFollowModel.listSubscribedIn(user.Account.Actor.id, handles) | ||
166 | |||
167 | const existObject: { [id: string ]: boolean } = {} | ||
168 | for (const handle of handles) { | ||
169 | const obj = results.find(r => { | ||
170 | const server = r.ActorFollowing.Server | ||
171 | |||
172 | return r.ActorFollowing.preferredUsername === handle.name && | ||
173 | ( | ||
174 | (!server && !handle.host) || | ||
175 | (server.host === handle.host) | ||
176 | ) | ||
177 | }) | ||
178 | |||
179 | existObject[handle.uri] = obj !== undefined | ||
180 | } | ||
181 | |||
182 | return res.json(existObject) | ||
183 | } | ||
184 | |||
185 | async function addUserSubscription (req: express.Request, res: express.Response) { | ||
186 | const user = res.locals.oauth.token.User as UserModel | ||
187 | const [ name, host ] = req.body.uri.split('@') | ||
188 | |||
189 | const payload = { | ||
190 | name, | ||
191 | host, | ||
192 | followerActorId: user.Account.Actor.id | ||
193 | } | ||
194 | |||
195 | JobQueue.Instance.createJob({ type: 'activitypub-follow', payload }) | ||
196 | .catch(err => logger.error('Cannot create follow job for subscription %s.', req.body.uri, err)) | ||
197 | |||
198 | return res.status(204).end() | ||
199 | } | ||
200 | |||
201 | function getUserSubscription (req: express.Request, res: express.Response) { | ||
202 | const subscription: ActorFollowModel = res.locals.subscription | ||
203 | |||
204 | return res.json(subscription.ActorFollowing.VideoChannel.toFormattedJSON()) | ||
205 | } | ||
206 | |||
207 | async function deleteUserSubscription (req: express.Request, res: express.Response) { | ||
208 | const subscription: ActorFollowModel = res.locals.subscription | ||
209 | |||
210 | await sequelizeTypescript.transaction(async t => { | ||
211 | return subscription.destroy({ transaction: t }) | ||
212 | }) | ||
213 | |||
214 | return res.type('json').status(204).end() | ||
215 | } | ||
216 | |||
217 | async function getUserSubscriptions (req: express.Request, res: express.Response) { | ||
218 | const user = res.locals.oauth.token.User as UserModel | ||
219 | const actorId = user.Account.Actor.id | ||
220 | |||
221 | const resultList = await ActorFollowModel.listSubscriptionsForApi(actorId, req.query.start, req.query.count, req.query.sort) | ||
222 | |||
223 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | ||
224 | } | ||
225 | |||
226 | async function getUserSubscriptionVideos (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
227 | const user = res.locals.oauth.token.User as UserModel | ||
228 | const resultList = await VideoModel.listForApi({ | ||
229 | start: req.query.start, | ||
230 | count: req.query.count, | ||
231 | sort: req.query.sort, | ||
232 | includeLocalVideos: false, | ||
233 | categoryOneOf: req.query.categoryOneOf, | ||
234 | licenceOneOf: req.query.licenceOneOf, | ||
235 | languageOneOf: req.query.languageOneOf, | ||
236 | tagsOneOf: req.query.tagsOneOf, | ||
237 | tagsAllOf: req.query.tagsAllOf, | ||
238 | nsfw: buildNSFWFilter(res, req.query.nsfw), | ||
239 | filter: req.query.filter as VideoFilter, | ||
240 | withFiles: false, | ||
241 | actorId: user.Account.Actor.id | ||
242 | }) | ||
243 | |||
244 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | ||
245 | } | ||
246 | |||
247 | async function getUserVideos (req: express.Request, res: express.Response, next: express.NextFunction) { | 96 | async function getUserVideos (req: express.Request, res: express.Response, next: express.NextFunction) { |
248 | const user = res.locals.oauth.token.User as UserModel | 97 | const user = res.locals.oauth.token.User as UserModel |
249 | const resultList = await VideoModel.listUserVideosForApi( | 98 | const resultList = await VideoModel.listUserVideosForApi( |
@@ -318,7 +167,7 @@ async function deleteMe (req: express.Request, res: express.Response) { | |||
318 | return res.sendStatus(204) | 167 | return res.sendStatus(204) |
319 | } | 168 | } |
320 | 169 | ||
321 | async function updateMe (req: express.Request, res: express.Response, next: express.NextFunction) { | 170 | async function updateMe (req: express.Request, res: express.Response) { |
322 | const body: UserUpdateMe = req.body | 171 | const body: UserUpdateMe = req.body |
323 | 172 | ||
324 | const user: UserModel = res.locals.oauth.token.user | 173 | const user: UserModel = res.locals.oauth.token.user |
@@ -327,7 +176,9 @@ async function updateMe (req: express.Request, res: express.Response, next: expr | |||
327 | if (body.password !== undefined) user.password = body.password | 176 | if (body.password !== undefined) user.password = body.password |
328 | if (body.email !== undefined) user.email = body.email | 177 | if (body.email !== undefined) user.email = body.email |
329 | if (body.nsfwPolicy !== undefined) user.nsfwPolicy = body.nsfwPolicy | 178 | if (body.nsfwPolicy !== undefined) user.nsfwPolicy = body.nsfwPolicy |
179 | if (body.webTorrentEnabled !== undefined) user.webTorrentEnabled = body.webTorrentEnabled | ||
330 | if (body.autoPlayVideo !== undefined) user.autoPlayVideo = body.autoPlayVideo | 180 | if (body.autoPlayVideo !== undefined) user.autoPlayVideo = body.autoPlayVideo |
181 | if (body.videosHistoryEnabled !== undefined) user.videosHistoryEnabled = body.videosHistoryEnabled | ||
331 | 182 | ||
332 | await sequelizeTypescript.transaction(async t => { | 183 | await sequelizeTypescript.transaction(async t => { |
333 | const userAccount = await AccountModel.load(user.Account.id) | 184 | const userAccount = await AccountModel.load(user.Account.id) |
@@ -346,7 +197,7 @@ async function updateMe (req: express.Request, res: express.Response, next: expr | |||
346 | return res.sendStatus(204) | 197 | return res.sendStatus(204) |
347 | } | 198 | } |
348 | 199 | ||
349 | async function updateMyAvatar (req: express.Request, res: express.Response, next: express.NextFunction) { | 200 | async function updateMyAvatar (req: express.Request, res: express.Response) { |
350 | const avatarPhysicalFile = req.files[ 'avatarfile' ][ 0 ] | 201 | const avatarPhysicalFile = req.files[ 'avatarfile' ][ 0 ] |
351 | const user: UserModel = res.locals.oauth.token.user | 202 | const user: UserModel = res.locals.oauth.token.user |
352 | const oldUserAuditView = new UserAuditView(user.toFormattedJSON()) | 203 | const oldUserAuditView = new UserAuditView(user.toFormattedJSON()) |
diff --git a/server/controllers/api/users/my-blocklist.ts b/server/controllers/api/users/my-blocklist.ts new file mode 100644 index 000000000..9575eab46 --- /dev/null +++ b/server/controllers/api/users/my-blocklist.ts | |||
@@ -0,0 +1,125 @@ | |||
1 | import * as express from 'express' | ||
2 | import 'multer' | ||
3 | import { getFormattedObjects } from '../../../helpers/utils' | ||
4 | import { | ||
5 | asyncMiddleware, | ||
6 | asyncRetryTransactionMiddleware, | ||
7 | authenticate, | ||
8 | paginationValidator, | ||
9 | setDefaultPagination, | ||
10 | setDefaultSort, | ||
11 | unblockAccountByAccountValidator | ||
12 | } from '../../../middlewares' | ||
13 | import { | ||
14 | accountsBlocklistSortValidator, | ||
15 | blockAccountValidator, | ||
16 | blockServerValidator, | ||
17 | serversBlocklistSortValidator, | ||
18 | unblockServerByAccountValidator | ||
19 | } from '../../../middlewares/validators' | ||
20 | import { UserModel } from '../../../models/account/user' | ||
21 | import { AccountModel } from '../../../models/account/account' | ||
22 | import { AccountBlocklistModel } from '../../../models/account/account-blocklist' | ||
23 | import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../../../lib/blocklist' | ||
24 | import { ServerBlocklistModel } from '../../../models/server/server-blocklist' | ||
25 | import { ServerModel } from '../../../models/server/server' | ||
26 | |||
27 | const myBlocklistRouter = express.Router() | ||
28 | |||
29 | myBlocklistRouter.get('/me/blocklist/accounts', | ||
30 | authenticate, | ||
31 | paginationValidator, | ||
32 | accountsBlocklistSortValidator, | ||
33 | setDefaultSort, | ||
34 | setDefaultPagination, | ||
35 | asyncMiddleware(listBlockedAccounts) | ||
36 | ) | ||
37 | |||
38 | myBlocklistRouter.post('/me/blocklist/accounts', | ||
39 | authenticate, | ||
40 | asyncMiddleware(blockAccountValidator), | ||
41 | asyncRetryTransactionMiddleware(blockAccount) | ||
42 | ) | ||
43 | |||
44 | myBlocklistRouter.delete('/me/blocklist/accounts/:accountName', | ||
45 | authenticate, | ||
46 | asyncMiddleware(unblockAccountByAccountValidator), | ||
47 | asyncRetryTransactionMiddleware(unblockAccount) | ||
48 | ) | ||
49 | |||
50 | myBlocklistRouter.get('/me/blocklist/servers', | ||
51 | authenticate, | ||
52 | paginationValidator, | ||
53 | serversBlocklistSortValidator, | ||
54 | setDefaultSort, | ||
55 | setDefaultPagination, | ||
56 | asyncMiddleware(listBlockedServers) | ||
57 | ) | ||
58 | |||
59 | myBlocklistRouter.post('/me/blocklist/servers', | ||
60 | authenticate, | ||
61 | asyncMiddleware(blockServerValidator), | ||
62 | asyncRetryTransactionMiddleware(blockServer) | ||
63 | ) | ||
64 | |||
65 | myBlocklistRouter.delete('/me/blocklist/servers/:host', | ||
66 | authenticate, | ||
67 | asyncMiddleware(unblockServerByAccountValidator), | ||
68 | asyncRetryTransactionMiddleware(unblockServer) | ||
69 | ) | ||
70 | |||
71 | export { | ||
72 | myBlocklistRouter | ||
73 | } | ||
74 | |||
75 | // --------------------------------------------------------------------------- | ||
76 | |||
77 | async function listBlockedAccounts (req: express.Request, res: express.Response) { | ||
78 | const user: UserModel = res.locals.oauth.token.User | ||
79 | |||
80 | const resultList = await AccountBlocklistModel.listForApi(user.Account.id, req.query.start, req.query.count, req.query.sort) | ||
81 | |||
82 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | ||
83 | } | ||
84 | |||
85 | async function blockAccount (req: express.Request, res: express.Response) { | ||
86 | const user: UserModel = res.locals.oauth.token.User | ||
87 | const accountToBlock: AccountModel = res.locals.account | ||
88 | |||
89 | await addAccountInBlocklist(user.Account.id, accountToBlock.id) | ||
90 | |||
91 | return res.status(204).end() | ||
92 | } | ||
93 | |||
94 | async function unblockAccount (req: express.Request, res: express.Response) { | ||
95 | const accountBlock: AccountBlocklistModel = res.locals.accountBlock | ||
96 | |||
97 | await removeAccountFromBlocklist(accountBlock) | ||
98 | |||
99 | return res.status(204).end() | ||
100 | } | ||
101 | |||
102 | async function listBlockedServers (req: express.Request, res: express.Response) { | ||
103 | const user: UserModel = res.locals.oauth.token.User | ||
104 | |||
105 | const resultList = await ServerBlocklistModel.listForApi(user.Account.id, req.query.start, req.query.count, req.query.sort) | ||
106 | |||
107 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | ||
108 | } | ||
109 | |||
110 | async function blockServer (req: express.Request, res: express.Response) { | ||
111 | const user: UserModel = res.locals.oauth.token.User | ||
112 | const serverToBlock: ServerModel = res.locals.server | ||
113 | |||
114 | await addServerInBlocklist(user.Account.id, serverToBlock.id) | ||
115 | |||
116 | return res.status(204).end() | ||
117 | } | ||
118 | |||
119 | async function unblockServer (req: express.Request, res: express.Response) { | ||
120 | const serverBlock: ServerBlocklistModel = res.locals.serverBlock | ||
121 | |||
122 | await removeServerFromBlocklist(serverBlock) | ||
123 | |||
124 | return res.status(204).end() | ||
125 | } | ||
diff --git a/server/controllers/api/users/my-history.ts b/server/controllers/api/users/my-history.ts new file mode 100644 index 000000000..6cd782c47 --- /dev/null +++ b/server/controllers/api/users/my-history.ts | |||
@@ -0,0 +1,57 @@ | |||
1 | import * as express from 'express' | ||
2 | import { | ||
3 | asyncMiddleware, | ||
4 | asyncRetryTransactionMiddleware, | ||
5 | authenticate, | ||
6 | paginationValidator, | ||
7 | setDefaultPagination, | ||
8 | userHistoryRemoveValidator | ||
9 | } from '../../../middlewares' | ||
10 | import { UserModel } from '../../../models/account/user' | ||
11 | import { getFormattedObjects } from '../../../helpers/utils' | ||
12 | import { UserVideoHistoryModel } from '../../../models/account/user-video-history' | ||
13 | import { sequelizeTypescript } from '../../../initializers' | ||
14 | |||
15 | const myVideosHistoryRouter = express.Router() | ||
16 | |||
17 | myVideosHistoryRouter.get('/me/history/videos', | ||
18 | authenticate, | ||
19 | paginationValidator, | ||
20 | setDefaultPagination, | ||
21 | asyncMiddleware(listMyVideosHistory) | ||
22 | ) | ||
23 | |||
24 | myVideosHistoryRouter.post('/me/history/videos/remove', | ||
25 | authenticate, | ||
26 | userHistoryRemoveValidator, | ||
27 | asyncRetryTransactionMiddleware(removeUserHistory) | ||
28 | ) | ||
29 | |||
30 | // --------------------------------------------------------------------------- | ||
31 | |||
32 | export { | ||
33 | myVideosHistoryRouter | ||
34 | } | ||
35 | |||
36 | // --------------------------------------------------------------------------- | ||
37 | |||
38 | async function listMyVideosHistory (req: express.Request, res: express.Response) { | ||
39 | const user: UserModel = res.locals.oauth.token.User | ||
40 | |||
41 | const resultList = await UserVideoHistoryModel.listForApi(user, req.query.start, req.query.count) | ||
42 | |||
43 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | ||
44 | } | ||
45 | |||
46 | async function removeUserHistory (req: express.Request, res: express.Response) { | ||
47 | const user: UserModel = res.locals.oauth.token.User | ||
48 | const beforeDate = req.body.beforeDate || null | ||
49 | |||
50 | await sequelizeTypescript.transaction(t => { | ||
51 | return UserVideoHistoryModel.removeHistoryBefore(user, beforeDate, t) | ||
52 | }) | ||
53 | |||
54 | // Do not send the delete to other instances, we delete OUR copy of this video abuse | ||
55 | |||
56 | return res.type('json').status(204).end() | ||
57 | } | ||
diff --git a/server/controllers/api/users/my-notifications.ts b/server/controllers/api/users/my-notifications.ts new file mode 100644 index 000000000..76cf97587 --- /dev/null +++ b/server/controllers/api/users/my-notifications.ts | |||
@@ -0,0 +1,108 @@ | |||
1 | import * as express from 'express' | ||
2 | import 'multer' | ||
3 | import { | ||
4 | asyncMiddleware, | ||
5 | asyncRetryTransactionMiddleware, | ||
6 | authenticate, | ||
7 | paginationValidator, | ||
8 | setDefaultPagination, | ||
9 | setDefaultSort, | ||
10 | userNotificationsSortValidator | ||
11 | } from '../../../middlewares' | ||
12 | import { UserModel } from '../../../models/account/user' | ||
13 | import { getFormattedObjects } from '../../../helpers/utils' | ||
14 | import { UserNotificationModel } from '../../../models/account/user-notification' | ||
15 | import { meRouter } from './me' | ||
16 | import { | ||
17 | listUserNotificationsValidator, | ||
18 | markAsReadUserNotificationsValidator, | ||
19 | updateNotificationSettingsValidator | ||
20 | } from '../../../middlewares/validators/user-notifications' | ||
21 | import { UserNotificationSetting } from '../../../../shared/models/users' | ||
22 | import { UserNotificationSettingModel } from '../../../models/account/user-notification-setting' | ||
23 | |||
24 | const myNotificationsRouter = express.Router() | ||
25 | |||
26 | meRouter.put('/me/notification-settings', | ||
27 | authenticate, | ||
28 | updateNotificationSettingsValidator, | ||
29 | asyncRetryTransactionMiddleware(updateNotificationSettings) | ||
30 | ) | ||
31 | |||
32 | myNotificationsRouter.get('/me/notifications', | ||
33 | authenticate, | ||
34 | paginationValidator, | ||
35 | userNotificationsSortValidator, | ||
36 | setDefaultSort, | ||
37 | setDefaultPagination, | ||
38 | listUserNotificationsValidator, | ||
39 | asyncMiddleware(listUserNotifications) | ||
40 | ) | ||
41 | |||
42 | myNotificationsRouter.post('/me/notifications/read', | ||
43 | authenticate, | ||
44 | markAsReadUserNotificationsValidator, | ||
45 | asyncMiddleware(markAsReadUserNotifications) | ||
46 | ) | ||
47 | |||
48 | myNotificationsRouter.post('/me/notifications/read-all', | ||
49 | authenticate, | ||
50 | asyncMiddleware(markAsReadAllUserNotifications) | ||
51 | ) | ||
52 | |||
53 | export { | ||
54 | myNotificationsRouter | ||
55 | } | ||
56 | |||
57 | // --------------------------------------------------------------------------- | ||
58 | |||
59 | async function updateNotificationSettings (req: express.Request, res: express.Response) { | ||
60 | const user: UserModel = res.locals.oauth.token.User | ||
61 | const body = req.body | ||
62 | |||
63 | const query = { | ||
64 | where: { | ||
65 | userId: user.id | ||
66 | } | ||
67 | } | ||
68 | |||
69 | const values: UserNotificationSetting = { | ||
70 | newVideoFromSubscription: body.newVideoFromSubscription, | ||
71 | newCommentOnMyVideo: body.newCommentOnMyVideo, | ||
72 | videoAbuseAsModerator: body.videoAbuseAsModerator, | ||
73 | blacklistOnMyVideo: body.blacklistOnMyVideo, | ||
74 | myVideoPublished: body.myVideoPublished, | ||
75 | myVideoImportFinished: body.myVideoImportFinished, | ||
76 | newFollow: body.newFollow, | ||
77 | newUserRegistration: body.newUserRegistration, | ||
78 | commentMention: body.commentMention | ||
79 | } | ||
80 | |||
81 | await UserNotificationSettingModel.update(values, query) | ||
82 | |||
83 | return res.status(204).end() | ||
84 | } | ||
85 | |||
86 | async function listUserNotifications (req: express.Request, res: express.Response) { | ||
87 | const user: UserModel = res.locals.oauth.token.User | ||
88 | |||
89 | const resultList = await UserNotificationModel.listForApi(user.id, req.query.start, req.query.count, req.query.sort, req.query.unread) | ||
90 | |||
91 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | ||
92 | } | ||
93 | |||
94 | async function markAsReadUserNotifications (req: express.Request, res: express.Response) { | ||
95 | const user: UserModel = res.locals.oauth.token.User | ||
96 | |||
97 | await UserNotificationModel.markAsRead(user.id, req.body.ids) | ||
98 | |||
99 | return res.status(204).end() | ||
100 | } | ||
101 | |||
102 | async function markAsReadAllUserNotifications (req: express.Request, res: express.Response) { | ||
103 | const user: UserModel = res.locals.oauth.token.User | ||
104 | |||
105 | await UserNotificationModel.markAllAsRead(user.id) | ||
106 | |||
107 | return res.status(204).end() | ||
108 | } | ||
diff --git a/server/controllers/api/users/my-subscriptions.ts b/server/controllers/api/users/my-subscriptions.ts new file mode 100644 index 000000000..accca6d52 --- /dev/null +++ b/server/controllers/api/users/my-subscriptions.ts | |||
@@ -0,0 +1,170 @@ | |||
1 | import * as express from 'express' | ||
2 | import 'multer' | ||
3 | import { getFormattedObjects } from '../../../helpers/utils' | ||
4 | import { CONFIG, sequelizeTypescript } from '../../../initializers' | ||
5 | import { | ||
6 | asyncMiddleware, | ||
7 | asyncRetryTransactionMiddleware, | ||
8 | authenticate, | ||
9 | commonVideosFiltersValidator, | ||
10 | paginationValidator, | ||
11 | setDefaultPagination, | ||
12 | setDefaultSort, | ||
13 | userSubscriptionAddValidator, | ||
14 | userSubscriptionGetValidator | ||
15 | } from '../../../middlewares' | ||
16 | import { areSubscriptionsExistValidator, userSubscriptionsSortValidator, videosSortValidator } from '../../../middlewares/validators' | ||
17 | import { UserModel } from '../../../models/account/user' | ||
18 | import { VideoModel } from '../../../models/video/video' | ||
19 | import { buildNSFWFilter } from '../../../helpers/express-utils' | ||
20 | import { VideoFilter } from '../../../../shared/models/videos/video-query.type' | ||
21 | import { ActorFollowModel } from '../../../models/activitypub/actor-follow' | ||
22 | import { JobQueue } from '../../../lib/job-queue' | ||
23 | import { logger } from '../../../helpers/logger' | ||
24 | |||
25 | const mySubscriptionsRouter = express.Router() | ||
26 | |||
27 | mySubscriptionsRouter.get('/me/subscriptions/videos', | ||
28 | authenticate, | ||
29 | paginationValidator, | ||
30 | videosSortValidator, | ||
31 | setDefaultSort, | ||
32 | setDefaultPagination, | ||
33 | commonVideosFiltersValidator, | ||
34 | asyncMiddleware(getUserSubscriptionVideos) | ||
35 | ) | ||
36 | |||
37 | mySubscriptionsRouter.get('/me/subscriptions/exist', | ||
38 | authenticate, | ||
39 | areSubscriptionsExistValidator, | ||
40 | asyncMiddleware(areSubscriptionsExist) | ||
41 | ) | ||
42 | |||
43 | mySubscriptionsRouter.get('/me/subscriptions', | ||
44 | authenticate, | ||
45 | paginationValidator, | ||
46 | userSubscriptionsSortValidator, | ||
47 | setDefaultSort, | ||
48 | setDefaultPagination, | ||
49 | asyncMiddleware(getUserSubscriptions) | ||
50 | ) | ||
51 | |||
52 | mySubscriptionsRouter.post('/me/subscriptions', | ||
53 | authenticate, | ||
54 | userSubscriptionAddValidator, | ||
55 | asyncMiddleware(addUserSubscription) | ||
56 | ) | ||
57 | |||
58 | mySubscriptionsRouter.get('/me/subscriptions/:uri', | ||
59 | authenticate, | ||
60 | userSubscriptionGetValidator, | ||
61 | getUserSubscription | ||
62 | ) | ||
63 | |||
64 | mySubscriptionsRouter.delete('/me/subscriptions/:uri', | ||
65 | authenticate, | ||
66 | userSubscriptionGetValidator, | ||
67 | asyncRetryTransactionMiddleware(deleteUserSubscription) | ||
68 | ) | ||
69 | |||
70 | // --------------------------------------------------------------------------- | ||
71 | |||
72 | export { | ||
73 | mySubscriptionsRouter | ||
74 | } | ||
75 | |||
76 | // --------------------------------------------------------------------------- | ||
77 | |||
78 | async function areSubscriptionsExist (req: express.Request, res: express.Response) { | ||
79 | const uris = req.query.uris as string[] | ||
80 | const user = res.locals.oauth.token.User as UserModel | ||
81 | |||
82 | const handles = uris.map(u => { | ||
83 | let [ name, host ] = u.split('@') | ||
84 | if (host === CONFIG.WEBSERVER.HOST) host = null | ||
85 | |||
86 | return { name, host, uri: u } | ||
87 | }) | ||
88 | |||
89 | const results = await ActorFollowModel.listSubscribedIn(user.Account.Actor.id, handles) | ||
90 | |||
91 | const existObject: { [id: string ]: boolean } = {} | ||
92 | for (const handle of handles) { | ||
93 | const obj = results.find(r => { | ||
94 | const server = r.ActorFollowing.Server | ||
95 | |||
96 | return r.ActorFollowing.preferredUsername === handle.name && | ||
97 | ( | ||
98 | (!server && !handle.host) || | ||
99 | (server.host === handle.host) | ||
100 | ) | ||
101 | }) | ||
102 | |||
103 | existObject[handle.uri] = obj !== undefined | ||
104 | } | ||
105 | |||
106 | return res.json(existObject) | ||
107 | } | ||
108 | |||
109 | async function addUserSubscription (req: express.Request, res: express.Response) { | ||
110 | const user = res.locals.oauth.token.User as UserModel | ||
111 | const [ name, host ] = req.body.uri.split('@') | ||
112 | |||
113 | const payload = { | ||
114 | name, | ||
115 | host, | ||
116 | followerActorId: user.Account.Actor.id | ||
117 | } | ||
118 | |||
119 | JobQueue.Instance.createJob({ type: 'activitypub-follow', payload }) | ||
120 | .catch(err => logger.error('Cannot create follow job for subscription %s.', req.body.uri, err)) | ||
121 | |||
122 | return res.status(204).end() | ||
123 | } | ||
124 | |||
125 | function getUserSubscription (req: express.Request, res: express.Response) { | ||
126 | const subscription: ActorFollowModel = res.locals.subscription | ||
127 | |||
128 | return res.json(subscription.ActorFollowing.VideoChannel.toFormattedJSON()) | ||
129 | } | ||
130 | |||
131 | async function deleteUserSubscription (req: express.Request, res: express.Response) { | ||
132 | const subscription: ActorFollowModel = res.locals.subscription | ||
133 | |||
134 | await sequelizeTypescript.transaction(async t => { | ||
135 | return subscription.destroy({ transaction: t }) | ||
136 | }) | ||
137 | |||
138 | return res.type('json').status(204).end() | ||
139 | } | ||
140 | |||
141 | async function getUserSubscriptions (req: express.Request, res: express.Response) { | ||
142 | const user = res.locals.oauth.token.User as UserModel | ||
143 | const actorId = user.Account.Actor.id | ||
144 | |||
145 | const resultList = await ActorFollowModel.listSubscriptionsForApi(actorId, req.query.start, req.query.count, req.query.sort) | ||
146 | |||
147 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | ||
148 | } | ||
149 | |||
150 | async function getUserSubscriptionVideos (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
151 | const user = res.locals.oauth.token.User as UserModel | ||
152 | const resultList = await VideoModel.listForApi({ | ||
153 | start: req.query.start, | ||
154 | count: req.query.count, | ||
155 | sort: req.query.sort, | ||
156 | includeLocalVideos: false, | ||
157 | categoryOneOf: req.query.categoryOneOf, | ||
158 | licenceOneOf: req.query.licenceOneOf, | ||
159 | languageOneOf: req.query.languageOneOf, | ||
160 | tagsOneOf: req.query.tagsOneOf, | ||
161 | tagsAllOf: req.query.tagsAllOf, | ||
162 | nsfw: buildNSFWFilter(res, req.query.nsfw), | ||
163 | filter: req.query.filter as VideoFilter, | ||
164 | withFiles: false, | ||
165 | followerActorId: user.Account.Actor.id, | ||
166 | user | ||
167 | }) | ||
168 | |||
169 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | ||
170 | } | ||