Skip to content
Merged
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
6 changes: 1 addition & 5 deletions server/src/database/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,6 @@ export async function markAsRead(
reader: UserID,
message: MessageID,
) {
const val = {
readerId: reader,
messageId: message,
relationId: rel,
};
return await prisma.message.updateMany({
where: {
id: {
Expand Down Expand Up @@ -161,6 +156,7 @@ export async function sendDM(
try {
const message = await prisma.message.create({
data: {
// isPicture: false, // todo: bring it back
relationId: relation,
read: false,
...content,
Expand Down
2 changes: 1 addition & 1 deletion server/src/functions/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export async function sendDM(
};

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

Expand Down
2 changes: 1 addition & 1 deletion server/src/router/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ router.post("/dm/to/:userid", async (req, res) => {

const result = await core.sendDM(user.value, friend.value, send.data);
if (result.ok) {
ws.sendMessage(result?.body, friend.value);
ws.sendMessage(result.body, friend.value);
}
res.status(result.code).send(result.body);
});
Expand Down
65 changes: 34 additions & 31 deletions web/api/internal/endpoints.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { CourseID, Day, GUID } from "common/types";
import type { MessageID, ShareRoomID } from "common/types";
import { panic } from "~/lib/utils";

export const origin: string | null = process.env.NEXT_PUBLIC_API_ENDPOINT ?? "";
if (!origin) throw new Error("process.env.NEXT_PUBLIC_API_ENDPOINT not found!");
export const API_ENDPOINT: string =
process.env.NEXT_PUBLIC_API_ENDPOINT ??
panic("process.env.NEXT_PUBLIC_API_ENDPOINT not found!");

// TODO: de-export this and use one from /common
export type UserID = number;
Expand All @@ -17,7 +19,7 @@ export type UserID = number;
* - 500: internal error.
**/
export const user = (userId: UserID) => {
return `${origin}/users/id/${userId}`;
return `${API_ENDPOINT}/users/id/${userId}`;
};

/**
Expand All @@ -36,7 +38,7 @@ export const user = (userId: UserID) => {
* - 500: internal error.
*
**/
export const users = `${origin}/users`;
export const users = `${API_ENDPOINT}/users`;

/**
* [v] 実装済み
Expand All @@ -47,7 +49,7 @@ export const users = `${origin}/users`;
* - 401: auth error.
* - 500: internal error
**/
export const recommendedUsers = `${origin}/users/recommended`;
export const recommendedUsers = `${API_ENDPOINT}/users/recommended`;

/**
* [v] 実装済み
Expand All @@ -72,7 +74,7 @@ export const recommendedUsers = `${origin}/users/recommended`;
* - 500: internal error.
*
**/
export const me = `${origin}/users/me`;
export const me = `${API_ENDPOINT}/users/me`;

/**
* [v] 実装済み
Expand All @@ -83,7 +85,7 @@ export const me = `${origin}/users/me`;
* - 401: unauthorized.
* - 500: internal error.
**/
export const matchedUsers = `${origin}/users/matched`;
export const matchedUsers = `${API_ENDPOINT}/users/matched`;

/**
* [v] 実装済み
Expand All @@ -94,7 +96,7 @@ export const matchedUsers = `${origin}/users/matched`;
* - 401: unauthorized.
* - 500: internal error.
**/
export const pendingRequestsToMe = `${origin}/users/pending/to-me`;
export const pendingRequestsToMe = `${API_ENDPOINT}/users/pending/to-me`;

/**
* [v] 実装済み
Expand All @@ -105,7 +107,7 @@ export const pendingRequestsToMe = `${origin}/users/pending/to-me`;
* - 401: unauthorized.
* - 500: internal error.
**/
export const pendingRequestsFromMe = `${origin}/users/pending/from-me`;
export const pendingRequestsFromMe = `${API_ENDPOINT}/users/pending/from-me`;

/**
* [v] 実装済み
Expand All @@ -117,7 +119,7 @@ export const pendingRequestsFromMe = `${origin}/users/pending/from-me`;
* - 500: internal error.
**/
export const userByGUID = (guid: GUID) => {
return `${origin}/users/guid/${guid}`;
return `${API_ENDPOINT}/users/guid/${guid}`;
};

// this one may be public to anyone.
Expand All @@ -131,7 +133,7 @@ export const userByGUID = (guid: GUID) => {
* - 500: internal error.
**/
export const userExists = (guid: GUID) => {
return `${origin}/users/exists/${guid}`;
return `${API_ENDPOINT}/users/exists/${guid}`;
};

/**
Expand All @@ -145,7 +147,7 @@ export const userExists = (guid: GUID) => {
* - 500: internal error.
**/
export const match = (opponentID: UserID) => {
return `${origin}/matches/${opponentID}`;
return `${API_ENDPOINT}/matches/${opponentID}`;
};

/**
Expand All @@ -158,7 +160,7 @@ export const match = (opponentID: UserID) => {
* - 401: unauthorized.
* - 500: internal error.
**/
export const matches = `${origin}/matches`;
export const matches = `${API_ENDPOINT}/matches`;

/**
* [x] 実装済み
Expand All @@ -180,7 +182,7 @@ export const matches = `${origin}/matches`;
* - 500: internal error.
*/
export const coursesUserId = (userId: UserID) => {
return `${origin}/courses/userId/${userId}`;
return `${API_ENDPOINT}/courses/userId/${userId}`;
};

/**
Expand All @@ -206,7 +208,7 @@ export const coursesUserId = (userId: UserID) => {
* - 401: unauthorized.
* - 500: internal error.
*/
export const coursesMine = `${origin}/courses/mine`;
export const coursesMine = `${API_ENDPOINT}/courses/mine`;

/**
* [v] 実装済み
Expand All @@ -218,7 +220,7 @@ export const coursesMine = `${origin}/courses/mine`;
* - 500: internal error.
*/
export const coursesMineOverlaps = (courseId: CourseID) => {
return `${origin}/courses/mine/overlaps/${courseId}`;
return `${API_ENDPOINT}/courses/mine/overlaps/${courseId}`;
};

/**
Expand All @@ -231,7 +233,7 @@ export const coursesMineOverlaps = (courseId: CourseID) => {
* - 500: internal error.
**/
export const coursesDayPeriod = (day: Day, period: number) => {
return `${origin}/courses/day-period?day=${day}&period=${period}`;
return `${API_ENDPOINT}/courses/day-period?day=${day}&period=${period}`;
};

/**
Expand All @@ -243,7 +245,7 @@ export const coursesDayPeriod = (day: Day, period: number) => {
* - 500: internal error.
**/
export const sendRequest = (opponentId: UserID) => {
return `${origin}/requests/send/${opponentId}`;
return `${API_ENDPOINT}/requests/send/${opponentId}`;
};

/**
Expand All @@ -254,7 +256,7 @@ export const sendRequest = (opponentId: UserID) => {
* - 500: internal error
**/
export const cancelRequest = (opponentId: UserID) => {
return `${origin}/requests/cancel/${opponentId}`;
return `${API_ENDPOINT}/requests/cancel/${opponentId}`;
};
/**
* [v] 実装済み
Expand All @@ -265,7 +267,7 @@ export const cancelRequest = (opponentId: UserID) => {
* - 500: internal error.
**/
export const acceptRequest = (opponentId: UserID) => {
return `${origin}/requests/accept/${opponentId}`;
return `${API_ENDPOINT}/requests/accept/${opponentId}`;
};

/**
Expand All @@ -278,13 +280,13 @@ export const acceptRequest = (opponentId: UserID) => {
* - 500: internal error.
**/
export const rejectRequest = (opponentId: UserID) => {
return `${origin}/requests/reject/${opponentId}`;
return `${API_ENDPOINT}/requests/reject/${opponentId}`;
};

/**
**/
export const markAsRead = (friendId: UserID, messageId: MessageID) => {
return `${origin}/chat/mark-as-read/${friendId}/${messageId}`;
return `${API_ENDPOINT}/chat/mark-as-read/${friendId}/${messageId}`;
};
/**
* []実装済み
Expand All @@ -295,7 +297,7 @@ export const markAsRead = (friendId: UserID, messageId: MessageID) => {
* - 401: unauthorized
* - 500: internal error
*/
export const roomOverview = `${origin}/chat/overview`;
export const roomOverview = `${API_ENDPOINT}/chat/overview`;

/**
* []実装済み
Expand All @@ -308,7 +310,7 @@ export const roomOverview = `${origin}/chat/overview`;
* - 403: Forbidden
* - 500: internal error
**/
export const dmTo = (userId: UserID) => `${origin}/chat/dm/to/${userId}`;
export const dmTo = (userId: UserID) => `${API_ENDPOINT}/chat/dm/to/${userId}`;

/**
* PUT -> start dm with userId. created one if none was found. authorized.
Expand All @@ -322,7 +324,8 @@ export const dmTo = (userId: UserID) => `${origin}/chat/dm/to/${userId}`;
* - 403: forbidden. you and the user are not matched yet.
* - 500: internal error.
**/
export const dmWith = (userId: UserID) => `${origin}/chat/dm/with/${userId}`;
export const dmWith = (userId: UserID) =>
`${API_ENDPOINT}/chat/dm/with/${userId}`;

/**
* POST -> Create a room. authenticated
Expand All @@ -334,15 +337,15 @@ export const dmWith = (userId: UserID) => `${origin}/chat/dm/with/${userId}`;
* - 403: forbidden (cannot invite non-friends)
* - 500: internal error
**/
export const sharedRooms = `${origin}/chat/shared`;
export const sharedRooms = `${API_ENDPOINT}/chat/shared`;

/** authorized
* GET -> Get info of a room (including the message log).
* PATCH -> update room info. (excluding the message log).
* - body: UpdateRoom
**/
export const sharedRoom = (roomId: ShareRoomID) =>
`${origin}/chat/shared/${roomId}`;
`${API_ENDPOINT}/chat/shared/${roomId}`;

/** POST: invite. authorized
* - body: UserID[]
Expand All @@ -353,24 +356,24 @@ export const sharedRoom = (roomId: ShareRoomID) =>
* - 500: internal error
**/
export const roomInvite = (roomId: ShareRoomID) =>
`${origin}/chat/shared/id/${roomId}/invite`;
`${API_ENDPOINT}/chat/shared/id/${roomId}/invite`;

/**
* PATCH: authorized body=SendMessage
* DELETE: authorized
**/
export const message = (messageId: MessageID) =>
`${origin}/chat/messages/id/${messageId}`;
`${API_ENDPOINT}/chat/messages/id/${messageId}`;

/**
* GET: get profile picture of URL (this is usually hard-encoded in pictureURL so this variable is barely used)
*/
export const pictureOf = (guid: GUID) => `${origin}/picture/${guid}`;
export const pictureOf = (guid: GUID) => `${API_ENDPOINT}/picture/${guid}`;

/**
* POST: update my profile picture.
*/
export const picture = `${origin}/picture`;
export const picture = `${API_ENDPOINT}/picture`;

export default {
user,
Expand Down
Loading
Loading