From 50d6de9c286abcb34ff4234d56d9cbb803db7665 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 14 Dec 2017 17:38:41 +0100 Subject: Begin moving video channel to actor --- server/models/account/account-follow.ts | 228 -------------------------------- server/models/account/account.ts | 111 +++++----------- server/models/account/user.ts | 23 +--- 3 files changed, 38 insertions(+), 324 deletions(-) delete mode 100644 server/models/account/account-follow.ts (limited to 'server/models/account') diff --git a/server/models/account/account-follow.ts b/server/models/account/account-follow.ts deleted file mode 100644 index 975e7ee7d..000000000 --- a/server/models/account/account-follow.ts +++ /dev/null @@ -1,228 +0,0 @@ -import * as Bluebird from 'bluebird' -import { values } from 'lodash' -import * as Sequelize from 'sequelize' -import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' -import { FollowState } from '../../../shared/models/accounts' -import { FOLLOW_STATES } from '../../initializers/constants' -import { ServerModel } from '../server/server' -import { getSort } from '../utils' -import { AccountModel } from './account' - -@Table({ - tableName: 'accountFollow', - indexes: [ - { - fields: [ 'accountId' ] - }, - { - fields: [ 'targetAccountId' ] - }, - { - fields: [ 'accountId', 'targetAccountId' ], - unique: true - } - ] -}) -export class AccountFollowModel extends Model { - - @AllowNull(false) - @Column(DataType.ENUM(values(FOLLOW_STATES))) - state: FollowState - - @CreatedAt - createdAt: Date - - @UpdatedAt - updatedAt: Date - - @ForeignKey(() => AccountModel) - @Column - accountId: number - - @BelongsTo(() => AccountModel, { - foreignKey: { - name: 'accountId', - allowNull: false - }, - as: 'AccountFollower', - onDelete: 'CASCADE' - }) - AccountFollower: AccountModel - - @ForeignKey(() => AccountModel) - @Column - targetAccountId: number - - @BelongsTo(() => AccountModel, { - foreignKey: { - name: 'targetAccountId', - allowNull: false - }, - as: 'AccountFollowing', - onDelete: 'CASCADE' - }) - AccountFollowing: AccountModel - - static loadByAccountAndTarget (accountId: number, targetAccountId: number, t?: Sequelize.Transaction) { - const query = { - where: { - accountId, - targetAccountId - }, - include: [ - { - model: AccountModel, - required: true, - as: 'AccountFollower' - }, - { - model: AccountModel, - required: true, - as: 'AccountFollowing' - } - ], - transaction: t - } - - return AccountFollowModel.findOne(query) - } - - static listFollowingForApi (id: number, start: number, count: number, sort: string) { - const query = { - distinct: true, - offset: start, - limit: count, - order: [ getSort(sort) ], - include: [ - { - model: AccountModel, - required: true, - as: 'AccountFollower', - where: { - id - } - }, - { - model: AccountModel, - as: 'AccountFollowing', - required: true, - include: [ ServerModel ] - } - ] - } - - return AccountFollowModel.findAndCountAll(query) - .then(({ rows, count }) => { - return { - data: rows, - total: count - } - }) - } - - static listFollowersForApi (id: number, start: number, count: number, sort: string) { - const query = { - distinct: true, - offset: start, - limit: count, - order: [ getSort(sort) ], - include: [ - { - model: AccountModel, - required: true, - as: 'AccountFollower', - include: [ ServerModel ] - }, - { - model: AccountModel, - as: 'AccountFollowing', - required: true, - where: { - id - } - } - ] - } - - return AccountFollowModel.findAndCountAll(query) - .then(({ rows, count }) => { - return { - data: rows, - total: count - } - }) - } - - static listAcceptedFollowerUrlsForApi (accountIds: number[], t: Sequelize.Transaction, start?: number, count?: number) { - return AccountFollowModel.createListAcceptedFollowForApiQuery('followers', accountIds, t, start, count) - } - - static listAcceptedFollowerSharedInboxUrls (accountIds: number[], t: Sequelize.Transaction) { - return AccountFollowModel.createListAcceptedFollowForApiQuery('followers', accountIds, t, undefined, undefined, 'sharedInboxUrl') - } - - static listAcceptedFollowingUrlsForApi (accountIds: number[], t: Sequelize.Transaction, start?: number, count?: number) { - return AccountFollowModel.createListAcceptedFollowForApiQuery('following', accountIds, t, start, count) - } - - private static async createListAcceptedFollowForApiQuery (type: 'followers' | 'following', - accountIds: number[], - t: Sequelize.Transaction, - start?: number, - count?: number, - columnUrl = 'url') { - let firstJoin: string - let secondJoin: string - - if (type === 'followers') { - firstJoin = 'targetAccountId' - secondJoin = 'accountId' - } else { - firstJoin = 'accountId' - secondJoin = 'targetAccountId' - } - - const selections = [ '"Follows"."' + columnUrl + '" AS "url"', 'COUNT(*) AS "total"' ] - const tasks: Bluebird[] = [] - - for (const selection of selections) { - let query = 'SELECT ' + selection + ' FROM "account" ' + - 'INNER JOIN "accountFollow" ON "accountFollow"."' + firstJoin + '" = "account"."id" ' + - 'INNER JOIN "account" AS "Follows" ON "accountFollow"."' + secondJoin + '" = "Follows"."id" ' + - 'WHERE "account"."id" = ANY ($accountIds) AND "accountFollow"."state" = \'accepted\' ' - - if (count !== undefined) query += 'LIMIT ' + count - if (start !== undefined) query += ' OFFSET ' + start - - const options = { - bind: { accountIds }, - type: Sequelize.QueryTypes.SELECT, - transaction: t - } - tasks.push(AccountFollowModel.sequelize.query(query, options)) - } - - const [ followers, [ { total } ] ] = await - Promise.all(tasks) - const urls: string[] = followers.map(f => f.url) - - return { - data: urls, - total: parseInt(total, 10) - } - } - - toFormattedJSON () { - const follower = this.AccountFollower.toFormattedJSON() - const following = this.AccountFollowing.toFormattedJSON() - - return { - id: this.id, - follower, - following, - state: this.state, - createdAt: this.createdAt, - updatedAt: this.updatedAt - } - } -} diff --git a/server/models/account/account.ts b/server/models/account/account.ts index b26395fd4..1ee232537 100644 --- a/server/models/account/account.ts +++ b/server/models/account/account.ts @@ -5,18 +5,16 @@ import { BelongsTo, Column, CreatedAt, - DataType, - Default, + DefaultScope, ForeignKey, HasMany, Is, - IsUUID, Model, Table, UpdatedAt } from 'sequelize-typescript' import { isUserUsernameValid } from '../../helpers/custom-validators/users' -import { sendDeleteAccount } from '../../lib/activitypub/send' +import { sendDeleteActor } from '../../lib/activitypub/send' import { ActorModel } from '../activitypub/actor' import { ApplicationModel } from '../application/application' import { ServerModel } from '../server/server' @@ -24,31 +22,30 @@ import { throwIfNotValid } from '../utils' import { VideoChannelModel } from '../video/video-channel' import { UserModel } from './user' -@Table({ - tableName: 'account', - indexes: [ - { - fields: [ 'name' ] - }, - { - fields: [ 'serverId' ] - }, - { - fields: [ 'userId' ], - unique: true - }, - { - fields: [ 'applicationId' ], - unique: true - }, +@DefaultScope({ + include: [ { - fields: [ 'name', 'serverId', 'applicationId' ], - unique: true + model: () => ActorModel, + required: true, + include: [ + { + model: () => ServerModel, + required: false + } + ] } ] }) +@Table({ + tableName: 'account' +}) export class AccountModel extends Model { + @AllowNull(false) + @Is('AccountName', value => throwIfNotValid(value, isUserUsernameValid, 'account name')) + @Column + name: string + @CreatedAt createdAt: Date @@ -89,7 +86,7 @@ export class AccountModel extends Model { }, onDelete: 'cascade' }) - Application: ApplicationModel + Account: ApplicationModel @HasMany(() => VideoChannelModel, { foreignKey: { @@ -103,32 +100,27 @@ export class AccountModel extends Model { @AfterDestroy static sendDeleteIfOwned (instance: AccountModel) { if (instance.isOwned()) { - return sendDeleteAccount(instance, undefined) + return sendDeleteActor(instance.Actor, undefined) } return undefined } - static loadApplication () { - return AccountModel.findOne({ - include: [ - { - model: ApplicationModel, - required: true - } - ] - }) - } - static load (id: number) { return AccountModel.findById(id) } static loadByUUID (uuid: string) { const query = { - where: { - uuid - } + include: [ + { + model: ActorModel, + required: true, + where: { + uuid + } + } + ] } return AccountModel.findOne(query) @@ -156,25 +148,6 @@ export class AccountModel extends Model { return AccountModel.findOne(query) } - static loadByNameAndHost (name: string, host: string) { - const query = { - where: { - name - }, - include: [ - { - model: ServerModel, - required: true, - where: { - host - } - } - ] - } - - return AccountModel.findOne(query) - } - static loadByUrl (url: string, transaction?: Sequelize.Transaction) { const query = { include: [ @@ -192,29 +165,11 @@ export class AccountModel extends Model { return AccountModel.findOne(query) } - static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) { - const query = { - include: [ - { - model: ActorModel, - required: true, - where: { - followersUrl: { - [ Sequelize.Op.in ]: followersUrls - } - } - } - ], - transaction - } - - return AccountModel.findAll(query) - } - toFormattedJSON () { const actor = this.Actor.toFormattedJSON() const account = { id: this.id, + name: this.name, createdAt: this.createdAt, updatedAt: this.updatedAt } @@ -223,7 +178,7 @@ export class AccountModel extends Model { } toActivityPubObject () { - return this.Actor.toActivityPubObject(this.name, this.uuid, 'Account') + return this.Actor.toActivityPubObject(this.name, 'Account') } isOwned () { diff --git a/server/models/account/user.ts b/server/models/account/user.ts index 70ed61e07..1d5759ea3 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts @@ -1,26 +1,13 @@ import * as Sequelize from 'sequelize' import { - AllowNull, - BeforeCreate, - BeforeUpdate, - Column, CreatedAt, - DataType, - Default, DefaultScope, - HasMany, - HasOne, - Is, - IsEmail, - Model, Scopes, - Table, UpdatedAt + AllowNull, BeforeCreate, BeforeUpdate, Column, CreatedAt, DataType, Default, DefaultScope, HasMany, HasOne, Is, IsEmail, Model, + Scopes, Table, UpdatedAt } from 'sequelize-typescript' import { hasUserRight, USER_ROLE_LABELS, UserRight } from '../../../shared' +import { comparePassword, cryptPassword } from '../../helpers' import { - comparePassword, - cryptPassword -} from '../../helpers' -import { - isUserDisplayNSFWValid, isUserPasswordValid, isUserRoleValid, isUserUsernameValid, - isUserVideoQuotaValid, isUserAutoPlayVideoValid + isUserAutoPlayVideoValid, isUserDisplayNSFWValid, isUserPasswordValid, isUserRoleValid, isUserUsernameValid, + isUserVideoQuotaValid } from '../../helpers/custom-validators/users' import { OAuthTokenModel } from '../oauth/oauth-token' import { getSort, throwIfNotValid } from '../utils' -- cgit v1.2.3