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