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 +++++++++++++++++++++++------- 3 files changed, 47 insertions(+), 18 deletions(-) (limited to 'server/models/account') 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 } -- cgit v1.2.3