2017-11-13 10:39:41 -06:00
|
|
|
import { values } from 'lodash'
|
2017-11-09 10:51:58 -06:00
|
|
|
import * as Sequelize from 'sequelize'
|
|
|
|
|
2017-11-15 03:10:41 -06:00
|
|
|
import { addMethodsToModel, getSort } from '../utils'
|
2017-11-13 10:39:41 -06:00
|
|
|
import { AccountFollowAttributes, AccountFollowInstance, AccountFollowMethods } from './account-follow-interface'
|
|
|
|
import { FOLLOW_STATES } from '../../initializers/constants'
|
2017-11-09 10:51:58 -06:00
|
|
|
|
|
|
|
let AccountFollow: Sequelize.Model<AccountFollowInstance, AccountFollowAttributes>
|
2017-11-13 10:39:41 -06:00
|
|
|
let loadByAccountAndTarget: AccountFollowMethods.LoadByAccountAndTarget
|
2017-11-15 03:10:41 -06:00
|
|
|
let listFollowingForApi: AccountFollowMethods.ListFollowingForApi
|
|
|
|
let listFollowersForApi: AccountFollowMethods.ListFollowersForApi
|
|
|
|
let listAcceptedFollowerUrlsForApi: AccountFollowMethods.ListAcceptedFollowerUrlsForApi
|
|
|
|
let listAcceptedFollowingUrlsForApi: AccountFollowMethods.ListAcceptedFollowingUrlsForApi
|
2017-11-16 04:08:25 -06:00
|
|
|
let listAcceptedFollowerSharedInboxUrls: AccountFollowMethods.ListAcceptedFollowerSharedInboxUrls
|
2017-11-20 04:19:23 -06:00
|
|
|
let toFormattedJSON: AccountFollowMethods.ToFormattedJSON
|
2017-11-09 10:51:58 -06:00
|
|
|
|
|
|
|
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
|
|
|
|
AccountFollow = sequelize.define<AccountFollowInstance, AccountFollowAttributes>('AccountFollow',
|
2017-11-13 10:39:41 -06:00
|
|
|
{
|
|
|
|
state: {
|
|
|
|
type: DataTypes.ENUM(values(FOLLOW_STATES)),
|
|
|
|
allowNull: false
|
|
|
|
}
|
|
|
|
},
|
2017-11-09 10:51:58 -06:00
|
|
|
{
|
|
|
|
indexes: [
|
|
|
|
{
|
2017-11-14 10:31:26 -06:00
|
|
|
fields: [ 'accountId' ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'targetAccountId' ]
|
2017-11-09 10:51:58 -06:00
|
|
|
},
|
|
|
|
{
|
2017-11-14 10:31:26 -06:00
|
|
|
fields: [ 'accountId', 'targetAccountId' ],
|
2017-11-09 10:51:58 -06:00
|
|
|
unique: true
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const classMethods = [
|
2017-11-14 10:31:26 -06:00
|
|
|
associate,
|
2017-11-15 03:10:41 -06:00
|
|
|
loadByAccountAndTarget,
|
|
|
|
listFollowingForApi,
|
|
|
|
listFollowersForApi,
|
|
|
|
listAcceptedFollowerUrlsForApi,
|
2017-11-16 04:08:25 -06:00
|
|
|
listAcceptedFollowingUrlsForApi,
|
|
|
|
listAcceptedFollowerSharedInboxUrls
|
2017-11-09 10:51:58 -06:00
|
|
|
]
|
2017-11-20 04:19:23 -06:00
|
|
|
const instanceMethods = [
|
|
|
|
toFormattedJSON
|
|
|
|
]
|
|
|
|
addMethodsToModel(AccountFollow, classMethods, instanceMethods)
|
2017-11-09 10:51:58 -06:00
|
|
|
|
|
|
|
return AccountFollow
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------ STATICS ------------------------------
|
|
|
|
|
|
|
|
function associate (models) {
|
|
|
|
AccountFollow.belongsTo(models.Account, {
|
|
|
|
foreignKey: {
|
|
|
|
name: 'accountId',
|
|
|
|
allowNull: false
|
|
|
|
},
|
2017-11-15 03:10:41 -06:00
|
|
|
as: 'AccountFollower',
|
2017-11-09 10:51:58 -06:00
|
|
|
onDelete: 'CASCADE'
|
|
|
|
})
|
|
|
|
|
|
|
|
AccountFollow.belongsTo(models.Account, {
|
|
|
|
foreignKey: {
|
|
|
|
name: 'targetAccountId',
|
|
|
|
allowNull: false
|
|
|
|
},
|
2017-11-15 03:10:41 -06:00
|
|
|
as: 'AccountFollowing',
|
2017-11-09 10:51:58 -06:00
|
|
|
onDelete: 'CASCADE'
|
|
|
|
})
|
|
|
|
}
|
2017-11-13 10:39:41 -06:00
|
|
|
|
2017-11-20 04:19:23 -06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-11-23 07:19:55 -06:00
|
|
|
loadByAccountAndTarget = function (accountId: number, targetAccountId: number, t?: Sequelize.Transaction) {
|
2017-11-13 10:39:41 -06:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
accountId,
|
|
|
|
targetAccountId
|
2017-11-20 02:43:39 -06:00
|
|
|
},
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: AccountFollow[ 'sequelize' ].models.Account,
|
|
|
|
required: true,
|
|
|
|
as: 'AccountFollower'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
model: AccountFollow['sequelize'].models.Account,
|
|
|
|
required: true,
|
|
|
|
as: 'AccountFollowing'
|
|
|
|
}
|
2017-11-23 07:19:55 -06:00
|
|
|
],
|
|
|
|
transaction: t
|
2017-11-13 10:39:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return AccountFollow.findOne(query)
|
|
|
|
}
|
2017-11-15 03:10:41 -06:00
|
|
|
|
|
|
|
listFollowingForApi = function (id: number, start: number, count: number, sort: string) {
|
|
|
|
const query = {
|
|
|
|
distinct: true,
|
|
|
|
offset: start,
|
|
|
|
limit: count,
|
|
|
|
order: [ getSort(sort) ],
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: AccountFollow[ 'sequelize' ].models.Account,
|
|
|
|
required: true,
|
|
|
|
as: 'AccountFollower',
|
|
|
|
where: {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
model: AccountFollow['sequelize'].models.Account,
|
|
|
|
as: 'AccountFollowing',
|
|
|
|
required: true,
|
2017-11-15 04:00:25 -06:00
|
|
|
include: [ AccountFollow['sequelize'].models.Server ]
|
2017-11-15 03:10:41 -06:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
return AccountFollow.findAndCountAll(query).then(({ rows, count }) => {
|
|
|
|
return {
|
2017-11-20 04:19:23 -06:00
|
|
|
data: rows,
|
2017-11-15 03:10:41 -06:00
|
|
|
total: count
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
listFollowersForApi = function (id: number, start: number, count: number, sort: string) {
|
|
|
|
const query = {
|
|
|
|
distinct: true,
|
|
|
|
offset: start,
|
|
|
|
limit: count,
|
|
|
|
order: [ getSort(sort) ],
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: AccountFollow[ 'sequelize' ].models.Account,
|
|
|
|
required: true,
|
|
|
|
as: 'AccountFollower',
|
2017-11-15 04:00:25 -06:00
|
|
|
include: [ AccountFollow['sequelize'].models.Server ]
|
2017-11-15 03:10:41 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
model: AccountFollow['sequelize'].models.Account,
|
|
|
|
as: 'AccountFollowing',
|
|
|
|
required: true,
|
|
|
|
where: {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
return AccountFollow.findAndCountAll(query).then(({ rows, count }) => {
|
|
|
|
return {
|
2017-11-20 04:19:23 -06:00
|
|
|
data: rows,
|
2017-11-15 03:10:41 -06:00
|
|
|
total: count
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-11-30 04:31:15 -06:00
|
|
|
listAcceptedFollowerUrlsForApi = function (accountIds: number[], t: Sequelize.Transaction, start?: number, count?: number) {
|
|
|
|
return createListAcceptedFollowForApiQuery('followers', accountIds, t, start, count)
|
2017-11-15 03:10:41 -06:00
|
|
|
}
|
|
|
|
|
2017-11-30 04:31:15 -06:00
|
|
|
listAcceptedFollowerSharedInboxUrls = function (accountIds: number[], t: Sequelize.Transaction) {
|
|
|
|
return createListAcceptedFollowForApiQuery('followers', accountIds, t, undefined, undefined, 'sharedInboxUrl')
|
2017-11-16 04:08:25 -06:00
|
|
|
}
|
|
|
|
|
2017-11-30 04:31:15 -06:00
|
|
|
listAcceptedFollowingUrlsForApi = function (accountIds: number[], t: Sequelize.Transaction, start?: number, count?: number) {
|
|
|
|
return createListAcceptedFollowForApiQuery('following', accountIds, t, start, count)
|
2017-11-15 03:10:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------ UTILS ------------------------------
|
|
|
|
|
2017-11-16 04:08:25 -06:00
|
|
|
async function createListAcceptedFollowForApiQuery (
|
|
|
|
type: 'followers' | 'following',
|
|
|
|
accountIds: number[],
|
2017-11-30 04:31:15 -06:00
|
|
|
t: Sequelize.Transaction,
|
2017-11-16 04:08:25 -06:00
|
|
|
start?: number,
|
|
|
|
count?: number,
|
|
|
|
columnUrl = 'url'
|
|
|
|
) {
|
2017-11-15 03:10:41 -06:00
|
|
|
let firstJoin: string
|
|
|
|
let secondJoin: string
|
|
|
|
|
|
|
|
if (type === 'followers') {
|
|
|
|
firstJoin = 'targetAccountId'
|
|
|
|
secondJoin = 'accountId'
|
|
|
|
} else {
|
|
|
|
firstJoin = 'accountId'
|
|
|
|
secondJoin = 'targetAccountId'
|
|
|
|
}
|
|
|
|
|
2017-11-16 04:08:25 -06:00
|
|
|
const selections = [ '"Follows"."' + columnUrl + '" AS "url"', 'COUNT(*) AS "total"' ]
|
2017-11-15 03:10:41 -06:00
|
|
|
const tasks: Promise<any>[] = []
|
|
|
|
|
|
|
|
for (const selection of selections) {
|
2017-11-15 09:28:35 -06:00
|
|
|
let query = 'SELECT ' + selection + ' FROM "Accounts" ' +
|
|
|
|
'INNER JOIN "AccountFollows" ON "AccountFollows"."' + firstJoin + '" = "Accounts"."id" ' +
|
|
|
|
'INNER JOIN "Accounts" AS "Follows" ON "AccountFollows"."' + secondJoin + '" = "Follows"."id" ' +
|
2017-11-16 08:55:01 -06:00
|
|
|
'WHERE "Accounts"."id" = ANY ($accountIds) AND "AccountFollows"."state" = \'accepted\' '
|
2017-11-15 03:10:41 -06:00
|
|
|
|
2017-11-21 11:23:10 -06:00
|
|
|
if (count !== undefined) query += 'LIMIT ' + count
|
|
|
|
if (start !== undefined) query += ' OFFSET ' + start
|
2017-11-15 03:10:41 -06:00
|
|
|
|
|
|
|
const options = {
|
2017-11-16 08:55:01 -06:00
|
|
|
bind: { accountIds },
|
2017-11-30 04:31:15 -06:00
|
|
|
type: Sequelize.QueryTypes.SELECT,
|
|
|
|
transaction: t
|
2017-11-15 03:10:41 -06:00
|
|
|
}
|
|
|
|
tasks.push(AccountFollow['sequelize'].query(query, options))
|
|
|
|
}
|
|
|
|
|
|
|
|
const [ followers, [ { total } ]] = await Promise.all(tasks)
|
|
|
|
const urls: string[] = followers.map(f => f.url)
|
|
|
|
|
|
|
|
return {
|
|
|
|
data: urls,
|
|
|
|
total: parseInt(total, 10)
|
|
|
|
}
|
|
|
|
}
|