1 import { values } from 'lodash'
2 import * as Sequelize from 'sequelize'
4 import { addMethodsToModel, getSort } from '../utils'
5 import { AccountFollowAttributes, AccountFollowInstance, AccountFollowMethods } from './account-follow-interface'
6 import { FOLLOW_STATES } from '../../initializers/constants'
8 let AccountFollow: Sequelize.Model<AccountFollowInstance, AccountFollowAttributes>
9 let loadByAccountAndTarget: AccountFollowMethods.LoadByAccountAndTarget
10 let listFollowingForApi: AccountFollowMethods.ListFollowingForApi
11 let listFollowersForApi: AccountFollowMethods.ListFollowersForApi
12 let listAcceptedFollowerUrlsForApi: AccountFollowMethods.ListAcceptedFollowerUrlsForApi
13 let listAcceptedFollowingUrlsForApi: AccountFollowMethods.ListAcceptedFollowingUrlsForApi
14 let listAcceptedFollowerSharedInboxUrls: AccountFollowMethods.ListAcceptedFollowerSharedInboxUrls
16 export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
17 AccountFollow = sequelize.define<AccountFollowInstance, AccountFollowAttributes>('AccountFollow',
20 type: DataTypes.ENUM(values(FOLLOW_STATES)),
27 fields: [ 'accountId' ]
30 fields: [ 'targetAccountId' ]
33 fields: [ 'accountId', 'targetAccountId' ],
40 const classMethods = [
42 loadByAccountAndTarget,
45 listAcceptedFollowerUrlsForApi,
46 listAcceptedFollowingUrlsForApi,
47 listAcceptedFollowerSharedInboxUrls
49 addMethodsToModel(AccountFollow, classMethods)
54 // ------------------------------ STATICS ------------------------------
56 function associate (models) {
57 AccountFollow.belongsTo(models.Account, {
62 as: 'AccountFollower',
66 AccountFollow.belongsTo(models.Account, {
68 name: 'targetAccountId',
71 as: 'AccountFollowing',
76 loadByAccountAndTarget = function (accountId: number, targetAccountId: number) {
84 model: AccountFollow[ 'sequelize' ].models.Account,
89 model: AccountFollow['sequelize'].models.Account,
91 as: 'AccountFollowing'
96 return AccountFollow.findOne(query)
99 listFollowingForApi = function (id: number, start: number, count: number, sort: string) {
104 order: [ getSort(sort) ],
107 model: AccountFollow[ 'sequelize' ].models.Account,
109 as: 'AccountFollower',
115 model: AccountFollow['sequelize'].models.Account,
116 as: 'AccountFollowing',
118 include: [ AccountFollow['sequelize'].models.Server ]
123 return AccountFollow.findAndCountAll(query).then(({ rows, count }) => {
125 data: rows.map(r => r.AccountFollowing),
131 listFollowersForApi = function (id: number, start: number, count: number, sort: string) {
136 order: [ getSort(sort) ],
139 model: AccountFollow[ 'sequelize' ].models.Account,
141 as: 'AccountFollower',
142 include: [ AccountFollow['sequelize'].models.Server ]
145 model: AccountFollow['sequelize'].models.Account,
146 as: 'AccountFollowing',
155 return AccountFollow.findAndCountAll(query).then(({ rows, count }) => {
157 data: rows.map(r => r.AccountFollower),
163 listAcceptedFollowerUrlsForApi = function (accountIds: number[], start?: number, count?: number) {
164 return createListAcceptedFollowForApiQuery('followers', accountIds, start, count)
167 listAcceptedFollowerSharedInboxUrls = function (accountIds: number[]) {
168 return createListAcceptedFollowForApiQuery('followers', accountIds, undefined, undefined, 'sharedInboxUrl')
171 listAcceptedFollowingUrlsForApi = function (accountIds: number[], start?: number, count?: number) {
172 return createListAcceptedFollowForApiQuery('following', accountIds, start, count)
175 // ------------------------------ UTILS ------------------------------
177 async function createListAcceptedFollowForApiQuery (
178 type: 'followers' | 'following',
179 accountIds: number[],
184 let firstJoin: string
185 let secondJoin: string
187 if (type === 'followers') {
188 firstJoin = 'targetAccountId'
189 secondJoin = 'accountId'
191 firstJoin = 'accountId'
192 secondJoin = 'targetAccountId'
195 const selections = [ '"Follows"."' + columnUrl + '" AS "url"', 'COUNT(*) AS "total"' ]
196 const tasks: Promise<any>[] = []
198 for (const selection of selections) {
199 let query = 'SELECT ' + selection + ' FROM "Accounts" ' +
200 'INNER JOIN "AccountFollows" ON "AccountFollows"."' + firstJoin + '" = "Accounts"."id" ' +
201 'INNER JOIN "Accounts" AS "Follows" ON "AccountFollows"."' + secondJoin + '" = "Follows"."id" ' +
202 'WHERE "Accounts"."id" = ANY ($accountIds) AND "AccountFollows"."state" = \'accepted\' '
204 if (start !== undefined) query += 'LIMIT ' + start
205 if (count !== undefined) query += ', ' + count
208 bind: { accountIds },
209 type: Sequelize.QueryTypes.SELECT
211 tasks.push(AccountFollow['sequelize'].query(query, options))
214 const [ followers, [ { total } ]] = await Promise.all(tasks)
215 const urls: string[] = followers.map(f => f.url)
219 total: parseInt(total, 10)