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