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