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