aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/author.ts
blob: 43f84c3eada0a8c70d929af0c8efa3873647f504 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import * as Sequelize from 'sequelize'

import { isUserUsernameValid } from '../../helpers'
import { removeVideoAuthorToFriends } from '../../lib'

import { addMethodsToModel } from '../utils'
import {
  AuthorInstance,
  AuthorAttributes,

  AuthorMethods
} from './author-interface'

let Author: Sequelize.Model<AuthorInstance, AuthorAttributes>
let loadAuthorByPodAndUUID: AuthorMethods.LoadAuthorByPodAndUUID
let load: AuthorMethods.Load
let loadByUUID: AuthorMethods.LoadByUUID
let listOwned: AuthorMethods.ListOwned
let isOwned: AuthorMethods.IsOwned
let toAddRemoteJSON: AuthorMethods.ToAddRemoteJSON

export default function defineAuthor (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
  Author = sequelize.define<AuthorInstance, AuthorAttributes>('Author',
    {
      uuid: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        allowNull: false,
        validate: {
          isUUID: 4
        }
      },
      name: {
        type: DataTypes.STRING,
        allowNull: false,
        validate: {
          usernameValid: value => {
            const res = isUserUsernameValid(value)
            if (res === false) throw new Error('Username is not valid.')
          }
        }
      }
    },
    {
      indexes: [
        {
          fields: [ 'name' ]
        },
        {
          fields: [ 'podId' ]
        },
        {
          fields: [ 'userId' ],
          unique: true
        },
        {
          fields: [ 'name', 'podId' ],
          unique: true
        }
      ],
      hooks: { afterDestroy }
    }
  )

  const classMethods = [
    associate,
    loadAuthorByPodAndUUID,
    load,
    loadByUUID,
    listOwned
  ]
  const instanceMethods = [
    isOwned,
    toAddRemoteJSON
  ]
  addMethodsToModel(Author, classMethods, instanceMethods)

  return Author
}

// ---------------------------------------------------------------------------

function associate (models) {
  Author.belongsTo(models.Pod, {
    foreignKey: {
      name: 'podId',
      allowNull: true
    },
    onDelete: 'cascade'
  })

  Author.belongsTo(models.User, {
    foreignKey: {
      name: 'userId',
      allowNull: true
    },
    onDelete: 'cascade'
  })

  Author.hasMany(models.VideoChannel, {
    foreignKey: {
      name: 'authorId',
      allowNull: false
    },
    onDelete: 'cascade',
    hooks: true
  })
}

function afterDestroy (author: AuthorInstance) {
  if (author.isOwned()) {
    const removeVideoAuthorToFriendsParams = {
      uuid: author.uuid
    }

    return removeVideoAuthorToFriends(removeVideoAuthorToFriendsParams)
  }

  return undefined
}

toAddRemoteJSON = function (this: AuthorInstance) {
  const json = {
    uuid: this.uuid,
    name: this.name
  }

  return json
}

isOwned = function (this: AuthorInstance) {
  return this.podId === null
}

// ------------------------------ STATICS ------------------------------

listOwned = function () {
  const query: Sequelize.FindOptions<AuthorAttributes> = {
    where: {
      podId: null
    }
  }

  return Author.findAll(query)
}

load = function (id: number) {
  return Author.findById(id)
}

loadByUUID = function (uuid: string) {
  const query: Sequelize.FindOptions<AuthorAttributes> = {
    where: {
      uuid
    }
  }

  return Author.findOne(query)
}

loadAuthorByPodAndUUID = function (uuid: string, podId: number, transaction: Sequelize.Transaction) {
  const query: Sequelize.FindOptions<AuthorAttributes> = {
    where: {
      podId,
      uuid
    },
    transaction
  }

  return Author.find(query)
}