Skip to content

Commit 32754f2

Browse files
authored
Merge branch 'main' into feat-person-detailed-menu
2 parents 85e7d37 + 00df094 commit 32754f2

File tree

29 files changed

+544
-219
lines changed

29 files changed

+544
-219
lines changed

Makefile

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@ setup-ci:
1616
make generate-sql
1717

1818
sync: sync-server sync-web sync-root sync-common
19-
bunx lefthook install || true
2019
@echo '----------------------------------------------------------------------------------------------------------'
2120
@echo '| Most work is done. now running prisma-generate-sql (which might fail if .env.dev is not set configured)|'
2221
@echo '----------------------------------------------------------------------------------------------------------'
2322
make generate-sql || true
2423

2524
generate-sql:
26-
@cd server; bun run prisma-generate-sql
25+
@cd server; \
26+
if command -v dotenv && command -v prisma; \
27+
then dotenv -e .env.dev -- prisma generate --sql; \
28+
else bunx dotenv -e .env.dev -- bunx prisma generate --sql; \
29+
fi
2730

2831
start: start-all # build -> serve
2932
build: build-server build-web
@@ -44,6 +47,9 @@ test: dev-db
4447

4548
prepare-deploy-web: sync-common
4649
cd web; bun install; bun run build
50+
deploy-web:
51+
@if [ "${PORT}" == "" ]; then echo 'env PORT not found!'; exit 1; fi
52+
cd web; bun next start --port ${PORT}
4753
prepare-deploy-server: sync-common sync-server generate-sql
4854
deploy-server:
4955
cd server; bun src/main.ts
@@ -56,7 +62,7 @@ docker-watch:
5662
docker compose up --build --watch
5763

5864
seed:
59-
cd server; bunx prisma db seed
65+
cd server; if command -v prisma; then prisma db seed; else bunx prisma db seed; fi
6066

6167
## server/.envをDATABASE_URL=postgres://user:password@localhost:5432/databaseにしてから行う
6268
dev-db: export DATABASE_URL=$(LOCAL_DB)
@@ -76,12 +82,9 @@ dev-db:
7682
sleep 1; \
7783
done
7884
@echo "PostgreSQL is ready. Running seed..."
79-
@cd server; \
80-
if command -v prisma; then\
81-
prisma generate; prisma db push;\
82-
else \
83-
bunx prisma generate; bunx prisma db push;\
84-
fi
85+
@cd server; if command -v prisma; then \
86+
prisma generate; prisma db push; else \
87+
bunx prisma generate; bunx prisma db push; fi
8588
@make seed
8689
@echo "Seeding completed."
8790

bun.lockb

16 Bytes
Binary file not shown.

common/zod/schemas.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export const MessageSchema = z.object({
126126
creator: UserIDSchema,
127127
createdAt: z.date(),
128128
content: ContentSchema,
129+
isPicture: z.boolean(),
129130
edited: z.boolean(),
130131
});
131132

@@ -172,6 +173,8 @@ export const PersonalizedDMRoomSchema = z.object({
172173
name: NameSchema,
173174
thumbnail: z.string(),
174175
matchingStatus: MatchingStatusSchema,
176+
unreadMessages: z.number(),
177+
friendId: z.number(),
175178
});
176179

177180
export const SharedRoomSchema = z.object({

flake.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
openssl
3030
lefthook
3131
pkgs.prisma
32+
dotenv-cli
3233
] ++ [
3334
rust-pkgs
3435
];

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"prepare": "lefthook install"
89
},
910
"keywords": [],
1011
"author": "",

