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