f21d2b8a29894d50dedcfa6d143b407b1ce3df9f
[oweals/peertube.git] / server / models / activitypub / actor-follow.ts
1 import * as Bluebird from 'bluebird'
2 import { values, difference } from 'lodash'
3 import {
4   AfterCreate,
5   AfterDestroy,
6   AfterUpdate,
7   AllowNull,
8   BelongsTo,
9   Column,
10   CreatedAt,
11   DataType,
12   Default,
13   ForeignKey,
14   IsInt,
15   Max,
16   Model,
17   Table,
18   UpdatedAt
19 } from 'sequelize-typescript'
20 import { FollowState } from '../../../shared/models/actors'
21 import { ActorFollow } from '../../../shared/models/actors/follow.model'
22 import { logger } from '../../helpers/logger'
23 import { getServerActor } from '../../helpers/utils'
24 import { ACTOR_FOLLOW_SCORE, FOLLOW_STATES, SERVER_ACTOR_NAME } from '../../initializers/constants'
25 import { ServerModel } from '../server/server'
26 import { createSafeIn, getSort, getFollowsSort } from '../utils'
27 import { ActorModel, unusedActorAttributesForAPI } from './actor'
28 import { VideoChannelModel } from '../video/video-channel'
29 import { AccountModel } from '../account/account'
30 import { IncludeOptions, Op, QueryTypes, Transaction, WhereOptions } from 'sequelize'
31 import {
32   MActorFollowActorsDefault,
33   MActorFollowActorsDefaultSubscription,
34   MActorFollowFollowingHost,
35   MActorFollowFormattable,
36   MActorFollowSubscriptions
37 } from '@server/typings/models'
38 import { ActivityPubActorType } from '@shared/models'
39 import { afterCommitIfTransaction } from '@server/helpers/database-utils'
40
41 @Table({
42   tableName: 'actorFollow',
43   indexes: [
44     {
45       fields: [ 'actorId' ]
46     },
47     {
48       fields: [ 'targetActorId' ]
49     },
50     {
51       fields: [ 'actorId', 'targetActorId' ],
52       unique: true
53     },
54     {
55       fields: [ 'score' ]
56     }
57   ]
58 })
59 export class ActorFollowModel extends Model<ActorFollowModel> {
60
61   @AllowNull(false)
62   @Column(DataType.ENUM(...values(FOLLOW_STATES)))
63   state: FollowState
64
65   @AllowNull(false)
66   @Default(ACTOR_FOLLOW_SCORE.BASE)
67   @IsInt
68   @Max(ACTOR_FOLLOW_SCORE.MAX)
69   @Column
70   score: number
71
72   @CreatedAt
73   createdAt: Date
74
75   @UpdatedAt
76   updatedAt: Date
77
78   @ForeignKey(() => ActorModel)
79   @Column
80   actorId: number
81
82   @BelongsTo(() => ActorModel, {
83     foreignKey: {
84       name: 'actorId',
85       allowNull: false
86     },
87     as: 'ActorFollower',
88     onDelete: 'CASCADE'
89   })
90   ActorFollower: ActorModel
91
92   @ForeignKey(() => ActorModel)
93   @Column
94   targetActorId: number
95
96   @BelongsTo(() => ActorModel, {
97     foreignKey: {
98       name: 'targetActorId',
99       allowNull: false
100     },
101     as: 'ActorFollowing',
102     onDelete: 'CASCADE'
103   })
104   ActorFollowing: ActorModel
105
106   @AfterCreate
107   @AfterUpdate
108   static incrementFollowerAndFollowingCount (instance: ActorFollowModel, options: any) {
109     if (instance.state !== 'accepted') return undefined
110
111     return Promise.all([
112       ActorModel.rebuildFollowsCount(instance.actorId, 'following', options.transaction),
113       ActorModel.rebuildFollowsCount(instance.targetActorId, 'followers', options.transaction)
114     ])
115   }
116
117   @AfterDestroy
118   static decrementFollowerAndFollowingCount (instance: ActorFollowModel, options: any) {
119     return Promise.all([
120       ActorModel.rebuildFollowsCount(instance.actorId, 'following', options.transaction),
121       ActorModel.rebuildFollowsCount(instance.targetActorId, 'followers', options.transaction)
122     ])
123   }
124
125   static removeFollowsOf (actorId: number, t?: Transaction) {
126     const query = {
127       where: {
128         [Op.or]: [
129           {
130             actorId
131           },
132           {
133             targetActorId: actorId
134           }
135         ]
136       },
137       transaction: t
138     }
139
140     return ActorFollowModel.destroy(query)
141   }
142
143   // Remove actor follows with a score of 0 (too many requests where they were unreachable)
144   static async removeBadActorFollows () {
145     const actorFollows = await ActorFollowModel.listBadActorFollows()
146
147     const actorFollowsRemovePromises = actorFollows.map(actorFollow => actorFollow.destroy())
148     await Promise.all(actorFollowsRemovePromises)
149
150     const numberOfActorFollowsRemoved = actorFollows.length
151
152     if (numberOfActorFollowsRemoved) logger.info('Removed bad %d actor follows.', numberOfActorFollowsRemoved)
153   }
154
155   static loadByActorAndTarget (actorId: number, targetActorId: number, t?: Transaction): Bluebird<MActorFollowActorsDefault> {
156     const query = {
157       where: {
158         actorId,
159         targetActorId: targetActorId
160       },
161       include: [
162         {
163           model: ActorModel,
164           required: true,
165           as: 'ActorFollower'
166         },
167         {
168           model: ActorModel,
169           required: true,
170           as: 'ActorFollowing'
171         }
172       ],
173       transaction: t
174     }
175
176     return ActorFollowModel.findOne(query)
177   }
178
179   static loadByActorAndTargetNameAndHostForAPI (
180     actorId: number,
181     targetName: string,
182     targetHost: string,
183     t?: Transaction
184   ): Bluebird<MActorFollowActorsDefaultSubscription> {
185     const actorFollowingPartInclude: IncludeOptions = {
186       model: ActorModel,
187       required: true,
188       as: 'ActorFollowing',
189       where: {
190         preferredUsername: targetName
191       },
192       include: [
193         {
194           model: VideoChannelModel.unscoped(),
195           required: false
196         }
197       ]
198     }
199
200     if (targetHost === null) {
201       actorFollowingPartInclude.where['serverId'] = null
202     } else {
203       actorFollowingPartInclude.include.push({
204         model: ServerModel,
205         required: true,
206         where: {
207           host: targetHost
208         }
209       })
210     }
211
212     const query = {
213       where: {
214         actorId
215       },
216       include: [
217         actorFollowingPartInclude,
218         {
219           model: ActorModel,
220           required: true,
221           as: 'ActorFollower'
222         }
223       ],
224       transaction: t
225     }
226
227     return ActorFollowModel.findOne(query)
228       .then(result => {
229         if (result && result.ActorFollowing.VideoChannel) {
230           result.ActorFollowing.VideoChannel.Actor = result.ActorFollowing
231         }
232
233         return result
234       })
235   }
236
237   static listSubscribedIn (actorId: number, targets: { name: string, host?: string }[]): Bluebird<MActorFollowFollowingHost[]> {
238     const whereTab = targets
239       .map(t => {
240         if (t.host) {
241           return {
242             [ Op.and ]: [
243               {
244                 '$preferredUsername$': t.name
245               },
246               {
247                 '$host$': t.host
248               }
249             ]
250           }
251         }
252
253         return {
254           [ Op.and ]: [
255             {
256               '$preferredUsername$': t.name
257             },
258             {
259               '$serverId$': null
260             }
261           ]
262         }
263       })
264
265     const query = {
266       attributes: [],
267       where: {
268         [ Op.and ]: [
269           {
270             [ Op.or ]: whereTab
271           },
272           {
273             actorId
274           }
275         ]
276       },
277       include: [
278         {
279           attributes: [ 'preferredUsername' ],
280           model: ActorModel.unscoped(),
281           required: true,
282           as: 'ActorFollowing',
283           include: [
284             {
285               attributes: [ 'host' ],
286               model: ServerModel.unscoped(),
287               required: false
288             }
289           ]
290         }
291       ]
292     }
293
294     return ActorFollowModel.findAll(query)
295   }
296
297   static listFollowingForApi (options: {
298     id: number,
299     start: number,
300     count: number,
301     sort: string,
302     state?: FollowState,
303     actorType?: ActivityPubActorType,
304     search?: string
305   }) {
306     const { id, start, count, sort, search, state, actorType } = options
307
308     const followWhere = state ? { state } : {}
309     const followingWhere: WhereOptions = {}
310     const followingServerWhere: WhereOptions = {}
311
312     if (search) {
313       Object.assign(followingServerWhere, {
314         host: {
315           [ Op.iLike ]: '%' + search + '%'
316         }
317       })
318     }
319
320     if (actorType) {
321       Object.assign(followingWhere, { type: actorType })
322     }
323
324     const query = {
325       distinct: true,
326       offset: start,
327       limit: count,
328       order: getFollowsSort(sort),
329       where: followWhere,
330       include: [
331         {
332           model: ActorModel,
333           required: true,
334           as: 'ActorFollower',
335           where: {
336             id
337           }
338         },
339         {
340           model: ActorModel,
341           as: 'ActorFollowing',
342           required: true,
343           where: followingWhere,
344           include: [
345             {
346               model: ServerModel,
347               required: true,
348               where: followingServerWhere
349             }
350           ]
351         }
352       ]
353     }
354
355     return ActorFollowModel.findAndCountAll<MActorFollowActorsDefault>(query)
356       .then(({ rows, count }) => {
357         return {
358           data: rows,
359           total: count
360         }
361       })
362   }
363
364   static listFollowersForApi (options: {
365     actorId: number,
366     start: number,
367     count: number,
368     sort: string,
369     state?: FollowState,
370     actorType?: ActivityPubActorType,
371     search?: string
372   }) {
373     const { actorId, start, count, sort, search, state, actorType } = options
374
375     const followWhere = state ? { state } : {}
376     const followerWhere: WhereOptions = {}
377     const followerServerWhere: WhereOptions = {}
378
379     if (search) {
380       Object.assign(followerServerWhere, {
381         host: {
382           [ Op.iLike ]: '%' + search + '%'
383         }
384       })
385     }
386
387     if (actorType) {
388       Object.assign(followerWhere, { type: actorType })
389     }
390
391     const query = {
392       distinct: true,
393       offset: start,
394       limit: count,
395       order: getFollowsSort(sort),
396       where: followWhere,
397       include: [
398         {
399           model: ActorModel,
400           required: true,
401           as: 'ActorFollower',
402           where: followerWhere,
403           include: [
404             {
405               model: ServerModel,
406               required: true,
407               where: followerServerWhere
408             }
409           ]
410         },
411         {
412           model: ActorModel,
413           as: 'ActorFollowing',
414           required: true,
415           where: {
416             id: actorId
417           }
418         }
419       ]
420     }
421
422     return ActorFollowModel.findAndCountAll<MActorFollowActorsDefault>(query)
423                            .then(({ rows, count }) => {
424                              return {
425                                data: rows,
426                                total: count
427                              }
428                            })
429   }
430
431   static listSubscriptionsForApi (actorId: number, start: number, count: number, sort: string) {
432     const query = {
433       attributes: [],
434       distinct: true,
435       offset: start,
436       limit: count,
437       order: getSort(sort),
438       where: {
439         actorId: actorId
440       },
441       include: [
442         {
443           attributes: [ 'id' ],
444           model: ActorModel.unscoped(),
445           as: 'ActorFollowing',
446           required: true,
447           include: [
448             {
449               model: VideoChannelModel.unscoped(),
450               required: true,
451               include: [
452                 {
453                   attributes: {
454                     exclude: unusedActorAttributesForAPI
455                   },
456                   model: ActorModel,
457                   required: true
458                 },
459                 {
460                   model: AccountModel.unscoped(),
461                   required: true,
462                   include: [
463                     {
464                       attributes: {
465                         exclude: unusedActorAttributesForAPI
466                       },
467                       model: ActorModel,
468                       required: true
469                     }
470                   ]
471                 }
472               ]
473             }
474           ]
475         }
476       ]
477     }
478
479     return ActorFollowModel.findAndCountAll<MActorFollowSubscriptions>(query)
480                            .then(({ rows, count }) => {
481                              return {
482                                data: rows.map(r => r.ActorFollowing.VideoChannel),
483                                total: count
484                              }
485                            })
486   }
487
488   static async keepUnfollowedInstance (hosts: string[]) {
489     const followerId = (await getServerActor()).id
490
491     const query = {
492       attributes: [ 'id' ],
493       where: {
494         actorId: followerId
495       },
496       include: [
497         {
498           attributes: [ 'id' ],
499           model: ActorModel.unscoped(),
500           required: true,
501           as: 'ActorFollowing',
502           where: {
503             preferredUsername: SERVER_ACTOR_NAME
504           },
505           include: [
506             {
507               attributes: [ 'host' ],
508               model: ServerModel.unscoped(),
509               required: true,
510               where: {
511                 host: {
512                   [Op.in]: hosts
513                 }
514               }
515             }
516           ]
517         }
518       ]
519     }
520
521     const res = await ActorFollowModel.findAll(query)
522     const followedHosts = res.map(row => row.ActorFollowing.Server.host)
523
524     return difference(hosts, followedHosts)
525   }
526
527   static listAcceptedFollowerUrlsForAP (actorIds: number[], t: Transaction, start?: number, count?: number) {
528     return ActorFollowModel.createListAcceptedFollowForApiQuery('followers', actorIds, t, start, count)
529   }
530
531   static listAcceptedFollowerSharedInboxUrls (actorIds: number[], t: Transaction) {
532     return ActorFollowModel.createListAcceptedFollowForApiQuery(
533       'followers',
534       actorIds,
535       t,
536       undefined,
537       undefined,
538       'sharedInboxUrl',
539       true
540     )
541   }
542
543   static listAcceptedFollowingUrlsForApi (actorIds: number[], t: Transaction, start?: number, count?: number) {
544     return ActorFollowModel.createListAcceptedFollowForApiQuery('following', actorIds, t, start, count)
545   }
546
547   static async getStats () {
548     const serverActor = await getServerActor()
549
550     const totalInstanceFollowing = await ActorFollowModel.count({
551       where: {
552         actorId: serverActor.id
553       }
554     })
555
556     const totalInstanceFollowers = await ActorFollowModel.count({
557       where: {
558         targetActorId: serverActor.id
559       }
560     })
561
562     return {
563       totalInstanceFollowing,
564       totalInstanceFollowers
565     }
566   }
567
568   static updateScore (inboxUrl: string, value: number, t?: Transaction) {
569     const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` +
570       'WHERE id IN (' +
571         'SELECT "actorFollow"."id" FROM "actorFollow" ' +
572         'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."actorId" ' +
573         `WHERE "actor"."inboxUrl" = '${inboxUrl}' OR "actor"."sharedInboxUrl" = '${inboxUrl}'` +
574       ')'
575
576     const options = {
577       type: QueryTypes.BULKUPDATE,
578       transaction: t
579     }
580
581     return ActorFollowModel.sequelize.query(query, options)
582   }
583
584   static async updateScoreByFollowingServers (serverIds: number[], value: number, t?: Transaction) {
585     if (serverIds.length === 0) return
586
587     const me = await getServerActor()
588     const serverIdsString = createSafeIn(ActorFollowModel, serverIds)
589
590     const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` +
591       'WHERE id IN (' +
592         'SELECT "actorFollow"."id" FROM "actorFollow" ' +
593         'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."targetActorId" ' +
594         `WHERE "actorFollow"."actorId" = ${me.Account.actorId} ` + // I'm the follower
595         `AND "actor"."serverId" IN (${serverIdsString})` + // Criteria on followings
596       ')'
597
598     const options = {
599       type: QueryTypes.BULKUPDATE,
600       transaction: t
601     }
602
603     return ActorFollowModel.sequelize.query(query, options)
604   }
605
606   private static async createListAcceptedFollowForApiQuery (
607     type: 'followers' | 'following',
608     actorIds: number[],
609     t: Transaction,
610     start?: number,
611     count?: number,
612     columnUrl = 'url',
613     distinct = false
614   ) {
615     let firstJoin: string
616     let secondJoin: string
617
618     if (type === 'followers') {
619       firstJoin = 'targetActorId'
620       secondJoin = 'actorId'
621     } else {
622       firstJoin = 'actorId'
623       secondJoin = 'targetActorId'
624     }
625
626     const selections: string[] = []
627     if (distinct === true) selections.push(`DISTINCT("Follows"."${columnUrl}") AS "selectionUrl"`)
628     else selections.push(`"Follows"."${columnUrl}" AS "selectionUrl"`)
629
630     selections.push('COUNT(*) AS "total"')
631
632     const tasks: Bluebird<any>[] = []
633
634     for (let selection of selections) {
635       let query = 'SELECT ' + selection + ' FROM "actor" ' +
636         'INNER JOIN "actorFollow" ON "actorFollow"."' + firstJoin + '" = "actor"."id" ' +
637         'INNER JOIN "actor" AS "Follows" ON "actorFollow"."' + secondJoin + '" = "Follows"."id" ' +
638         `WHERE "actor"."id" = ANY ($actorIds) AND "actorFollow"."state" = 'accepted' AND "Follows"."${columnUrl}" IS NOT NULL `
639
640       if (count !== undefined) query += 'LIMIT ' + count
641       if (start !== undefined) query += ' OFFSET ' + start
642
643       const options = {
644         bind: { actorIds },
645         type: QueryTypes.SELECT,
646         transaction: t
647       }
648       tasks.push(ActorFollowModel.sequelize.query(query, options))
649     }
650
651     const [ followers, [ dataTotal ] ] = await Promise.all(tasks)
652     const urls: string[] = followers.map(f => f.selectionUrl)
653
654     return {
655       data: urls,
656       total: dataTotal ? parseInt(dataTotal.total, 10) : 0
657     }
658   }
659
660   private static listBadActorFollows () {
661     const query = {
662       where: {
663         score: {
664           [Op.lte]: 0
665         }
666       },
667       logging: false
668     }
669
670     return ActorFollowModel.findAll(query)
671   }
672
673   toFormattedJSON (this: MActorFollowFormattable): ActorFollow {
674     const follower = this.ActorFollower.toFormattedJSON()
675     const following = this.ActorFollowing.toFormattedJSON()
676
677     return {
678       id: this.id,
679       follower,
680       following,
681       score: this.score,
682       state: this.state,
683       createdAt: this.createdAt,
684       updatedAt: this.updatedAt
685     }
686   }
687 }