]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/actor/actor-follow.ts
Generate random uuid for video files
[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'
23import { VideoModel } from '@server/models/video/video'
453e83ea
C
24import {
25 MActorFollowActorsDefault,
26 MActorFollowActorsDefaultSubscription,
27 MActorFollowFollowingHost,
1ca9f7c3 28 MActorFollowFormattable,
453e83ea 29 MActorFollowSubscriptions
26d6bf65 30} from '@server/types/models'
16c016e8 31import { AttributesOnly } from '@shared/core-utils'
97ecddae 32import { ActivityPubActorType } from '@shared/models'
de94ac86
C
33import { FollowState } from '../../../shared/models/actors'
34import { ActorFollow } from '../../../shared/models/actors/follow.model'
35import { logger } from '../../helpers/logger'
36import { ACTOR_FOLLOW_SCORE, CONSTRAINTS_FIELDS, FOLLOW_STATES, SERVER_ACTOR_NAME } from '../../initializers/constants'
37import { AccountModel } from '../account/account'
38import { ServerModel } from '../server/server'
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'
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
b49f22d8 179 static loadByActorAndTarget (actorId: number, targetActorId: number, t?: Transaction): Promise<MActorFollowActorsDefault> {
50d6de9c
C
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
453e83ea
C
203 static loadByActorAndTargetNameAndHostForAPI (
204 actorId: number,
205 targetName: string,
206 targetHost: string,
207 t?: Transaction
b49f22d8 208 ): Promise<MActorFollowActorsDefaultSubscription> {
1735c825 209 const actorFollowingPartInclude: IncludeOptions = {
06a05d5f
C
210 model: ActorModel,
211 required: true,
212 as: 'ActorFollowing',
213 where: {
214 preferredUsername: targetName
99492dbc
C
215 },
216 include: [
217 {
f37dc0dd 218 model: VideoChannelModel.unscoped(),
99492dbc
C
219 required: false
220 }
221 ]
06a05d5f
C
222 }
223
224 if (targetHost === null) {
225 actorFollowingPartInclude.where['serverId'] = null
226 } else {
99492dbc
C
227 actorFollowingPartInclude.include.push({
228 model: ServerModel,
229 required: true,
230 where: {
231 host: targetHost
232 }
06a05d5f
C
233 })
234 }
235
50d6de9c
C
236 const query = {
237 where: {
238 actorId
239 },
240 include: [
aa55a4da
C
241 actorFollowingPartInclude,
242 {
243 model: ActorModel,
244 required: true,
245 as: 'ActorFollower'
246 }
50d6de9c
C
247 ],
248 transaction: t
249 }
250
6502c3d4 251 return ActorFollowModel.findOne(query)
f37dc0dd
C
252 }
253
b49f22d8 254 static listSubscribedIn (actorId: number, targets: { name: string, host?: string }[]): Promise<MActorFollowFollowingHost[]> {
f37dc0dd
C
255 const whereTab = targets
256 .map(t => {
257 if (t.host) {
258 return {
a1587156 259 [Op.and]: [
f37dc0dd 260 {
a1587156 261 $preferredUsername$: t.name
f37dc0dd
C
262 },
263 {
a1587156 264 $host$: t.host
f37dc0dd
C
265 }
266 ]
267 }
268 }
269
270 return {
a1587156 271 [Op.and]: [
f37dc0dd 272 {
a1587156 273 $preferredUsername$: t.name
f37dc0dd
C
274 },
275 {
a1587156 276 $serverId$: null
f37dc0dd
C
277 }
278 ]
279 }
280 })
281
282 const query = {
b49f22d8 283 attributes: [ 'id' ],
f37dc0dd 284 where: {
a1587156 285 [Op.and]: [
f37dc0dd 286 {
a1587156 287 [Op.or]: whereTab
f37dc0dd
C
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)
6502c3d4
C
312 }
313
b8f4167f 314 static listFollowingForApi (options: {
a1587156
C
315 id: number
316 start: number
317 count: number
318 sort: string
319 state?: FollowState
320 actorType?: ActivityPubActorType
b8f4167f
C
321 search?: string
322 }) {
97ecddae 323 const { id, start, count, sort, search, state, actorType } = options
b8f4167f
C
324
325 const followWhere = state ? { state } : {}
97ecddae 326 const followingWhere: WhereOptions = {}
97ecddae
C
327
328 if (search) {
4d029ef8
C
329 Object.assign(followWhere, {
330 [Op.or]: [
331 searchAttribute(options.search, '$ActorFollowing.preferredUsername$'),
332 searchAttribute(options.search, '$ActorFollowing.Server.host$')
333 ]
97ecddae
C
334 })
335 }
336
337 if (actorType) {
338 Object.assign(followingWhere, { type: actorType })
339 }
b8f4167f 340
50d6de9c
C
341 const query = {
342 distinct: true,
343 offset: start,
344 limit: count,
cb5ce4cb 345 order: getFollowsSort(sort),
b8f4167f 346 where: followWhere,
50d6de9c
C
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,
97ecddae 360 where: followingWhere,
b014b6b9
C
361 include: [
362 {
363 model: ServerModel,
4d029ef8 364 required: true
b014b6b9
C
365 }
366 ]
50d6de9c
C
367 }
368 ]
369 }
370
453e83ea 371 return ActorFollowModel.findAndCountAll<MActorFollowActorsDefault>(query)
50d6de9c
C
372 .then(({ rows, count }) => {
373 return {
374 data: rows,
375 total: count
376 }
377 })
378 }
379
b8f4167f 380 static listFollowersForApi (options: {
a1587156
C
381 actorId: number
382 start: number
383 count: number
384 sort: string
385 state?: FollowState
386 actorType?: ActivityPubActorType
b8f4167f
C
387 search?: string
388 }) {
97ecddae 389 const { actorId, start, count, sort, search, state, actorType } = options
b8f4167f
C
390
391 const followWhere = state ? { state } : {}
97ecddae 392 const followerWhere: WhereOptions = {}
97ecddae
C
393
394 if (search) {
4d029ef8
C
395 Object.assign(followWhere, {
396 [Op.or]: [
397 searchAttribute(search, '$ActorFollower.preferredUsername$'),
398 searchAttribute(search, '$ActorFollower.Server.host$')
399 ]
97ecddae
C
400 })
401 }
402
403 if (actorType) {
404 Object.assign(followerWhere, { type: actorType })
405 }
b8f4167f 406
b014b6b9
C
407 const query = {
408 distinct: true,
409 offset: start,
410 limit: count,
cb5ce4cb 411 order: getFollowsSort(sort),
b8f4167f 412 where: followWhere,
b014b6b9
C
413 include: [
414 {
415 model: ActorModel,
416 required: true,
417 as: 'ActorFollower',
97ecddae 418 where: followerWhere,
b014b6b9
C
419 include: [
420 {
421 model: ServerModel,
4d029ef8 422 required: true
b014b6b9
C
423 }
424 ]
425 },
426 {
427 model: ActorModel,
428 as: 'ActorFollowing',
429 required: true,
430 where: {
cef534ed 431 id: actorId
b014b6b9
C
432 }
433 }
434 ]
435 }
436
453e83ea 437 return ActorFollowModel.findAndCountAll<MActorFollowActorsDefault>(query)
b014b6b9
C
438 .then(({ rows, count }) => {
439 return {
440 data: rows,
441 total: count
442 }
443 })
444 }
445
4f5d0459
RK
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
06a05d5f 467 const query = {
f37dc0dd 468 attributes: [],
06a05d5f
C
469 distinct: true,
470 offset: start,
471 limit: count,
472 order: getSort(sort),
4f5d0459 473 where,
06a05d5f
C
474 include: [
475 {
f5b0af50
C
476 attributes: [ 'id' ],
477 model: ActorModel.unscoped(),
06a05d5f
C
478 as: 'ActorFollowing',
479 required: true,
480 include: [
481 {
f5b0af50 482 model: VideoChannelModel.unscoped(),
22a16e36
C
483 required: true,
484 include: [
485 {
f37dc0dd
C
486 attributes: {
487 exclude: unusedActorAttributesForAPI
488 },
489 model: ActorModel,
22a16e36 490 required: true
f37dc0dd
C
491 },
492 {
f5b0af50 493 model: AccountModel.unscoped(),
f37dc0dd
C
494 required: true,
495 include: [
496 {
497 attributes: {
498 exclude: unusedActorAttributesForAPI
499 },
500 model: ActorModel,
501 required: true
502 }
503 ]
22a16e36
C
504 }
505 ]
06a05d5f
C
506 }
507 ]
508 }
509 ]
510 }
511
453e83ea 512 return ActorFollowModel.findAndCountAll<MActorFollowSubscriptions>(query)
06a05d5f
C
513 .then(({ rows, count }) => {
514 return {
515 data: rows.map(r => r.ActorFollowing.VideoChannel),
516 total: count
517 }
518 })
519 }
520
6f1b4fa4
C
521 static async keepUnfollowedInstance (hosts: string[]) {
522 const followerId = (await getServerActor()).id
523
524 const query = {
10a105f0 525 attributes: [ 'id' ],
6f1b4fa4
C
526 where: {
527 actorId: followerId
528 },
529 include: [
530 {
10a105f0 531 attributes: [ 'id' ],
6f1b4fa4
C
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)
10a105f0 555 const followedHosts = res.map(row => row.ActorFollowing.Server.host)
6f1b4fa4
C
556
557 return difference(hosts, followedHosts)
558 }
559
1735c825 560 static listAcceptedFollowerUrlsForAP (actorIds: number[], t: Transaction, start?: number, count?: number) {
50d6de9c
C
561 return ActorFollowModel.createListAcceptedFollowForApiQuery('followers', actorIds, t, start, count)
562 }
563
1735c825 564 static listAcceptedFollowerSharedInboxUrls (actorIds: number[], t: Transaction) {
ca309a9f 565 return ActorFollowModel.createListAcceptedFollowForApiQuery(
759f8a29 566 'followers',
ca309a9f
C
567 actorIds,
568 t,
569 undefined,
570 undefined,
759f8a29
C
571 'sharedInboxUrl',
572 true
ca309a9f 573 )
50d6de9c
C
574 }
575
1735c825 576 static listAcceptedFollowingUrlsForApi (actorIds: number[], t: Transaction, start?: number, count?: number) {
50d6de9c
C
577 return ActorFollowModel.createListAcceptedFollowForApiQuery('following', actorIds, t, start, count)
578 }
579
09cababd
C
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
6b9c966f 601 static updateScore (inboxUrl: string, value: number, t?: Transaction) {
2f5c6b2f
C
602 const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` +
603 'WHERE id IN (' +
cef534ed
C
604 'SELECT "actorFollow"."id" FROM "actorFollow" ' +
605 'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."actorId" ' +
606 `WHERE "actor"."inboxUrl" = '${inboxUrl}' OR "actor"."sharedInboxUrl" = '${inboxUrl}'` +
2f5c6b2f
C
607 ')'
608
609 const options = {
1735c825 610 type: QueryTypes.BULKUPDATE,
2f5c6b2f
C
611 transaction: t
612 }
613
614 return ActorFollowModel.sequelize.query(query, options)
615 }
616
6b9c966f
C
617 static async updateScoreByFollowingServers (serverIds: number[], value: number, t?: Transaction) {
618 if (serverIds.length === 0) return
619
620 const me = await getServerActor()
16c016e8 621 const serverIdsString = createSafeIn(ActorFollowModel.sequelize, serverIds)
6b9c966f 622
327b3318 623 const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` +
6b9c966f
C
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
759f8a29
C
639 private static async createListAcceptedFollowForApiQuery (
640 type: 'followers' | 'following',
641 actorIds: number[],
1735c825 642 t: Transaction,
759f8a29
C
643 start?: number,
644 count?: number,
645 columnUrl = 'url',
646 distinct = false
647 ) {
50d6de9c
C
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
759f8a29 659 const selections: string[] = []
862ead21
C
660 if (distinct === true) selections.push(`DISTINCT("Follows"."${columnUrl}") AS "selectionUrl"`)
661 else selections.push(`"Follows"."${columnUrl}" AS "selectionUrl"`)
759f8a29
C
662
663 selections.push('COUNT(*) AS "total"')
664
b49f22d8 665 const tasks: Promise<any>[] = []
50d6de9c 666
a1587156 667 for (const selection of selections) {
50d6de9c
C
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" ' +
862ead21 671 `WHERE "actor"."id" = ANY ($actorIds) AND "actorFollow"."state" = 'accepted' AND "Follows"."${columnUrl}" IS NOT NULL `
50d6de9c
C
672
673 if (count !== undefined) query += 'LIMIT ' + count
674 if (start !== undefined) query += ' OFFSET ' + start
675
676 const options = {
677 bind: { actorIds },
1735c825 678 type: QueryTypes.SELECT,
50d6de9c
C
679 transaction: t
680 }
681 tasks.push(ActorFollowModel.sequelize.query(query, options))
682 }
683
babecc3c 684 const [ followers, [ dataTotal ] ] = await Promise.all(tasks)
47581df0 685 const urls: string[] = followers.map(f => f.selectionUrl)
50d6de9c
C
686
687 return {
688 data: urls,
babecc3c 689 total: dataTotal ? parseInt(dataTotal.total, 10) : 0
50d6de9c
C
690 }
691 }
692
60650c77
C
693 private static listBadActorFollows () {
694 const query = {
695 where: {
696 score: {
1735c825 697 [Op.lte]: 0
60650c77 698 }
54e74059 699 },
23e27dd5 700 logging: false
60650c77
C
701 }
702
703 return ActorFollowModel.findAll(query)
704 }
705
1ca9f7c3 706 toFormattedJSON (this: MActorFollowFormattable): ActorFollow {
50d6de9c
C
707 const follower = this.ActorFollower.toFormattedJSON()
708 const following = this.ActorFollowing.toFormattedJSON()
709
710 return {
711 id: this.id,
712 follower,
713 following,
60650c77 714 score: this.score,
50d6de9c
C
715 state: this.state,
716 createdAt: this.createdAt,
717 updatedAt: this.updatedAt
718 }
719 }
720}