]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/account/account-follow.ts
Follow works
[github/Chocobozzz/PeerTube.git] / server / models / account / account-follow.ts
1 import { values } from 'lodash'
2 import * as Sequelize from 'sequelize'
3
4 import { addMethodsToModel } from '../utils'
5 import { AccountFollowAttributes, AccountFollowInstance, AccountFollowMethods } from './account-follow-interface'
6 import { FOLLOW_STATES } from '../../initializers/constants'
7
8 let AccountFollow: Sequelize.Model<AccountFollowInstance, AccountFollowAttributes>
9 let loadByAccountAndTarget: AccountFollowMethods.LoadByAccountAndTarget
10
11 export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
12 AccountFollow = sequelize.define<AccountFollowInstance, AccountFollowAttributes>('AccountFollow',
13 {
14 state: {
15 type: DataTypes.ENUM(values(FOLLOW_STATES)),
16 allowNull: false
17 }
18 },
19 {
20 indexes: [
21 {
22 fields: [ 'accountId' ]
23 },
24 {
25 fields: [ 'targetAccountId' ]
26 },
27 {
28 fields: [ 'accountId', 'targetAccountId' ],
29 unique: true
30 }
31 ]
32 }
33 )
34
35 const classMethods = [
36 associate,
37 loadByAccountAndTarget
38 ]
39 addMethodsToModel(AccountFollow, classMethods)
40
41 return AccountFollow
42 }
43
44 // ------------------------------ STATICS ------------------------------
45
46 function associate (models) {
47 AccountFollow.belongsTo(models.Account, {
48 foreignKey: {
49 name: 'accountId',
50 allowNull: false
51 },
52 as: 'accountFollowers',
53 onDelete: 'CASCADE'
54 })
55
56 AccountFollow.belongsTo(models.Account, {
57 foreignKey: {
58 name: 'targetAccountId',
59 allowNull: false
60 },
61 as: 'accountFollowing',
62 onDelete: 'CASCADE'
63 })
64 }
65
66 loadByAccountAndTarget = function (accountId: number, targetAccountId: number) {
67 const query = {
68 where: {
69 accountId,
70 targetAccountId
71 }
72 }
73
74 return AccountFollow.findOne(query)
75 }