aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/account/account.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/account/account.ts')
-rw-r--r--server/models/account/account.ts132
1 files changed, 7 insertions, 125 deletions
diff --git a/server/models/account/account.ts b/server/models/account/account.ts
index d2293a939..e90eaae5e 100644
--- a/server/models/account/account.ts
+++ b/server/models/account/account.ts
@@ -23,7 +23,7 @@ import {
23 AccountMethods 23 AccountMethods
24} from './account-interface' 24} from './account-interface'
25import { sendDeleteAccount } from '../../lib/activitypub/send-request' 25import { sendDeleteAccount } from '../../lib/activitypub/send-request'
26import { CONSTRAINTS_FIELDS } from '../../initializers/constants' 26import { CONFIG, CONSTRAINTS_FIELDS } from '../../initializers/constants'
27 27
28let Account: Sequelize.Model<AccountInstance, AccountAttributes> 28let Account: Sequelize.Model<AccountInstance, AccountAttributes>
29let loadAccountByPodAndUUID: AccountMethods.LoadAccountByPodAndUUID 29let loadAccountByPodAndUUID: AccountMethods.LoadAccountByPodAndUUID
@@ -34,10 +34,6 @@ let loadByUrl: AccountMethods.LoadByUrl
34let loadLocalByName: AccountMethods.LoadLocalByName 34let loadLocalByName: AccountMethods.LoadLocalByName
35let loadByNameAndHost: AccountMethods.LoadByNameAndHost 35let loadByNameAndHost: AccountMethods.LoadByNameAndHost
36let listOwned: AccountMethods.ListOwned 36let listOwned: AccountMethods.ListOwned
37let listAcceptedFollowerUrlsForApi: AccountMethods.ListAcceptedFollowerUrlsForApi
38let listAcceptedFollowingUrlsForApi: AccountMethods.ListAcceptedFollowingUrlsForApi
39let listFollowingForApi: AccountMethods.ListFollowingForApi
40let listFollowersForApi: AccountMethods.ListFollowersForApi
41let isOwned: AccountMethods.IsOwned 37let isOwned: AccountMethods.IsOwned
42let toActivityPubObject: AccountMethods.ToActivityPubObject 38let toActivityPubObject: AccountMethods.ToActivityPubObject
43let toFormattedJSON: AccountMethods.ToFormattedJSON 39let toFormattedJSON: AccountMethods.ToFormattedJSON
@@ -185,7 +181,7 @@ export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes
185 unique: true 181 unique: true
186 }, 182 },
187 { 183 {
188 fields: [ 'name', 'podId' ], 184 fields: [ 'name', 'podId', 'applicationId' ],
189 unique: true 185 unique: true
190 } 186 }
191 ], 187 ],
@@ -202,11 +198,7 @@ export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes
202 loadByUrl, 198 loadByUrl,
203 loadLocalByName, 199 loadLocalByName,
204 loadByNameAndHost, 200 loadByNameAndHost,
205 listOwned, 201 listOwned
206 listAcceptedFollowerUrlsForApi,
207 listAcceptedFollowingUrlsForApi,
208 listFollowingForApi,
209 listFollowersForApi
210 ] 202 ]
211 const instanceMethods = [ 203 const instanceMethods = [
212 isOwned, 204 isOwned,
@@ -286,9 +278,11 @@ function afterDestroy (account: AccountInstance) {
286} 278}
287 279
288toFormattedJSON = function (this: AccountInstance) { 280toFormattedJSON = function (this: AccountInstance) {
281 let host = this.Pod ? this.Pod.host : CONFIG.WEBSERVER.HOST
282
289 const json = { 283 const json = {
290 id: this.id, 284 id: this.id,
291 host: this.Pod.host, 285 host,
292 name: this.name 286 name: this.name
293 } 287 }
294 288
@@ -346,7 +340,7 @@ getFollowerSharedInboxUrls = function (this: AccountInstance) {
346} 340}
347 341
348getFollowingUrl = function (this: AccountInstance) { 342getFollowingUrl = function (this: AccountInstance) {
349 return this.url + '/followers' 343 return this.url + '/following'
350} 344}
351 345
352getFollowersUrl = function (this: AccountInstance) { 346getFollowersUrl = function (this: AccountInstance) {
@@ -369,76 +363,6 @@ listOwned = function () {
369 return Account.findAll(query) 363 return Account.findAll(query)
370} 364}
371 365
372listAcceptedFollowerUrlsForApi = function (id: number, start: number, count?: number) {
373 return createListAcceptedFollowForApiQuery('followers', id, start, count)
374}
375
376listAcceptedFollowingUrlsForApi = function (id: number, start: number, count?: number) {
377 return createListAcceptedFollowForApiQuery('following', id, start, count)
378}
379
380listFollowingForApi = function (id: number, start: number, count: number, sort: string) {
381 const query = {
382 distinct: true,
383 offset: start,
384 limit: count,
385 order: [ getSort(sort) ],
386 include: [
387 {
388 model: Account['sequelize'].models.AccountFollow,
389 required: true,
390 as: 'following',
391 include: [
392 {
393 model: Account['sequelize'].models.Account,
394 as: 'accountFollowing',
395 required: true,
396 include: [ Account['sequelize'].models.Pod ]
397 }
398 ]
399 }
400 ]
401 }
402
403 return Account.findAndCountAll(query).then(({ rows, count }) => {
404 return {
405 data: rows,
406 total: count
407 }
408 })
409}
410
411listFollowersForApi = function (id: number, start: number, count: number, sort: string) {
412 const query = {
413 distinct: true,
414 offset: start,
415 limit: count,
416 order: [ getSort(sort) ],
417 include: [
418 {
419 model: Account['sequelize'].models.AccountFollow,
420 required: true,
421 as: 'followers',
422 include: [
423 {
424 model: Account['sequelize'].models.Account,
425 as: 'accountFollowers',
426 required: true,
427 include: [ Account['sequelize'].models.Pod ]
428 }
429 ]
430 }
431 ]
432 }
433
434 return Account.findAndCountAll(query).then(({ rows, count }) => {
435 return {
436 data: rows,
437 total: count
438 }
439 })
440}
441
442loadApplication = function () { 366loadApplication = function () {
443 return Account.findOne({ 367 return Account.findOne({
444 include: [ 368 include: [
@@ -527,45 +451,3 @@ loadAccountByPodAndUUID = function (uuid: string, podId: number, transaction: Se
527 451
528 return Account.find(query) 452 return Account.find(query)
529} 453}
530
531// ------------------------------ UTILS ------------------------------
532
533async function createListAcceptedFollowForApiQuery (type: 'followers' | 'following', id: number, start: number, count?: number) {
534 let firstJoin: string
535 let secondJoin: string
536
537 if (type === 'followers') {
538 firstJoin = 'targetAccountId'
539 secondJoin = 'accountId'
540 } else {
541 firstJoin = 'accountId'
542 secondJoin = 'targetAccountId'
543 }
544
545 const selections = [ '"Followers"."url" AS "url"', 'COUNT(*) AS "total"' ]
546 const tasks: Promise<any>[] = []
547
548 for (const selection of selections) {
549 let query = 'SELECT ' + selection + ' FROM "Account" ' +
550 'INNER JOIN "AccountFollow" ON "AccountFollow"."' + firstJoin + '" = "Account"."id" ' +
551 'INNER JOIN "Account" AS "Follows" ON "Followers"."id" = "Follows"."' + secondJoin + '" ' +
552 'WHERE "Account"."id" = $id AND "AccountFollow"."state" = \'accepted\' ' +
553 'LIMIT ' + start
554
555 if (count !== undefined) query += ', ' + count
556
557 const options = {
558 bind: { id },
559 type: Sequelize.QueryTypes.SELECT
560 }
561 tasks.push(Account['sequelize'].query(query, options))
562 }
563
564 const [ followers, [ { total } ]] = await Promise.all(tasks)
565 const urls: string[] = followers.map(f => f.url)
566
567 return {
568 data: urls,
569 total: parseInt(total, 10)
570 }
571}