]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/account/account.ts
Rename Pod -> Server
[github/Chocobozzz/PeerTube.git] / server / models / account / account.ts
1 import * as Sequelize from 'sequelize'
2
3 import {
4 isUserUsernameValid,
5 isAccountPublicKeyValid,
6 isAccountUrlValid,
7 isAccountPrivateKeyValid,
8 isAccountFollowersCountValid,
9 isAccountFollowingCountValid,
10 isAccountInboxValid,
11 isAccountOutboxValid,
12 isAccountSharedInboxValid,
13 isAccountFollowersValid,
14 isAccountFollowingValid,
15 activityPubContextify
16 } from '../../helpers'
17
18 import { addMethodsToModel, getSort } from '../utils'
19 import {
20 AccountInstance,
21 AccountAttributes,
22
23 AccountMethods
24 } from './account-interface'
25 import { sendDeleteAccount } from '../../lib/activitypub/send-request'
26 import { CONFIG, CONSTRAINTS_FIELDS } from '../../initializers/constants'
27
28 let Account: Sequelize.Model<AccountInstance, AccountAttributes>
29 let loadAccountByServerAndUUID: AccountMethods.LoadAccountByServerAndUUID
30 let load: AccountMethods.Load
31 let loadApplication: AccountMethods.LoadApplication
32 let loadByUUID: AccountMethods.LoadByUUID
33 let loadByUrl: AccountMethods.LoadByUrl
34 let loadLocalByName: AccountMethods.LoadLocalByName
35 let loadByNameAndHost: AccountMethods.LoadByNameAndHost
36 let listOwned: AccountMethods.ListOwned
37 let isOwned: AccountMethods.IsOwned
38 let toActivityPubObject: AccountMethods.ToActivityPubObject
39 let toFormattedJSON: AccountMethods.ToFormattedJSON
40 let getFollowerSharedInboxUrls: AccountMethods.GetFollowerSharedInboxUrls
41 let getFollowingUrl: AccountMethods.GetFollowingUrl
42 let getFollowersUrl: AccountMethods.GetFollowersUrl
43 let getPublicKeyUrl: AccountMethods.GetPublicKeyUrl
44
45 export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
46 Account = sequelize.define<AccountInstance, AccountAttributes>('Account',
47 {
48 uuid: {
49 type: DataTypes.UUID,
50 defaultValue: DataTypes.UUIDV4,
51 allowNull: false,
52 validate: {
53 isUUID: 4
54 }
55 },
56 name: {
57 type: DataTypes.STRING,
58 allowNull: false,
59 validate: {
60 nameValid: value => {
61 const res = isUserUsernameValid(value)
62 if (res === false) throw new Error('Name is not valid.')
63 }
64 }
65 },
66 url: {
67 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
68 allowNull: false,
69 validate: {
70 urlValid: value => {
71 const res = isAccountUrlValid(value)
72 if (res === false) throw new Error('URL is not valid.')
73 }
74 }
75 },
76 publicKey: {
77 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY.max),
78 allowNull: false,
79 validate: {
80 publicKeyValid: value => {
81 const res = isAccountPublicKeyValid(value)
82 if (res === false) throw new Error('Public key is not valid.')
83 }
84 }
85 },
86 privateKey: {
87 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY.max),
88 allowNull: true,
89 validate: {
90 privateKeyValid: value => {
91 const res = isAccountPrivateKeyValid(value)
92 if (res === false) throw new Error('Private key is not valid.')
93 }
94 }
95 },
96 followersCount: {
97 type: DataTypes.INTEGER,
98 allowNull: false,
99 validate: {
100 followersCountValid: value => {
101 const res = isAccountFollowersCountValid(value)
102 if (res === false) throw new Error('Followers count is not valid.')
103 }
104 }
105 },
106 followingCount: {
107 type: DataTypes.INTEGER,
108 allowNull: false,
109 validate: {
110 followingCountValid: value => {
111 const res = isAccountFollowingCountValid(value)
112 if (res === false) throw new Error('Following count is not valid.')
113 }
114 }
115 },
116 inboxUrl: {
117 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
118 allowNull: false,
119 validate: {
120 inboxUrlValid: value => {
121 const res = isAccountInboxValid(value)
122 if (res === false) throw new Error('Inbox URL is not valid.')
123 }
124 }
125 },
126 outboxUrl: {
127 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
128 allowNull: false,
129 validate: {
130 outboxUrlValid: value => {
131 const res = isAccountOutboxValid(value)
132 if (res === false) throw new Error('Outbox URL is not valid.')
133 }
134 }
135 },
136 sharedInboxUrl: {
137 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
138 allowNull: false,
139 validate: {
140 sharedInboxUrlValid: value => {
141 const res = isAccountSharedInboxValid(value)
142 if (res === false) throw new Error('Shared inbox URL is not valid.')
143 }
144 }
145 },
146 followersUrl: {
147 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
148 allowNull: false,
149 validate: {
150 followersUrlValid: value => {
151 const res = isAccountFollowersValid(value)
152 if (res === false) throw new Error('Followers URL is not valid.')
153 }
154 }
155 },
156 followingUrl: {
157 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
158 allowNull: false,
159 validate: {
160 followingUrlValid: value => {
161 const res = isAccountFollowingValid(value)
162 if (res === false) throw new Error('Following URL is not valid.')
163 }
164 }
165 }
166 },
167 {
168 indexes: [
169 {
170 fields: [ 'name' ]
171 },
172 {
173 fields: [ 'serverId' ]
174 },
175 {
176 fields: [ 'userId' ],
177 unique: true
178 },
179 {
180 fields: [ 'applicationId' ],
181 unique: true
182 },
183 {
184 fields: [ 'name', 'serverId', 'applicationId' ],
185 unique: true
186 }
187 ],
188 hooks: { afterDestroy }
189 }
190 )
191
192 const classMethods = [
193 associate,
194 loadAccountByServerAndUUID,
195 loadApplication,
196 load,
197 loadByUUID,
198 loadByUrl,
199 loadLocalByName,
200 loadByNameAndHost,
201 listOwned
202 ]
203 const instanceMethods = [
204 isOwned,
205 toActivityPubObject,
206 toFormattedJSON,
207 getFollowerSharedInboxUrls,
208 getFollowingUrl,
209 getFollowersUrl,
210 getPublicKeyUrl
211 ]
212 addMethodsToModel(Account, classMethods, instanceMethods)
213
214 return Account
215 }
216
217 // ---------------------------------------------------------------------------
218
219 function associate (models) {
220 Account.belongsTo(models.Server, {
221 foreignKey: {
222 name: 'serverId',
223 allowNull: true
224 },
225 onDelete: 'cascade'
226 })
227
228 Account.belongsTo(models.User, {
229 foreignKey: {
230 name: 'userId',
231 allowNull: true
232 },
233 onDelete: 'cascade'
234 })
235
236 Account.belongsTo(models.Application, {
237 foreignKey: {
238 name: 'applicationId',
239 allowNull: true
240 },
241 onDelete: 'cascade'
242 })
243
244 Account.hasMany(models.VideoChannel, {
245 foreignKey: {
246 name: 'accountId',
247 allowNull: false
248 },
249 onDelete: 'cascade',
250 hooks: true
251 })
252
253 Account.hasMany(models.AccountFollow, {
254 foreignKey: {
255 name: 'accountId',
256 allowNull: false
257 },
258 as: 'following',
259 onDelete: 'cascade'
260 })
261
262 Account.hasMany(models.AccountFollow, {
263 foreignKey: {
264 name: 'targetAccountId',
265 allowNull: false
266 },
267 as: 'followers',
268 onDelete: 'cascade'
269 })
270 }
271
272 function afterDestroy (account: AccountInstance) {
273 if (account.isOwned()) {
274 return sendDeleteAccount(account, undefined)
275 }
276
277 return undefined
278 }
279
280 toFormattedJSON = function (this: AccountInstance) {
281 let host = CONFIG.WEBSERVER.HOST
282 let score: number
283
284 if (this.Server) {
285 host = this.Server.host
286 score = this.Server.score as number
287 }
288
289 const json = {
290 id: this.id,
291 host,
292 score,
293 name: this.name,
294 createdAt: this.createdAt,
295 updatedAt: this.updatedAt
296 }
297
298 return json
299 }
300
301 toActivityPubObject = function (this: AccountInstance) {
302 const type = this.serverId ? 'Application' as 'Application' : 'Person' as 'Person'
303
304 const json = {
305 type,
306 id: this.url,
307 following: this.getFollowingUrl(),
308 followers: this.getFollowersUrl(),
309 inbox: this.inboxUrl,
310 outbox: this.outboxUrl,
311 preferredUsername: this.name,
312 url: this.url,
313 name: this.name,
314 endpoints: {
315 sharedInbox: this.sharedInboxUrl
316 },
317 uuid: this.uuid,
318 publicKey: {
319 id: this.getPublicKeyUrl(),
320 owner: this.url,
321 publicKeyPem: this.publicKey
322 }
323 }
324
325 return activityPubContextify(json)
326 }
327
328 isOwned = function (this: AccountInstance) {
329 return this.serverId === null
330 }
331
332 getFollowerSharedInboxUrls = function (this: AccountInstance) {
333 const query: Sequelize.FindOptions<AccountAttributes> = {
334 attributes: [ 'sharedInboxUrl' ],
335 include: [
336 {
337 model: Account['sequelize'].models.AccountFollow,
338 required: true,
339 as: 'followers',
340 where: {
341 targetAccountId: this.id
342 }
343 }
344 ]
345 }
346
347 return Account.findAll(query)
348 .then(accounts => accounts.map(a => a.sharedInboxUrl))
349 }
350
351 getFollowingUrl = function (this: AccountInstance) {
352 return this.url + '/following'
353 }
354
355 getFollowersUrl = function (this: AccountInstance) {
356 return this.url + '/followers'
357 }
358
359 getPublicKeyUrl = function (this: AccountInstance) {
360 return this.url + '#main-key'
361 }
362
363 // ------------------------------ STATICS ------------------------------
364
365 listOwned = function () {
366 const query: Sequelize.FindOptions<AccountAttributes> = {
367 where: {
368 serverId: null
369 }
370 }
371
372 return Account.findAll(query)
373 }
374
375 loadApplication = function () {
376 return Account.findOne({
377 include: [
378 {
379 model: Account['sequelize'].models.Application,
380 required: true
381 }
382 ]
383 })
384 }
385
386 load = function (id: number) {
387 return Account.findById(id)
388 }
389
390 loadByUUID = function (uuid: string) {
391 const query: Sequelize.FindOptions<AccountAttributes> = {
392 where: {
393 uuid
394 }
395 }
396
397 return Account.findOne(query)
398 }
399
400 loadLocalByName = function (name: string) {
401 const query: Sequelize.FindOptions<AccountAttributes> = {
402 where: {
403 name,
404 [Sequelize.Op.or]: [
405 {
406 userId: {
407 [Sequelize.Op.ne]: null
408 }
409 },
410 {
411 applicationId: {
412 [Sequelize.Op.ne]: null
413 }
414 }
415 ]
416 }
417 }
418
419 return Account.findOne(query)
420 }
421
422 loadByNameAndHost = function (name: string, host: string) {
423 const query: Sequelize.FindOptions<AccountAttributes> = {
424 where: {
425 name
426 },
427 include: [
428 {
429 model: Account['sequelize'].models.Server,
430 required: true,
431 where: {
432 host
433 }
434 }
435 ]
436 }
437
438 return Account.findOne(query)
439 }
440
441 loadByUrl = function (url: string, transaction?: Sequelize.Transaction) {
442 const query: Sequelize.FindOptions<AccountAttributes> = {
443 where: {
444 url
445 },
446 transaction
447 }
448
449 return Account.findOne(query)
450 }
451
452 loadAccountByServerAndUUID = function (uuid: string, serverId: number, transaction: Sequelize.Transaction) {
453 const query: Sequelize.FindOptions<AccountAttributes> = {
454 where: {
455 serverId,
456 uuid
457 },
458 transaction
459 }
460
461 return Account.find(query)
462 }