aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers')
-rw-r--r--server/controllers/api/accounts.ts38
-rw-r--r--server/controllers/api/index.ts2
-rw-r--r--server/controllers/api/users.ts17
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 @@
1import * as express from 'express'
2import { getFormattedObjects } from '../../helpers/utils'
3import { asyncMiddleware, paginationValidator, setAccountsSort, setPagination } from '../../middlewares'
4import { accountsGetValidator, accountsSortValidator } from '../../middlewares/validators'
5import { AccountModel } from '../../models/account/account'
6
7const accountsRouter = express.Router()
8
9accountsRouter.get('/',
10 paginationValidator,
11 accountsSortValidator,
12 setAccountsSort,
13 setPagination,
14 asyncMiddleware(listAccounts)
15)
16
17accountsRouter.get('/:id',
18 asyncMiddleware(accountsGetValidator),
19 getAccount
20)
21
22// ---------------------------------------------------------------------------
23
24export {
25 accountsRouter
26}
27
28// ---------------------------------------------------------------------------
29
30function getAccount (req: express.Request, res: express.Response, next: express.NextFunction) {
31 return res.json(res.locals.account.toFormattedJSON())
32}
33
34async 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'
5import { oauthClientsRouter } from './oauth-clients' 5import { oauthClientsRouter } from './oauth-clients'
6import { serverRouter } from './server' 6import { serverRouter } from './server'
7import { usersRouter } from './users' 7import { usersRouter } from './users'
8import { accountsRouter } from './accounts'
8import { videosRouter } from './videos' 9import { videosRouter } from './videos'
9 10
10const apiRouter = express.Router() 11const apiRouter = express.Router()
@@ -13,6 +14,7 @@ apiRouter.use('/server', serverRouter)
13apiRouter.use('/oauth-clients', oauthClientsRouter) 14apiRouter.use('/oauth-clients', oauthClientsRouter)
14apiRouter.use('/config', configRouter) 15apiRouter.use('/config', configRouter)
15apiRouter.use('/users', usersRouter) 16apiRouter.use('/users', usersRouter)
17apiRouter.use('/accounts', accountsRouter)
16apiRouter.use('/videos', videosRouter) 18apiRouter.use('/videos', videosRouter)
17apiRouter.use('/jobs', jobsRouter) 19apiRouter.use('/jobs', jobsRouter)
18apiRouter.use('/ping', pong) 20apiRouter.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'
8import { logger } from '../../helpers/logger' 8import { logger } from '../../helpers/logger'
9import { createReqFiles, getFormattedObjects } from '../../helpers/utils' 9import { createReqFiles, getFormattedObjects } from '../../helpers/utils'
10import { AVATAR_MIMETYPE_EXT, AVATARS_SIZE, CONFIG, sequelizeTypescript } from '../../initializers' 10import { AVATAR_MIMETYPE_EXT, AVATARS_SIZE, CONFIG, sequelizeTypescript } from '../../initializers'
11import { sendUpdateUser } from '../../lib/activitypub/send'
11import { createUserAccountAndChannel } from '../../lib/user' 12import { createUserAccountAndChannel } from '../../lib/user'
12import { 13import {
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
217async function updateMe (req: express.Request, res: express.Response, next: express.NextFunction) { 218async 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
233async function updateMyAvatar (req: express.Request, res: express.Response, next: express.NextFunction) { 234async 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