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