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