]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/actor/actor-follow.ts
Filter host for channels and playlists search
[github/Chocobozzz/PeerTube.git] / server / models / actor / actor-follow.ts
CommitLineData
a1587156 1import { difference, values } from 'lodash'
de94ac86 2import { IncludeOptions, Op, QueryTypes, Transaction, WhereOptions } from 'sequelize'
60650c77 3import {
06a05d5f
C
4 AfterCreate,
5 AfterDestroy,
6 AfterUpdate,
7 AllowNull,
8 BelongsTo,
9 Column,
10 CreatedAt,
11 DataType,
12 Default,
13 ForeignKey,
de94ac86 14 Is,
06a05d5f
C
15 IsInt,
16 Max,
17 Model,
18 Table,
225a7682 19 UpdatedAt
60650c77 20} from 'sequelize-typescript'
de94ac86
C
21import { isActivityPubUrlValid } from '@server/helpers/custom-validators/activitypub/misc'
22import { getServerActor } from '@server/models/application/application'
453e83ea
C
23import {
24 MActorFollowActorsDefault,
25 MActorFollowActorsDefaultSubscription,
26 MActorFollowFollowingHost,
1ca9f7c3 27 MActorFollowFormattable,
453e83ea 28 MActorFollowSubscriptions
26d6bf65 29} from '@server/types/models'
16c016e8 30import { AttributesOnly } from '@shared/core-utils'
97ecddae 31import { ActivityPubActorType } from '@shared/models'
de94ac86
C
32import { FollowState } from '../../../shared/models/actors'
33import { ActorFollow } from '../../../shared/models/actors/follow.model'
34import { logger } from '../../helpers/logger'
35import { ACTOR_FOLLOW_SCORE, CONSTRAINTS_FIELDS, FOLLOW_STATES, SERVER_ACTOR_NAME } from '../../initializers/constants'
36import { AccountModel } from '../account/account'
37import { ServerModel } from '../server/server'
fa47956e 38import { doesExist } from '../shared/query'
de94ac86
C
39import { createSafeIn, getFollowsSort, getSort, searchAttribute, throwIfNotValid } from '../utils'
40import { VideoChannelModel } from '../video/video-channel'
41import { ActorModel, unusedActorAttributesForAPI } from './actor'
50d6de9c
C
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
60650c77
C
55 },
56 {
57 fields: [ 'score' ]
de94ac86
C
58 },
59 {
60 fields: [ 'url' ],
61 unique: true
50d6de9c
C
62 }
63 ]
64})
16c016e8 65export class ActorFollowModel extends Model<Partial<AttributesOnly<ActorFollowModel>>> {
50d6de9c
C
66
67 @AllowNull(false)
1735c825 68 @Column(DataType.ENUM(...values(FOLLOW_STATES)))
50d6de9c
C
69 state: FollowState
70
60650c77
C
71 @AllowNull(false)
72 @Default(ACTOR_FOLLOW_SCORE.BASE)
73 @IsInt
74 @Max(ACTOR_FOLLOW_SCORE.MAX)
75 @Column
76 score: number
77
de94ac86
C
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
50d6de9c
C
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
32b2b43c
C
118 @AfterCreate
119 @AfterUpdate
e6122097 120 static incrementFollowerAndFollowingCount (instance: ActorFollowModel, options: any) {
38768a36 121 if (instance.state !== 'accepted') return undefined
32b2b43c
C
122
123 return Promise.all([
e6122097
C
124 ActorModel.rebuildFollowsCount(instance.actorId, 'following', options.transaction),
125 ActorModel.rebuildFollowsCount(instance.targetActorId, 'followers', options.transaction)
32b2b43c
C
126 ])
127 }
128
129 @AfterDestroy
e6122097 130 static decrementFollowerAndFollowingCount (instance: ActorFollowModel, options: any) {
32b2b43c 131 return Promise.all([
e6122097
C
132 ActorModel.rebuildFollowsCount(instance.actorId, 'following', options.transaction),
133 ActorModel.rebuildFollowsCount(instance.targetActorId, 'followers', options.transaction)
32b2b43c
C
134 ])
135 }
136
44b88f18
C
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
60650c77
C
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
8c9e7875
C
167 static isFollowedBy (actorId: number, followerActorId: number) {
168 const query = 'SELECT 1 FROM "actorFollow" WHERE "actorId" = $followerActorId AND "targetActorId" = $actorId LIMIT 1'
8c9e7875 169
764b1a14 170 return doesExist(query, { actorId, followerActorId })
8c9e7875
C
171 }
172
b49f22d8 173 static loadByActorAndTarget (actorId: number, targetActorId: number, t?: Transaction): Promise<MActorFollowActorsDefault> {
50d6de9c
C
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
453e83ea
C
197 static loadByActorAndTargetNameAndHostForAPI (
198 actorId: number,
199 targetName: string,
200 targetHost: string,
201 t?: Transaction
b49f22d8 202 ): Promise<MActorFollowActorsDefaultSubscription> {
1735c825 203 const actorFollowingPartInclude: IncludeOptions = {
06a05d5f
C
204 model: ActorModel,
205 required: true,
206 as: 'ActorFollowing',
207 where: {
208 preferredUsername: targetName
99492dbc
C
209 },
210 include: [
211 {
f37dc0dd 212 model: VideoChannelModel.unscoped(),
99492dbc
C
213 required: false
214 }
215 ]
06a05d5f
C
216 }
217
218 if (targetHost === null) {
219 actorFollowingPartInclude.where['serverId'] = null
220 } else {
99492dbc
C
221 actorFollowingPartInclude.include.push({
222 model: ServerModel,
223 required: true,
224 where: {
225 host: targetHost
226 }
06a05d5f
C
227 })
228 }
229
50d6de9c
C
230 const query = {
231 where: {
232 actorId
233 },
234 include: [
aa55a4da
C
235 actorFollowingPartInclude,
236 {
237 model: ActorModel,
238 required: true,
239 as: 'ActorFollower'
240 }
50d6de9c
C
241 ],
242 transaction: t
243 }
244
6502c3d4 245 return ActorFollowModel.findOne(query)
f37dc0dd
C
246 }
247
b49f22d8 248 static listSubscribedIn (actorId: number, targets: { name: string, host?: string }[]): Promise<MActorFollowFollowingHost[]> {
f37dc0dd
C
249 const whereTab = targets
250 .map(t => {
251 if (t.host) {
252 return {
a1587156 253 [Op.and]: [
f37dc0dd 254 {
a1587156 255 $preferredUsername$: t.name
f37dc0dd
C
256 },
257 {
a1587156 258 $host$: t.host
f37dc0dd
C
259 }
260 ]
261 }
262 }
263
264 return {
a1587156 265 [Op.and]: [
f37dc0dd 266 {
a1587156 267 $preferredUsername$: t.name
f37dc0dd
C
268 },
269 {
a1587156 270 $serverId$: null
f37dc0dd
C
271 }
272 ]
273 }
274 })
275
276 const query = {
b49f22d8 277 attributes: [ 'id' ],
f37dc0dd 278 where: {
a1587156 279 [Op.and]: [
f37dc0dd 280 {
a1587156 281 [Op.or]: whereTab
f37dc0dd
C
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)
6502c3d4
C
306 }
307
b8f4167f 308 static listFollowingForApi (options: {
a1587156
C
309 id: number
310 start: number
311 count: number
312 sort: string
313 state?: FollowState
314 actorType?: ActivityPubActorType
b8f4167f
C
315 search?: string
316 }) {
97ecddae 317 const { id, start, count, sort, search, state, actorType } = options
b8f4167f
C
318
319 const followWhere = state ? { state } : {}
97ecddae 320 const followingWhere: WhereOptions = {}
97ecddae
C
321
322 if (search) {
4d029ef8
C
323 Object.assign(followWhere, {
324 [Op.or]: [
325 searchAttribute(options.search, '$ActorFollowing.preferredUsername$'),
326 searchAttribute(options.search, '$ActorFollowing.Server.host$')
327 ]
97ecddae
C
328 })
329 }
330
331 if (actorType) {
332 Object.assign(followingWhere, { type: actorType })
333 }
b8f4167f 334
50d6de9c
C
335 const query = {
336 distinct: true,
337 offset: start,
338 limit: count,
cb5ce4cb 339 order: getFollowsSort(sort),
b8f4167f 340 where: followWhere,
50d6de9c
C
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,
97ecddae 354 where: followingWhere,
b014b6b9
C
355 include: [
356 {
357 model: ServerModel,
4d029ef8 358 required: true
b014b6b9
C
359 }
360 ]
50d6de9c
C
361 }
362 ]
363 }
364
453e83ea 365 return ActorFollowModel.findAndCountAll<MActorFollowActorsDefault>(query)
50d6de9c
C
366 .then(({ rows, count }) => {
367 return {
368 data: rows,
369 total: count
370 }
371 })
372 }
373
b8f4167f 374 static listFollowersForApi (options: {
a1587156
C
375 actorId: number
376 start: number
377 count: number
378 sort: string
379 state?: FollowState
380 actorType?: ActivityPubActorType
b8f4167f
C
381 search?: string
382 }) {
97ecddae 383 const { actorId, start, count, sort, search, state, actorType } = options
b8f4167f
C
384
385 const followWhere = state ? { state } : {}
97ecddae 386 const followerWhere: WhereOptions = {}
97ecddae
C
387
388 if (search) {
4d029ef8
C
389 Object.assign(followWhere, {
390 [Op.or]: [
391 searchAttribute(search, '$ActorFollower.preferredUsername$'),
392 searchAttribute(search, '$ActorFollower.Server.host$')
393 ]
97ecddae
C
394 })
395 }
396
397 if (actorType) {
398 Object.assign(followerWhere, { type: actorType })
399 }
b8f4167f 400
b014b6b9
C
401 const query = {
402 distinct: true,
403 offset: start,
404 limit: count,
cb5ce4cb 405 order: getFollowsSort(sort),
b8f4167f 406 where: followWhere,
b014b6b9
C
407 include: [
408 {
409 model: ActorModel,
410 required: true,
411 as: 'ActorFollower',
97ecddae 412 where: followerWhere,
b014b6b9
C
413 include: [
414 {
415 model: ServerModel,
4d029ef8 416 required: true
b014b6b9
C
417 }
418 ]
419 },
420 {
421 model: ActorModel,
422 as: 'ActorFollowing',
423 required: true,
424 where: {
cef534ed 425 id: actorId
b014b6b9
C
426 }
427 }
428 ]
429 }
430
453e83ea 431 return ActorFollowModel.findAndCountAll<MActorFollowActorsDefault>(query)
b014b6b9
C
432 .then(({ rows, count }) => {
433 return {
434 data: rows,
435 total: count
436 }
437 })
438 }
439
4f5d0459
RK
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
06a05d5f 461 const query = {
f37dc0dd 462 attributes: [],
06a05d5f
C
463 distinct: true,
464 offset: start,
465 limit: count,
466 order: getSort(sort),
4f5d0459 467 where,
06a05d5f
C
468 include: [
469 {
f5b0af50
C
470 attributes: [ 'id' ],
471 model: ActorModel.unscoped(),
06a05d5f
C
472 as: 'ActorFollowing',
473 required: true,
474 include: [
475 {
f5b0af50 476 model: VideoChannelModel.unscoped(),
22a16e36
C
477 required: true,
478 include: [
479 {
f37dc0dd
C
480 attributes: {
481 exclude: unusedActorAttributesForAPI
482 },
483 model: ActorModel,
22a16e36 484 required: true
f37dc0dd
C
485 },
486 {
f5b0af50 487 model: AccountModel.unscoped(),
f37dc0dd
C
488 required: true,
489 include: [
490 {
491 attributes: {
492 exclude: unusedActorAttributesForAPI
493 },
494 model: ActorModel,
495 required: true
496 }
497 ]
22a16e36
C
498 }
499 ]
06a05d5f
C
500 }
501 ]
502 }
503 ]
504 }
505
453e83ea 506 return ActorFollowModel.findAndCountAll<MActorFollowSubscriptions>(query)
06a05d5f
C
507 .then(({ rows, count }) => {
508 return {
509 data: rows.map(r => r.ActorFollowing.VideoChannel),
510 total: count
511 }
512 })
513 }
514
6f1b4fa4
C
515 static async keepUnfollowedInstance (hosts: string[]) {
516 const followerId = (await getServerActor()).id
517
518 const query = {
10a105f0 519 attributes: [ 'id' ],
6f1b4fa4
C
520 where: {
521 actorId: followerId
522 },
523 include: [
524 {
10a105f0 525 attributes: [ 'id' ],
6f1b4fa4
C
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)
10a105f0 549 const followedHosts = res.map(row => row.ActorFollowing.Server.host)
6f1b4fa4
C
550
551 return difference(hosts, followedHosts)
552 }
553
1735c825 554 static listAcceptedFollowerUrlsForAP (actorIds: number[], t: Transaction, start?: number, count?: number) {
50d6de9c
C
555 return ActorFollowModel.createListAcceptedFollowForApiQuery('followers', actorIds, t, start, count)
556 }
557
1735c825 558 static listAcceptedFollowerSharedInboxUrls (actorIds: number[], t: Transaction) {
ca309a9f 559 return ActorFollowModel.createListAcceptedFollowForApiQuery(
759f8a29 560 'followers',
ca309a9f
C
561 actorIds,
562 t,
563 undefined,
564 undefined,
759f8a29
C
565 'sharedInboxUrl',
566 true
ca309a9f 567 )
50d6de9c
C
568 }
569
1735c825 570 static listAcceptedFollowingUrlsForApi (actorIds: number[], t: Transaction, start?: number, count?: number) {
50d6de9c
C
571 return ActorFollowModel.createListAcceptedFollowForApiQuery('following', actorIds, t, start, count)
572 }
573
09cababd
C
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
6b9c966f 595 static updateScore (inboxUrl: string, value: number, t?: Transaction) {
2f5c6b2f
C
596 const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` +
597 'WHERE id IN (' +
cef534ed
C
598 'SELECT "actorFollow"."id" FROM "actorFollow" ' +
599 'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."actorId" ' +
600 `WHERE "actor"."inboxUrl" = '${inboxUrl}' OR "actor"."sharedInboxUrl" = '${inboxUrl}'` +
2f5c6b2f
C
601 ')'
602
603 const options = {
1735c825 604 type: QueryTypes.BULKUPDATE,
2f5c6b2f
C
605 transaction: t
606 }
607
608 return ActorFollowModel.sequelize.query(query, options)
609 }
610
6b9c966f
C
611 static async updateScoreByFollowingServers (serverIds: number[], value: number, t?: Transaction) {
612 if (serverIds.length === 0) return
613
614 const me = await getServerActor()
16c016e8 615 const serverIdsString = createSafeIn(ActorFollowModel.sequelize, serverIds)
6b9c966f 616
327b3318 617 const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` +
6b9c966f
C
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
759f8a29
C
633 private static async createListAcceptedFollowForApiQuery (
634 type: 'followers' | 'following',
635 actorIds: number[],
1735c825 636 t: Transaction,
759f8a29
C
637 start?: number,
638 count?: number,
639 columnUrl = 'url',
640 distinct = false
641 ) {
50d6de9c
C
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
759f8a29 653 const selections: string[] = []
862ead21
C
654 if (distinct === true) selections.push(`DISTINCT("Follows"."${columnUrl}") AS "selectionUrl"`)
655 else selections.push(`"Follows"."${columnUrl}" AS "selectionUrl"`)
759f8a29
C
656
657 selections.push('COUNT(*) AS "total"')
658
b49f22d8 659 const tasks: Promise<any>[] = []
50d6de9c 660
a1587156 661 for (const selection of selections) {
50d6de9c
C
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" ' +
862ead21 665 `WHERE "actor"."id" = ANY ($actorIds) AND "actorFollow"."state" = 'accepted' AND "Follows"."${columnUrl}" IS NOT NULL `
50d6de9c
C
666
667 if (count !== undefined) query += 'LIMIT ' + count
668 if (start !== undefined) query += ' OFFSET ' + start
669
670 const options = {
671 bind: { actorIds },
1735c825 672 type: QueryTypes.SELECT,
50d6de9c
C
673 transaction: t
674 }
675 tasks.push(ActorFollowModel.sequelize.query(query, options))
676 }
677
babecc3c 678 const [ followers, [ dataTotal ] ] = await Promise.all(tasks)
47581df0 679 const urls: string[] = followers.map(f => f.selectionUrl)
50d6de9c
C
680
681 return {
682 data: urls,
babecc3c 683 total: dataTotal ? parseInt(dataTotal.total, 10) : 0
50d6de9c
C
684 }
685 }
686
60650c77
C
687 private static listBadActorFollows () {
688 const query = {
689 where: {
690 score: {
1735c825 691 [Op.lte]: 0
60650c77 692 }
54e74059 693 },
23e27dd5 694 logging: false
60650c77
C
695 }
696
697 return ActorFollowModel.findAll(query)
698 }
699
1ca9f7c3 700 toFormattedJSON (this: MActorFollowFormattable): ActorFollow {
50d6de9c
C
701 const follower = this.ActorFollower.toFormattedJSON()
702 const following = this.ActorFollowing.toFormattedJSON()
703
704 return {
705 id: this.id,
706 follower,
707 following,
60650c77 708 score: this.score,
50d6de9c
C
709 state: this.state,
710 createdAt: this.createdAt,
711 updatedAt: this.updatedAt
712 }
713 }
714}