diff --git a/src/index.ts b/src/index.ts index f28a4c0..01f1469 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,6 +9,7 @@ import { handler as launcherLatestHandler } from './routes/launcher/latest' import { handler as launcherLoaderLatestHandler } from './routes/launcher/loader/latest' import { handler as launcherLoaderUpdateDataHandler } from './routes/launcher/loader/update-data' import { handler as berrydashLeaderboardsHandler } from './routes/berrydash/leaderboards' +import { handler as berrydashProfilePostsGetHandler } from './routes/berrydash/profile/posts/get' dotenv.config() @@ -41,6 +42,9 @@ app.get('/berrydash/leaderboards/legacy', context => app.get('/berrydash/leaderboards/total', context => berrydashLeaderboardsHandler(context, 4) ) +app.get('/berrydash/profile/posts', context => + berrydashProfilePostsGetHandler(context) +) app.all('*', () => jsonResponse( { diff --git a/src/routes/berrydash/profile/posts/get.ts b/src/routes/berrydash/profile/posts/get.ts new file mode 100644 index 0000000..43292a0 --- /dev/null +++ b/src/routes/berrydash/profile/posts/get.ts @@ -0,0 +1,76 @@ +import { Context } from 'elysia' +import { getDatabaseConnection, jsonResponse } from '../../../../lib/util' +import { berryDashUserPosts, users } from '../../../../lib/tables' +import { and, eq } from 'drizzle-orm' + +export async function handler (context: Context) { + const dbInfo0 = getDatabaseConnection(0) + const dbInfo1 = getDatabaseConnection(1) + + if (!dbInfo0 || !dbInfo1) + return jsonResponse( + { success: false, message: 'Failed to connect to database', data: null }, + 500 + ) + const { connection: connection0, db: db0 } = dbInfo0 + const { connection: connection1, db: db1 } = dbInfo1 + + let idQuery = context.query.id ? parseInt(context.query.id, 10) : 0 + if (!idQuery || idQuery < 1) { + connection0.end() + connection1.end() + return jsonResponse( + { success: false, message: 'No valid user ID provided', data: null }, + 400 + ) + } + + const user = await db0 + .select({ id: users.id }) + .from(users) + .where(eq(users.id, idQuery)) + .execute() + + if (!user[0]) { + connection0.end() + connection1.end() + return jsonResponse( + { success: false, message: 'User does not exist', data: null }, + 404 + ) + } + + const userPosts = await db1 + .select({ + id: berryDashUserPosts.id, + content: berryDashUserPosts.content, + timestamp: berryDashUserPosts.timestamp, + votes: berryDashUserPosts.votes + }) + .from(berryDashUserPosts) + .where( + and( + eq(berryDashUserPosts.userId, user[0].id), + eq(berryDashUserPosts.deletedAt, 0) + ) + ) + .execute() + + const result = {} as Record + for (const userPost of userPosts) { + let likes = 0 + for (const vote of Object.values(JSON.parse(userPost.votes)) as boolean[]) + likes += vote ? 1 : -1 + + result[userPost.id] = { + content: btoa(userPost.content), + timestamp: 'n/a', + likes: likes + } + } + + connection0.end() + connection1.end() + + return result +}