From 350e31d6b64e4973dfa5e9f7b46841cb09aeb1ad Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 14 Nov 2017 17:31:26 +0100 Subject: Follow works --- server/models/account/account-follow.ts | 15 +++++--- server/models/account/account-interface.ts | 6 ++- server/models/account/account.ts | 44 +++++++++++++++++----- server/models/application/application-interface.ts | 9 +++-- server/models/application/application.ts | 11 +++++- server/models/job/job.ts | 9 +++-- 6 files changed, 68 insertions(+), 26 deletions(-) (limited to 'server/models') 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 { indexes: [ { - fields: [ 'accountId' ], - unique: true + fields: [ 'accountId' ] + }, + { + fields: [ 'targetAccountId' ] }, { - fields: [ 'targetAccountId' ], + fields: [ 'accountId', 'targetAccountId' ], unique: true } ] @@ -31,7 +33,8 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da ) const classMethods = [ - associate + associate, + loadByAccountAndTarget ] addMethodsToModel(AccountFollow, classMethods) @@ -46,7 +49,7 @@ function associate (models) { name: 'accountId', allowNull: false }, - as: 'followers', + as: 'accountFollowers', onDelete: 'CASCADE' }) @@ -55,7 +58,7 @@ function associate (models) { name: 'targetAccountId', allowNull: false }, - as: 'following', + as: 'accountFollowing', onDelete: 'CASCADE' }) } 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 { export type LoadByUUID = (uuid: string) => Bluebird export type LoadByUrl = (url: string, transaction?: Sequelize.Transaction) => Bluebird export type LoadAccountByPodAndUUID = (uuid: string, podId: number, transaction: Sequelize.Transaction) => Bluebird - export type LoadLocalAccountByNameAndPod = (name: string, host: string) => Bluebird + export type LoadLocalByName = (name: string) => Bluebird + export type LoadByNameAndHost = (name: string, host: string) => Bluebird export type ListOwned = () => Bluebird export type ListAcceptedFollowerUrlsForApi = (id: number, start: number, count?: number) => Promise< ResultList > export type ListAcceptedFollowingUrlsForApi = (id: number, start: number, count?: number) => Promise< ResultList > @@ -34,7 +35,8 @@ export interface AccountClass { load: AccountMethods.Load loadByUUID: AccountMethods.LoadByUUID loadByUrl: AccountMethods.LoadByUrl - loadLocalAccountByNameAndPod: AccountMethods.LoadLocalAccountByNameAndPod + loadLocalByName: AccountMethods.LoadLocalByName + loadByNameAndHost: AccountMethods.LoadByNameAndHost listOwned: AccountMethods.ListOwned listAcceptedFollowerUrlsForApi: AccountMethods.ListAcceptedFollowerUrlsForApi 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 let loadApplication: AccountMethods.LoadApplication let loadByUUID: AccountMethods.LoadByUUID let loadByUrl: AccountMethods.LoadByUrl -let loadLocalAccountByNameAndPod: AccountMethods.LoadLocalAccountByNameAndPod +let loadLocalByName: AccountMethods.LoadLocalByName +let loadByNameAndHost: AccountMethods.LoadByNameAndHost let listOwned: AccountMethods.ListOwned let listAcceptedFollowerUrlsForApi: AccountMethods.ListAcceptedFollowerUrlsForApi let listAcceptedFollowingUrlsForApi: AccountMethods.ListAcceptedFollowingUrlsForApi @@ -88,7 +89,7 @@ export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes }, privateKey: { type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY.max), - allowNull: false, + allowNull: true, validate: { privateKeyValid: value => { const res = isAccountPrivateKeyValid(value) @@ -199,7 +200,8 @@ export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes load, loadByUUID, loadByUrl, - loadLocalAccountByNameAndPod, + loadLocalByName, + loadByNameAndHost, listOwned, listAcceptedFollowerUrlsForApi, listAcceptedFollowingUrlsForApi, @@ -330,6 +332,8 @@ getFollowerSharedInboxUrls = function (this: AccountInstance) { include: [ { model: Account['sequelize'].models.AccountFollow, + required: true, + as: 'followers', where: { targetAccountId: this.id } @@ -387,7 +391,7 @@ listFollowingForApi = function (id: number, start: number, count: number, sort: include: [ { model: Account['sequelize'].models.Account, - as: 'following', + as: 'accountFollowing', required: true, include: [ Account['sequelize'].models.Pod ] } @@ -418,7 +422,7 @@ listFollowersForApi = function (id: number, start: number, count: number, sort: include: [ { model: Account['sequelize'].models.Account, - as: 'followers', + as: 'accountFollowers', required: true, include: [ Account['sequelize'].models.Pod ] } @@ -439,7 +443,7 @@ loadApplication = function () { return Account.findOne({ include: [ { - model: Account['sequelize'].model.Application, + model: Account['sequelize'].models.Application, required: true } ] @@ -460,17 +464,37 @@ loadByUUID = function (uuid: string) { return Account.findOne(query) } -loadLocalAccountByNameAndPod = function (name: string, host: string) { +loadLocalByName = function (name: string) { const query: Sequelize.FindOptions = { where: { name, - userId: { - [Sequelize.Op.ne]: null - } + [Sequelize.Op.or]: [ + { + userId: { + [Sequelize.Op.ne]: null + } + }, + { + applicationId: { + [Sequelize.Op.ne]: null + } + } + ] + } + } + + return Account.findOne(query) +} + +loadByNameAndHost = function (name: string, host: string) { + const query: Sequelize.FindOptions = { + where: { + name }, include: [ { model: Account['sequelize'].models.Pod, + required: true, where: { host } 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 @@ import * as Sequelize from 'sequelize' -import * as Promise from 'bluebird' +import * as Bluebird from 'bluebird' export namespace ApplicationMethods { - export type LoadMigrationVersion = () => Promise + export type LoadMigrationVersion = () => Bluebird export type UpdateMigrationVersion = ( newVersion: number, transaction: Sequelize.Transaction - ) => Promise<[ number, ApplicationInstance[] ]> + ) => Bluebird<[ number, ApplicationInstance[] ]> + + export type CountTotal = () => Bluebird } export interface ApplicationClass { loadMigrationVersion: ApplicationMethods.LoadMigrationVersion updateMigrationVersion: ApplicationMethods.UpdateMigrationVersion + countTotal: ApplicationMethods.CountTotal } export 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 { let Application: Sequelize.Model let loadMigrationVersion: ApplicationMethods.LoadMigrationVersion let updateMigrationVersion: ApplicationMethods.UpdateMigrationVersion +let countTotal: ApplicationMethods.CountTotal export default function defineApplication (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { Application = sequelize.define('Application', @@ -26,7 +27,11 @@ export default function defineApplication (sequelize: Sequelize.Sequelize, DataT } ) - const classMethods = [ loadMigrationVersion, updateMigrationVersion ] + const classMethods = [ + countTotal, + loadMigrationVersion, + updateMigrationVersion + ] addMethodsToModel(Application, classMethods) return Application @@ -34,6 +39,10 @@ export default function defineApplication (sequelize: Sequelize.Sequelize, DataT // --------------------------------------------------------------------------- +countTotal = function () { + return this.count() +} + loadMigrationVersion = function () { const query = { 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 { JobMethods } from './job-interface' -import { JobState } from '../../../shared/models/job.model' +import { JobCategory, JobState } from '../../../shared/models/job.model' let Job: Sequelize.Model let listWithLimitByCategory: JobMethods.ListWithLimitByCategory @@ -38,7 +38,7 @@ export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes: Se { indexes: [ { - fields: [ 'state' ] + fields: [ 'state', 'category' ] } ] } @@ -52,14 +52,15 @@ export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes: Se // --------------------------------------------------------------------------- -listWithLimitByCategory = function (limit: number, state: JobState) { +listWithLimitByCategory = function (limit: number, state: JobState, jobCategory: JobCategory) { const query = { order: [ [ 'id', 'ASC' ] ], limit: limit, where: { - state + state, + category: jobCategory } } -- cgit v1.2.3