diff options
author | Chocobozzz <me@florianbigard.com> | 2018-01-18 14:59:27 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-01-18 15:42:20 +0100 |
commit | 54e740594bc2eacd8026b5d2d6cfdfc06416a65b (patch) | |
tree | 486ce489a59653c667728be8f54ab038d37e1a99 /server/models/activitypub/actor.ts | |
parent | f05a1c30c15d2ae35c11e241ca039a72eeb7d6ad (diff) | |
download | PeerTube-54e740594bc2eacd8026b5d2d6cfdfc06416a65b.tar.gz PeerTube-54e740594bc2eacd8026b5d2d6cfdfc06416a65b.tar.zst PeerTube-54e740594bc2eacd8026b5d2d6cfdfc06416a65b.zip |
Avoid too many requests and fetching outbox
Diffstat (limited to 'server/models/activitypub/actor.ts')
-rw-r--r-- | server/models/activitypub/actor.ts | 51 |
1 files changed, 46 insertions, 5 deletions
diff --git a/server/models/activitypub/actor.ts b/server/models/activitypub/actor.ts index 408d4df23..269149a31 100644 --- a/server/models/activitypub/actor.ts +++ b/server/models/activitypub/actor.ts | |||
@@ -167,17 +167,17 @@ export class ActorModel extends Model<ActorModel> { | |||
167 | }, | 167 | }, |
168 | onDelete: 'cascade' | 168 | onDelete: 'cascade' |
169 | }) | 169 | }) |
170 | AccountFollowing: ActorFollowModel[] | 170 | ActorFollowing: ActorFollowModel[] |
171 | 171 | ||
172 | @HasMany(() => ActorFollowModel, { | 172 | @HasMany(() => ActorFollowModel, { |
173 | foreignKey: { | 173 | foreignKey: { |
174 | name: 'targetActorId', | 174 | name: 'targetActorId', |
175 | allowNull: false | 175 | allowNull: false |
176 | }, | 176 | }, |
177 | as: 'followers', | 177 | as: 'ActorFollowers', |
178 | onDelete: 'cascade' | 178 | onDelete: 'cascade' |
179 | }) | 179 | }) |
180 | AccountFollowers: ActorFollowModel[] | 180 | ActorFollowers: ActorFollowModel[] |
181 | 181 | ||
182 | @ForeignKey(() => ServerModel) | 182 | @ForeignKey(() => ServerModel) |
183 | @Column | 183 | @Column |
@@ -277,6 +277,45 @@ export class ActorModel extends Model<ActorModel> { | |||
277 | }) | 277 | }) |
278 | } | 278 | } |
279 | 279 | ||
280 | static async getActorsFollowerSharedInboxUrls (actors: ActorModel[], t: Sequelize.Transaction) { | ||
281 | const query = { | ||
282 | // attribute: [], | ||
283 | where: { | ||
284 | id: { | ||
285 | [Sequelize.Op.in]: actors.map(a => a.id) | ||
286 | } | ||
287 | }, | ||
288 | include: [ | ||
289 | { | ||
290 | // attributes: [ ], | ||
291 | model: ActorFollowModel.unscoped(), | ||
292 | required: true, | ||
293 | as: 'ActorFollowers', | ||
294 | where: { | ||
295 | state: 'accepted' | ||
296 | }, | ||
297 | include: [ | ||
298 | { | ||
299 | attributes: [ 'sharedInboxUrl' ], | ||
300 | model: ActorModel.unscoped(), | ||
301 | as: 'ActorFollower', | ||
302 | required: true | ||
303 | } | ||
304 | ] | ||
305 | } | ||
306 | ], | ||
307 | transaction: t | ||
308 | } | ||
309 | |||
310 | const hash: { [ id: number ]: string[] } = {} | ||
311 | const res = await ActorModel.findAll(query) | ||
312 | for (const actor of res) { | ||
313 | hash[actor.id] = actor.ActorFollowers.map(follow => follow.ActorFollower.sharedInboxUrl) | ||
314 | } | ||
315 | |||
316 | return hash | ||
317 | } | ||
318 | |||
280 | toFormattedJSON () { | 319 | toFormattedJSON () { |
281 | let avatar: Avatar = null | 320 | let avatar: Avatar = null |
282 | if (this.Avatar) { | 321 | if (this.Avatar) { |
@@ -347,10 +386,12 @@ export class ActorModel extends Model<ActorModel> { | |||
347 | attributes: [ 'sharedInboxUrl' ], | 386 | attributes: [ 'sharedInboxUrl' ], |
348 | include: [ | 387 | include: [ |
349 | { | 388 | { |
350 | model: ActorFollowModel, | 389 | attribute: [], |
390 | model: ActorFollowModel.unscoped(), | ||
351 | required: true, | 391 | required: true, |
352 | as: 'followers', | 392 | as: 'ActorFollowers', |
353 | where: { | 393 | where: { |
394 | state: 'accepted', | ||
354 | targetActorId: this.id | 395 | targetActorId: this.id |
355 | } | 396 | } |
356 | } | 397 | } |