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