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

76 lines
1.8 KiB
TypeScript
Raw Normal View History

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'
import { addMethodsToModel } 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-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,
loadByAccountAndTarget
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-14 10:31:26 -06:00
as: 'accountFollowers',
2017-11-09 10:51:58 -06:00
onDelete: 'CASCADE'
})
AccountFollow.belongsTo(models.Account, {
foreignKey: {
name: 'targetAccountId',
allowNull: false
},
2017-11-14 10:31:26 -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)
}