]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/actor/actor-follow.ts
Merge branch 'release/3.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / models / actor / actor-follow.ts
1 import { difference, values } from 'lodash'
2 import { IncludeOptions, Op, QueryTypes, Transaction, WhereOptions } from 'sequelize'
3 import {
4 AfterCreate,
5 AfterDestroy,
6 AfterUpdate,
7 AllowNull,
8 BelongsTo,
9 Column,
10 CreatedAt,
11 DataType,
12 Default,
13 ForeignKey,
14 Is,
15 IsInt,
16 Max,
17 Model,
18 Table,
19 UpdatedAt
20 } from 'sequelize-typescript'
21 import { isActivityPubUrlValid } from '@server/helpers/custom-validators/activitypub/misc'
22 import { getServerActor } from '@server/models/application/application'
23 import { VideoModel } from '@server/models/video/video'
24 import {
25 MActorFollowActorsDefault,
26 MActorFollowActorsDefaultSubscription,
27 MActorFollowFollowingHost,
28 MActorFollowFormattable,
29 MActorFollowSubscriptions
30 } from '@server/types/models'
31 import { AttributesOnly } from '@shared/core-utils'
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<Partial<AttributesOnly<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): Promise<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 ): Promise<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 }
253
254 static listSubscribedIn (actorId: number, targets: { name: string, host?: string }[]): Promise<MActorFollowFollowingHost[]> {
255 const whereTab = targets
256 .map(t => {
257 if (t.host) {
258 return {
259 [Op.and]: [
260 {
261 $preferredUsername$: t.name
262 },
263 {
264 $host$: t.host
265 }
266 ]
267 }
268 }
269
270 return {
271 [Op.and]: [
272 {
273 $preferredUsername$: t.name
274 },
275 {
276 $serverId$: null
277 }
278 ]
279 }
280 })
281
282 const query = {
283 attributes: [ 'id' ],
284 where: {
285 [Op.and]: [
286 {
287 [Op.or]: whereTab
288 },
289 {
290 actorId
291 }
292 ]
293 },
294 include: [
295 {
296 attributes: [ 'preferredUsername' ],
297 model: ActorModel.unscoped(),
298 required: true,
299 as: 'ActorFollowing',
300 include: [
301 {
302 attributes: [ 'host' ],
303 model: ServerModel.unscoped(),
304 required: false
305 }
306 ]
307 }
308 ]
309 }
310
311 return ActorFollowModel.findAll(query)
312 }
313
314 static listFollowingForApi (options: {
315 id: number
316 start: number
317 count: number
318 sort: string
319 state?: FollowState
320 actorType?: ActivityPubActorType
321 search?: string
322 }) {
323 const { id, start, count, sort, search, state, actorType } = options
324
325 const followWhere = state ? { state } : {}
326 const followingWhere: WhereOptions = {}
327 const followingServerWhere: WhereOptions = {}
328
329 if (search) {
330 Object.assign(followingServerWhere, {
331 host: {
332 [Op.iLike]: '%' + search + '%'
333 }
334 })
335 }
336
337 if (actorType) {
338 Object.assign(followingWhere, { type: actorType })
339 }
340
341 const query = {
342 distinct: true,
343 offset: start,
344 limit: count,
345 order: getFollowsSort(sort),
346 where: followWhere,
347 include: [
348 {
349 model: ActorModel,
350 required: true,
351 as: 'ActorFollower',
352 where: {
353 id
354 }
355 },
356 {
357 model: ActorModel,
358 as: 'ActorFollowing',
359 required: true,
360 where: followingWhere,
361 include: [
362 {
363 model: ServerModel,
364 required: true,
365 where: followingServerWhere
366 }
367 ]
368 }
369 ]
370 }
371
372 return ActorFollowModel.findAndCountAll<MActorFollowActorsDefault>(query)
373 .then(({ rows, count }) => {
374 return {
375 data: rows,
376 total: count
377 }
378 })
379 }
380
381 static listFollowersForApi (options: {
382 actorId: number
383 start: number
384 count: number
385 sort: string
386 state?: FollowState
387 actorType?: ActivityPubActorType
388 search?: string
389 }) {
390 const { actorId, start, count, sort, search, state, actorType } = options
391
392 const followWhere = state ? { state } : {}
393 const followerWhere: WhereOptions = {}
394 const followerServerWhere: WhereOptions = {}
395
396 if (search) {
397 Object.assign(followerServerWhere, {
398 host: {
399 [Op.iLike]: '%' + search + '%'
400 }
401 })
402 }
403
404 if (actorType) {
405 Object.assign(followerWhere, { type: actorType })
406 }
407
408 const query = {
409 distinct: true,
410 offset: start,
411 limit: count,
412 order: getFollowsSort(sort),
413 where: followWhere,
414 include: [
415 {
416 model: ActorModel,
417 required: true,
418 as: 'ActorFollower',
419 where: followerWhere,
420 include: [
421 {
422 model: ServerModel,
423 required: true,
424 where: followerServerWhere
425 }
426 ]
427 },
428 {
429 model: ActorModel,
430 as: 'ActorFollowing',
431 required: true,
432 where: {
433 id: actorId
434 }
435 }
436 ]
437 }
438
439 return ActorFollowModel.findAndCountAll<MActorFollowActorsDefault>(query)
440 .then(({ rows, count }) => {
441 return {
442 data: rows,
443 total: count
444 }
445 })
446 }
447
448 static listSubscriptionsForApi (options: {
449 actorId: number
450 start: number
451 count: number
452 sort: string
453 search?: string
454 }) {
455 const { actorId, start, count, sort } = options
456 const where = {
457 actorId: actorId
458 }
459
460 if (options.search) {
461 Object.assign(where, {
462 [Op.or]: [
463 searchAttribute(options.search, '$ActorFollowing.preferredUsername$'),
464 searchAttribute(options.search, '$ActorFollowing.VideoChannel.name$')
465 ]
466 })
467 }
468
469 const query = {
470 attributes: [],
471 distinct: true,
472 offset: start,
473 limit: count,
474 order: getSort(sort),
475 where,
476 include: [
477 {
478 attributes: [ 'id' ],
479 model: ActorModel.unscoped(),
480 as: 'ActorFollowing',
481 required: true,
482 include: [
483 {
484 model: VideoChannelModel.unscoped(),
485 required: true,
486 include: [
487 {
488 attributes: {
489 exclude: unusedActorAttributesForAPI
490 },
491 model: ActorModel,
492 required: true
493 },
494 {
495 model: AccountModel.unscoped(),
496 required: true,
497 include: [
498 {
499 attributes: {
500 exclude: unusedActorAttributesForAPI
501 },
502 model: ActorModel,
503 required: true
504 }
505 ]
506 }
507 ]
508 }
509 ]
510 }
511 ]
512 }
513
514 return ActorFollowModel.findAndCountAll<MActorFollowSubscriptions>(query)
515 .then(({ rows, count }) => {
516 return {
517 data: rows.map(r => r.ActorFollowing.VideoChannel),
518 total: count
519 }
520 })
521 }
522
523 static async keepUnfollowedInstance (hosts: string[]) {
524 const followerId = (await getServerActor()).id
525
526 const query = {
527 attributes: [ 'id' ],
528 where: {
529 actorId: followerId
530 },
531 include: [
532 {
533 attributes: [ 'id' ],
534 model: ActorModel.unscoped(),
535 required: true,
536 as: 'ActorFollowing',
537 where: {
538 preferredUsername: SERVER_ACTOR_NAME
539 },
540 include: [
541 {
542 attributes: [ 'host' ],
543 model: ServerModel.unscoped(),
544 required: true,
545 where: {
546 host: {
547 [Op.in]: hosts
548 }
549 }
550 }
551 ]
552 }
553 ]
554 }
555
556 const res = await ActorFollowModel.findAll(query)
557 const followedHosts = res.map(row => row.ActorFollowing.Server.host)
558
559 return difference(hosts, followedHosts)
560 }
561
562 static listAcceptedFollowerUrlsForAP (actorIds: number[], t: Transaction, start?: number, count?: number) {
563 return ActorFollowModel.createListAcceptedFollowForApiQuery('followers', actorIds, t, start, count)
564 }
565
566 static listAcceptedFollowerSharedInboxUrls (actorIds: number[], t: Transaction) {
567 return ActorFollowModel.createListAcceptedFollowForApiQuery(
568 'followers',
569 actorIds,
570 t,
571 undefined,
572 undefined,
573 'sharedInboxUrl',
574 true
575 )
576 }
577
578 static listAcceptedFollowingUrlsForApi (actorIds: number[], t: Transaction, start?: number, count?: number) {
579 return ActorFollowModel.createListAcceptedFollowForApiQuery('following', actorIds, t, start, count)
580 }
581
582 static async getStats () {
583 const serverActor = await getServerActor()
584
585 const totalInstanceFollowing = await ActorFollowModel.count({
586 where: {
587 actorId: serverActor.id
588 }
589 })
590
591 const totalInstanceFollowers = await ActorFollowModel.count({
592 where: {
593 targetActorId: serverActor.id
594 }
595 })
596
597 return {
598 totalInstanceFollowing,
599 totalInstanceFollowers
600 }
601 }
602
603 static updateScore (inboxUrl: string, value: number, t?: Transaction) {
604 const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` +
605 'WHERE id IN (' +
606 'SELECT "actorFollow"."id" FROM "actorFollow" ' +
607 'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."actorId" ' +
608 `WHERE "actor"."inboxUrl" = '${inboxUrl}' OR "actor"."sharedInboxUrl" = '${inboxUrl}'` +
609 ')'
610
611 const options = {
612 type: QueryTypes.BULKUPDATE,
613 transaction: t
614 }
615
616 return ActorFollowModel.sequelize.query(query, options)
617 }
618
619 static async updateScoreByFollowingServers (serverIds: number[], value: number, t?: Transaction) {
620 if (serverIds.length === 0) return
621
622 const me = await getServerActor()
623 const serverIdsString = createSafeIn(ActorFollowModel.sequelize, serverIds)
624
625 const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` +
626 'WHERE id IN (' +
627 'SELECT "actorFollow"."id" FROM "actorFollow" ' +
628 'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."targetActorId" ' +
629 `WHERE "actorFollow"."actorId" = ${me.Account.actorId} ` + // I'm the follower
630 `AND "actor"."serverId" IN (${serverIdsString})` + // Criteria on followings
631 ')'
632
633 const options = {
634 type: QueryTypes.BULKUPDATE,
635 transaction: t
636 }
637
638 return ActorFollowModel.sequelize.query(query, options)
639 }
640
641 private static async createListAcceptedFollowForApiQuery (
642 type: 'followers' | 'following',
643 actorIds: number[],
644 t: Transaction,
645 start?: number,
646 count?: number,
647 columnUrl = 'url',
648 distinct = false
649 ) {
650 let firstJoin: string
651 let secondJoin: string
652
653 if (type === 'followers') {
654 firstJoin = 'targetActorId'
655 secondJoin = 'actorId'
656 } else {
657 firstJoin = 'actorId'
658 secondJoin = 'targetActorId'
659 }
660
661 const selections: string[] = []
662 if (distinct === true) selections.push(`DISTINCT("Follows"."${columnUrl}") AS "selectionUrl"`)
663 else selections.push(`"Follows"."${columnUrl}" AS "selectionUrl"`)
664
665 selections.push('COUNT(*) AS "total"')
666
667 const tasks: Promise<any>[] = []
668
669 for (const selection of selections) {
670 let query = 'SELECT ' + selection + ' FROM "actor" ' +
671 'INNER JOIN "actorFollow" ON "actorFollow"."' + firstJoin + '" = "actor"."id" ' +
672 'INNER JOIN "actor" AS "Follows" ON "actorFollow"."' + secondJoin + '" = "Follows"."id" ' +
673 `WHERE "actor"."id" = ANY ($actorIds) AND "actorFollow"."state" = 'accepted' AND "Follows"."${columnUrl}" IS NOT NULL `
674
675 if (count !== undefined) query += 'LIMIT ' + count
676 if (start !== undefined) query += ' OFFSET ' + start
677
678 const options = {
679 bind: { actorIds },
680 type: QueryTypes.SELECT,
681 transaction: t
682 }
683 tasks.push(ActorFollowModel.sequelize.query(query, options))
684 }
685
686 const [ followers, [ dataTotal ] ] = await Promise.all(tasks)
687 const urls: string[] = followers.map(f => f.selectionUrl)
688
689 return {
690 data: urls,
691 total: dataTotal ? parseInt(dataTotal.total, 10) : 0
692 }
693 }
694
695 private static listBadActorFollows () {
696 const query = {
697 where: {
698 score: {
699 [Op.lte]: 0
700 }
701 },
702 logging: false
703 }
704
705 return ActorFollowModel.findAll(query)
706 }
707
708 toFormattedJSON (this: MActorFollowFormattable): ActorFollow {
709 const follower = this.ActorFollower.toFormattedJSON()
710 const following = this.ActorFollowing.toFormattedJSON()
711
712 return {
713 id: this.id,
714 follower,
715 following,
716 score: this.score,
717 state: this.state,
718 createdAt: this.createdAt,
719 updatedAt: this.updatedAt
720 }
721 }
722 }