]>
Commit | Line | Data |
---|---|---|
50d6de9c C |
1 | import * as Bluebird from 'bluebird' |
2 | import { values } from 'lodash' | |
60650c77 | 3 | import { |
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 | 20 | import { FollowState } from '../../../shared/models/actors' |
c48e82b5 | 21 | import { ActorFollow } from '../../../shared/models/actors/follow.model' |
60650c77 | 22 | import { logger } from '../../helpers/logger' |
09cababd | 23 | import { getServerActor } from '../../helpers/utils' |
76062d9f | 24 | import { ACTOR_FOLLOW_SCORE, FOLLOW_STATES } from '../../initializers/constants' |
50d6de9c | 25 | import { ServerModel } from '../server/server' |
6b9c966f | 26 | import { createSafeIn, getSort } from '../utils' |
f37dc0dd | 27 | import { ActorModel, unusedActorAttributesForAPI } from './actor' |
06a05d5f | 28 | import { VideoChannelModel } from '../video/video-channel' |
22a16e36 | 29 | import { AccountModel } from '../account/account' |
453e83ea C |
30 | import { IncludeOptions, Op, QueryTypes, Transaction } from 'sequelize' |
31 | import { | |
32 | MActorFollowActorsDefault, | |
33 | MActorFollowActorsDefaultSubscription, | |
34 | MActorFollowFollowingHost, | |
1ca9f7c3 | 35 | MActorFollowFormattable, |
453e83ea C |
36 | MActorFollowSubscriptions |
37 | } from '@server/typings/models' | |
50d6de9c C |
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 | |
60650c77 C |
51 | }, |
52 | { | |
53 | fields: [ 'score' ] | |
50d6de9c C |
54 | } |
55 | ] | |
56 | }) | |
57 | export class ActorFollowModel extends Model<ActorFollowModel> { | |
58 | ||
59 | @AllowNull(false) | |
1735c825 | 60 | @Column(DataType.ENUM(...values(FOLLOW_STATES))) |
50d6de9c C |
61 | state: FollowState |
62 | ||
60650c77 C |
63 | @AllowNull(false) |
64 | @Default(ACTOR_FOLLOW_SCORE.BASE) | |
65 | @IsInt | |
66 | @Max(ACTOR_FOLLOW_SCORE.MAX) | |
67 | @Column | |
68 | score: number | |
69 | ||
50d6de9c C |
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 | ||
32b2b43c C |
104 | @AfterCreate |
105 | @AfterUpdate | |
106 | static incrementFollowerAndFollowingCount (instance: ActorFollowModel) { | |
38768a36 | 107 | if (instance.state !== 'accepted') return undefined |
32b2b43c C |
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 | ||
44b88f18 C |
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 | ||
60650c77 C |
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 | ||
453e83ea | 153 | static loadByActorAndTarget (actorId: number, targetActorId: number, t?: Transaction): Bluebird<MActorFollowActorsDefault> { |
50d6de9c C |
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 | ||
453e83ea C |
177 | static loadByActorAndTargetNameAndHostForAPI ( |
178 | actorId: number, | |
179 | targetName: string, | |
180 | targetHost: string, | |
181 | t?: Transaction | |
182 | ): Bluebird<MActorFollowActorsDefaultSubscription> { | |
1735c825 | 183 | const actorFollowingPartInclude: IncludeOptions = { |
06a05d5f C |
184 | model: ActorModel, |
185 | required: true, | |
186 | as: 'ActorFollowing', | |
187 | where: { | |
188 | preferredUsername: targetName | |
99492dbc C |
189 | }, |
190 | include: [ | |
191 | { | |
f37dc0dd | 192 | model: VideoChannelModel.unscoped(), |
99492dbc C |
193 | required: false |
194 | } | |
195 | ] | |
06a05d5f C |
196 | } |
197 | ||
198 | if (targetHost === null) { | |
199 | actorFollowingPartInclude.where['serverId'] = null | |
200 | } else { | |
99492dbc C |
201 | actorFollowingPartInclude.include.push({ |
202 | model: ServerModel, | |
203 | required: true, | |
204 | where: { | |
205 | host: targetHost | |
206 | } | |
06a05d5f C |
207 | }) |
208 | } | |
209 | ||
50d6de9c C |
210 | const query = { |
211 | where: { | |
212 | actorId | |
213 | }, | |
214 | include: [ | |
aa55a4da C |
215 | actorFollowingPartInclude, |
216 | { | |
217 | model: ActorModel, | |
218 | required: true, | |
219 | as: 'ActorFollower' | |
220 | } | |
50d6de9c C |
221 | ], |
222 | transaction: t | |
223 | } | |
224 | ||
6502c3d4 | 225 | return ActorFollowModel.findOne(query) |
f37dc0dd C |
226 | .then(result => { |
227 | if (result && result.ActorFollowing.VideoChannel) { | |
228 | result.ActorFollowing.VideoChannel.Actor = result.ActorFollowing | |
229 | } | |
230 | ||
231 | return result | |
232 | }) | |
233 | } | |
234 | ||
453e83ea | 235 | static listSubscribedIn (actorId: number, targets: { name: string, host?: string }[]): Bluebird<MActorFollowFollowingHost[]> { |
f37dc0dd C |
236 | const whereTab = targets |
237 | .map(t => { | |
238 | if (t.host) { | |
239 | return { | |
1735c825 | 240 | [ Op.and ]: [ |
f37dc0dd C |
241 | { |
242 | '$preferredUsername$': t.name | |
243 | }, | |
244 | { | |
245 | '$host$': t.host | |
246 | } | |
247 | ] | |
248 | } | |
249 | } | |
250 | ||
251 | return { | |
1735c825 | 252 | [ Op.and ]: [ |
f37dc0dd C |
253 | { |
254 | '$preferredUsername$': t.name | |
255 | }, | |
256 | { | |
257 | '$serverId$': null | |
258 | } | |
259 | ] | |
260 | } | |
261 | }) | |
262 | ||
263 | const query = { | |
264 | attributes: [], | |
265 | where: { | |
1735c825 | 266 | [ Op.and ]: [ |
f37dc0dd | 267 | { |
1735c825 | 268 | [ Op.or ]: whereTab |
f37dc0dd C |
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) | |
6502c3d4 C |
293 | } |
294 | ||
b014b6b9 | 295 | static listFollowingForApi (id: number, start: number, count: number, sort: string, search?: string) { |
50d6de9c C |
296 | const query = { |
297 | distinct: true, | |
298 | offset: start, | |
299 | limit: count, | |
3bb6c526 | 300 | order: getSort(sort), |
50d6de9c C |
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, | |
b014b6b9 C |
314 | include: [ |
315 | { | |
316 | model: ServerModel, | |
317 | required: true, | |
318 | where: search ? { | |
319 | host: { | |
1735c825 | 320 | [Op.iLike]: '%' + search + '%' |
b014b6b9 C |
321 | } |
322 | } : undefined | |
323 | } | |
324 | ] | |
50d6de9c C |
325 | } |
326 | ] | |
327 | } | |
328 | ||
453e83ea | 329 | return ActorFollowModel.findAndCountAll<MActorFollowActorsDefault>(query) |
50d6de9c C |
330 | .then(({ rows, count }) => { |
331 | return { | |
332 | data: rows, | |
333 | total: count | |
334 | } | |
335 | }) | |
336 | } | |
337 | ||
cef534ed | 338 | static listFollowersForApi (actorId: number, start: number, count: number, sort: string, search?: string) { |
b014b6b9 C |
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: { | |
1735c825 | 355 | [ Op.iLike ]: '%' + search + '%' |
b014b6b9 C |
356 | } |
357 | } : undefined | |
358 | } | |
359 | ] | |
360 | }, | |
361 | { | |
362 | model: ActorModel, | |
363 | as: 'ActorFollowing', | |
364 | required: true, | |
365 | where: { | |
cef534ed | 366 | id: actorId |
b014b6b9 C |
367 | } |
368 | } | |
369 | ] | |
370 | } | |
371 | ||
453e83ea | 372 | return ActorFollowModel.findAndCountAll<MActorFollowActorsDefault>(query) |
b014b6b9 C |
373 | .then(({ rows, count }) => { |
374 | return { | |
375 | data: rows, | |
376 | total: count | |
377 | } | |
378 | }) | |
379 | } | |
380 | ||
cef534ed | 381 | static listSubscriptionsForApi (actorId: number, start: number, count: number, sort: string) { |
06a05d5f | 382 | const query = { |
f37dc0dd | 383 | attributes: [], |
06a05d5f C |
384 | distinct: true, |
385 | offset: start, | |
386 | limit: count, | |
387 | order: getSort(sort), | |
388 | where: { | |
cef534ed | 389 | actorId: actorId |
06a05d5f C |
390 | }, |
391 | include: [ | |
392 | { | |
f5b0af50 C |
393 | attributes: [ 'id' ], |
394 | model: ActorModel.unscoped(), | |
06a05d5f C |
395 | as: 'ActorFollowing', |
396 | required: true, | |
397 | include: [ | |
398 | { | |
f5b0af50 | 399 | model: VideoChannelModel.unscoped(), |
22a16e36 C |
400 | required: true, |
401 | include: [ | |
402 | { | |
f37dc0dd C |
403 | attributes: { |
404 | exclude: unusedActorAttributesForAPI | |
405 | }, | |
406 | model: ActorModel, | |
22a16e36 | 407 | required: true |
f37dc0dd C |
408 | }, |
409 | { | |
f5b0af50 | 410 | model: AccountModel.unscoped(), |
f37dc0dd C |
411 | required: true, |
412 | include: [ | |
413 | { | |
414 | attributes: { | |
415 | exclude: unusedActorAttributesForAPI | |
416 | }, | |
417 | model: ActorModel, | |
418 | required: true | |
419 | } | |
420 | ] | |
22a16e36 C |
421 | } |
422 | ] | |
06a05d5f C |
423 | } |
424 | ] | |
425 | } | |
426 | ] | |
427 | } | |
428 | ||
453e83ea | 429 | return ActorFollowModel.findAndCountAll<MActorFollowSubscriptions>(query) |
06a05d5f C |
430 | .then(({ rows, count }) => { |
431 | return { | |
432 | data: rows.map(r => r.ActorFollowing.VideoChannel), | |
433 | total: count | |
434 | } | |
435 | }) | |
436 | } | |
437 | ||
1735c825 | 438 | static listAcceptedFollowerUrlsForAP (actorIds: number[], t: Transaction, start?: number, count?: number) { |
50d6de9c C |
439 | return ActorFollowModel.createListAcceptedFollowForApiQuery('followers', actorIds, t, start, count) |
440 | } | |
441 | ||
1735c825 | 442 | static listAcceptedFollowerSharedInboxUrls (actorIds: number[], t: Transaction) { |
ca309a9f | 443 | return ActorFollowModel.createListAcceptedFollowForApiQuery( |
759f8a29 | 444 | 'followers', |
ca309a9f C |
445 | actorIds, |
446 | t, | |
447 | undefined, | |
448 | undefined, | |
759f8a29 C |
449 | 'sharedInboxUrl', |
450 | true | |
ca309a9f | 451 | ) |
50d6de9c C |
452 | } |
453 | ||
1735c825 | 454 | static listAcceptedFollowingUrlsForApi (actorIds: number[], t: Transaction, start?: number, count?: number) { |
50d6de9c C |
455 | return ActorFollowModel.createListAcceptedFollowForApiQuery('following', actorIds, t, start, count) |
456 | } | |
457 | ||
09cababd C |
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 | ||
6b9c966f | 479 | static updateScore (inboxUrl: string, value: number, t?: Transaction) { |
2f5c6b2f C |
480 | const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` + |
481 | 'WHERE id IN (' + | |
cef534ed C |
482 | 'SELECT "actorFollow"."id" FROM "actorFollow" ' + |
483 | 'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."actorId" ' + | |
484 | `WHERE "actor"."inboxUrl" = '${inboxUrl}' OR "actor"."sharedInboxUrl" = '${inboxUrl}'` + | |
2f5c6b2f C |
485 | ')' |
486 | ||
487 | const options = { | |
1735c825 | 488 | type: QueryTypes.BULKUPDATE, |
2f5c6b2f C |
489 | transaction: t |
490 | } | |
491 | ||
492 | return ActorFollowModel.sequelize.query(query, options) | |
493 | } | |
494 | ||
6b9c966f C |
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 | ||
327b3318 | 501 | const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` + |
6b9c966f C |
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 | ||
759f8a29 C |
517 | private static async createListAcceptedFollowForApiQuery ( |
518 | type: 'followers' | 'following', | |
519 | actorIds: number[], | |
1735c825 | 520 | t: Transaction, |
759f8a29 C |
521 | start?: number, |
522 | count?: number, | |
523 | columnUrl = 'url', | |
524 | distinct = false | |
525 | ) { | |
50d6de9c C |
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 | ||
759f8a29 C |
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 | ||
50d6de9c C |
543 | const tasks: Bluebird<any>[] = [] |
544 | ||
759f8a29 | 545 | for (let selection of selections) { |
50d6de9c C |
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 }, | |
1735c825 | 556 | type: QueryTypes.SELECT, |
50d6de9c C |
557 | transaction: t |
558 | } | |
559 | tasks.push(ActorFollowModel.sequelize.query(query, options)) | |
560 | } | |
561 | ||
babecc3c | 562 | const [ followers, [ dataTotal ] ] = await Promise.all(tasks) |
50d6de9c C |
563 | const urls: string[] = followers.map(f => f.url) |
564 | ||
565 | return { | |
566 | data: urls, | |
babecc3c | 567 | total: dataTotal ? parseInt(dataTotal.total, 10) : 0 |
50d6de9c C |
568 | } |
569 | } | |
570 | ||
60650c77 C |
571 | private static listBadActorFollows () { |
572 | const query = { | |
573 | where: { | |
574 | score: { | |
1735c825 | 575 | [Op.lte]: 0 |
60650c77 | 576 | } |
54e74059 | 577 | }, |
23e27dd5 | 578 | logging: false |
60650c77 C |
579 | } |
580 | ||
581 | return ActorFollowModel.findAll(query) | |
582 | } | |
583 | ||
1ca9f7c3 | 584 | toFormattedJSON (this: MActorFollowFormattable): ActorFollow { |
50d6de9c C |
585 | const follower = this.ActorFollower.toFormattedJSON() |
586 | const following = this.ActorFollowing.toFormattedJSON() | |
587 | ||
588 | return { | |
589 | id: this.id, | |
590 | follower, | |
591 | following, | |
60650c77 | 592 | score: this.score, |
50d6de9c C |
593 | state: this.state, |
594 | createdAt: this.createdAt, | |
595 | updatedAt: this.updatedAt | |
596 | } | |
597 | } | |
598 | } |