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