PeerTube/server/models/account/account-follow.ts

229 lines
6.0 KiB
TypeScript
Raw Normal View History

2017-12-12 17:53:50 +01:00
import * as Bluebird from 'bluebird'
2017-11-13 17:39:41 +01:00
import { values } from 'lodash'
2017-11-09 17:51:58 +01:00
import * as Sequelize from 'sequelize'
2017-12-12 17:53:50 +01:00
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
import { FollowState } from '../../../shared/models/accounts'
2017-11-13 17:39:41 +01:00
import { FOLLOW_STATES } from '../../initializers/constants'
2017-12-12 17:53:50 +01:00
import { ServerModel } from '../server/server'
import { getSort } from '../utils'
import { AccountModel } from './account'
2017-11-09 17:51:58 +01:00
2017-12-12 17:53:50 +01:00
@Table({
tableName: 'accountFollow',
indexes: [
2017-11-13 17:39:41 +01:00
{
2017-12-12 17:53:50 +01:00
fields: [ 'accountId' ]
2017-11-13 17:39:41 +01:00
},
2017-11-09 17:51:58 +01:00
{
2017-12-12 17:53:50 +01:00
fields: [ 'targetAccountId' ]
},
{
fields: [ 'accountId', 'targetAccountId' ],
unique: true
2017-11-09 17:51:58 +01:00
}
]
2017-12-12 17:53:50 +01:00
})
export class AccountFollowModel extends Model<AccountFollowModel> {
2017-11-09 17:51:58 +01:00
2017-12-12 17:53:50 +01:00
@AllowNull(false)
@Column(DataType.ENUM(values(FOLLOW_STATES)))
state: FollowState
2017-11-09 17:51:58 +01:00
2017-12-12 17:53:50 +01:00
@CreatedAt
createdAt: Date
2017-11-09 17:51:58 +01:00
2017-12-12 17:53:50 +01:00
@UpdatedAt
updatedAt: Date
@ForeignKey(() => AccountModel)
@Column
accountId: number
@BelongsTo(() => AccountModel, {
2017-11-09 17:51:58 +01:00
foreignKey: {
name: 'accountId',
allowNull: false
},
as: 'AccountFollower',
2017-11-09 17:51:58 +01:00
onDelete: 'CASCADE'
})
2017-12-12 17:53:50 +01:00
AccountFollower: AccountModel
2017-11-09 17:51:58 +01:00
2017-12-12 17:53:50 +01:00
@ForeignKey(() => AccountModel)
@Column
targetAccountId: number
@BelongsTo(() => AccountModel, {
2017-11-09 17:51:58 +01:00
foreignKey: {
name: 'targetAccountId',
allowNull: false
},
as: 'AccountFollowing',
2017-11-09 17:51:58 +01:00
onDelete: 'CASCADE'
})
2017-12-12 17:53:50 +01:00
AccountFollowing: AccountModel
2017-11-13 17:39:41 +01:00
2017-12-12 17:53:50 +01:00
static loadByAccountAndTarget (accountId: number, targetAccountId: number, t?: Sequelize.Transaction) {
const query = {
where: {
accountId,
targetAccountId
2017-11-20 09:43:39 +01:00
},
2017-12-12 17:53:50 +01:00
include: [
{
model: AccountModel,
required: true,
as: 'AccountFollower'
},
{
model: AccountModel,
required: true,
as: 'AccountFollowing'
}
],
transaction: t
}
return AccountFollowModel.findOne(query)
2017-11-13 17:39:41 +01:00
}
2017-12-12 17:53:50 +01:00
static listFollowingForApi (id: number, start: number, count: number, sort: string) {
const query = {
distinct: true,
offset: start,
limit: count,
order: [ getSort(sort) ],
include: [
{
model: AccountModel,
required: true,
as: 'AccountFollower',
where: {
id
}
},
{
model: AccountModel,
as: 'AccountFollowing',
required: true,
include: [ ServerModel ]
}
]
}
2017-12-12 17:53:50 +01:00
return AccountFollowModel.findAndCountAll(query)
.then(({ rows, count }) => {
return {
data: rows,
total: count
}
2017-12-12 17:53:50 +01:00
})
}
2017-12-12 17:53:50 +01:00
static listFollowersForApi (id: number, start: number, count: number, sort: string) {
const query = {
distinct: true,
offset: start,
limit: count,
order: [ getSort(sort) ],
include: [
{
model: AccountModel,
required: true,
as: 'AccountFollower',
include: [ ServerModel ]
},
{
model: AccountModel,
as: 'AccountFollowing',
required: true,
where: {
id
}
}
]
}
2017-12-12 17:53:50 +01:00
return AccountFollowModel.findAndCountAll(query)
.then(({ rows, count }) => {
return {
data: rows,
total: count
}
2017-12-12 17:53:50 +01:00
})
}
2017-12-12 17:53:50 +01:00
static listAcceptedFollowerUrlsForApi (accountIds: number[], t: Sequelize.Transaction, start?: number, count?: number) {
return AccountFollowModel.createListAcceptedFollowForApiQuery('followers', accountIds, t, start, count)
}
2017-12-12 17:53:50 +01:00
static listAcceptedFollowerSharedInboxUrls (accountIds: number[], t: Sequelize.Transaction) {
return AccountFollowModel.createListAcceptedFollowForApiQuery('followers', accountIds, t, undefined, undefined, 'sharedInboxUrl')
}
2017-12-12 17:53:50 +01:00
static listAcceptedFollowingUrlsForApi (accountIds: number[], t: Sequelize.Transaction, start?: number, count?: number) {
return AccountFollowModel.createListAcceptedFollowForApiQuery('following', accountIds, t, start, count)
}
2017-12-12 17:53:50 +01:00
private static async createListAcceptedFollowForApiQuery (type: 'followers' | 'following',
accountIds: number[],
t: Sequelize.Transaction,
start?: number,
count?: number,
columnUrl = 'url') {
let firstJoin: string
let secondJoin: string
if (type === 'followers') {
firstJoin = 'targetAccountId'
secondJoin = 'accountId'
} else {
firstJoin = 'accountId'
secondJoin = 'targetAccountId'
}
2017-12-12 17:53:50 +01:00
const selections = [ '"Follows"."' + columnUrl + '" AS "url"', 'COUNT(*) AS "total"' ]
const tasks: Bluebird<any>[] = []
2017-12-12 17:53:50 +01:00
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 "accountFollow"."' + secondJoin + '" = "Follows"."id" ' +
'WHERE "account"."id" = ANY ($accountIds) AND "accountFollow"."state" = \'accepted\' '
2017-12-12 17:53:50 +01:00
if (count !== undefined) query += 'LIMIT ' + count
if (start !== undefined) query += ' OFFSET ' + start
2017-12-12 17:53:50 +01:00
const options = {
bind: { accountIds },
type: Sequelize.QueryTypes.SELECT,
transaction: t
}
tasks.push(AccountFollowModel.sequelize.query(query, options))
}
2017-12-12 17:53:50 +01:00
const [ followers, [ { total } ] ] = await
Promise.all(tasks)
const urls: string[] = followers.map(f => f.url)
return {
data: urls,
total: parseInt(total, 10)
}
}
2017-12-12 17:53:50 +01:00
toFormattedJSON () {
const follower = this.AccountFollower.toFormattedJSON()
const following = this.AccountFollowing.toFormattedJSON()
2017-12-12 17:53:50 +01:00
return {
id: this.id,
follower,
following,
state: this.state,
createdAt: this.createdAt,
updatedAt: this.updatedAt
}
}
}