]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/account/account.ts
Follow works
[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 { CONSTRAINTS_FIELDS } from '../../initializers/constants'
27
28 let Account: Sequelize.Model<AccountInstance, AccountAttributes>
29 let loadAccountByPodAndUUID: AccountMethods.LoadAccountByPodAndUUID
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 listAcceptedFollowerUrlsForApi: AccountMethods.ListAcceptedFollowerUrlsForApi
38 let listAcceptedFollowingUrlsForApi: AccountMethods.ListAcceptedFollowingUrlsForApi
39 let listFollowingForApi: AccountMethods.ListFollowingForApi
40 let listFollowersForApi: AccountMethods.ListFollowersForApi
41 let isOwned: AccountMethods.IsOwned
42 let toActivityPubObject: AccountMethods.ToActivityPubObject
43 let toFormattedJSON: AccountMethods.ToFormattedJSON
44 let getFollowerSharedInboxUrls: AccountMethods.GetFollowerSharedInboxUrls
45 let getFollowingUrl: AccountMethods.GetFollowingUrl
46 let getFollowersUrl: AccountMethods.GetFollowersUrl
47 let getPublicKeyUrl: AccountMethods.GetPublicKeyUrl
48
49 export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
50 Account = sequelize.define<AccountInstance, AccountAttributes>('Account',
51 {
52 uuid: {
53 type: DataTypes.UUID,
54 defaultValue: DataTypes.UUIDV4,
55 allowNull: false,
56 validate: {
57 isUUID: 4
58 }
59 },
60 name: {
61 type: DataTypes.STRING,
62 allowNull: false,
63 validate: {
64 nameValid: value => {
65 const res = isUserUsernameValid(value)
66 if (res === false) throw new Error('Name is not valid.')
67 }
68 }
69 },
70 url: {
71 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
72 allowNull: false,
73 validate: {
74 urlValid: value => {
75 const res = isAccountUrlValid(value)
76 if (res === false) throw new Error('URL is not valid.')
77 }
78 }
79 },
80 publicKey: {
81 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY.max),
82 allowNull: false,
83 validate: {
84 publicKeyValid: value => {
85 const res = isAccountPublicKeyValid(value)
86 if (res === false) throw new Error('Public key is not valid.')
87 }
88 }
89 },
90 privateKey: {
91 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY.max),
92 allowNull: true,
93 validate: {
94 privateKeyValid: value => {
95 const res = isAccountPrivateKeyValid(value)
96 if (res === false) throw new Error('Private key is not valid.')
97 }
98 }
99 },
100 followersCount: {
101 type: DataTypes.INTEGER,
102 allowNull: false,
103 validate: {
104 followersCountValid: value => {
105 const res = isAccountFollowersCountValid(value)
106 if (res === false) throw new Error('Followers count is not valid.')
107 }
108 }
109 },
110 followingCount: {
111 type: DataTypes.INTEGER,
112 allowNull: false,
113 validate: {
114 followingCountValid: value => {
115 const res = isAccountFollowingCountValid(value)
116 if (res === false) throw new Error('Following count is not valid.')
117 }
118 }
119 },
120 inboxUrl: {
121 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
122 allowNull: false,
123 validate: {
124 inboxUrlValid: value => {
125 const res = isAccountInboxValid(value)
126 if (res === false) throw new Error('Inbox URL is not valid.')
127 }
128 }
129 },
130 outboxUrl: {
131 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
132 allowNull: false,
133 validate: {
134 outboxUrlValid: value => {
135 const res = isAccountOutboxValid(value)
136 if (res === false) throw new Error('Outbox URL is not valid.')
137 }
138 }
139 },
140 sharedInboxUrl: {
141 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
142 allowNull: false,
143 validate: {
144 sharedInboxUrlValid: value => {
145 const res = isAccountSharedInboxValid(value)
146 if (res === false) throw new Error('Shared inbox URL is not valid.')
147 }
148 }
149 },
150 followersUrl: {
151 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
152 allowNull: false,
153 validate: {
154 followersUrlValid: value => {
155 const res = isAccountFollowersValid(value)
156 if (res === false) throw new Error('Followers URL is not valid.')
157 }
158 }
159 },
160 followingUrl: {
161 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
162 allowNull: false,
163 validate: {
164 followingUrlValid: value => {
165 const res = isAccountFollowingValid(value)
166 if (res === false) throw new Error('Following URL is not valid.')
167 }
168 }
169 }
170 },
171 {
172 indexes: [
173 {
174 fields: [ 'name' ]
175 },
176 {
177 fields: [ 'podId' ]
178 },
179 {
180 fields: [ 'userId' ],
181 unique: true
182 },
183 {
184 fields: [ 'applicationId' ],
185 unique: true
186 },
187 {
188 fields: [ 'name', 'podId' ],
189 unique: true
190 }
191 ],
192 hooks: { afterDestroy }
193 }
194 )
195
196 const classMethods = [
197 associate,
198 loadAccountByPodAndUUID,
199 loadApplication,
200 load,
201 loadByUUID,
202 loadByUrl,
203 loadLocalByName,
204 loadByNameAndHost,
205 listOwned,
206 listAcceptedFollowerUrlsForApi,
207 listAcceptedFollowingUrlsForApi,
208 listFollowingForApi,
209 listFollowersForApi
210 ]
211 const instanceMethods = [
212 isOwned,
213 toActivityPubObject,
214 toFormattedJSON,
215 getFollowerSharedInboxUrls,
216 getFollowingUrl,
217 getFollowersUrl,
218 getPublicKeyUrl
219 ]
220 addMethodsToModel(Account, classMethods, instanceMethods)
221
222 return Account
223 }
224
225 // ---------------------------------------------------------------------------
226
227 function associate (models) {
228 Account.belongsTo(models.Pod, {
229 foreignKey: {
230 name: 'podId',
231 allowNull: true
232 },
233 onDelete: 'cascade'
234 })
235
236 Account.belongsTo(models.User, {
237 foreignKey: {
238 name: 'userId',
239 allowNull: true
240 },
241 onDelete: 'cascade'
242 })
243
244 Account.belongsTo(models.Application, {
245 foreignKey: {
246 name: 'applicationId',
247 allowNull: true
248 },
249 onDelete: 'cascade'
250 })
251
252 Account.hasMany(models.VideoChannel, {
253 foreignKey: {
254 name: 'accountId',
255 allowNull: false
256 },
257 onDelete: 'cascade',
258 hooks: true
259 })
260
261 Account.hasMany(models.AccountFollow, {
262 foreignKey: {
263 name: 'accountId',
264 allowNull: false
265 },
266 as: 'following',
267 onDelete: 'cascade'
268 })
269
270 Account.hasMany(models.AccountFollow, {
271 foreignKey: {
272 name: 'targetAccountId',
273 allowNull: false
274 },
275 as: 'followers',
276 onDelete: 'cascade'
277 })
278 }
279
280 function afterDestroy (account: AccountInstance) {
281 if (account.isOwned()) {
282 return sendDeleteAccount(account, undefined)
283 }
284
285 return undefined
286 }
287
288 toFormattedJSON = function (this: AccountInstance) {
289 const json = {
290 id: this.id,
291 host: this.Pod.host,
292 name: this.name
293 }
294
295 return json
296 }
297
298 toActivityPubObject = function (this: AccountInstance) {
299 const type = this.podId ? 'Application' as 'Application' : 'Person' as 'Person'
300
301 const json = {
302 type,
303 id: this.url,
304 following: this.getFollowingUrl(),
305 followers: this.getFollowersUrl(),
306 inbox: this.inboxUrl,
307 outbox: this.outboxUrl,
308 preferredUsername: this.name,
309 url: this.url,
310 name: this.name,
311 endpoints: {
312 sharedInbox: this.sharedInboxUrl
313 },
314 uuid: this.uuid,
315 publicKey: {
316 id: this.getPublicKeyUrl(),
317 owner: this.url,
318 publicKeyPem: this.publicKey
319 }
320 }
321
322 return activityPubContextify(json)
323 }
324
325 isOwned = function (this: AccountInstance) {
326 return this.podId === null
327 }
328
329 getFollowerSharedInboxUrls = function (this: AccountInstance) {
330 const query: Sequelize.FindOptions<AccountAttributes> = {
331 attributes: [ 'sharedInboxUrl' ],
332 include: [
333 {
334 model: Account['sequelize'].models.AccountFollow,
335 required: true,
336 as: 'followers',
337 where: {
338 targetAccountId: this.id
339 }
340 }
341 ]
342 }
343
344 return Account.findAll(query)
345 .then(accounts => accounts.map(a => a.sharedInboxUrl))
346 }
347
348 getFollowingUrl = function (this: AccountInstance) {
349 return this.url + '/followers'
350 }
351
352 getFollowersUrl = function (this: AccountInstance) {
353 return this.url + '/followers'
354 }
355
356 getPublicKeyUrl = function (this: AccountInstance) {
357 return this.url + '#main-key'
358 }
359
360 // ------------------------------ STATICS ------------------------------
361
362 listOwned = function () {
363 const query: Sequelize.FindOptions<AccountAttributes> = {
364 where: {
365 podId: null
366 }
367 }
368
369 return Account.findAll(query)
370 }
371
372 listAcceptedFollowerUrlsForApi = function (id: number, start: number, count?: number) {
373 return createListAcceptedFollowForApiQuery('followers', id, start, count)
374 }
375
376 listAcceptedFollowingUrlsForApi = function (id: number, start: number, count?: number) {
377 return createListAcceptedFollowForApiQuery('following', id, start, count)
378 }
379
380 listFollowingForApi = function (id: number, start: number, count: number, sort: string) {
381 const query = {
382 distinct: true,
383 offset: start,
384 limit: count,
385 order: [ getSort(sort) ],
386 include: [
387 {
388 model: Account['sequelize'].models.AccountFollow,
389 required: true,
390 as: 'following',
391 include: [
392 {
393 model: Account['sequelize'].models.Account,
394 as: 'accountFollowing',
395 required: true,
396 include: [ Account['sequelize'].models.Pod ]
397 }
398 ]
399 }
400 ]
401 }
402
403 return Account.findAndCountAll(query).then(({ rows, count }) => {
404 return {
405 data: rows,
406 total: count
407 }
408 })
409 }
410
411 listFollowersForApi = function (id: number, start: number, count: number, sort: string) {
412 const query = {
413 distinct: true,
414 offset: start,
415 limit: count,
416 order: [ getSort(sort) ],
417 include: [
418 {
419 model: Account['sequelize'].models.AccountFollow,
420 required: true,
421 as: 'followers',
422 include: [
423 {
424 model: Account['sequelize'].models.Account,
425 as: 'accountFollowers',
426 required: true,
427 include: [ Account['sequelize'].models.Pod ]
428 }
429 ]
430 }
431 ]
432 }
433
434 return Account.findAndCountAll(query).then(({ rows, count }) => {
435 return {
436 data: rows,
437 total: count
438 }
439 })
440 }
441
442 loadApplication = function () {
443 return Account.findOne({
444 include: [
445 {
446 model: Account['sequelize'].models.Application,
447 required: true
448 }
449 ]
450 })
451 }
452
453 load = function (id: number) {
454 return Account.findById(id)
455 }
456
457 loadByUUID = function (uuid: string) {
458 const query: Sequelize.FindOptions<AccountAttributes> = {
459 where: {
460 uuid
461 }
462 }
463
464 return Account.findOne(query)
465 }
466
467 loadLocalByName = function (name: string) {
468 const query: Sequelize.FindOptions<AccountAttributes> = {
469 where: {
470 name,
471 [Sequelize.Op.or]: [
472 {
473 userId: {
474 [Sequelize.Op.ne]: null
475 }
476 },
477 {
478 applicationId: {
479 [Sequelize.Op.ne]: null
480 }
481 }
482 ]
483 }
484 }
485
486 return Account.findOne(query)
487 }
488
489 loadByNameAndHost = function (name: string, host: string) {
490 const query: Sequelize.FindOptions<AccountAttributes> = {
491 where: {
492 name
493 },
494 include: [
495 {
496 model: Account['sequelize'].models.Pod,
497 required: true,
498 where: {
499 host
500 }
501 }
502 ]
503 }
504
505 return Account.findOne(query)
506 }
507
508 loadByUrl = function (url: string, transaction?: Sequelize.Transaction) {
509 const query: Sequelize.FindOptions<AccountAttributes> = {
510 where: {
511 url
512 },
513 transaction
514 }
515
516 return Account.findOne(query)
517 }
518
519 loadAccountByPodAndUUID = function (uuid: string, podId: number, transaction: Sequelize.Transaction) {
520 const query: Sequelize.FindOptions<AccountAttributes> = {
521 where: {
522 podId,
523 uuid
524 },
525 transaction
526 }
527
528 return Account.find(query)
529 }
530
531 // ------------------------------ UTILS ------------------------------
532
533 async function createListAcceptedFollowForApiQuery (type: 'followers' | 'following', id: number, start: number, count?: number) {
534 let firstJoin: string
535 let secondJoin: string
536
537 if (type === 'followers') {
538 firstJoin = 'targetAccountId'
539 secondJoin = 'accountId'
540 } else {
541 firstJoin = 'accountId'
542 secondJoin = 'targetAccountId'
543 }
544
545 const selections = [ '"Followers"."url" AS "url"', 'COUNT(*) AS "total"' ]
546 const tasks: Promise<any>[] = []
547
548 for (const selection of selections) {
549 let query = 'SELECT ' + selection + ' FROM "Account" ' +
550 'INNER JOIN "AccountFollow" ON "AccountFollow"."' + firstJoin + '" = "Account"."id" ' +
551 'INNER JOIN "Account" AS "Follows" ON "Followers"."id" = "Follows"."' + secondJoin + '" ' +
552 'WHERE "Account"."id" = $id AND "AccountFollow"."state" = \'accepted\' ' +
553 'LIMIT ' + start
554
555 if (count !== undefined) query += ', ' + count
556
557 const options = {
558 bind: { id },
559 type: Sequelize.QueryTypes.SELECT
560 }
561 tasks.push(Account['sequelize'].query(query, options))
562 }
563
564 const [ followers, [ { total } ]] = await Promise.all(tasks)
565 const urls: string[] = followers.map(f => f.url)
566
567 return {
568 data: urls,
569 total: parseInt(total, 10)
570 }
571 }