server/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
"test": "echo \"Error: no test specified\" && exit 1",
88
"dev": "bun --watch src/main.ts",
99
"build": "tsc",
10-
"serve": "bun target/main.js",
11-
"prisma-generate-sql": "bunx dotenv -e .env.dev -- prisma generate --sql"
10+
"serve": "bun target/main.js"
1211
},
1312
"prisma": {
1413
"seed": "bun src/seeds/seed-test.ts"

server/prisma/schema.prisma

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,17 @@ model User {
4242
interests Interest[]
4343
}
4444

45-
// プロフィールの画像。
4645
model Avatar {
4746
guid String @id
4847
data Bytes
4948
}
5049

50+
model Picture {
51+
hash String @id
52+
data Bytes
53+
key String // password
54+
}
55+
5156
model InterestSubject {
5257
id Int @id @default(autoincrement())
5358
name String
@@ -122,8 +127,6 @@ enum MatchingStatus {
122127
REJECTED
123128
}
124129

125-
// TODO: lazy load MessageLog s.t. it doesn't need to be loaded on Overview query.
126-
// https://www.prisma.io/docs/orm/prisma-client/queries/select-fields
127130
model SharedRoom {
128131
id Int @id @default(autoincrement())
129132
thumbnail String // URL to thumbnail picture
@@ -134,11 +137,12 @@ model SharedRoom {
134137

135138
model Message {
136139
id Int @id @default(autoincrement())
137-
creator Int // refers to UserId
140+
creator Int // refers to UserId
138141
createdAt DateTime @default(now()) // @readonly
139142
edited Boolean @default(false)
140143
content String
141-
read Boolean
144+
isPicture Boolean // iff the message is a picture. if true, then content is a url of picture.
145+
read Boolean @default(false)
142146
relation Relationship? @relation(fields: [relationId], references: [id], onDelete: Cascade)
143147
relationId Int?
144148
sharedRoom SharedRoom? @relation(fields: [sharedRoomId], references: [id], onDelete: Cascade)

server/src/database/chat.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,6 @@ export async function markAsRead(
127127
reader: UserID,
128128
message: MessageID,
129129
) {
130-
const val = {
131-
readerId: reader,
132-
messageId: message,
133-
relationId: rel,
134-
};
135130
return await prisma.message.updateMany({
136131
where: {
137132
id: {
@@ -156,12 +151,14 @@ export async function markAsRead(
156151
**/
157152
export async function sendDM(
158153
relation: RelationshipID,
159-
content: Omit<Message, "id">,
154+
content: Omit<Omit<Message, "id">, "isPicture">,
160155
): Promise<Result<Message>> {
161156
try {
162157
const message = await prisma.message.create({
163158
data: {
159+
// isPicture: false, // todo: bring it back
164160
relationId: relation,
161+
isPicture: false,
165162
read: false,
166163
...content,
167164
},
@@ -171,6 +168,26 @@ export async function sendDM(
171168
return Err(e);
172169
}
173170
}
171+
/**
172+
this doesn't create the image. use uploadPic in database/picture.ts to create the image.
173+
**/
174+
export async function createImageMessage(
175+
sender: UserID,
176+
relation: RelationshipID,
177+
url: string,
178+
) {
179+
return prisma.message
180+
.create({
181+
data: {
182+
creator: sender,
183+
relationId: relation,
184+
content: url,
185+
isPicture: true,
186+
},
187+
})
188+
.then((val) => Ok(val))
189+
.catch((err) => Err(err));
190+
}
174191

175192
export async function createSharedRoom(
176193
room: InitRoom,

server/src/database/picture.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,54 @@ import { Err, Ok, type Result } from "common/lib/result";
22
import type { GUID } from "common/types";
33
import { prisma } from "./client";
44

5+
/**
6+
* @returns URL of the uploaded file.
7+
* @throws on database conn fail.
8+
**/
9+
export async function uploadPic(
10+
hash: string,
11+
buf: Buffer,
12+
passkey: string,
13+
): Promise<string> {
14+
await prisma.picture.upsert({
15+
where: { hash },
16+
create: { hash, data: buf, key: passkey },
17+
update: { data: buf, key: passkey },
18+
});
19+
const url = `/picture/${hash}?key=${passkey}`;
20+
return url;
21+
}
22+
23+
export async function getPic(hash: string, passkey: string) {
24+
return prisma.picture
25+
.findUnique({
26+
where: {
27+
hash,
28+
key: passkey,
29+
},
30+
})
31+
.then((val) => val?.data);
32+
}
33+
534
/**
635
* is safe to await.
736
* @returns URL of the file.
837
**/
9-
export async function set(guid: GUID, buf: Buffer): Promise<Result<string>> {
38+
export async function setProf(
39+
guid: GUID,
40+
buf: Buffer,
41+
): Promise<Result<string>> {
1042
return prisma.avatar
1143
.upsert({
1244
where: {
13-
guid: guid,
45+
guid,
1446
},
1547
create: { guid, data: buf },
1648
update: { data: buf },
1749
})
1850
.then(() => {
1951
// ?update=${date} is necessary to let the browsers properly cache the image.
20-
const pictureUrl = `/picture/${guid}?update=${new Date().getTime()}`;
52+
const pictureUrl = `/picture/profile/${guid}?update=${new Date().getTime()}`;
2153
return Ok(pictureUrl);
2254
})
2355
.catch((err) => {
@@ -27,7 +59,7 @@ export async function set(guid: GUID, buf: Buffer): Promise<Result<string>> {
2759
}
2860

2961
// is await-safe.
30-
export async function get(guid: GUID): Promise<Result<Buffer>> {
62+
export async function getProf(guid: GUID): Promise<Result<Buffer>> {
3163
return prisma.avatar
3264
.findUnique({
3365
where: { guid },

server/src/functions/chat.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {
1010
ShareRoomID,
1111
} from "common/types";
1212
import * as db from "../database/chat";
13-
import { areAllMatched, areMatched, getRelation } from "../database/matches";
13+
import { areAllMatched, getRelation } from "../database/matches";
1414
import { getUserByID } from "../database/users";
1515
import * as http from "./share/http";
1616

@@ -48,15 +48,15 @@ export async function sendDM(
4848
);
4949

5050
// they are now MATCHED
51-
const msg: Omit<Message, "id"> = {
51+
const msg: Omit<Omit<Message, "id">, "isPicture"> = {
5252
creator: from,
5353
createdAt: new Date(),
5454
edited: false,
5555
...send,
5656
};
5757

5858
const result = await db.sendDM(rel.value.id, msg);
59-
if (!result.ok) return http.internalError("");
59+
if (!result.ok) return http.internalError("Failed to send DM");
6060
return http.created(result.value);
6161
}
6262

@@ -73,8 +73,14 @@ export async function getDM(
7373

7474
const friendData = await getUserByID(friend);
7575
if (!friendData.ok) return http.notFound("friend not found");
76+
const unreadCount = db.unreadMessages(user, rel.value.id).then((val) => {
77+
if (val.ok) return val.value;
78+
throw val.error;
79+
});
7680

7781
const personalized: PersonalizedDMRoom & DMRoom = {
82+
unreadMessages: await unreadCount,
83+
friendId: friendData.value.id,
7884
name: friendData.value.name,
7985
thumbnail: friendData.value.pictureUrl,
8086
matchingStatus:

0 commit comments

Comments
 (0)