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