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-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,
|
|
|
|
listAcceptedFollowingUrlsForApi
|
2017-11-09 10:51:58 -06:00
|
|
|
]
|
|
|
|
addMethodsToModel(AccountFollow, classMethods)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
loadByAccountAndTarget = function (accountId: number, targetAccountId: number) {
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
accountId,
|
|
|
|
targetAccountId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
include: [ AccountFollow['sequelize'].models.Pod ]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
return AccountFollow.findAndCountAll(query).then(({ rows, count }) => {
|
|
|
|
return {
|
|
|
|
data: rows.map(r => r.AccountFollowing),
|
|
|
|
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',
|
|
|
|
include: [ AccountFollow['sequelize'].models.Pod ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
model: AccountFollow['sequelize'].models.Account,
|
|
|
|
as: 'AccountFollowing',
|
|
|
|
required: true,
|
|
|
|
where: {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
return AccountFollow.findAndCountAll(query).then(({ rows, count }) => {
|
|
|
|
return {
|
|
|
|
data: rows.map(r => r.AccountFollower),
|
|
|
|
total: count
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
listAcceptedFollowerUrlsForApi = function (id: number, start: number, count?: number) {
|
|
|
|
return createListAcceptedFollowForApiQuery('followers', id, start, count)
|
|
|
|
}
|
|
|
|
|
|
|
|
listAcceptedFollowingUrlsForApi = function (id: number, start: number, count?: number) {
|
|
|
|
return createListAcceptedFollowForApiQuery('following', id, start, count)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------ UTILS ------------------------------
|
|
|
|
|
|
|
|
async function createListAcceptedFollowForApiQuery (type: 'followers' | 'following', id: number, start: number, count?: number) {
|
|
|
|
let firstJoin: string
|
|
|
|
let secondJoin: string
|
|
|
|
|
|
|
|
if (type === 'followers') {
|
|
|
|
firstJoin = 'targetAccountId'
|
|
|
|
secondJoin = 'accountId'
|
|
|
|
} else {
|
|
|
|
firstJoin = 'accountId'
|
|
|
|
secondJoin = 'targetAccountId'
|
|
|
|
}
|
|
|
|
|
|
|
|
const selections = [ '"Followers"."url" AS "url"', 'COUNT(*) AS "total"' ]
|
|
|
|
const tasks: Promise<any>[] = []
|
|
|
|
|
|
|
|
for (const selection of selections) {
|
|
|
|
let query = 'SELECT ' + selection + ' FROM "Account" ' +
|
|
|
|
'INNER JOIN "AccountFollow" ON "AccountFollow"."' + firstJoin + '" = "Account"."id" ' +
|
|
|
|
'INNER JOIN "Account" AS "Follows" ON "Followers"."id" = "Follows"."' + secondJoin + '" ' +
|
|
|
|
'WHERE "Account"."id" = $id AND "AccountFollow"."state" = \'accepted\' ' +
|
|
|
|
'LIMIT ' + start
|
|
|
|
|
|
|
|
if (count !== undefined) query += ', ' + count
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
bind: { id },
|
|
|
|
type: Sequelize.QueryTypes.SELECT
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|