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