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