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