]>
Commit | Line | Data |
---|---|---|
50d6de9c C |
1 | import * as Bluebird from 'bluebird' |
2 | import { values } from 'lodash' | |
3 | import * as Sequelize 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, | |
15 | IsInt, | |
16 | Max, | |
17 | Model, | |
18 | Table, | |
19 | UpdatedAt | |
60650c77 | 20 | } from 'sequelize-typescript' |
50d6de9c | 21 | import { FollowState } from '../../../shared/models/actors' |
c48e82b5 | 22 | import { ActorFollow } from '../../../shared/models/actors/follow.model' |
60650c77 | 23 | import { logger } from '../../helpers/logger' |
09cababd | 24 | import { getServerActor } from '../../helpers/utils' |
60650c77 | 25 | import { ACTOR_FOLLOW_SCORE } from '../../initializers' |
50d6de9c C |
26 | import { FOLLOW_STATES } from '../../initializers/constants' |
27 | import { ServerModel } from '../server/server' | |
28 | import { getSort } from '../utils' | |
f37dc0dd | 29 | import { ActorModel, unusedActorAttributesForAPI } from './actor' |
06a05d5f | 30 | import { VideoChannelModel } from '../video/video-channel' |
99492dbc | 31 | import { IIncludeOptions } from '../../../node_modules/sequelize-typescript/lib/interfaces/IIncludeOptions' |
22a16e36 | 32 | import { AccountModel } from '../account/account' |
50d6de9c C |
33 | |
34 | @Table({ | |
35 | tableName: 'actorFollow', | |
36 | indexes: [ | |
37 | { | |
38 | fields: [ 'actorId' ] | |
39 | }, | |
40 | { | |
41 | fields: [ 'targetActorId' ] | |
42 | }, | |
43 | { | |
44 | fields: [ 'actorId', 'targetActorId' ], | |
45 | unique: true | |
60650c77 C |
46 | }, |
47 | { | |
48 | fields: [ 'score' ] | |
50d6de9c C |
49 | } |
50 | ] | |
51 | }) | |
52 | export class ActorFollowModel extends Model<ActorFollowModel> { | |
53 | ||
54 | @AllowNull(false) | |
55 | @Column(DataType.ENUM(values(FOLLOW_STATES))) | |
56 | state: FollowState | |
57 | ||
60650c77 C |
58 | @AllowNull(false) |
59 | @Default(ACTOR_FOLLOW_SCORE.BASE) | |
60 | @IsInt | |
61 | @Max(ACTOR_FOLLOW_SCORE.MAX) | |
62 | @Column | |
63 | score: number | |
64 | ||
50d6de9c C |
65 | @CreatedAt |
66 | createdAt: Date | |
67 | ||
68 | @UpdatedAt | |
69 | updatedAt: Date | |
70 | ||
71 | @ForeignKey(() => ActorModel) | |
72 | @Column | |
73 | actorId: number | |
74 | ||
75 | @BelongsTo(() => ActorModel, { | |
76 | foreignKey: { | |
77 | name: 'actorId', | |
78 | allowNull: false | |
79 | }, | |
80 | as: 'ActorFollower', | |
81 | onDelete: 'CASCADE' | |
82 | }) | |
83 | ActorFollower: ActorModel | |
84 | ||
85 | @ForeignKey(() => ActorModel) | |
86 | @Column | |
87 | targetActorId: number | |
88 | ||
89 | @BelongsTo(() => ActorModel, { | |
90 | foreignKey: { | |
91 | name: 'targetActorId', | |
92 | allowNull: false | |
93 | }, | |
94 | as: 'ActorFollowing', | |
95 | onDelete: 'CASCADE' | |
96 | }) | |
97 | ActorFollowing: ActorModel | |
98 | ||
32b2b43c C |
99 | @AfterCreate |
100 | @AfterUpdate | |
101 | static incrementFollowerAndFollowingCount (instance: ActorFollowModel) { | |
38768a36 | 102 | if (instance.state !== 'accepted') return undefined |
32b2b43c C |
103 | |
104 | return Promise.all([ | |
105 | ActorModel.incrementFollows(instance.actorId, 'followingCount', 1), | |
106 | ActorModel.incrementFollows(instance.targetActorId, 'followersCount', 1) | |
107 | ]) | |
108 | } | |
109 | ||
110 | @AfterDestroy | |
111 | static decrementFollowerAndFollowingCount (instance: ActorFollowModel) { | |
112 | return Promise.all([ | |
113 | ActorModel.incrementFollows(instance.actorId, 'followingCount',-1), | |
114 | ActorModel.incrementFollows(instance.targetActorId, 'followersCount', -1) | |
115 | ]) | |
116 | } | |
117 | ||
60650c77 C |
118 | // Remove actor follows with a score of 0 (too many requests where they were unreachable) |
119 | static async removeBadActorFollows () { | |
120 | const actorFollows = await ActorFollowModel.listBadActorFollows() | |
121 | ||
122 | const actorFollowsRemovePromises = actorFollows.map(actorFollow => actorFollow.destroy()) | |
123 | await Promise.all(actorFollowsRemovePromises) | |
124 | ||
125 | const numberOfActorFollowsRemoved = actorFollows.length | |
126 | ||
127 | if (numberOfActorFollowsRemoved) logger.info('Removed bad %d actor follows.', numberOfActorFollowsRemoved) | |
128 | } | |
129 | ||
50d6de9c C |
130 | static loadByActorAndTarget (actorId: number, targetActorId: number, t?: Sequelize.Transaction) { |
131 | const query = { | |
132 | where: { | |
133 | actorId, | |
134 | targetActorId: targetActorId | |
135 | }, | |
136 | include: [ | |
137 | { | |
138 | model: ActorModel, | |
139 | required: true, | |
140 | as: 'ActorFollower' | |
141 | }, | |
142 | { | |
143 | model: ActorModel, | |
144 | required: true, | |
145 | as: 'ActorFollowing' | |
146 | } | |
147 | ], | |
148 | transaction: t | |
149 | } | |
150 | ||
151 | return ActorFollowModel.findOne(query) | |
152 | } | |
153 | ||
f37dc0dd | 154 | static loadByActorAndTargetNameAndHostForAPI (actorId: number, targetName: string, targetHost: string, t?: Sequelize.Transaction) { |
99492dbc | 155 | const actorFollowingPartInclude: IIncludeOptions = { |
06a05d5f C |
156 | model: ActorModel, |
157 | required: true, | |
158 | as: 'ActorFollowing', | |
159 | where: { | |
160 | preferredUsername: targetName | |
99492dbc C |
161 | }, |
162 | include: [ | |
163 | { | |
f37dc0dd | 164 | model: VideoChannelModel.unscoped(), |
99492dbc C |
165 | required: false |
166 | } | |
167 | ] | |
06a05d5f C |
168 | } |
169 | ||
170 | if (targetHost === null) { | |
171 | actorFollowingPartInclude.where['serverId'] = null | |
172 | } else { | |
99492dbc C |
173 | actorFollowingPartInclude.include.push({ |
174 | model: ServerModel, | |
175 | required: true, | |
176 | where: { | |
177 | host: targetHost | |
178 | } | |
06a05d5f C |
179 | }) |
180 | } | |
181 | ||
50d6de9c C |
182 | const query = { |
183 | where: { | |
184 | actorId | |
185 | }, | |
186 | include: [ | |
aa55a4da C |
187 | actorFollowingPartInclude, |
188 | { | |
189 | model: ActorModel, | |
190 | required: true, | |
191 | as: 'ActorFollower' | |
192 | } | |
50d6de9c C |
193 | ], |
194 | transaction: t | |
195 | } | |
196 | ||
6502c3d4 | 197 | return ActorFollowModel.findOne(query) |
f37dc0dd C |
198 | .then(result => { |
199 | if (result && result.ActorFollowing.VideoChannel) { | |
200 | result.ActorFollowing.VideoChannel.Actor = result.ActorFollowing | |
201 | } | |
202 | ||
203 | return result | |
204 | }) | |
205 | } | |
206 | ||
207 | static listSubscribedIn (actorId: number, targets: { name: string, host?: string }[]) { | |
208 | const whereTab = targets | |
209 | .map(t => { | |
210 | if (t.host) { | |
211 | return { | |
212 | [ Sequelize.Op.and ]: [ | |
213 | { | |
214 | '$preferredUsername$': t.name | |
215 | }, | |
216 | { | |
217 | '$host$': t.host | |
218 | } | |
219 | ] | |
220 | } | |
221 | } | |
222 | ||
223 | return { | |
224 | [ Sequelize.Op.and ]: [ | |
225 | { | |
226 | '$preferredUsername$': t.name | |
227 | }, | |
228 | { | |
229 | '$serverId$': null | |
230 | } | |
231 | ] | |
232 | } | |
233 | }) | |
234 | ||
235 | const query = { | |
236 | attributes: [], | |
237 | where: { | |
238 | [ Sequelize.Op.and ]: [ | |
239 | { | |
240 | [ Sequelize.Op.or ]: whereTab | |
241 | }, | |
242 | { | |
243 | actorId | |
244 | } | |
245 | ] | |
246 | }, | |
247 | include: [ | |
248 | { | |
249 | attributes: [ 'preferredUsername' ], | |
250 | model: ActorModel.unscoped(), | |
251 | required: true, | |
252 | as: 'ActorFollowing', | |
253 | include: [ | |
254 | { | |
255 | attributes: [ 'host' ], | |
256 | model: ServerModel.unscoped(), | |
257 | required: false | |
258 | } | |
259 | ] | |
260 | } | |
261 | ] | |
262 | } | |
263 | ||
264 | return ActorFollowModel.findAll(query) | |
6502c3d4 C |
265 | } |
266 | ||
b014b6b9 | 267 | static listFollowingForApi (id: number, start: number, count: number, sort: string, search?: string) { |
50d6de9c C |
268 | const query = { |
269 | distinct: true, | |
270 | offset: start, | |
271 | limit: count, | |
3bb6c526 | 272 | order: getSort(sort), |
50d6de9c C |
273 | include: [ |
274 | { | |
275 | model: ActorModel, | |
276 | required: true, | |
277 | as: 'ActorFollower', | |
278 | where: { | |
279 | id | |
280 | } | |
281 | }, | |
282 | { | |
283 | model: ActorModel, | |
284 | as: 'ActorFollowing', | |
285 | required: true, | |
b014b6b9 C |
286 | include: [ |
287 | { | |
288 | model: ServerModel, | |
289 | required: true, | |
290 | where: search ? { | |
291 | host: { | |
292 | [Sequelize.Op.iLike]: '%' + search + '%' | |
293 | } | |
294 | } : undefined | |
295 | } | |
296 | ] | |
50d6de9c C |
297 | } |
298 | ] | |
299 | } | |
300 | ||
301 | return ActorFollowModel.findAndCountAll(query) | |
302 | .then(({ rows, count }) => { | |
303 | return { | |
304 | data: rows, | |
305 | total: count | |
306 | } | |
307 | }) | |
308 | } | |
309 | ||
cef534ed | 310 | static listFollowersForApi (actorId: number, start: number, count: number, sort: string, search?: string) { |
b014b6b9 C |
311 | const query = { |
312 | distinct: true, | |
313 | offset: start, | |
314 | limit: count, | |
315 | order: getSort(sort), | |
316 | include: [ | |
317 | { | |
318 | model: ActorModel, | |
319 | required: true, | |
320 | as: 'ActorFollower', | |
321 | include: [ | |
322 | { | |
323 | model: ServerModel, | |
324 | required: true, | |
325 | where: search ? { | |
326 | host: { | |
327 | [ Sequelize.Op.iLike ]: '%' + search + '%' | |
328 | } | |
329 | } : undefined | |
330 | } | |
331 | ] | |
332 | }, | |
333 | { | |
334 | model: ActorModel, | |
335 | as: 'ActorFollowing', | |
336 | required: true, | |
337 | where: { | |
cef534ed | 338 | id: actorId |
b014b6b9 C |
339 | } |
340 | } | |
341 | ] | |
342 | } | |
343 | ||
344 | return ActorFollowModel.findAndCountAll(query) | |
345 | .then(({ rows, count }) => { | |
346 | return { | |
347 | data: rows, | |
348 | total: count | |
349 | } | |
350 | }) | |
351 | } | |
352 | ||
cef534ed | 353 | static listSubscriptionsForApi (actorId: number, start: number, count: number, sort: string) { |
06a05d5f | 354 | const query = { |
f37dc0dd | 355 | attributes: [], |
06a05d5f C |
356 | distinct: true, |
357 | offset: start, | |
358 | limit: count, | |
359 | order: getSort(sort), | |
360 | where: { | |
cef534ed | 361 | actorId: actorId |
06a05d5f C |
362 | }, |
363 | include: [ | |
364 | { | |
f5b0af50 C |
365 | attributes: [ 'id' ], |
366 | model: ActorModel.unscoped(), | |
06a05d5f C |
367 | as: 'ActorFollowing', |
368 | required: true, | |
369 | include: [ | |
370 | { | |
f5b0af50 | 371 | model: VideoChannelModel.unscoped(), |
22a16e36 C |
372 | required: true, |
373 | include: [ | |
374 | { | |
f37dc0dd C |
375 | attributes: { |
376 | exclude: unusedActorAttributesForAPI | |
377 | }, | |
378 | model: ActorModel, | |
22a16e36 | 379 | required: true |
f37dc0dd C |
380 | }, |
381 | { | |
f5b0af50 | 382 | model: AccountModel.unscoped(), |
f37dc0dd C |
383 | required: true, |
384 | include: [ | |
385 | { | |
386 | attributes: { | |
387 | exclude: unusedActorAttributesForAPI | |
388 | }, | |
389 | model: ActorModel, | |
390 | required: true | |
391 | } | |
392 | ] | |
22a16e36 C |
393 | } |
394 | ] | |
06a05d5f C |
395 | } |
396 | ] | |
397 | } | |
398 | ] | |
399 | } | |
400 | ||
401 | return ActorFollowModel.findAndCountAll(query) | |
402 | .then(({ rows, count }) => { | |
403 | return { | |
404 | data: rows.map(r => r.ActorFollowing.VideoChannel), | |
405 | total: count | |
406 | } | |
407 | }) | |
408 | } | |
409 | ||
418d092a | 410 | static listAcceptedFollowerUrlsForAP (actorIds: number[], t: Sequelize.Transaction, start?: number, count?: number) { |
50d6de9c C |
411 | return ActorFollowModel.createListAcceptedFollowForApiQuery('followers', actorIds, t, start, count) |
412 | } | |
413 | ||
414 | static listAcceptedFollowerSharedInboxUrls (actorIds: number[], t: Sequelize.Transaction) { | |
ca309a9f | 415 | return ActorFollowModel.createListAcceptedFollowForApiQuery( |
759f8a29 | 416 | 'followers', |
ca309a9f C |
417 | actorIds, |
418 | t, | |
419 | undefined, | |
420 | undefined, | |
759f8a29 C |
421 | 'sharedInboxUrl', |
422 | true | |
ca309a9f | 423 | ) |
50d6de9c C |
424 | } |
425 | ||
426 | static listAcceptedFollowingUrlsForApi (actorIds: number[], t: Sequelize.Transaction, start?: number, count?: number) { | |
427 | return ActorFollowModel.createListAcceptedFollowForApiQuery('following', actorIds, t, start, count) | |
428 | } | |
429 | ||
09cababd C |
430 | static async getStats () { |
431 | const serverActor = await getServerActor() | |
432 | ||
433 | const totalInstanceFollowing = await ActorFollowModel.count({ | |
434 | where: { | |
435 | actorId: serverActor.id | |
436 | } | |
437 | }) | |
438 | ||
439 | const totalInstanceFollowers = await ActorFollowModel.count({ | |
440 | where: { | |
441 | targetActorId: serverActor.id | |
442 | } | |
443 | }) | |
444 | ||
445 | return { | |
446 | totalInstanceFollowing, | |
447 | totalInstanceFollowers | |
448 | } | |
449 | } | |
450 | ||
2f5c6b2f C |
451 | static updateFollowScore (inboxUrl: string, value: number, t?: Sequelize.Transaction) { |
452 | const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` + | |
453 | 'WHERE id IN (' + | |
cef534ed C |
454 | 'SELECT "actorFollow"."id" FROM "actorFollow" ' + |
455 | 'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."actorId" ' + | |
456 | `WHERE "actor"."inboxUrl" = '${inboxUrl}' OR "actor"."sharedInboxUrl" = '${inboxUrl}'` + | |
2f5c6b2f C |
457 | ')' |
458 | ||
459 | const options = { | |
460 | type: Sequelize.QueryTypes.BULKUPDATE, | |
461 | transaction: t | |
462 | } | |
463 | ||
464 | return ActorFollowModel.sequelize.query(query, options) | |
465 | } | |
466 | ||
759f8a29 C |
467 | private static async createListAcceptedFollowForApiQuery ( |
468 | type: 'followers' | 'following', | |
469 | actorIds: number[], | |
470 | t: Sequelize.Transaction, | |
471 | start?: number, | |
472 | count?: number, | |
473 | columnUrl = 'url', | |
474 | distinct = false | |
475 | ) { | |
50d6de9c C |
476 | let firstJoin: string |
477 | let secondJoin: string | |
478 | ||
479 | if (type === 'followers') { | |
480 | firstJoin = 'targetActorId' | |
481 | secondJoin = 'actorId' | |
482 | } else { | |
483 | firstJoin = 'actorId' | |
484 | secondJoin = 'targetActorId' | |
485 | } | |
486 | ||
759f8a29 C |
487 | const selections: string[] = [] |
488 | if (distinct === true) selections.push('DISTINCT("Follows"."' + columnUrl + '") AS "url"') | |
489 | else selections.push('"Follows"."' + columnUrl + '" AS "url"') | |
490 | ||
491 | selections.push('COUNT(*) AS "total"') | |
492 | ||
50d6de9c C |
493 | const tasks: Bluebird<any>[] = [] |
494 | ||
759f8a29 | 495 | for (let selection of selections) { |
50d6de9c C |
496 | let query = 'SELECT ' + selection + ' FROM "actor" ' + |
497 | 'INNER JOIN "actorFollow" ON "actorFollow"."' + firstJoin + '" = "actor"."id" ' + | |
498 | 'INNER JOIN "actor" AS "Follows" ON "actorFollow"."' + secondJoin + '" = "Follows"."id" ' + | |
499 | 'WHERE "actor"."id" = ANY ($actorIds) AND "actorFollow"."state" = \'accepted\' ' | |
500 | ||
501 | if (count !== undefined) query += 'LIMIT ' + count | |
502 | if (start !== undefined) query += ' OFFSET ' + start | |
503 | ||
504 | const options = { | |
505 | bind: { actorIds }, | |
506 | type: Sequelize.QueryTypes.SELECT, | |
507 | transaction: t | |
508 | } | |
509 | tasks.push(ActorFollowModel.sequelize.query(query, options)) | |
510 | } | |
511 | ||
babecc3c | 512 | const [ followers, [ dataTotal ] ] = await Promise.all(tasks) |
50d6de9c C |
513 | const urls: string[] = followers.map(f => f.url) |
514 | ||
515 | return { | |
516 | data: urls, | |
babecc3c | 517 | total: dataTotal ? parseInt(dataTotal.total, 10) : 0 |
50d6de9c C |
518 | } |
519 | } | |
520 | ||
60650c77 C |
521 | private static listBadActorFollows () { |
522 | const query = { | |
523 | where: { | |
524 | score: { | |
525 | [Sequelize.Op.lte]: 0 | |
526 | } | |
54e74059 | 527 | }, |
23e27dd5 | 528 | logging: false |
60650c77 C |
529 | } |
530 | ||
531 | return ActorFollowModel.findAll(query) | |
532 | } | |
533 | ||
c48e82b5 | 534 | toFormattedJSON (): ActorFollow { |
50d6de9c C |
535 | const follower = this.ActorFollower.toFormattedJSON() |
536 | const following = this.ActorFollowing.toFormattedJSON() | |
537 | ||
538 | return { | |
539 | id: this.id, | |
540 | follower, | |
541 | following, | |
60650c77 | 542 | score: this.score, |
50d6de9c C |
543 | state: this.state, |
544 | createdAt: this.createdAt, | |
545 | updatedAt: this.updatedAt | |
546 | } | |
547 | } | |
548 | } |