From 7e9334c34db23e5ad1e118151b24c720dd985984 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 20 Nov 2017 11:19:23 +0100 Subject: Add ability to unfollow a server --- server/initializers/constants.ts | 2 +- server/lib/activitypub/process/process-undo.ts | 2 +- server/middlewares/validators/follows.ts | 6 +++--- server/models/account/account-follow-interface.ts | 11 ++++++---- server/models/account/account-follow.ts | 26 ++++++++++++++++++++--- 5 files changed, 35 insertions(+), 12 deletions(-) (limited to 'server') diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index 9c7c31a61..c46043931 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -134,7 +134,7 @@ const CONSTRAINTS_FIELDS = { VIEWS: { min: 0 }, LIKES: { min: 0 }, DISLIKES: { min: 0 }, - FILE_SIZE: { min: 10, max: 1024 * 1024 * 1024 * 3 /* 3Go */ }, + FILE_SIZE: { min: 10, max: 1024 * 1024 * 1024 * 10 /* 10Go */ }, URL: { min: 3, max: 2000 } // Length }, ACCOUNTS: { diff --git a/server/lib/activitypub/process/process-undo.ts b/server/lib/activitypub/process/process-undo.ts index 5d09423e1..610b800fb 100644 --- a/server/lib/activitypub/process/process-undo.ts +++ b/server/lib/activitypub/process/process-undo.ts @@ -10,7 +10,7 @@ async function processUndoActivity (activity: ActivityUndo) { const following = await db.Account.loadByUrl(activityToUndo.object) const accountFollow = await db.AccountFollow.loadByAccountAndTarget(follower.id, following.id) - if (!accountFollow) throw new Error(`'Unknown account follow (${follower.id} -> ${following.id}.`) + if (!accountFollow) throw new Error(`'Unknown account follow ${follower.id} -> ${following.id}.`) await accountFollow.destroy() diff --git a/server/middlewares/validators/follows.ts b/server/middlewares/validators/follows.ts index e22349726..dfd6e7f03 100644 --- a/server/middlewares/validators/follows.ts +++ b/server/middlewares/validators/follows.ts @@ -1,12 +1,12 @@ import * as express from 'express' -import { body } from 'express-validator/check' +import { body, param } from 'express-validator/check' import { isTestInstance } from '../../helpers/core-utils' -import { isAccountIdValid } from '../../helpers/custom-validators/activitypub/account' import { isEachUniqueHostValid } from '../../helpers/custom-validators/servers' import { logger } from '../../helpers/logger' import { CONFIG, database as db } from '../../initializers' import { checkErrors } from './utils' import { getServerAccount } from '../../helpers/utils' +import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' const followValidator = [ body('hosts').custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'), @@ -28,7 +28,7 @@ const followValidator = [ ] const removeFollowingValidator = [ - body('accountId').custom(isAccountIdValid).withMessage('Should have a valid account id'), + param('accountId').custom(isIdOrUUIDValid).withMessage('Should have a valid account id'), (req: express.Request, res: express.Response, next: express.NextFunction) => { logger.debug('Checking follow parameters', { parameters: req.body }) diff --git a/server/models/account/account-follow-interface.ts b/server/models/account/account-follow-interface.ts index 21fda98ce..6f228c790 100644 --- a/server/models/account/account-follow-interface.ts +++ b/server/models/account/account-follow-interface.ts @@ -1,18 +1,19 @@ -import * as Sequelize from 'sequelize' import * as Bluebird from 'bluebird' -import { FollowState } from '../../../shared/models/accounts/follow.model' +import * as Sequelize from 'sequelize' +import { AccountFollow, FollowState } from '../../../shared/models/accounts/follow.model' import { ResultList } from '../../../shared/models/result-list.model' import { AccountInstance } from './account-interface' export namespace AccountFollowMethods { export type LoadByAccountAndTarget = (accountId: number, targetAccountId: number) => Bluebird - export type ListFollowingForApi = (id: number, start: number, count: number, sort: string) => Bluebird< ResultList > - export type ListFollowersForApi = (id: number, start: number, count: number, sort: string) => Bluebird< ResultList > + export type ListFollowingForApi = (id: number, start: number, count: number, sort: string) => Bluebird< ResultList> + export type ListFollowersForApi = (id: number, start: number, count: number, sort: string) => Bluebird< ResultList> export type ListAcceptedFollowerUrlsForApi = (accountId: number[], start?: number, count?: number) => Promise< ResultList > export type ListAcceptedFollowingUrlsForApi = (accountId: number[], start?: number, count?: number) => Promise< ResultList > export type ListAcceptedFollowerSharedInboxUrls = (accountId: number[]) => Promise< ResultList > + export type ToFormattedJSON = (this: AccountFollowInstance) => AccountFollow } export interface AccountFollowClass { @@ -38,6 +39,8 @@ export interface AccountFollowInstance extends AccountFollowClass, AccountFollow AccountFollower?: AccountInstance AccountFollowing?: AccountInstance + + toFormattedJSON: AccountFollowMethods.ToFormattedJSON } export interface AccountFollowModel extends AccountFollowClass, Sequelize.Model {} diff --git a/server/models/account/account-follow.ts b/server/models/account/account-follow.ts index f00c7dcd9..34ba3f8db 100644 --- a/server/models/account/account-follow.ts +++ b/server/models/account/account-follow.ts @@ -12,6 +12,7 @@ let listFollowersForApi: AccountFollowMethods.ListFollowersForApi let listAcceptedFollowerUrlsForApi: AccountFollowMethods.ListAcceptedFollowerUrlsForApi let listAcceptedFollowingUrlsForApi: AccountFollowMethods.ListAcceptedFollowingUrlsForApi let listAcceptedFollowerSharedInboxUrls: AccountFollowMethods.ListAcceptedFollowerSharedInboxUrls +let toFormattedJSON: AccountFollowMethods.ToFormattedJSON export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { AccountFollow = sequelize.define('AccountFollow', @@ -46,7 +47,10 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da listAcceptedFollowingUrlsForApi, listAcceptedFollowerSharedInboxUrls ] - addMethodsToModel(AccountFollow, classMethods) + const instanceMethods = [ + toFormattedJSON + ] + addMethodsToModel(AccountFollow, classMethods, instanceMethods) return AccountFollow } @@ -73,6 +77,22 @@ function associate (models) { }) } +toFormattedJSON = function (this: AccountFollowInstance) { + const follower = this.AccountFollower.toFormattedJSON() + const following = this.AccountFollowing.toFormattedJSON() + + const json = { + id: this.id, + follower, + following, + state: this.state, + createdAt: this.createdAt, + updatedAt: this.updatedAt + } + + return json +} + loadByAccountAndTarget = function (accountId: number, targetAccountId: number) { const query = { where: { @@ -122,7 +142,7 @@ listFollowingForApi = function (id: number, start: number, count: number, sort: return AccountFollow.findAndCountAll(query).then(({ rows, count }) => { return { - data: rows.map(r => r.AccountFollowing), + data: rows, total: count } }) @@ -154,7 +174,7 @@ listFollowersForApi = function (id: number, start: number, count: number, sort: return AccountFollow.findAndCountAll(query).then(({ rows, count }) => { return { - data: rows.map(r => r.AccountFollower), + data: rows, total: count } }) -- cgit v1.2.3