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/models/account/account-follow-interface.ts | 11 ++++++---- server/models/account/account-follow.ts | 26 ++++++++++++++++++++--- 2 files changed, 30 insertions(+), 7 deletions(-) (limited to 'server/models/account') 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