Skip to content

Commit 37d0436

Browse files
author
MattDHill
committed
let borker lib handle tags and handle tags in orphans
1 parent d750180 commit 37d0436

File tree

12 files changed

+140
-83
lines changed

12 files changed

+140
-83
lines changed

package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"test": "mocha -r ts-node/register test/**/*.spec.ts"
1818
},
1919
"dependencies": {
20-
"borker-rs-node": "0.1.17",
20+
"borker-rs-node": "0.1.21",
2121
"cors": "^2.8.5",
2222
"express": "^4.17.1",
2323
"reflect-metadata": "^0.1.13",

src/api/handlers/bork.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class BorkHandler {
5151
.getQuery()
5252
return `borks.sender_address NOT IN ${subQuery}`
5353
})
54-
.orderBy(order)
54+
.orderBy(order as any)
5555
.take(perPage)
5656
.skip(perPage * (page - 1))
5757

@@ -170,7 +170,7 @@ export class BorkHandler {
170170
.getQuery()
171171
return `address IN ${subQuery}`
172172
})
173-
.orderBy(order)
173+
.orderBy(order as any)
174174
.take(perPage)
175175
.offset(perPage * (page - 1))
176176
.getMany()

src/api/handlers/user.ts

+23-2
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ export class UserHandler {
4646
@PathParam('address') address: string,
4747
): Promise<ApiUser> {
4848

49-
const [user, blocked, iStuff] = await Promise.all([
49+
const [user, blocked, counts, iStuff] = await Promise.all([
5050
getRepository(User).findOne(address),
5151
checkBlocked(myAddress, address),
52+
this.getCounts(address),
5253
iFollowBlock(myAddress, address),
5354
])
5455

@@ -61,6 +62,7 @@ export class UserHandler {
6162

6263
return {
6364
...user,
65+
...counts,
6466
...iStuff,
6567
}
6668
}
@@ -164,7 +166,7 @@ export class UserHandler {
164166

165167
return `address IN ${subquery.getQuery()}`
166168
})
167-
.orderBy(order)
169+
.orderBy(order as any)
168170
.take(perPage)
169171
.offset(perPage * (page - 1))
170172
.setParameter('address', address)
@@ -185,9 +187,28 @@ export class UserHandler {
185187
}
186188
}))
187189
}
190+
191+
private async getCounts (address: string): Promise<{
192+
followersCount: number
193+
followingCount: number
194+
}> {
195+
196+
const conditions = {
197+
type: BorkType.Follow,
198+
}
199+
200+
const [ followersCount, followingCount ] = await Promise.all([
201+
getRepository(Bork).count({ ...conditions, recipient: { address } }),
202+
getRepository(Bork).count({ ...conditions, sender: { address } }),
203+
])
204+
205+
return { followersCount, followingCount }
206+
}
188207
}
189208

190209
export interface ApiUser extends User {
191210
iFollow: boolean
192211
iBlock: boolean
212+
followersCount?: number
213+
followingCount?: number
193214
}

src/db/entities/bork.ts

