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