aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models
diff options
context:
space:
mode:
Diffstat (limited to 'server/models')
-rw-r--r--server/models/account/account-follow.ts15
-rw-r--r--server/models/account/account-interface.ts6
-rw-r--r--server/models/account/account.ts44
-rw-r--r--server/models/application/application-interface.ts9
-rw-r--r--server/models/application/application.ts11
-rw-r--r--server/models/job/job.ts9
6 files changed, 68 insertions, 26 deletions
diff --git a/server/models/account/account-follow.ts b/server/models/account/account-follow.ts
index e6abc893a..7c129ab9d 100644
--- a/server/models/account/account-follow.ts
+++ b/server/models/account/account-follow.ts
@@ -19,11 +19,13 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
19 { 19 {
20 indexes: [ 20 indexes: [
21 { 21 {
22 fields: [ 'accountId' ], 22 fields: [ 'accountId' ]
23 unique: true 23 },
24 {
25 fields: [ 'targetAccountId' ]
24 }, 26 },
25 { 27 {
26 fields: [ 'targetAccountId' ], 28 fields: [ 'accountId', 'targetAccountId' ],
27 unique: true 29 unique: true
28 } 30 }
29 ] 31 ]
@@ -31,7 +33,8 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
31 ) 33 )
32 34
33 const classMethods = [ 35 const classMethods = [
34 associate 36 associate,
37 loadByAccountAndTarget
35 ] 38 ]
36 addMethodsToModel(AccountFollow, classMethods) 39 addMethodsToModel(AccountFollow, classMethods)
37 40
@@ -46,7 +49,7 @@ function associate (models) {
46 name: 'accountId', 49 name: 'accountId',
47 allowNull: false 50 allowNull: false
48 }, 51 },
49 as: 'followers', 52 as: 'accountFollowers',
50 onDelete: 'CASCADE' 53 onDelete: 'CASCADE'
51 }) 54 })
52 55
@@ -55,7 +58,7 @@ function associate (models) {
55 name: 'targetAccountId', 58 name: 'targetAccountId',
56 allowNull: false 59 allowNull: false
57 }, 60 },
58 as: 'following', 61 as: 'accountFollowing',
59 onDelete: 'CASCADE' 62 onDelete: 'CASCADE'
60 }) 63 })
61} 64}
diff --git a/server/models/account/account-interface.ts b/server/models/account/account-interface.ts
index 2468dc6e1..6fc36ae9d 100644
--- a/server/models/account/account-interface.ts
+++ b/server/models/account/account-interface.ts
@@ -12,7 +12,8 @@ export namespace AccountMethods {
12 export type LoadByUUID = (uuid: string) => Bluebird<AccountInstance> 12 export type LoadByUUID = (uuid: string) => Bluebird<AccountInstance>
13 export type LoadByUrl = (url: string, transaction?: Sequelize.Transaction) => Bluebird<AccountInstance> 13 export type LoadByUrl = (url: string, transaction?: Sequelize.Transaction) => Bluebird<AccountInstance>
14 export type LoadAccountByPodAndUUID = (uuid: string, podId: number, transaction: Sequelize.Transaction) => Bluebird<AccountInstance> 14 export type LoadAccountByPodAndUUID = (uuid: string, podId: number, transaction: Sequelize.Transaction) => Bluebird<AccountInstance>
15 export type LoadLocalAccountByNameAndPod = (name: string, host: string) => Bluebird<AccountInstance> 15 export type LoadLocalByName = (name: string) => Bluebird<AccountInstance>
16 export type LoadByNameAndHost = (name: string, host: string) => Bluebird<AccountInstance>
16 export type ListOwned = () => Bluebird<AccountInstance[]> 17 export type ListOwned = () => Bluebird<AccountInstance[]>
17 export type ListAcceptedFollowerUrlsForApi = (id: number, start: number, count?: number) => Promise< ResultList<string> > 18 export type ListAcceptedFollowerUrlsForApi = (id: number, start: number, count?: number) => Promise< ResultList<string> >
18 export type ListAcceptedFollowingUrlsForApi = (id: number, start: number, count?: number) => Promise< ResultList<string> > 19 export type ListAcceptedFollowingUrlsForApi = (id: number, start: number, count?: number) => Promise< ResultList<string> >
@@ -34,7 +35,8 @@ export interface AccountClass {
34 load: AccountMethods.Load 35 load: AccountMethods.Load
35 loadByUUID: AccountMethods.LoadByUUID 36 loadByUUID: AccountMethods.LoadByUUID
36 loadByUrl: AccountMethods.LoadByUrl 37 loadByUrl: AccountMethods.LoadByUrl
37 loadLocalAccountByNameAndPod: AccountMethods.LoadLocalAccountByNameAndPod 38 loadLocalByName: AccountMethods.LoadLocalByName
39 loadByNameAndHost: AccountMethods.LoadByNameAndHost
38 listOwned: AccountMethods.ListOwned 40 listOwned: AccountMethods.ListOwned
39 listAcceptedFollowerUrlsForApi: AccountMethods.ListAcceptedFollowerUrlsForApi 41 listAcceptedFollowerUrlsForApi: AccountMethods.ListAcceptedFollowerUrlsForApi
40 listAcceptedFollowingUrlsForApi: AccountMethods.ListAcceptedFollowingUrlsForApi 42 listAcceptedFollowingUrlsForApi: AccountMethods.ListAcceptedFollowingUrlsForApi
diff --git a/server/models/account/account.ts b/server/models/account/account.ts
index cd6c822f1..d2293a939 100644
--- a/server/models/account/account.ts
+++ b/server/models/account/account.ts
@@ -31,7 +31,8 @@ let load: AccountMethods.Load
31let loadApplication: AccountMethods.LoadApplication 31let loadApplication: AccountMethods.LoadApplication
32let loadByUUID: AccountMethods.LoadByUUID 32let loadByUUID: AccountMethods.LoadByUUID
33let loadByUrl: AccountMethods.LoadByUrl 33let loadByUrl: AccountMethods.LoadByUrl
34let loadLocalAccountByNameAndPod: AccountMethods.LoadLocalAccountByNameAndPod 34let loadLocalByName: AccountMethods.LoadLocalByName
35let loadByNameAndHost: AccountMethods.LoadByNameAndHost
35let listOwned: AccountMethods.ListOwned 36let listOwned: AccountMethods.ListOwned
36let listAcceptedFollowerUrlsForApi: AccountMethods.ListAcceptedFollowerUrlsForApi 37let listAcceptedFollowerUrlsForApi: AccountMethods.ListAcceptedFollowerUrlsForApi
37let listAcceptedFollowingUrlsForApi: AccountMethods.ListAcceptedFollowingUrlsForApi 38let listAcceptedFollowingUrlsForApi: AccountMethods.ListAcceptedFollowingUrlsForApi
@@ -88,7 +89,7 @@ export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes
88 }, 89 },
89 privateKey: { 90 privateKey: {
90 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY.max), 91 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY.max),
91 allowNull: false, 92 allowNull: true,
92 validate: { 93 validate: {
93 privateKeyValid: value => { 94 privateKeyValid: value => {
94 const res = isAccountPrivateKeyValid(value) 95 const res = isAccountPrivateKeyValid(value)
@@ -199,7 +200,8 @@ export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes
199 load, 200 load,
200 loadByUUID, 201 loadByUUID,
201 loadByUrl, 202 loadByUrl,
202 loadLocalAccountByNameAndPod, 203 loadLocalByName,
204 loadByNameAndHost,
203 listOwned, 205 listOwned,
204 listAcceptedFollowerUrlsForApi, 206 listAcceptedFollowerUrlsForApi,
205 listAcceptedFollowingUrlsForApi, 207 listAcceptedFollowingUrlsForApi,
@@ -330,6 +332,8 @@ getFollowerSharedInboxUrls = function (this: AccountInstance) {
330 include: [ 332 include: [
331 { 333 {
332 model: Account['sequelize'].models.AccountFollow, 334 model: Account['sequelize'].models.AccountFollow,
335 required: true,
336 as: 'followers',
333 where: { 337 where: {
334 targetAccountId: this.id 338 targetAccountId: this.id
335 } 339 }
@@ -387,7 +391,7 @@ listFollowingForApi = function (id: number, start: number, count: number, sort:
387 include: [ 391 include: [
388 { 392 {
389 model: Account['sequelize'].models.Account, 393 model: Account['sequelize'].models.Account,
390 as: 'following', 394 as: 'accountFollowing',
391 required: true, 395 required: true,
392 include: [ Account['sequelize'].models.Pod ] 396 include: [ Account['sequelize'].models.Pod ]
393 } 397 }
@@ -418,7 +422,7 @@ listFollowersForApi = function (id: number, start: number, count: number, sort:
418 include: [ 422 include: [
419 { 423 {
420 model: Account['sequelize'].models.Account, 424 model: Account['sequelize'].models.Account,
421 as: 'followers', 425 as: 'accountFollowers',
422 required: true, 426 required: true,
423 include: [ Account['sequelize'].models.Pod ] 427 include: [ Account['sequelize'].models.Pod ]
424 } 428 }
@@ -439,7 +443,7 @@ loadApplication = function () {
439 return Account.findOne({ 443 return Account.findOne({
440 include: [ 444 include: [
441 { 445 {
442 model: Account['sequelize'].model.Application, 446 model: Account['sequelize'].models.Application,
443 required: true 447 required: true
444 } 448 }
445 ] 449 ]
@@ -460,17 +464,37 @@ loadByUUID = function (uuid: string) {
460 return Account.findOne(query) 464 return Account.findOne(query)
461} 465}
462 466
463loadLocalAccountByNameAndPod = function (name: string, host: string) { 467loadLocalByName = function (name: string) {
464 const query: Sequelize.FindOptions<AccountAttributes> = { 468 const query: Sequelize.FindOptions<AccountAttributes> = {
465 where: { 469 where: {
466 name, 470 name,
467 userId: { 471 [Sequelize.Op.or]: [
468 [Sequelize.Op.ne]: null 472 {
469 } 473 userId: {
474 [Sequelize.Op.ne]: null
475 }
476 },
477 {
478 applicationId: {
479 [Sequelize.Op.ne]: null
480 }
481 }
482 ]
483 }
484 }
485
486 return Account.findOne(query)
487}
488
489loadByNameAndHost = function (name: string, host: string) {
490 const query: Sequelize.FindOptions<AccountAttributes> = {
491 where: {
492 name
470 }, 493 },
471 include: [ 494 include: [
472 { 495 {
473 model: Account['sequelize'].models.Pod, 496 model: Account['sequelize'].models.Pod,
497 required: true,
474 where: { 498 where: {
475 host 499 host
476 } 500 }
diff --git a/server/models/application/application-interface.ts b/server/models/application/application-interface.ts
index 33254ba2d..2c391dba3 100644
--- a/server/models/application/application-interface.ts
+++ b/server/models/application/application-interface.ts
@@ -1,18 +1,21 @@
1import * as Sequelize from 'sequelize' 1import * as Sequelize from 'sequelize'
2import * as Promise from 'bluebird' 2import * as Bluebird from 'bluebird'
3 3
4export namespace ApplicationMethods { 4export namespace ApplicationMethods {
5 export type LoadMigrationVersion = () => Promise<number> 5 export type LoadMigrationVersion = () => Bluebird<number>
6 6
7 export type UpdateMigrationVersion = ( 7 export type UpdateMigrationVersion = (
8 newVersion: number, 8 newVersion: number,
9 transaction: Sequelize.Transaction 9 transaction: Sequelize.Transaction
10 ) => Promise<[ number, ApplicationInstance[] ]> 10 ) => Bluebird<[ number, ApplicationInstance[] ]>
11
12 export type CountTotal = () => Bluebird<number>
11} 13}
12 14
13export interface ApplicationClass { 15export interface ApplicationClass {
14 loadMigrationVersion: ApplicationMethods.LoadMigrationVersion 16 loadMigrationVersion: ApplicationMethods.LoadMigrationVersion
15 updateMigrationVersion: ApplicationMethods.UpdateMigrationVersion 17 updateMigrationVersion: ApplicationMethods.UpdateMigrationVersion
18 countTotal: ApplicationMethods.CountTotal
16} 19}
17 20
18export interface ApplicationAttributes { 21export interface ApplicationAttributes {
diff --git a/server/models/application/application.ts b/server/models/application/application.ts
index 507b7a843..8ba40a895 100644
--- a/server/models/application/application.ts
+++ b/server/models/application/application.ts
@@ -11,6 +11,7 @@ import {
11let Application: Sequelize.Model<ApplicationInstance, ApplicationAttributes> 11let Application: Sequelize.Model<ApplicationInstance, ApplicationAttributes>
12let loadMigrationVersion: ApplicationMethods.LoadMigrationVersion 12let loadMigrationVersion: ApplicationMethods.LoadMigrationVersion
13let updateMigrationVersion: ApplicationMethods.UpdateMigrationVersion 13let updateMigrationVersion: ApplicationMethods.UpdateMigrationVersion
14let countTotal: ApplicationMethods.CountTotal
14 15
15export default function defineApplication (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { 16export default function defineApplication (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
16 Application = sequelize.define<ApplicationInstance, ApplicationAttributes>('Application', 17 Application = sequelize.define<ApplicationInstance, ApplicationAttributes>('Application',
@@ -26,7 +27,11 @@ export default function defineApplication (sequelize: Sequelize.Sequelize, DataT
26 } 27 }
27 ) 28 )
28 29
29 const classMethods = [ loadMigrationVersion, updateMigrationVersion ] 30 const classMethods = [
31 countTotal,
32 loadMigrationVersion,
33 updateMigrationVersion
34 ]
30 addMethodsToModel(Application, classMethods) 35 addMethodsToModel(Application, classMethods)
31 36
32 return Application 37 return Application
@@ -34,6 +39,10 @@ export default function defineApplication (sequelize: Sequelize.Sequelize, DataT
34 39
35// --------------------------------------------------------------------------- 40// ---------------------------------------------------------------------------
36 41
42countTotal = function () {
43 return this.count()
44}
45
37loadMigrationVersion = function () { 46loadMigrationVersion = function () {
38 const query = { 47 const query = {
39 attributes: [ 'migrationVersion' ] 48 attributes: [ 'migrationVersion' ]
diff --git a/server/models/job/job.ts b/server/models/job/job.ts
index ce1203e5a..c2d088090 100644
--- a/server/models/job/job.ts
+++ b/server/models/job/job.ts
@@ -10,7 +10,7 @@ import {
10 10
11 JobMethods 11 JobMethods
12} from './job-interface' 12} from './job-interface'
13import { JobState } from '../../../shared/models/job.model' 13import { JobCategory, JobState } from '../../../shared/models/job.model'
14 14
15let Job: Sequelize.Model<JobInstance, JobAttributes> 15let Job: Sequelize.Model<JobInstance, JobAttributes>
16let listWithLimitByCategory: JobMethods.ListWithLimitByCategory 16let listWithLimitByCategory: JobMethods.ListWithLimitByCategory
@@ -38,7 +38,7 @@ export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes: Se
38 { 38 {
39 indexes: [ 39 indexes: [
40 { 40 {
41 fields: [ 'state' ] 41 fields: [ 'state', 'category' ]
42 } 42 }
43 ] 43 ]
44 } 44 }
@@ -52,14 +52,15 @@ export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes: Se
52 52
53// --------------------------------------------------------------------------- 53// ---------------------------------------------------------------------------
54 54
55listWithLimitByCategory = function (limit: number, state: JobState) { 55listWithLimitByCategory = function (limit: number, state: JobState, jobCategory: JobCategory) {
56 const query = { 56 const query = {
57 order: [ 57 order: [
58 [ 'id', 'ASC' ] 58 [ 'id', 'ASC' ]
59 ], 59 ],
60 limit: limit, 60 limit: limit,
61 where: { 61 where: {
62 state 62 state,
63 category: jobCategory
63 } 64 }
64 } 65 }
65 66