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