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

73 lines
1.7 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: [
{
fields: [ 'accountId' ],
unique: true
},
{
fields: [ 'targetAccountId' ],
unique: true
}
]
}
)
const classMethods = [
associate
]
addMethodsToModel(AccountFollow, classMethods)
return AccountFollow
}
// ------------------------------ STATICS ------------------------------
function associate (models) {
AccountFollow.belongsTo(models.Account, {
foreignKey: {
name: 'accountId',
allowNull: false
},
2017-11-13 10:39:41 -06:00
as: 'followers',
2017-11-09 10:51:58 -06:00
onDelete: 'CASCADE'
})
AccountFollow.belongsTo(models.Account, {
foreignKey: {
name: 'targetAccountId',
allowNull: false
},
2017-11-13 10:39:41 -06:00
as: 'following',
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)
}