]>
Commit | Line | Data |
---|---|---|
50d6de9c | 1 | import * as Bluebird from 'bluebird' |
a1587156 | 2 | import { difference, values } from 'lodash' |
de94ac86 | 3 | import { IncludeOptions, Op, QueryTypes, Transaction, WhereOptions } from 'sequelize' |
60650c77 | 4 | import { |
06a05d5f C |
5 | AfterCreate, |
6 | AfterDestroy, | |
7 | AfterUpdate, | |
8 | AllowNull, | |
9 | BelongsTo, | |
10 | Column, | |
11 | CreatedAt, | |
12 | DataType, | |
13 | Default, | |
14 | ForeignKey, | |
de94ac86 | 15 | Is, |
06a05d5f C |
16 | IsInt, |
17 | Max, | |
18 | Model, | |
19 | Table, | |
225a7682 | 20 | UpdatedAt |
60650c77 | 21 | } from 'sequelize-typescript' |
de94ac86 C |
22 | import { isActivityPubUrlValid } from '@server/helpers/custom-validators/activitypub/misc' |
23 | import { getServerActor } from '@server/models/application/application' | |
24 | import { VideoModel } from '@server/models/video/video' | |
453e83ea C |
25 | import { |
26 | MActorFollowActorsDefault, | |
27 | MActorFollowActorsDefaultSubscription, | |
28 | MActorFollowFollowingHost, | |
1ca9f7c3 | 29 | MActorFollowFormattable, |
453e83ea | 30 | MActorFollowSubscriptions |
26d6bf65 | 31 | } from '@server/types/models' |
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 | }) | |
65 | export class ActorFollowModel extends Model<ActorFollowModel> { | |
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 | ||
453e83ea | 179 | static loadByActorAndTarget (actorId: number, targetActorId: number, t?: Transaction): Bluebird<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 | |
208 | ): Bluebird<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 | 252 | .then(result => { |
a1587156 | 253 | if (result?.ActorFollowing.VideoChannel) { |
f37dc0dd C |
254 | result.ActorFollowing.VideoChannel.Actor = result.ActorFollowing |
255 | } | |
256 | ||
257 | return result | |
258 | }) | |
259 | } | |
260 | ||
453e83ea | 261 | static listSubscribedIn (actorId: number, targets: { name: string, host?: string }[]): Bluebird<MActorFollowFollowingHost[]> { |
f37dc0dd C |
262 | const whereTab = targets |
263 | .map(t => { | |
264 | if (t.host) { | |
265 | return { | |
a1587156 | 266 | [Op.and]: [ |
f37dc0dd | 267 | { |
a1587156 | 268 | $preferredUsername$: t.name |
f37dc0dd C |
269 | }, |
270 | { | |
a1587156 | 271 | $host$: t.host |
f37dc0dd C |
272 | } |
273 | ] | |
274 | } | |
275 | } | |
276 | ||
277 | return { | |
a1587156 | 278 | [Op.and]: [ |
f37dc0dd | 279 | { |
a1587156 | 280 | $preferredUsername$: t.name |
f37dc0dd C |
281 | }, |
282 | { | |
a1587156 | 283 | $serverId$: null |
f37dc0dd C |
284 | } |
285 | ] | |
286 | } | |
287 | }) | |
288 | ||
289 | const query = { | |
290 | attributes: [], | |
291 | where: { | |
a1587156 | 292 | [Op.and]: [ |
f37dc0dd | 293 | { |
a1587156 | 294 | [Op.or]: whereTab |
f37dc0dd C |
295 | }, |
296 | { | |
297 | actorId | |
298 | } | |
299 | ] | |
300 | }, | |
301 | include: [ | |
302 | { | |
303 | attributes: [ 'preferredUsername' ], | |
304 | model: ActorModel.unscoped(), | |
305 | required: true, | |
306 | as: 'ActorFollowing', | |
307 | include: [ | |
308 | { | |
309 | attributes: [ 'host' ], | |
310 | model: ServerModel.unscoped(), | |
311 | required: false | |
312 | } | |
313 | ] | |
314 | } | |
315 | ] | |
316 | } | |
317 | ||
318 | return ActorFollowModel.findAll(query) | |
6502c3d4 C |
319 | } |
320 | ||
b8f4167f | 321 | static listFollowingForApi (options: { |
a1587156 C |
322 | id: number |
323 | start: number | |
324 | count: number | |
325 | sort: string | |
326 | state?: FollowState | |
327 | actorType?: ActivityPubActorType | |
b8f4167f C |
328 | search?: string |
329 | }) { | |
97ecddae | 330 | const { id, start, count, sort, search, state, actorType } = options |
b8f4167f C |
331 | |
332 | const followWhere = state ? { state } : {} | |
97ecddae C |
333 | const followingWhere: WhereOptions = {} |
334 | const followingServerWhere: WhereOptions = {} | |
335 | ||
336 | if (search) { | |
337 | Object.assign(followingServerWhere, { | |
338 | host: { | |
a1587156 | 339 | [Op.iLike]: '%' + search + '%' |
97ecddae C |
340 | } |
341 | }) | |
342 | } | |
343 | ||
344 | if (actorType) { | |
345 | Object.assign(followingWhere, { type: actorType }) | |
346 | } | |
b8f4167f | 347 | |
50d6de9c C |
348 | const query = { |
349 | distinct: true, | |
350 | offset: start, | |
351 | limit: count, | |
cb5ce4cb | 352 | order: getFollowsSort(sort), |
b8f4167f | 353 | where: followWhere, |
50d6de9c C |
354 | include: [ |
355 | { | |
356 | model: ActorModel, | |
357 | required: true, | |
358 | as: 'ActorFollower', | |
359 | where: { | |
360 | id | |
361 | } | |
362 | }, | |
363 | { | |
364 | model: ActorModel, | |
365 | as: 'ActorFollowing', | |
366 | required: true, | |
97ecddae | 367 | where: followingWhere, |
b014b6b9 C |
368 | include: [ |
369 | { | |
370 | model: ServerModel, | |
371 | required: true, | |
97ecddae | 372 | where: followingServerWhere |
b014b6b9 C |
373 | } |
374 | ] | |
50d6de9c C |
375 | } |
376 | ] | |
377 | } | |
378 | ||
453e83ea | 379 | return ActorFollowModel.findAndCountAll<MActorFollowActorsDefault>(query) |
50d6de9c C |
380 | .then(({ rows, count }) => { |
381 | return { | |
382 | data: rows, | |
383 | total: count | |
384 | } | |
385 | }) | |
386 | } | |
387 | ||
b8f4167f | 388 | static listFollowersForApi (options: { |
a1587156 C |
389 | actorId: number |
390 | start: number | |
391 | count: number | |
392 | sort: string | |
393 | state?: FollowState | |
394 | actorType?: ActivityPubActorType | |
b8f4167f C |
395 | search?: string |
396 | }) { | |
97ecddae | 397 | const { actorId, start, count, sort, search, state, actorType } = options |
b8f4167f C |
398 | |
399 | const followWhere = state ? { state } : {} | |
97ecddae C |
400 | const followerWhere: WhereOptions = {} |
401 | const followerServerWhere: WhereOptions = {} | |
402 | ||
403 | if (search) { | |
404 | Object.assign(followerServerWhere, { | |
405 | host: { | |
a1587156 | 406 | [Op.iLike]: '%' + search + '%' |
97ecddae C |
407 | } |
408 | }) | |
409 | } | |
410 | ||
411 | if (actorType) { | |
412 | Object.assign(followerWhere, { type: actorType }) | |
413 | } | |
b8f4167f | 414 | |
b014b6b9 C |
415 | const query = { |
416 | distinct: true, | |
417 | offset: start, | |
418 | limit: count, | |
cb5ce4cb | 419 | order: getFollowsSort(sort), |
b8f4167f | 420 | where: followWhere, |
b014b6b9 C |
421 | include: [ |
422 | { | |
423 | model: ActorModel, | |
424 | required: true, | |
425 | as: 'ActorFollower', | |
97ecddae | 426 | where: followerWhere, |
b014b6b9 C |
427 | include: [ |
428 | { | |
429 | model: ServerModel, | |
430 | required: true, | |
97ecddae | 431 | where: followerServerWhere |
b014b6b9 C |
432 | } |
433 | ] | |
434 | }, | |
435 | { | |
436 | model: ActorModel, | |
437 | as: 'ActorFollowing', | |
438 | required: true, | |
439 | where: { | |
cef534ed | 440 | id: actorId |
b014b6b9 C |
441 | } |
442 | } | |
443 | ] | |
444 | } | |
445 | ||
453e83ea | 446 | return ActorFollowModel.findAndCountAll<MActorFollowActorsDefault>(query) |
b014b6b9 C |
447 | .then(({ rows, count }) => { |
448 | return { | |
449 | data: rows, | |
450 | total: count | |
451 | } | |
452 | }) | |
453 | } | |
454 | ||
4f5d0459 RK |
455 | static listSubscriptionsForApi (options: { |
456 | actorId: number | |
457 | start: number | |
458 | count: number | |
459 | sort: string | |
460 | search?: string | |
461 | }) { | |
462 | const { actorId, start, count, sort } = options | |
463 | const where = { | |
464 | actorId: actorId | |
465 | } | |
466 | ||
467 | if (options.search) { | |
468 | Object.assign(where, { | |
469 | [Op.or]: [ | |
470 | searchAttribute(options.search, '$ActorFollowing.preferredUsername$'), | |
471 | searchAttribute(options.search, '$ActorFollowing.VideoChannel.name$') | |
472 | ] | |
473 | }) | |
474 | } | |
475 | ||
06a05d5f | 476 | const query = { |
f37dc0dd | 477 | attributes: [], |
06a05d5f C |
478 | distinct: true, |
479 | offset: start, | |
480 | limit: count, | |
481 | order: getSort(sort), | |
4f5d0459 | 482 | where, |
06a05d5f C |
483 | include: [ |
484 | { | |
f5b0af50 C |
485 | attributes: [ 'id' ], |
486 | model: ActorModel.unscoped(), | |
06a05d5f C |
487 | as: 'ActorFollowing', |
488 | required: true, | |
489 | include: [ | |
490 | { | |
f5b0af50 | 491 | model: VideoChannelModel.unscoped(), |
22a16e36 C |
492 | required: true, |
493 | include: [ | |
494 | { | |
f37dc0dd C |
495 | attributes: { |
496 | exclude: unusedActorAttributesForAPI | |
497 | }, | |
498 | model: ActorModel, | |
22a16e36 | 499 | required: true |
f37dc0dd C |
500 | }, |
501 | { | |
f5b0af50 | 502 | model: AccountModel.unscoped(), |
f37dc0dd C |
503 | required: true, |
504 | include: [ | |
505 | { | |
506 | attributes: { | |
507 | exclude: unusedActorAttributesForAPI | |
508 | }, | |
509 | model: ActorModel, | |
510 | required: true | |
511 | } | |
512 | ] | |
22a16e36 C |
513 | } |
514 | ] | |
06a05d5f C |
515 | } |
516 | ] | |
517 | } | |
518 | ] | |
519 | } | |
520 | ||
453e83ea | 521 | return ActorFollowModel.findAndCountAll<MActorFollowSubscriptions>(query) |
06a05d5f C |
522 | .then(({ rows, count }) => { |
523 | return { | |
524 | data: rows.map(r => r.ActorFollowing.VideoChannel), | |
525 | total: count | |
526 | } | |
527 | }) | |
528 | } | |
529 | ||
6f1b4fa4 C |
530 | static async keepUnfollowedInstance (hosts: string[]) { |
531 | const followerId = (await getServerActor()).id | |
532 | ||
533 | const query = { | |
10a105f0 | 534 | attributes: [ 'id' ], |
6f1b4fa4 C |
535 | where: { |
536 | actorId: followerId | |
537 | }, | |
538 | include: [ | |
539 | { | |
10a105f0 | 540 | attributes: [ 'id' ], |
6f1b4fa4 C |
541 | model: ActorModel.unscoped(), |
542 | required: true, | |
543 | as: 'ActorFollowing', | |
544 | where: { | |
545 | preferredUsername: SERVER_ACTOR_NAME | |
546 | }, | |
547 | include: [ | |
548 | { | |
549 | attributes: [ 'host' ], | |
550 | model: ServerModel.unscoped(), | |
551 | required: true, | |
552 | where: { | |
553 | host: { | |
554 | [Op.in]: hosts | |
555 | } | |
556 | } | |
557 | } | |
558 | ] | |
559 | } | |
560 | ] | |
561 | } | |
562 | ||
563 | const res = await ActorFollowModel.findAll(query) | |
10a105f0 | 564 | const followedHosts = res.map(row => row.ActorFollowing.Server.host) |
6f1b4fa4 C |
565 | |
566 | return difference(hosts, followedHosts) | |
567 | } | |
568 | ||
1735c825 | 569 | static listAcceptedFollowerUrlsForAP (actorIds: number[], t: Transaction, start?: number, count?: number) { |
50d6de9c C |
570 | return ActorFollowModel.createListAcceptedFollowForApiQuery('followers', actorIds, t, start, count) |
571 | } | |
572 | ||
1735c825 | 573 | static listAcceptedFollowerSharedInboxUrls (actorIds: number[], t: Transaction) { |
ca309a9f | 574 | return ActorFollowModel.createListAcceptedFollowForApiQuery( |
759f8a29 | 575 | 'followers', |
ca309a9f C |
576 | actorIds, |
577 | t, | |
578 | undefined, | |
579 | undefined, | |
759f8a29 C |
580 | 'sharedInboxUrl', |
581 | true | |
ca309a9f | 582 | ) |
50d6de9c C |
583 | } |
584 | ||
1735c825 | 585 | static listAcceptedFollowingUrlsForApi (actorIds: number[], t: Transaction, start?: number, count?: number) { |
50d6de9c C |
586 | return ActorFollowModel.createListAcceptedFollowForApiQuery('following', actorIds, t, start, count) |
587 | } | |
588 | ||
09cababd C |
589 | static async getStats () { |
590 | const serverActor = await getServerActor() | |
591 | ||
592 | const totalInstanceFollowing = await ActorFollowModel.count({ | |
593 | where: { | |
594 | actorId: serverActor.id | |
595 | } | |
596 | }) | |
597 | ||
598 | const totalInstanceFollowers = await ActorFollowModel.count({ | |
599 | where: { | |
600 | targetActorId: serverActor.id | |
601 | } | |
602 | }) | |
603 | ||
604 | return { | |
605 | totalInstanceFollowing, | |
606 | totalInstanceFollowers | |
607 | } | |
608 | } | |
609 | ||
6b9c966f | 610 | static updateScore (inboxUrl: string, value: number, t?: Transaction) { |
2f5c6b2f C |
611 | const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` + |
612 | 'WHERE id IN (' + | |
cef534ed C |
613 | 'SELECT "actorFollow"."id" FROM "actorFollow" ' + |
614 | 'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."actorId" ' + | |
615 | `WHERE "actor"."inboxUrl" = '${inboxUrl}' OR "actor"."sharedInboxUrl" = '${inboxUrl}'` + | |
2f5c6b2f C |
616 | ')' |
617 | ||
618 | const options = { | |
1735c825 | 619 | type: QueryTypes.BULKUPDATE, |
2f5c6b2f C |
620 | transaction: t |
621 | } | |
622 | ||
623 | return ActorFollowModel.sequelize.query(query, options) | |
624 | } | |
625 | ||
6b9c966f C |
626 | static async updateScoreByFollowingServers (serverIds: number[], value: number, t?: Transaction) { |
627 | if (serverIds.length === 0) return | |
628 | ||
629 | const me = await getServerActor() | |
630 | const serverIdsString = createSafeIn(ActorFollowModel, serverIds) | |
631 | ||
327b3318 | 632 | const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` + |
6b9c966f C |
633 | 'WHERE id IN (' + |
634 | 'SELECT "actorFollow"."id" FROM "actorFollow" ' + | |
635 | 'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."targetActorId" ' + | |
636 | `WHERE "actorFollow"."actorId" = ${me.Account.actorId} ` + // I'm the follower | |
637 | `AND "actor"."serverId" IN (${serverIdsString})` + // Criteria on followings | |
638 | ')' | |
639 | ||
640 | const options = { | |
641 | type: QueryTypes.BULKUPDATE, | |
642 | transaction: t | |
643 | } | |
644 | ||
645 | return ActorFollowModel.sequelize.query(query, options) | |
646 | } | |
647 | ||
759f8a29 C |
648 | private static async createListAcceptedFollowForApiQuery ( |
649 | type: 'followers' | 'following', | |
650 | actorIds: number[], | |
1735c825 | 651 | t: Transaction, |
759f8a29 C |
652 | start?: number, |
653 | count?: number, | |
654 | columnUrl = 'url', | |
655 | distinct = false | |
656 | ) { | |
50d6de9c C |
657 | let firstJoin: string |
658 | let secondJoin: string | |
659 | ||
660 | if (type === 'followers') { | |
661 | firstJoin = 'targetActorId' | |
662 | secondJoin = 'actorId' | |
663 | } else { | |
664 | firstJoin = 'actorId' | |
665 | secondJoin = 'targetActorId' | |
666 | } | |
667 | ||
759f8a29 | 668 | const selections: string[] = [] |
862ead21 C |
669 | if (distinct === true) selections.push(`DISTINCT("Follows"."${columnUrl}") AS "selectionUrl"`) |
670 | else selections.push(`"Follows"."${columnUrl}" AS "selectionUrl"`) | |
759f8a29 C |
671 | |
672 | selections.push('COUNT(*) AS "total"') | |
673 | ||
50d6de9c C |
674 | const tasks: Bluebird<any>[] = [] |
675 | ||
a1587156 | 676 | for (const selection of selections) { |
50d6de9c C |
677 | let query = 'SELECT ' + selection + ' FROM "actor" ' + |
678 | 'INNER JOIN "actorFollow" ON "actorFollow"."' + firstJoin + '" = "actor"."id" ' + | |
679 | 'INNER JOIN "actor" AS "Follows" ON "actorFollow"."' + secondJoin + '" = "Follows"."id" ' + | |
862ead21 | 680 | `WHERE "actor"."id" = ANY ($actorIds) AND "actorFollow"."state" = 'accepted' AND "Follows"."${columnUrl}" IS NOT NULL ` |
50d6de9c C |
681 | |
682 | if (count !== undefined) query += 'LIMIT ' + count | |
683 | if (start !== undefined) query += ' OFFSET ' + start | |
684 | ||
685 | const options = { | |
686 | bind: { actorIds }, | |
1735c825 | 687 | type: QueryTypes.SELECT, |
50d6de9c C |
688 | transaction: t |
689 | } | |
690 | tasks.push(ActorFollowModel.sequelize.query(query, options)) | |
691 | } | |
692 | ||
babecc3c | 693 | const [ followers, [ dataTotal ] ] = await Promise.all(tasks) |
47581df0 | 694 | const urls: string[] = followers.map(f => f.selectionUrl) |
50d6de9c C |
695 | |
696 | return { | |
697 | data: urls, | |
babecc3c | 698 | total: dataTotal ? parseInt(dataTotal.total, 10) : 0 |
50d6de9c C |
699 | } |
700 | } | |
701 | ||
60650c77 C |
702 | private static listBadActorFollows () { |
703 | const query = { | |
704 | where: { | |
705 | score: { | |
1735c825 | 706 | [Op.lte]: 0 |
60650c77 | 707 | } |
54e74059 | 708 | }, |
23e27dd5 | 709 | logging: false |
60650c77 C |
710 | } |
711 | ||
712 | return ActorFollowModel.findAll(query) | |
713 | } | |
714 | ||
1ca9f7c3 | 715 | toFormattedJSON (this: MActorFollowFormattable): ActorFollow { |
50d6de9c C |
716 | const follower = this.ActorFollower.toFormattedJSON() |
717 | const following = this.ActorFollowing.toFormattedJSON() | |
718 | ||
719 | return { | |
720 | id: this.id, | |
721 | follower, | |
722 | following, | |
60650c77 | 723 | score: this.score, |
50d6de9c C |
724 | state: this.state, |
725 | createdAt: this.createdAt, | |
726 | updatedAt: this.updatedAt | |
727 | } | |
728 | } | |
729 | } |