From 265ba139ebf56bbdc1c65f6ea4f367774c691fc0 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 3 Jan 2018 16:38:50 +0100 Subject: Send account activitypub update events --- server/controllers/api/accounts.ts | 38 ++++++++++++++++++++++++++++++++++++++ server/controllers/api/index.ts | 2 ++ server/controllers/api/users.ts | 17 ++++++++++++++--- 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 server/controllers/api/accounts.ts (limited to 'server/controllers/api') 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 @@ +import * as express from 'express' +import { getFormattedObjects } from '../../helpers/utils' +import { asyncMiddleware, paginationValidator, setAccountsSort, setPagination } from '../../middlewares' +import { accountsGetValidator, accountsSortValidator } from '../../middlewares/validators' +import { AccountModel } from '../../models/account/account' + +const accountsRouter = express.Router() + +accountsRouter.get('/', + paginationValidator, + accountsSortValidator, + setAccountsSort, + setPagination, + asyncMiddleware(listAccounts) +) + +accountsRouter.get('/:id', + asyncMiddleware(accountsGetValidator), + getAccount +) + +// --------------------------------------------------------------------------- + +export { + accountsRouter +} + +// --------------------------------------------------------------------------- + +function getAccount (req: express.Request, res: express.Response, next: express.NextFunction) { + return res.json(res.locals.account.toFormattedJSON()) +} + +async function listAccounts (req: express.Request, res: express.Response, next: express.NextFunction) { + const resultList = await AccountModel.listForApi(req.query.start, req.query.count, req.query.sort) + + return res.json(getFormattedObjects(resultList.data, resultList.total)) +} 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' import { oauthClientsRouter } from './oauth-clients' import { serverRouter } from './server' import { usersRouter } from './users' +import { accountsRouter } from './accounts' import { videosRouter } from './videos' const apiRouter = express.Router() @@ -13,6 +14,7 @@ apiRouter.use('/server', serverRouter) apiRouter.use('/oauth-clients', oauthClientsRouter) apiRouter.use('/config', configRouter) apiRouter.use('/users', usersRouter) +apiRouter.use('/accounts', accountsRouter) apiRouter.use('/videos', videosRouter) apiRouter.use('/jobs', jobsRouter) 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' import { logger } from '../../helpers/logger' import { createReqFiles, getFormattedObjects } from '../../helpers/utils' import { AVATAR_MIMETYPE_EXT, AVATARS_SIZE, CONFIG, sequelizeTypescript } from '../../initializers' +import { sendUpdateUser } from '../../lib/activitypub/send' import { createUserAccountAndChannel } from '../../lib/user' import { asyncMiddleware, authenticate, ensureUserHasRight, ensureUserRegistrationAllowed, paginationValidator, setPagination, setUsersSort, @@ -217,7 +218,6 @@ async function removeUser (req: express.Request, res: express.Response, next: ex async function updateMe (req: express.Request, res: express.Response, next: express.NextFunction) { const body: UserUpdateMe = req.body - // FIXME: user is not already a Sequelize instance? const user = res.locals.oauth.token.user if (body.password !== undefined) user.password = body.password @@ -226,13 +226,15 @@ async function updateMe (req: express.Request, res: express.Response, next: expr if (body.autoPlayVideo !== undefined) user.autoPlayVideo = body.autoPlayVideo await user.save() + await sendUpdateUser(user, undefined) return res.sendStatus(204) } async function updateMyAvatar (req: express.Request, res: express.Response, next: express.NextFunction) { const avatarPhysicalFile = req.files['avatarfile'][0] - const actor = res.locals.oauth.token.user.Account.Actor + const user = res.locals.oauth.token.user + const actor = user.Account.Actor const avatarDir = CONFIG.STORAGE.AVATARS_DIR const source = join(avatarDir, avatarPhysicalFile.filename) @@ -252,12 +254,19 @@ async function updateMyAvatar (req: express.Request, res: express.Response, next }, { transaction: t }) if (actor.Avatar) { - await actor.Avatar.destroy({ transaction: t }) + try { + await actor.Avatar.destroy({ transaction: t }) + } catch (err) { + logger.error('Cannot remove old avatar of user %s.', user.username, err) + } } actor.set('avatarId', avatar.id) + actor.Avatar = avatar await actor.save({ transaction: t }) + await sendUpdateUser(user, undefined) + return { actor, avatar } }) @@ -278,6 +287,8 @@ async function updateUser (req: express.Request, res: express.Response, next: ex await user.save() + // Don't need to send this update to followers, these attributes are not propagated + return res.sendStatus(204) } -- cgit v1.2.3