diff options
Diffstat (limited to 'server/controllers')
-rw-r--r-- | server/controllers/api/accounts.ts | 38 | ||||
-rw-r--r-- | server/controllers/api/index.ts | 2 | ||||
-rw-r--r-- | server/controllers/api/users.ts | 17 |
3 files changed, 54 insertions, 3 deletions
diff --git a/server/controllers/api/accounts.ts b/server/controllers/api/accounts.ts new file mode 100644 index 000000000..aded581a5 --- /dev/null +++ b/server/controllers/api/accounts.ts | |||
@@ -0,0 +1,38 @@ | |||
1 | import * as express from 'express' | ||
2 | import { getFormattedObjects } from '../../helpers/utils' | ||
3 | import { asyncMiddleware, paginationValidator, setAccountsSort, setPagination } from '../../middlewares' | ||
4 | import { accountsGetValidator, accountsSortValidator } from '../../middlewares/validators' | ||
5 | import { AccountModel } from '../../models/account/account' | ||
6 | |||
7 | const accountsRouter = express.Router() | ||
8 | |||
9 | accountsRouter.get('/', | ||
10 | paginationValidator, | ||
11 | accountsSortValidator, | ||
12 | setAccountsSort, | ||
13 | setPagination, | ||
14 | asyncMiddleware(listAccounts) | ||
15 | ) | ||
16 | |||
17 | accountsRouter.get('/:id', | ||
18 | asyncMiddleware(accountsGetValidator), | ||
19 | getAccount | ||
20 | ) | ||
21 | |||
22 | // --------------------------------------------------------------------------- | ||
23 | |||
24 | export { | ||
25 | accountsRouter | ||
26 | } | ||
27 | |||
28 | // --------------------------------------------------------------------------- | ||
29 | |||
30 | function getAccount (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
31 | return res.json(res.locals.account.toFormattedJSON()) | ||
32 | } | ||
33 | |||
34 | async function listAccounts (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
35 | const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort) | ||
36 | |||
37 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | ||
38 | } | ||
diff --git a/server/controllers/api/index.ts b/server/controllers/api/index.ts index 1fd44ac11..3b499f3b7 100644 --- a/server/controllers/api/index.ts +++ b/server/controllers/api/index.ts | |||
@@ -5,6 +5,7 @@ import { jobsRouter } from './jobs' | |||
5 | import { oauthClientsRouter } from './oauth-clients' | 5 | import { oauthClientsRouter } from './oauth-clients' |
6 | import { serverRouter } from './server' | 6 | import { serverRouter } from './server' |
7 | import { usersRouter } from './users' | 7 | import { usersRouter } from './users' |
8 | import { accountsRouter } from './accounts' | ||
8 | import { videosRouter } from './videos' | 9 | import { videosRouter } from './videos' |
9 | 10 | ||
10 | const apiRouter = express.Router() | 11 | const apiRouter = express.Router() |
@@ -13,6 +14,7 @@ apiRouter.use('/server', serverRouter) | |||
13 | apiRouter.use('/oauth-clients', oauthClientsRouter) | 14 | apiRouter.use('/oauth-clients', oauthClientsRouter) |
14 | apiRouter.use('/config', configRouter) | 15 | apiRouter.use('/config', configRouter) |
15 | apiRouter.use('/users', usersRouter) | 16 | apiRouter.use('/users', usersRouter) |
17 | apiRouter.use('/accounts', accountsRouter) | ||
16 | apiRouter.use('/videos', videosRouter) | 18 | apiRouter.use('/videos', videosRouter) |
17 | apiRouter.use('/jobs', jobsRouter) | 19 | apiRouter.use('/jobs', jobsRouter) |
18 | apiRouter.use('/ping', pong) | 20 | apiRouter.use('/ping', pong) |
diff --git a/server/controllers/api/users.ts b/server/controllers/api/users.ts index d37813595..ef2b63f51 100644 --- a/server/controllers/api/users.ts +++ b/server/controllers/api/users.ts | |||
@@ -8,6 +8,7 @@ import { retryTransactionWrapper } from '../../helpers/database-utils' | |||
8 | import { logger } from '../../helpers/logger' | 8 | import { logger } from '../../helpers/logger' |
9 | import { createReqFiles, getFormattedObjects } from '../../helpers/utils' | 9 | import { createReqFiles, getFormattedObjects } from '../../helpers/utils' |
10 | import { AVATAR_MIMETYPE_EXT, AVATARS_SIZE, CONFIG, sequelizeTypescript } from '../../initializers' | 10 | import { AVATAR_MIMETYPE_EXT, AVATARS_SIZE, CONFIG, sequelizeTypescript } from '../../initializers' |
11 | import { sendUpdateUser } from '../../lib/activitypub/send' | ||
11 | import { createUserAccountAndChannel } from '../../lib/user' | 12 | import { createUserAccountAndChannel } from '../../lib/user' |
12 | import { | 13 | import { |
13 | asyncMiddleware, authenticate, ensureUserHasRight, ensureUserRegistrationAllowed, paginationValidator, setPagination, setUsersSort, | 14 | asyncMiddleware, authenticate, ensureUserHasRight, ensureUserRegistrationAllowed, paginationValidator, setPagination, setUsersSort, |
@@ -217,7 +218,6 @@ async function removeUser (req: express.Request, res: express.Response, next: ex | |||
217 | async function updateMe (req: express.Request, res: express.Response, next: express.NextFunction) { | 218 | async function updateMe (req: express.Request, res: express.Response, next: express.NextFunction) { |
218 | const body: UserUpdateMe = req.body | 219 | const body: UserUpdateMe = req.body |
219 | 220 | ||
220 | // FIXME: user is not already a Sequelize instance? | ||
221 | const user = res.locals.oauth.token.user | 221 | const user = res.locals.oauth.token.user |
222 | 222 | ||
223 | if (body.password !== undefined) user.password = body.password | 223 | if (body.password !== undefined) user.password = body.password |
@@ -226,13 +226,15 @@ async function updateMe (req: express.Request, res: express.Response, next: expr | |||
226 | if (body.autoPlayVideo !== undefined) user.autoPlayVideo = body.autoPlayVideo | 226 | if (body.autoPlayVideo !== undefined) user.autoPlayVideo = body.autoPlayVideo |
227 | 227 | ||
228 | await user.save() | 228 | await user.save() |
229 | await sendUpdateUser(user, undefined) | ||
229 | 230 | ||
230 | return res.sendStatus(204) | 231 | return res.sendStatus(204) |
231 | } | 232 | } |
232 | 233 | ||
233 | async function updateMyAvatar (req: express.Request, res: express.Response, next: express.NextFunction) { | 234 | async function updateMyAvatar (req: express.Request, res: express.Response, next: express.NextFunction) { |
234 | const avatarPhysicalFile = req.files['avatarfile'][0] | 235 | const avatarPhysicalFile = req.files['avatarfile'][0] |
235 | const actor = res.locals.oauth.token.user.Account.Actor | 236 | const user = res.locals.oauth.token.user |
237 | const actor = user.Account.Actor | ||
236 | 238 | ||
237 | const avatarDir = CONFIG.STORAGE.AVATARS_DIR | 239 | const avatarDir = CONFIG.STORAGE.AVATARS_DIR |
238 | const source = join(avatarDir, avatarPhysicalFile.filename) | 240 | const source = join(avatarDir, avatarPhysicalFile.filename) |
@@ -252,12 +254,19 @@ async function updateMyAvatar (req: express.Request, res: express.Response, next | |||
252 | }, { transaction: t }) | 254 | }, { transaction: t }) |
253 | 255 | ||
254 | if (actor.Avatar) { | 256 | if (actor.Avatar) { |
255 | await actor.Avatar.destroy({ transaction: t }) | 257 | try { |
258 | await actor.Avatar.destroy({ transaction: t }) | ||
259 | } catch (err) { | ||
260 | logger.error('Cannot remove old avatar of user %s.', user.username, err) | ||
261 | } | ||
256 | } | 262 | } |
257 | 263 | ||
258 | actor.set('avatarId', avatar.id) | 264 | actor.set('avatarId', avatar.id) |
265 | actor.Avatar = avatar | ||
259 | await actor.save({ transaction: t }) | 266 | await actor.save({ transaction: t }) |
260 | 267 | ||
268 | await sendUpdateUser(user, undefined) | ||
269 | |||
261 | return { actor, avatar } | 270 | return { actor, avatar } |
262 | }) | 271 | }) |
263 | 272 | ||
@@ -278,6 +287,8 @@ async function updateUser (req: express.Request, res: express.Response, next: ex | |||
278 | 287 | ||
279 | await user.save() | 288 | await user.save() |
280 | 289 | ||
290 | // Don't need to send this update to followers, these attributes are not propagated | ||
291 | |||
281 | return res.sendStatus(204) | 292 | return res.sendStatus(204) |
282 | } | 293 | } |
283 | 294 | ||