Skip to content

Commit

Permalink
minor refactoring (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesNakayama authored Sep 11, 2024
1 parent 9cdd6a0 commit f9eac5b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
34 changes: 13 additions & 21 deletions app/repositories/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { checkIsAdminOrThrow } from '../utils/auth.server.ts'

export async function createPost(
trx: Transaction<DB>,
parentId: number | null, // TODO: use parentId?: number
parentId: number | null,
content: string,
authorId: string,
options?: { isPrivate: boolean; withUpvote?: boolean; createdAt?: number },
Expand Down Expand Up @@ -52,18 +52,6 @@ export async function createPost(
return persistedPost.id
}

export async function initPostStats(trx: Transaction<DB>, postId: number) {
await trx
.insertInto('PostStats')
.values({
postId: postId,
replies: 0,
})
// ignore conflict
.onConflict(oc => oc.column('postId').doNothing())
.execute()
}

export async function incrementReplyCount(
trx: Transaction<DB>,
postId: number,
Expand All @@ -78,6 +66,17 @@ export async function incrementReplyCount(
.execute()
}

export async function initPostStats(trx: Transaction<DB>, postId: number) {
await trx
.insertInto('PostStats')
.values({
postId: postId,
replies: 0,
})
.onConflict(oc => oc.column('postId').doNothing())
.execute()
}

export async function getPost(
trx: Transaction<DB>,
postId: number,
Expand Down Expand Up @@ -137,17 +136,10 @@ export async function getStatsPost(
let query = trx
.selectFrom('Post')
.innerJoin('FullScore', 'FullScore.postId', 'Post.id')
// TODO: check if this join is even necessary
.leftJoin('PostStats', join =>
join.onRef('PostStats.postId', '=', 'Post.id'),
)
.leftJoin('Poll', 'Poll.postId', 'Post.id')
.selectAll('Post')
.selectAll('FullScore')
.selectAll('Poll')
.select(eb =>
eb.fn.coalesce(sql<number>`replies`, sql<number>`0`).as('nReplies'),
)
.where('Post.id', '=', postId)

const scoredPost = (await query.execute())[0]
Expand Down Expand Up @@ -279,7 +271,7 @@ export async function getDescendantCount(
return result.count
}

export async function getDescendants(
export async function getDescendantIds(
trx: Transaction<DB>,
postId: number,
): Promise<number[]> {
Expand Down
4 changes: 2 additions & 2 deletions app/repositories/ranking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { type DB } from '../types/kysely-types.ts'
import { relativeEntropy } from '../utils/entropy.ts'
import {
getDescendantCount,
getDescendants,
getDescendantIds,
getPost,
getReplyIds,
getPostWithScore,
Expand Down Expand Up @@ -52,7 +52,7 @@ export async function getCommentTreeState(
targetPostId: number,
userId: string | null,
): Promise<CommentTreeState> {
const descendantIds = await getDescendants(trx, targetPostId)
const descendantIds = await getDescendantIds(trx, targetPostId)

const results = await trx
.selectFrom('Post')
Expand Down
2 changes: 1 addition & 1 deletion app/routes/fallacyDetection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function BullShredder() {
>(latestPlaygroundPosts)

const infoText = `
# Welcome to Jabble!
# Jabble Fallacy Detection
This tool analyzes your posts for [rhetorical fallacies](https://en.wikipedia.org/wiki/Fallacy).
You can use it to review your own social media posts or to detect whether someone else is trying to manipulate you.
Expand Down

0 comments on commit f9eac5b

Please sign in to comment.