]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/actor/actor-follow.ts
Add ability for instances to follow any actor
[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
328 if (search) {
329 Object.assign(followWhere, {
330 [Op.or]: [
331 searchAttribute(options.search, '$ActorFollowing.preferredUsername$'),
332 searchAttribute(options.search, '$ActorFollowing.Server.host$')
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 }
366 ]
367 }
368 ]
369 }
370
371 return ActorFollowModel.findAndCountAll<MActorFollowActorsDefault>(query)
372 .then(({ rows, count }) => {
373 return {
374 data: rows,
375 total: count
376 }
377 })
378 }
379
380 static listFollowersForApi (options: {
381 actorId: number
382 start: number
383 count: number
384 sort: string
385 state?: FollowState
386 actorType?: ActivityPubActorType
387 search?: string
388 }) {
389 const { actorId, start, count, sort, search, state, actorType } = options
390
391 const followWhere = state ? { state } : {}
392 const followerWhere: WhereOptions = {}
393
394 if (search) {
395 Object.assign(followWhere, {
396 [Op.or]: [
397 searchAttribute(search, '$ActorFollower.preferredUsername$'),
398 searchAttribute(search, '$ActorFollower.Server.host$')
399 ]
400 })
401 }
402
403 if (actorType) {
404 Object.assign(followerWhere, { type: actorType })
405 }
406
407 const query = {
408 distinct: true,
409 offset: start,
410 limit: count,
411 order: getFollowsSort(sort),
412 where: followWhere,
413 include: [
414 {
415 model: ActorModel,
416 required: true,
417 as: 'ActorFollower',
418 where: followerWhere,
419 include: [
420 {
421 model: ServerModel,
422 required: true
423 }
424 ]
425 },
426 {
427 model: ActorModel,
428 as: 'ActorFollowing',
429 required: true,
430 where: {
431 id: actorId
432 }
433 }
434 ]
435 }
436
437 return ActorFollowModel.findAndCountAll<MActorFollowActorsDefault>(query)
438 .then(({ rows, count }) => {
439 return {
440 data: rows,
441 total: count
442 }
443 })
444 }
445
446 static listSubscriptionsForApi (options: {
447 actorId: number
448 start: number
449 count: number
450 sort: string
451 search?: string
452 }) {
453 const { actorId, start, count, sort } = options
454 const where = {
455 actorId: actorId
456 }
457
458 if (options.search) {
459 Object.assign(where, {
460 [Op.or]: [
461 searchAttribute(options.search, '$ActorFollowing.preferredUsername$'),
462 searchAttribute(options.search, '$ActorFollowing.VideoChannel.name$')
463 ]
464 })
465 }
466
467 const query = {
468 attributes: [],
469 distinct: true,
470 offset: start,
471 limit: count,
472 order: getSort(sort),
473 where,
474 include: [
475 {
476 attributes: [ 'id' ],
477 model: ActorModel.unscoped(),
478 as: 'ActorFollowing',
479 required: true,
480 include: [
481 {
482 model: VideoChannelModel.unscoped(),
483 required: true,
484 include: [
485 {
486 attributes: {
487 exclude: unusedActorAttributesForAPI
488 },
489 model: ActorModel,
490 required: true
491 },
492 {
493 model: AccountModel.unscoped(),
494 required: true,
495 include: [
496 {
497 attributes: {
498 exclude: unusedActorAttributesForAPI
499 },
500 model: ActorModel,
501 required: true
502 }
503 ]
504 }
505 ]
506 }
507 ]
508 }
509 ]
510 }
511
512 return ActorFollowModel.findAndCountAll<MActorFollowSubscriptions>(query)
513 .then(({ rows, count }) => {
514 return {
515 data: rows.map(r => r.ActorFollowing.VideoChannel),
516 total: count
517 }
518 })
519 }
520
521 static async keepUnfollowedInstance (hosts: string[]) {
522 const followerId = (await getServerActor()).id
523
524 const query = {
525 attributes: [ 'id' ],
526 where: {
527 actorId: followerId
528 },
529 include: [
530 {
531 attributes: [ 'id' ],
532 model: ActorModel.unscoped(),
533 required: true,
534 as: 'ActorFollowing',
535 where: {
536 preferredUsername: SERVER_ACTOR_NAME
537 },
538 include: [
539 {
540 attributes: [ 'host' ],
541 model: ServerModel.unscoped(),
542 required: true,
543 where: {
544 host: {
545 [Op.in]: hosts
546 }
547 }
548 }
549 ]
550 }
551 ]
552 }
553
554 const res = await ActorFollowModel.findAll(query)
555 const followedHosts = res.map(row => row.ActorFollowing.Server.host)
556
557 return difference(hosts, followedHosts)
558 }
559
560 static listAcceptedFollowerUrlsForAP (actorIds: number[], t: Transaction, start?: number, count?: number) {
561 return ActorFollowModel.createListAcceptedFollowForApiQuery('followers', actorIds, t, start, count)
562 }
563
564 static listAcceptedFollowerSharedInboxUrls (actorIds: number[], t: Transaction) {
565 return ActorFollowModel.createListAcceptedFollowForApiQuery(
566 'followers',
567 actorIds,
568 t,
569 undefined,
570 undefined,
571 'sharedInboxUrl',
572 true
573 )
574 }
575
576 static listAcceptedFollowingUrlsForApi (actorIds: number[], t: Transaction, start?: number, count?: number) {
577 return ActorFollowModel.createListAcceptedFollowForApiQuery('following', actorIds, t, start, count)
578 }
579
580 static async getStats () {
581 const serverActor = await getServerActor()
582
583 const totalInstanceFollowing = await ActorFollowModel.count({
584 where: {
585 actorId: serverActor.id
586 }
587 })
588
589 const totalInstanceFollowers = await ActorFollowModel.count({
590 where: {
591 targetActorId: serverActor.id
592 }
593 })
594
595 return {
596 totalInstanceFollowing,
597 totalInstanceFollowers
598 }
599 }
600
601 static updateScore (inboxUrl: string, value: number, t?: Transaction) {
602 const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` +
603 'WHERE id IN (' +
604 'SELECT "actorFollow"."id" FROM "actorFollow" ' +
605 'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."actorId" ' +
606 `WHERE "actor"."inboxUrl" = '${inboxUrl}' OR "actor"."sharedInboxUrl" = '${inboxUrl}'` +
607 ')'
608
609 const options = {
610 type: QueryTypes.BULKUPDATE,
611 transaction: t
612 }
613
614 return ActorFollowModel.sequelize.query(query, options)
615 }
616
617 static async updateScoreByFollowingServers (serverIds: number[], value: number, t?: Transaction) {
618 if (serverIds.length === 0) return
619
620 const me = await getServerActor()
621 const serverIdsString = createSafeIn(ActorFollowModel.sequelize, serverIds)
622
623 const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` +
624 'WHERE id IN (' +
625 'SELECT "actorFollow"."id" FROM "actorFollow" ' +
626 'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."targetActorId" ' +
627 `WHERE "actorFollow"."actorId" = ${me.Account.actorId} ` + // I'm the follower
628 `AND "actor"."serverId" IN (${serverIdsString})` + // Criteria on followings
629 ')'
630
631 const options = {
632 type: QueryTypes.BULKUPDATE,
633 transaction: t
634 }
635
636 return ActorFollowModel.sequelize.query(query, options)
637 }
638
639 private static async createListAcceptedFollowForApiQuery (
640 type: 'followers' | 'following',
641 actorIds: number[],
642 t: Transaction,
643 start?: number,
644 count?: number,
645 columnUrl = 'url',
646 distinct = false
647 ) {
648 let firstJoin: string
649 let secondJoin: string
650
651 if (type === 'followers') {
652 firstJoin = 'targetActorId'
653 secondJoin = 'actorId'
654 } else {
655 firstJoin = 'actorId'
656 secondJoin = 'targetActorId'
657 }
658
659 const selections: string[] = []
660 if (distinct === true) selections.push(`DISTINCT("Follows"."${columnUrl}") AS "selectionUrl"`)
661 else selections.push(`"Follows"."${columnUrl}" AS "selectionUrl"`)
662
663 selections.push('COUNT(*) AS "total"')
664
665 const tasks: Promise<any>[] = []
666
667 for (const selection of selections) {
668 let query = 'SELECT ' + selection + ' FROM "actor" ' +
669 'INNER JOIN "actorFollow" ON "actorFollow"."' + firstJoin + '" = "actor"."id" ' +
670 'INNER JOIN "actor" AS "Follows" ON "actorFollow"."' + secondJoin + '" = "Follows"."id" ' +
671 `WHERE "actor"."id" = ANY ($actorIds) AND "actorFollow"."state" = 'accepted' AND "Follows"."${columnUrl}" IS NOT NULL `
672
673 if (count !== undefined) query += 'LIMIT ' + count
674 if (start !== undefined) query += ' OFFSET ' + start
675
676 const options = {
677 bind: { actorIds },
678 type: QueryTypes.SELECT,
679 transaction: t
680 }
681 tasks.push(ActorFollowModel.sequelize.query(query, options))
682 }
683
684 const [ followers, [ dataTotal ] ] = await Promise.all(tasks)
685 const urls: string[] = followers.map(f => f.selectionUrl)
686
687 return {
688 data: urls,
689 total: dataTotal ? parseInt(dataTotal.total, 10) : 0
690 }
691 }
692
693 private static listBadActorFollows () {
694 const query = {
695 where: {
696 score: {
697 [Op.lte]: 0
698 }
699 },
700 logging: false
701 }
702
703 return ActorFollowModel.findAll(query)
704 }
705
706 toFormattedJSON (this: MActorFollowFormattable): ActorFollow {
707 const follower = this.ActorFollower.toFormattedJSON()
708 const following = this.ActorFollowing.toFormattedJSON()
709
710 return {
711 id: this.id,
712 follower,
713 following,
714 score: this.score,
715 state: this.state,
716 createdAt: this.createdAt,
717 updatedAt: this.updatedAt
718 }
719 }
720 }