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