aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/activitypub
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/activitypub')
-rw-r--r--server/models/activitypub/actor-follow.ts148
-rw-r--r--server/models/activitypub/actor.ts1
2 files changed, 76 insertions, 73 deletions
diff --git a/server/models/activitypub/actor-follow.ts b/server/models/activitypub/actor-follow.ts
index 27bb43dae..796e07a42 100644
--- a/server/models/activitypub/actor-follow.ts
+++ b/server/models/activitypub/actor-follow.ts
@@ -127,22 +127,6 @@ export class ActorFollowModel extends Model<ActorFollowModel> {
127 if (numberOfActorFollowsRemoved) logger.info('Removed bad %d actor follows.', numberOfActorFollowsRemoved) 127 if (numberOfActorFollowsRemoved) logger.info('Removed bad %d actor follows.', numberOfActorFollowsRemoved)
128 } 128 }
129 129
130 static updateActorFollowsScore (goodInboxes: string[], badInboxes: string[], t: Sequelize.Transaction | undefined) {
131 if (goodInboxes.length === 0 && badInboxes.length === 0) return
132
133 logger.info('Updating %d good actor follows and %d bad actor follows scores.', goodInboxes.length, badInboxes.length)
134
135 if (goodInboxes.length !== 0) {
136 ActorFollowModel.incrementScores(goodInboxes, ACTOR_FOLLOW_SCORE.BONUS, t)
137 .catch(err => logger.error('Cannot increment scores of good actor follows.', { err }))
138 }
139
140 if (badInboxes.length !== 0) {
141 ActorFollowModel.incrementScores(badInboxes, ACTOR_FOLLOW_SCORE.PENALTY, t)
142 .catch(err => logger.error('Cannot decrement scores of bad actor follows.', { err }))
143 }
144 }
145
146 static loadByActorAndTarget (actorId: number, targetActorId: number, t?: Sequelize.Transaction) { 130 static loadByActorAndTarget (actorId: number, targetActorId: number, t?: Sequelize.Transaction) {
147 const query = { 131 const query = {
148 where: { 132 where: {
@@ -280,7 +264,7 @@ export class ActorFollowModel extends Model<ActorFollowModel> {
280 return ActorFollowModel.findAll(query) 264 return ActorFollowModel.findAll(query)
281 } 265 }
282 266
283 static listFollowingForApi (id: number, start: number, count: number, sort: string) { 267 static listFollowingForApi (id: number, start: number, count: number, sort: string, search?: string) {
284 const query = { 268 const query = {
285 distinct: true, 269 distinct: true,
286 offset: start, 270 offset: start,
@@ -299,7 +283,17 @@ export class ActorFollowModel extends Model<ActorFollowModel> {
299 model: ActorModel, 283 model: ActorModel,
300 as: 'ActorFollowing', 284 as: 'ActorFollowing',
301 required: true, 285 required: true,
302 include: [ ServerModel ] 286 include: [
287 {
288 model: ServerModel,
289 required: true,
290 where: search ? {
291 host: {
292 [Sequelize.Op.iLike]: '%' + search + '%'
293 }
294 } : undefined
295 }
296 ]
303 } 297 }
304 ] 298 ]
305 } 299 }
@@ -313,7 +307,50 @@ export class ActorFollowModel extends Model<ActorFollowModel> {
313 }) 307 })
314 } 308 }
315 309
316 static listSubscriptionsForApi (id: number, start: number, count: number, sort: string) { 310 static listFollowersForApi (actorId: number, start: number, count: number, sort: string, search?: string) {
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: {
338 id: actorId
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
353 static listSubscriptionsForApi (actorId: number, start: number, count: number, sort: string) {
317 const query = { 354 const query = {
318 attributes: [], 355 attributes: [],
319 distinct: true, 356 distinct: true,
@@ -321,7 +358,7 @@ export class ActorFollowModel extends Model<ActorFollowModel> {
321 limit: count, 358 limit: count,
322 order: getSort(sort), 359 order: getSort(sort),
323 where: { 360 where: {
324 actorId: id 361 actorId: actorId
325 }, 362 },
326 include: [ 363 include: [
327 { 364 {
@@ -370,39 +407,6 @@ export class ActorFollowModel extends Model<ActorFollowModel> {
370 }) 407 })
371 } 408 }
372 409
373 static listFollowersForApi (id: number, start: number, count: number, sort: string) {
374 const query = {
375 distinct: true,
376 offset: start,
377 limit: count,
378 order: getSort(sort),
379 include: [
380 {
381 model: ActorModel,
382 required: true,
383 as: 'ActorFollower',
384 include: [ ServerModel ]
385 },
386 {
387 model: ActorModel,
388 as: 'ActorFollowing',
389 required: true,
390 where: {
391 id
392 }
393 }
394 ]
395 }
396
397 return ActorFollowModel.findAndCountAll(query)
398 .then(({ rows, count }) => {
399 return {
400 data: rows,
401 total: count
402 }
403 })
404 }
405
406 static listAcceptedFollowerUrlsForApi (actorIds: number[], t: Sequelize.Transaction, start?: number, count?: number) { 410 static listAcceptedFollowerUrlsForApi (actorIds: number[], t: Sequelize.Transaction, start?: number, count?: number) {
407 return ActorFollowModel.createListAcceptedFollowForApiQuery('followers', actorIds, t, start, count) 411 return ActorFollowModel.createListAcceptedFollowForApiQuery('followers', actorIds, t, start, count)
408 } 412 }
@@ -444,6 +448,22 @@ export class ActorFollowModel extends Model<ActorFollowModel> {
444 } 448 }
445 } 449 }
446 450
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 (' +
454 'SELECT "actorFollow"."id" FROM "actorFollow" ' +
455 'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."actorId" ' +
456 `WHERE "actor"."inboxUrl" = '${inboxUrl}' OR "actor"."sharedInboxUrl" = '${inboxUrl}'` +
457 ')'
458
459 const options = {
460 type: Sequelize.QueryTypes.BULKUPDATE,
461 transaction: t
462 }
463
464 return ActorFollowModel.sequelize.query(query, options)
465 }
466
447 private static async createListAcceptedFollowForApiQuery ( 467 private static async createListAcceptedFollowForApiQuery (
448 type: 'followers' | 'following', 468 type: 'followers' | 'following',
449 actorIds: number[], 469 actorIds: number[],
@@ -489,33 +509,15 @@ export class ActorFollowModel extends Model<ActorFollowModel> {
489 tasks.push(ActorFollowModel.sequelize.query(query, options)) 509 tasks.push(ActorFollowModel.sequelize.query(query, options))
490 } 510 }
491 511
492 const [ followers, [ { total } ] ] = await Promise.all(tasks) 512 const [ followers, [ dataTotal ] ] = await Promise.all(tasks)
493 const urls: string[] = followers.map(f => f.url) 513 const urls: string[] = followers.map(f => f.url)
494 514
495 return { 515 return {
496 data: urls, 516 data: urls,
497 total: parseInt(total, 10) 517 total: dataTotal ? parseInt(dataTotal.total, 10) : 0
498 } 518 }
499 } 519 }
500 520
501 private static incrementScores (inboxUrls: string[], value: number, t: Sequelize.Transaction | undefined) {
502 const inboxUrlsString = inboxUrls.map(url => `'${url}'`).join(',')
503
504 const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` +
505 'WHERE id IN (' +
506 'SELECT "actorFollow"."id" FROM "actorFollow" ' +
507 'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."actorId" ' +
508 'WHERE "actor"."inboxUrl" IN (' + inboxUrlsString + ') OR "actor"."sharedInboxUrl" IN (' + inboxUrlsString + ')' +
509 ')'
510
511 const options = t ? {
512 type: Sequelize.QueryTypes.BULKUPDATE,
513 transaction: t
514 } : undefined
515
516 return ActorFollowModel.sequelize.query(query, options)
517 }
518
519 private static listBadActorFollows () { 521 private static listBadActorFollows () {
520 const query = { 522 const query = {
521 where: { 523 where: {
diff --git a/server/models/activitypub/actor.ts b/server/models/activitypub/actor.ts
index 12b83916e..dda57a8ba 100644
--- a/server/models/activitypub/actor.ts
+++ b/server/models/activitypub/actor.ts
@@ -219,6 +219,7 @@ export class ActorModel extends Model<ActorModel> {
219 name: 'actorId', 219 name: 'actorId',
220 allowNull: false 220 allowNull: false
221 }, 221 },
222 as: 'ActorFollowings',
222 onDelete: 'cascade' 223 onDelete: 'cascade'
223 }) 224 })
224 ActorFollowing: ActorFollowModel[] 225 ActorFollowing: ActorFollowModel[]