Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add Sub Account #343

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/cliUtils/addDummyDataForTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ async function main() {
for (const name of Object.keys(users) as (keyof typeof users)[]) {
users[name].name = name
users[name].screenName = name
users[name].displayScreenName = name
users[name].encryptedPassword = "!dummy"
if (name === "chihiro" || name === "producer") {
users[name].canMakeInviteCode = true
Expand All @@ -64,7 +65,7 @@ async function main() {
const token = new AccessToken()
token.application = app
token.user = user
token.token = user.screenName
token.token = user.displayScreenName
return token
})
)
Expand All @@ -73,7 +74,7 @@ async function main() {
const token = new AccessToken()
token.application = app
token.user = user
token.token = "revoked." + user.screenName
token.token = "revoked." + user.displayScreenName
token.revokedAt = new Date(0)
return token
})
Expand Down
13 changes: 12 additions & 1 deletion src/db/entities/user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Entity, PrimaryGeneratedColumn, Column, OneToOne, JoinColumn } from "typeorm"
import { Entity, PrimaryGeneratedColumn, Column, OneToOne, JoinColumn, OneToMany, ManyToOne } from "typeorm"
import { Matches, MaxLength } from "class-validator"
import { EntityWithTimestamps } from "../../utils/timestampColumns"
import { InviteCode } from "./inviteCode"
Expand All @@ -17,6 +17,17 @@ export class User extends EntityWithTimestamps {
@Matches(/^[0-9A-Za-z_]{1,20}$/)
screenName!: string

@Column({ name: "display_screen_name", nullable: false })
displayScreenName!: string

@ManyToOne(type => User)
@JoinColumn({ name: "owner_id", referencedColumnName: "id" })
owner!: User | null

@Column({ name: "owner_screen_name", type: "citext", nullable: true })
@Matches(/^[0-9A-Za-z_]{1,20}$/)
ownerScreenName!: string | null

@Column({ name: "encrypted_password", nullable: false })
encryptedPassword!: string

Expand Down
12 changes: 0 additions & 12 deletions src/db/migrations/1555355416048-EnableCITextExtension.ts

This file was deleted.

80 changes: 80 additions & 0 deletions src/db/migrations/1576567031553-AddSubAccountColumnToUsersTable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { MigrationInterface, QueryRunner, TableColumn, TableIndex, TableForeignKey, TableCheck } from "typeorm"

export class AddSubAccountColumnToUsersTable1576567031553 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<any> {
await queryRunner.addColumns("users", [
new TableColumn({ name: "owner_id", type: "int", isNullable: true }),
new TableColumn({ name: "owner_screen_name", type: "citext", isNullable: true }),
new TableColumn({ name: "display_screen_name", type: "citext", isNullable: true }),
])
await queryRunner.dropIndex("users", "UQ:users:screen_name")
await queryRunner.createIndices("users", [
new TableIndex({
name: "UQ:users:screen_name",
columnNames: ["screen_name"],
where: "owner_screen_name IS NULL",
isUnique: true,
}),
new TableIndex({
name: "UQ:users:screen_name:owner_screen_name",
columnNames: ["screen_name", "owner_screen_name"],
where: "owner_screen_name IS NOT NULL",
isUnique: true,
}),
new TableIndex({
name: "UQ:users:display_screen_name",
columnNames: ["display_screen_name"],
isUnique: true,
}),
])
await queryRunner.query("UPDATE users SET display_screen_name = screen_name")
await queryRunner.query("ALTER TABLE users ALTER COLUMN display_screen_name SET NOT NULL")
await queryRunner.createCheckConstraint(
"users",
new TableCheck({
name: "CHK:users:display_screen_name",
columnNames: ["display_screen_name"],
expression: "display_screen_name = COALESCE(screen_name || '.' || owner_screen_name, screen_name)",
})
)
await queryRunner.createForeignKeys("users", [
new TableForeignKey({
name: "FK:users:owner_id::users:id",
columnNames: ["owner_id"],
referencedTableName: "users",
referencedColumnNames: ["id"],
onDelete: "RESTRICT",
onUpdate: "CASCADE",
}),
new TableForeignKey({
name: "FK:users:owner_screen_name::users:screen_name",
columnNames: ["owner_screen_name"],
referencedTableName: "users",
referencedColumnNames: ["display_screen_name"],
onDelete: "RESTRICT",
onUpdate: "CASCADE",
}),
])
}

public async down(queryRunner: QueryRunner): Promise<any> {
await queryRunner.dropForeignKey("users", "FK:users:owner_id::users:id")
await queryRunner.dropForeignKey("users", "FK:users:owner_screen_name::users:screen_name")
await queryRunner.dropIndex("users", "UQ:users:screen_name")
await queryRunner.dropIndex("users", "UQ:users:screen_name:owner_screen_name")
await queryRunner.dropIndex("users", "UQ:users:display_screen_name")
await queryRunner.dropCheckConstraint("users", "CHK:users:display_screen_name")
await queryRunner.dropColumn("users", "owner_id")
await queryRunner.dropColumn("users", "owner_screen_name")
await queryRunner.dropColumn("users", "screen_name")
await queryRunner.renameColumn("users", "display_screen_name", "screen_name")
await queryRunner.createIndex(
"users",
new TableIndex({
name: "UQ:users:screen_name",
columnNames: ["screen_name"],
isUnique: true,
})
)
}
}
2 changes: 1 addition & 1 deletion src/db/repositories/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class UserRepository extends Repository<User> {
return {
id: user.id,
name: user.name,
screenName: user.screenName,
screenName: user.displayScreenName,
postsCount: user.postsCount,
createdAt: user.createdAt,
updatedAt: user.updatedAt,
Expand Down
4 changes: 2 additions & 2 deletions src/routers/api/v1/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@ router.post("/", koaBody(), async ctx => {
.createQueryBuilder("subscription")
.where("subscription.revokedAt IS NULL")
.innerJoin("subscription.user", "users")
.andWhere("users.screenName = ANY(:lusers)", { lusers: mentions })
.andWhere("users.displayScreenName = ANY(:lusers)", { lusers: mentions })
.getMany()
const payload = {
post: {
user: {
id: post.user.id,
name: post.user.name,
screenName: post.user.screenName,
screenName: post.user.displayScreenName,
icon: icon,
},
text: post.text,
Expand Down
2 changes: 1 addition & 1 deletion src/routers/web/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ router.post("/", koaBody(), checkReCaptcha, async ctx => {
password: $.string.compose($length({ min: 8 })),
}).transformOrThrow(ctx.request.body)
const user = await getRepository(User).findOne({
screenName: body.screen_name,
displayScreenName: body.screen_name,
})
if (user == null) return ctx.throw(400, "そんなユーザーいない")
const checkPasswordResult = await bcrypt.compare(body.password, user.encryptedPassword)
Expand Down
1 change: 1 addition & 0 deletions src/routers/web/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ router.post("/", koaBody(), checkReCaptcha, async ctx => {
const user = new User()
user.name = body.name
user.screenName = body.screen_name
user.displayScreenName = body.screen_name
user.encryptedPassword = await bcrypt.hash(body.password, 14)
const repo = getRepository(User)
const res = await repo.insert(user).catch(e => {
Expand Down
2 changes: 2 additions & 0 deletions src/routers/web/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import Router from "koa-router"
import { WebRouterState, WebRouterCustom } from ".."
import myDevelopedApplicationsRouter from "./myDevelopedApplications"
import inviteCodesRouter from "./inviteCodes"
import subAccountsRouter from "./subAccounts"

const router = new Router<WebRouterState, WebRouterCustom>()

router.use("/my_developed_applications", myDevelopedApplicationsRouter.routes())
router.use("/invite_codes", inviteCodesRouter.routes())
router.use("/sub_accounts", subAccountsRouter.routes())

router.get("/", async ctx => {
ctx.render("settings/index")
Expand Down
17 changes: 17 additions & 0 deletions src/routers/web/settings/subAccounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Router = require("koa-router")
import { WebRouterState, WebRouterCustom } from ".."
import { getRepository } from "typeorm"
import { User } from "../../../db/entities/user"

const router = new Router<WebRouterState, WebRouterCustom>()

router.get("/", async ctx => {
const session = ctx.state.session
if (session == null) throw "please login"
const subAccounts = await getRepository(User).find({
owner: session.user,
})
ctx.render("settings/sub_accounts/index", { subAccounts })
})

export default router