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