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