+17-17
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ export class Bork {
3434

3535
@Index()
3636
@Column('int', { name: 'nonce', nullable: true })
37-
nonce: number
37+
nonce: number | null
3838

3939
@Index()
4040
@Column('int', { name: 'position', nullable: true })
41-
position: number
41+
position: number | null
4242

4343
@Column('text', { name: 'content', nullable: true })
4444
content: string | null
@@ -48,26 +48,26 @@ export class Bork {
4848
@OneToMany(() => Bork, bork => bork.parent)
4949
children: Bork[]
5050

51-
@Index()
52-
@ManyToOne(() => Bork, bork => bork.children, { nullable: true })
53-
@JoinColumn({ name: 'parent_txid' })
54-
parent: Bork
55-
@RelationId((bork: Bork) => bork.parent)
56-
parentTxid: string
57-
5851
@Index()
5952
@ManyToOne(() => User, user => user.borks)
6053
@JoinColumn({ name: 'sender_address' })
6154
sender: User
6255
@RelationId((bork: Bork) => bork.sender)
6356
senderAddress: string
6457

58+
@Index()
59+
@ManyToOne(() => Bork, bork => bork.children, { nullable: true })
60+
@JoinColumn({ name: 'parent_txid' })
61+
parent: Bork | null
62+
@RelationId((bork: Bork) => bork.parent)
63+
parentTxid: string | null
64+
6565
@Index()
6666
@ManyToOne(() => User, user => user.receivedBorks, { nullable: true })
6767
@JoinColumn({ name: 'recipient_address' })
68-
recipient: User
68+
recipient: User | null
6969
@RelationId((bork: Bork) => bork.recipient)
70-
recipientAddress: string
70+
recipientAddress: string | null
7171

7272
@ManyToMany(() => Tag, tag => tag.borks)
7373
tags: Tag[]
@@ -81,10 +81,10 @@ export interface BorkSeed {
8181
createdAt: Date
8282
type: BorkType
8383
sender: User
84-
deletedAt?: Date
85-
nonce?: number
86-
position?: number
87-
content?: string
88-
parent?: Partial<Bork>
89-
recipient?: Partial<User>
84+
deletedAt?: Date | null
85+
nonce?: number | null
86+
position?: number | null
87+
content?: string | null
88+
parent?: Partial<Bork> | null
89+
recipient?: Partial<User> | null
9090
}

src/db/entities/orphan.ts

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ export class Orphan {
3939
@Column('text', { name: 'mentions', nullable: true })
4040
mentions: string | null
4141

42+
@Column('text', { name: 'tags', nullable: true })
43+
tags: string | null
44+
4245
// relations
4346

4447
@Index()

src/db/migrations/1560016992491-Init.ts src/db/migrations/1560112455733-Init.ts

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { MigrationInterface, QueryRunner } from 'typeorm'
22

3-
export class Init1560016992491 implements MigrationInterface {
3+
export class Init1560112455733 implements MigrationInterface {
44

55
public async up(queryRunner: QueryRunner): Promise<any> {
6-
await queryRunner.query(`CREATE TABLE "orphans" ("txid" text PRIMARY KEY NOT NULL, "created_at" datetime NOT NULL, "block_height" bigint NOT NULL, "nonce" integer NOT NULL, "position" integer NOT NULL, "content" text NOT NULL, "mentions" text, "sender_address" text)`)
6+
await queryRunner.query(`CREATE TABLE "orphans" ("txid" text PRIMARY KEY NOT NULL, "created_at" datetime NOT NULL, "block_height" bigint NOT NULL, "nonce" integer NOT NULL, "position" integer NOT NULL, "content" text NOT NULL, "mentions" text, "tags" text, "sender_address" text)`)
77
await queryRunner.query(`CREATE INDEX "IDX_4149b5d28a9d574cb35ac0b880" ON "orphans" ("created_at") `)
88
await queryRunner.query(`CREATE INDEX "IDX_08f49bf2cacd532e2e10eb5779" ON "orphans" ("block_height") `)
99
await queryRunner.query(`CREATE INDEX "IDX_f82c6e777d47e511b97334fe45" ON "orphans" ("nonce") `)
@@ -12,13 +12,13 @@ export class Init1560016992491 implements MigrationInterface {
1212
await queryRunner.query(`CREATE TABLE "users" ("address" text PRIMARY KEY NOT NULL, "created_at" datetime NOT NULL, "birth_block" bigint NOT NULL, "name" text NOT NULL, "bio" text, "avatar_link" text)`)
1313
await queryRunner.query(`CREATE INDEX "IDX_1b4832240d11291453c40c3281" ON "users" ("birth_block") `)
1414
await queryRunner.query(`CREATE TABLE "tags" ("name" text PRIMARY KEY NOT NULL)`)
15-
await queryRunner.query(`CREATE TABLE "borks" ("txid" text PRIMARY KEY NOT NULL, "created_at" datetime NOT NULL, "deleted_at" datetime, "type" text NOT NULL, "nonce" integer, "position" integer, "content" text, "parent_txid" text, "sender_address" text, "recipient_address" text)`)
15+
await queryRunner.query(`CREATE TABLE "borks" ("txid" text PRIMARY KEY NOT NULL, "created_at" datetime NOT NULL, "deleted_at" datetime, "type" text NOT NULL, "nonce" integer, "position" integer, "content" text, "sender_address" text, "parent_txid" text, "recipient_address" text)`)
1616
await queryRunner.query(`CREATE INDEX "IDX_225c8d7a66d2e44dc46bff2b89" ON "borks" ("created_at") `)
1717
await queryRunner.query(`CREATE INDEX "IDX_b556e61d2d0907016c8ee58465" ON "borks" ("type") `)
1818
await queryRunner.query(`CREATE INDEX "IDX_000418d2fa57716c62e77f7cb9" ON "borks" ("nonce") `)
1919
await queryRunner.query(`CREATE INDEX "IDX_f1d79da8758a51316b819cc38a" ON "borks" ("position") `)
20-
await queryRunner.query(`CREATE INDEX "IDX_2873c3b06729583654b6b70fe9" ON "borks" ("parent_txid") `)
2120
await queryRunner.query(`CREATE INDEX "IDX_6d695ba01f87a7cefa3617a85c" ON "borks" ("sender_address") `)
21+
await queryRunner.query(`CREATE INDEX "IDX_2873c3b06729583654b6b70fe9" ON "borks" ("parent_txid") `)
2222
await queryRunner.query(`CREATE INDEX "IDX_e4d51394b5757b0d74482ab356" ON "borks" ("recipient_address") `)
2323
await queryRunner.query(`CREATE TABLE "tx_blocks" ("height" bigint PRIMARY KEY NOT NULL, "hash" text NOT NULL, CONSTRAINT "UQ_d16046b3c22e0ac37b9367ba8d4" UNIQUE ("hash"))`)
2424
await queryRunner.query(`CREATE INDEX "IDX_d16046b3c22e0ac37b9367ba8d" ON "tx_blocks" ("hash") `)
@@ -32,8 +32,8 @@ export class Init1560016992491 implements MigrationInterface {
3232
await queryRunner.query(`DROP INDEX "IDX_f82c6e777d47e511b97334fe45"`)
3333
await queryRunner.query(`DROP INDEX "IDX_2ccda451e12bc68c5af8d3c9b4"`)
3434
await queryRunner.query(`DROP INDEX "IDX_d8dc419b6ee03babf580a7b3c3"`)
35-
await queryRunner.query(`CREATE TABLE "temporary_orphans" ("txid" text PRIMARY KEY NOT NULL, "created_at" datetime NOT NULL, "block_height" bigint NOT NULL, "nonce" integer NOT NULL, "position" integer NOT NULL, "content" text NOT NULL, "mentions" text, "sender_address" text, CONSTRAINT "FK_d8dc419b6ee03babf580a7b3c30" FOREIGN KEY ("sender_address") REFERENCES "users" ("address"))`)
36-
await queryRunner.query(`INSERT INTO "temporary_orphans"("txid", "created_at", "block_height", "nonce", "position", "content", "mentions", "sender_address") SELECT "txid", "created_at", "block_height", "nonce", "position", "content", "mentions", "sender_address" FROM "orphans"`)
35+
await queryRunner.query(`CREATE TABLE "temporary_orphans" ("txid" text PRIMARY KEY NOT NULL, "created_at" datetime NOT NULL, "block_height" bigint NOT NULL, "nonce" integer NOT NULL, "position" integer NOT NULL, "content" text NOT NULL, "mentions" text, "tags" text, "sender_address" text, CONSTRAINT "FK_d8dc419b6ee03babf580a7b3c30" FOREIGN KEY ("sender_address") REFERENCES "users" ("address"))`)
36+
await queryRunner.query(`INSERT INTO "temporary_orphans"("txid", "created_at", "block_height", "nonce", "position", "content", "mentions", "tags", "sender_address") SELECT "txid", "created_at", "block_height", "nonce", "position", "content", "mentions", "tags", "sender_address" FROM "orphans"`)
3737
await queryRunner.query(`DROP TABLE "orphans"`)
3838
await queryRunner.query(`ALTER TABLE "temporary_orphans" RENAME TO "orphans"`)
3939
await queryRunner.query(`CREATE INDEX "IDX_4149b5d28a9d574cb35ac0b880" ON "orphans" ("created_at") `)
@@ -45,19 +45,19 @@ export class Init1560016992491 implements MigrationInterface {
4545
await queryRunner.query(`DROP INDEX "IDX_b556e61d2d0907016c8ee58465"`)
4646
await queryRunner.query(`DROP INDEX "IDX_000418d2fa57716c62e77f7cb9"`)
4747
await queryRunner.query(`DROP INDEX "IDX_f1d79da8758a51316b819cc38a"`)
48-
await queryRunner.query(`DROP INDEX "IDX_2873c3b06729583654b6b70fe9"`)
4948
await queryRunner.query(`DROP INDEX "IDX_6d695ba01f87a7cefa3617a85c"`)
49+
await queryRunner.query(`DROP INDEX "IDX_2873c3b06729583654b6b70fe9"`)
5050
await queryRunner.query(`DROP INDEX "IDX_e4d51394b5757b0d74482ab356"`)
51-
await queryRunner.query(`CREATE TABLE "temporary_borks" ("txid" text PRIMARY KEY NOT NULL, "created_at" datetime NOT NULL, "deleted_at" datetime, "type" text NOT NULL, "nonce" integer, "position" integer, "content" text, "parent_txid" text, "sender_address" text, "recipient_address" text, CONSTRAINT "FK_2873c3b06729583654b6b70fe97" FOREIGN KEY ("parent_txid") REFERENCES "borks" ("txid"), CONSTRAINT "FK_6d695ba01f87a7cefa3617a85cd" FOREIGN KEY ("sender_address") REFERENCES "users" ("address"), CONSTRAINT "FK_e4d51394b5757b0d74482ab3561" FOREIGN KEY ("recipient_address") REFERENCES "users" ("address"))`)
52-
await queryRunner.query(`INSERT INTO "temporary_borks"("txid", "created_at", "deleted_at", "type", "nonce", "position", "content", "parent_txid", "sender_address", "recipient_address") SELECT "txid", "created_at", "deleted_at", "type", "nonce", "position", "content", "parent_txid", "sender_address", "recipient_address" FROM "borks"`)
51+
await queryRunner.query(`CREATE TABLE "temporary_borks" ("txid" text PRIMARY KEY NOT NULL, "created_at" datetime NOT NULL, "deleted_at" datetime, "type" text NOT NULL, "nonce" integer, "position" integer, "content" text, "sender_address" text, "parent_txid" text, "recipient_address" text, CONSTRAINT "FK_6d695ba01f87a7cefa3617a85cd" FOREIGN KEY ("sender_address") REFERENCES "users" ("address"), CONSTRAINT "FK_2873c3b06729583654b6b70fe97" FOREIGN KEY ("parent_txid") REFERENCES "borks" ("txid"), CONSTRAINT "FK_e4d51394b5757b0d74482ab3561" FOREIGN KEY ("recipient_address") REFERENCES "users" ("address"))`)
52+
await queryRunner.query(`INSERT INTO "temporary_borks"("txid", "created_at", "deleted_at", "type", "nonce", "position", "content", "sender_address", "parent_txid", "recipient_address") SELECT "txid", "created_at", "deleted_at", "type", "nonce", "position", "content", "sender_address", "parent_txid", "recipient_address" FROM "borks"`)
5353
await queryRunner.query(`DROP TABLE "borks"`)
5454
await queryRunner.query(`ALTER TABLE "temporary_borks" RENAME TO "borks"`)
5555
await queryRunner.query(`CREATE INDEX "IDX_225c8d7a66d2e44dc46bff2b89" ON "borks" ("created_at") `)
5656
await queryRunner.query(`CREATE INDEX "IDX_b556e61d2d0907016c8ee58465" ON "borks" ("type") `)
5757
await queryRunner.query(`CREATE INDEX "IDX_000418d2fa57716c62e77f7cb9" ON "borks" ("nonce") `)
5858
await queryRunner.query(`CREATE INDEX "IDX_f1d79da8758a51316b819cc38a" ON "borks" ("position") `)
59-
await queryRunner.query(`CREATE INDEX "IDX_2873c3b06729583654b6b70fe9" ON "borks" ("parent_txid") `)
6059
await queryRunner.query(`CREATE INDEX "IDX_6d695ba01f87a7cefa3617a85c" ON "borks" ("sender_address") `)
60+
await queryRunner.query(`CREATE INDEX "IDX_2873c3b06729583654b6b70fe9" ON "borks" ("parent_txid") `)
6161
await queryRunner.query(`CREATE INDEX "IDX_e4d51394b5757b0d74482ab356" ON "borks" ("recipient_address") `)
6262
await queryRunner.query(`CREATE TABLE "temporary_mentions" ("user_address" text NOT NULL, "bork_txid" text NOT NULL, CONSTRAINT "FK_73bf803442939c9f00e57958b3f" FOREIGN KEY ("user_address") REFERENCES "users" ("address") ON DELETE CASCADE, CONSTRAINT "FK_62b455467870e6f35a1d3d1e041" FOREIGN KEY ("bork_txid") REFERENCES "borks" ("txid") ON DELETE CASCADE, PRIMARY KEY ("user_address", "bork_txid"))`)
6363
await queryRunner.query(`INSERT INTO "temporary_mentions"("user_address", "bork_txid") SELECT "user_address", "bork_txid" FROM "mentions"`)
@@ -79,19 +79,19 @@ export class Init1560016992491 implements MigrationInterface {
7979
await queryRunner.query(`INSERT INTO "mentions"("user_address", "bork_txid") SELECT "user_address", "bork_txid" FROM "temporary_mentions"`)
8080
await queryRunner.query(`DROP TABLE "temporary_mentions"`)
8181
await queryRunner.query(`DROP INDEX "IDX_e4d51394b5757b0d74482ab356"`)
82-
await queryRunner.query(`DROP INDEX "IDX_6d695ba01f87a7cefa3617a85c"`)
8382
await queryRunner.query(`DROP INDEX "IDX_2873c3b06729583654b6b70fe9"`)
83+
await queryRunner.query(`DROP INDEX "IDX_6d695ba01f87a7cefa3617a85c"`)
8484
await queryRunner.query(`DROP INDEX "IDX_f1d79da8758a51316b819cc38a"`)
8585
await queryRunner.query(`DROP INDEX "IDX_000418d2fa57716c62e77f7cb9"`)
8686
await queryRunner.query(`DROP INDEX "IDX_b556e61d2d0907016c8ee58465"`)
8787
await queryRunner.query(`DROP INDEX "IDX_225c8d7a66d2e44dc46bff2b89"`)
8888
await queryRunner.query(`ALTER TABLE "borks" RENAME TO "temporary_borks"`)
89-
await queryRunner.query(`CREATE TABLE "borks" ("txid" text PRIMARY KEY NOT NULL, "created_at" datetime NOT NULL, "deleted_at" datetime, "type" text NOT NULL, "nonce" integer, "position" integer, "content" text, "parent_txid" text, "sender_address" text, "recipient_address" text)`)
90-
await queryRunner.query(`INSERT INTO "borks"("txid", "created_at", "deleted_at", "type", "nonce", "position", "content", "parent_txid", "sender_address", "recipient_address") SELECT "txid", "created_at", "deleted_at", "type", "nonce", "position", "content", "parent_txid", "sender_address", "recipient_address" FROM "temporary_borks"`)
89+
await queryRunner.query(`CREATE TABLE "borks" ("txid" text PRIMARY KEY NOT NULL, "created_at" datetime NOT NULL, "deleted_at" datetime, "type" text NOT NULL, "nonce" integer, "position" integer, "content" text, "sender_address" text, "parent_txid" text, "recipient_address" text)`)
90+
await queryRunner.query(`INSERT INTO "borks"("txid", "created_at", "deleted_at", "type", "nonce", "position", "content", "sender_address", "parent_txid", "recipient_address") SELECT "txid", "created_at", "deleted_at", "type", "nonce", "position", "content", "sender_address", "parent_txid", "recipient_address" FROM "temporary_borks"`)
9191
await queryRunner.query(`DROP TABLE "temporary_borks"`)
9292
await queryRunner.query(`CREATE INDEX "IDX_e4d51394b5757b0d74482ab356" ON "borks" ("recipient_address") `)
93-
await queryRunner.query(`CREATE INDEX "IDX_6d695ba01f87a7cefa3617a85c" ON "borks" ("sender_address") `)
9493
await queryRunner.query(`CREATE INDEX "IDX_2873c3b06729583654b6b70fe9" ON "borks" ("parent_txid") `)
94+
await queryRunner.query(`CREATE INDEX "IDX_6d695ba01f87a7cefa3617a85c" ON "borks" ("sender_address") `)
9595
await queryRunner.query(`CREATE INDEX "IDX_f1d79da8758a51316b819cc38a" ON "borks" ("position") `)
9696
await queryRunner.query(`CREATE INDEX "IDX_000418d2fa57716c62e77f7cb9" ON "borks" ("nonce") `)
9797
await queryRunner.query(`CREATE INDEX "IDX_b556e61d2d0907016c8ee58465" ON "borks" ("type") `)
@@ -102,8 +102,8 @@ export class Init1560016992491 implements MigrationInterface {
102102
await queryRunner.query(`DROP INDEX "IDX_08f49bf2cacd532e2e10eb5779"`)
103103
await queryRunner.query(`DROP INDEX "IDX_4149b5d28a9d574cb35ac0b880"`)
104104
await queryRunner.query(`ALTER TABLE "orphans" RENAME TO "temporary_orphans"`)
105-
await queryRunner.query(`CREATE TABLE "orphans" ("txid" text PRIMARY KEY NOT NULL, "created_at" datetime NOT NULL, "block_height" bigint NOT NULL, "nonce" integer NOT NULL, "position" integer NOT NULL, "content" text NOT NULL, "mentions" text, "sender_address" text)`)
106-
await queryRunner.query(`INSERT INTO "orphans"("txid", "created_at", "block_height", "nonce", "position", "content", "mentions", "sender_address") SELECT "txid", "created_at", "block_height", "nonce", "position", "content", "mentions", "sender_address" FROM "temporary_orphans"`)
105+
await queryRunner.query(`CREATE TABLE "orphans" ("txid" text PRIMARY KEY NOT NULL, "created_at" datetime NOT NULL, "block_height" bigint NOT NULL, "nonce" integer NOT NULL, "position" integer NOT NULL, "content" text NOT NULL, "mentions" text, "tags" text, "sender_address" text)`)
106+
await queryRunner.query(`INSERT INTO "orphans"("txid", "created_at", "block_height", "nonce", "position", "content", "mentions", "tags", "sender_address") SELECT "txid", "created_at", "block_height", "nonce", "position", "content", "mentions", "tags", "sender_address" FROM "temporary_orphans"`)
107107
await queryRunner.query(`DROP TABLE "temporary_orphans"`)
108108
await queryRunner.query(`CREATE INDEX "IDX_d8dc419b6ee03babf580a7b3c3" ON "orphans" ("sender_address") `)
109109
await queryRunner.query(`CREATE INDEX "IDX_2ccda451e12bc68c5af8d3c9b4" ON "orphans" ("position") `)
@@ -118,8 +118,8 @@ export class Init1560016992491 implements MigrationInterface {
118118
await queryRunner.query(`DROP INDEX "IDX_d16046b3c22e0ac37b9367ba8d"`)
119119
await queryRunner.query(`DROP TABLE "tx_blocks"`)
120120
await queryRunner.query(`DROP INDEX "IDX_e4d51394b5757b0d74482ab356"`)
121-
await queryRunner.query(`DROP INDEX "IDX_6d695ba01f87a7cefa3617a85c"`)
122121
await queryRunner.query(`DROP INDEX "IDX_2873c3b06729583654b6b70fe9"`)
122+
await queryRunner.query(`DROP INDEX "IDX_6d695ba01f87a7cefa3617a85c"`)
123123
await queryRunner.query(`DROP INDEX "IDX_f1d79da8758a51316b819cc38a"`)
124124
await queryRunner.query(`DROP INDEX "IDX_000418d2fa57716c62e77f7cb9"`)
125125
await queryRunner.query(`DROP INDEX "IDX_b556e61d2d0907016c8ee58465"`)

0 commit comments

Comments
 (0)