-
Notifications
You must be signed in to change notification settings - Fork 54
✨ feat: Extend frontend and microblog service to use rag service for spam post predictions #166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
LauraRandl
merged 8 commits into
rag-service
from
extend-existing-services-to-use-rag-service
Feb 17, 2026
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
72f783d
:sparkles: feat(microblog-service): Request spam label from rag-servi…
LauraRandl f2bd2c5
:sparkles: feat(frontend): Display predicted spam label in post
LauraRandl 448e64f
:sparkles: feat: Add user ratings for spam predictions
LauraRandl a269edb
:art: refactor(frontend): Reformat with prettier
LauraRandl 993516c
:recycle: refactor(frontend): Fix ESLint warnings & small improvements
LauraRandl 22cf85d
:sparkles: feat(microblog-service): Skip spam classification if rag s…
LauraRandl 66a390f
:bug: fix(microblog-service): Add missing env var in microblog-servic…
LauraRandl 8f3accb
:sparkles: feat: Improvements from review
LauraRandl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/frontend-nextjs/app/api/post/[postId]/spam-prediction-user-rating/downvote/route.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { NextResponse } from 'next/server'; | ||
|
|
||
| import { addSpamPredictionUserRatingDownvote } from '@/services/api/SpamPredictionVotesService'; | ||
|
|
||
| /** | ||
| * @swagger | ||
| * /ui/api/post/{postId}/spam-prediction-user-rating/downvote: | ||
| * post: | ||
| * description: Downvote the Spam Prediction. | ||
| */ | ||
|
|
||
| export type PostParams = { | ||
| postId: string; | ||
| }; | ||
|
|
||
| export async function POST(req: Request, { params }: { params: Promise<PostParams> }): Promise<NextResponse> { | ||
| const { postId } = await params; | ||
| const res = await addSpamPredictionUserRatingDownvote(postId); | ||
|
|
||
| return NextResponse.json(res.data, { status: res.status }); | ||
| } |
18 changes: 18 additions & 0 deletions
18
src/frontend-nextjs/app/api/post/[postId]/spam-prediction-user-rating/route.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { NextResponse } from 'next/server'; | ||
|
|
||
| import { fetchSpamPredictionUserRating } from '@/services/api/SpamPredictionVotesService'; | ||
| import { PostParams } from '@/app/api/like/[postId]/route'; | ||
|
|
||
| /** | ||
| * @swagger | ||
| * /ui/api/post/{postId}/spam-prediction-user-rating: | ||
| * get: | ||
| * description: Get the spam prediction user ratings for a post by its ID. | ||
| */ | ||
|
|
||
| export async function GET(req: Request, { params }: { params: Promise<PostParams> }): Promise<NextResponse> { | ||
| const { postId } = await params; | ||
| const res = await fetchSpamPredictionUserRating(postId); | ||
|
|
||
| return NextResponse.json(res.data); | ||
LauraRandl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
21 changes: 21 additions & 0 deletions
21
src/frontend-nextjs/app/api/post/[postId]/spam-prediction-user-rating/upvote/route.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { NextResponse } from 'next/server'; | ||
|
|
||
| import { addSpamPredictionUserRatingUpvote } from '@/services/api/SpamPredictionVotesService'; | ||
|
|
||
| /** | ||
| * @swagger | ||
| * /ui/api/post/{postId}/spam-prediction-user-rating/upvote: | ||
| * post: | ||
| * description: Handle spam prediction upvote action | ||
| */ | ||
|
|
||
| export type PostParams = { | ||
| postId: string; | ||
| }; | ||
|
|
||
| export async function POST(req: Request, { params }: { params: Promise<PostParams> }): Promise<NextResponse> { | ||
| const { postId } = await params; | ||
| const res = await addSpamPredictionUserRatingUpvote(postId); | ||
|
|
||
| return NextResponse.json(res.data, { status: res.status }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/frontend-nextjs/components/Timeline/PostSpamPrediction.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| 'use client'; | ||
|
|
||
| import React from 'react'; | ||
| import { Alert } from '@heroui/react'; | ||
| import { ErrorBoundary } from 'react-error-boundary'; | ||
|
|
||
| import { SpamPredictionUserRating } from '@/components/Timeline/SpamPredictionUserRating'; | ||
| import { useCheckLogin } from '@/hooks/queries/useCheckLogin'; | ||
| import { ErrorCard } from '@/components/ErrorCard'; | ||
|
|
||
| export interface PostSpamPredictionProps { | ||
| isSpamPredictedLabel?: boolean | null; | ||
| postId: string; | ||
| } | ||
|
|
||
| export function PostSpamPrediction(props: Readonly<PostSpamPredictionProps>) { | ||
| const { isLoggedIn } = useCheckLogin(); | ||
|
|
||
| if (props.isSpamPredictedLabel == null) return null; | ||
|
|
||
| const color = props.isSpamPredictedLabel ? 'danger' : 'primary'; | ||
|
|
||
| return ( | ||
| <div key={color} className='w-full flex items-center my-3'> | ||
| <div className='w-full'> | ||
| <Alert color={color}> | ||
| <div className='flex w-full items-center justify-between gap-3'> | ||
| <div className='font-semibold'> | ||
| {props.isSpamPredictedLabel ? 'Potential Spam Detected' : 'No Spam Detected'} | ||
| </div> | ||
| {isLoggedIn && ( | ||
| <ErrorBoundary fallbackRender={(props) => <ErrorCard message={props.error.message} />}> | ||
| <SpamPredictionUserRating | ||
| isSpamPredictedLabel={props.isSpamPredictedLabel} | ||
| postId={props.postId} | ||
| /> | ||
| </ErrorBoundary> | ||
LauraRandl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| )} | ||
| </div> | ||
| </Alert> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
44 changes: 44 additions & 0 deletions
44
src/frontend-nextjs/components/Timeline/SpamPredictionUserRating.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| 'use client'; | ||
|
|
||
| import React from 'react'; | ||
| import { BsHandThumbsDown, BsHandThumbsDownFill, BsHandThumbsUp, BsHandThumbsUpFill } from 'react-icons/bs'; | ||
| import { Button, Spinner } from '@heroui/react'; | ||
|
|
||
| import { useSpamPredictionUserRating } from '@/hooks/queries/useSpamPredictionUserRating'; | ||
| import { useRateSpamPrediction } from '@/hooks/mutations/useRateSpamPrediction'; | ||
|
|
||
| export interface SpamPredictionUserRatingProps { | ||
| isSpamPredictedLabel?: boolean | null; | ||
| postId: string; | ||
| } | ||
|
|
||
| export function SpamPredictionUserRating(props: Readonly<SpamPredictionUserRatingProps>) { | ||
| const { data: spamPredictionUserRatingData, isLoading } = useSpamPredictionUserRating(props.postId); | ||
| const { handleSpamPredictionUpvote, handleSpamPredictionDownvote } = useRateSpamPrediction(props.postId); | ||
|
|
||
| if (isLoading) { | ||
| return <Spinner />; | ||
| } | ||
|
|
||
| return ( | ||
| <div> | ||
| <Button | ||
| className='bg-transparent text-default-600 px-2 min-w-0 gap-1' | ||
| name='upvoteSpamRating' | ||
| onPress={() => handleSpamPredictionUpvote()} | ||
| > | ||
| <p>{spamPredictionUserRatingData?.spamPredictionUserUpvotes}</p> | ||
| {spamPredictionUserRatingData?.isUpvotedByUser ? <BsHandThumbsUpFill /> : <BsHandThumbsUp />} | ||
| </Button> | ||
|
|
||
| <Button | ||
| className='bg-transparent text-default-600 px-2 min-w-0 gap-1' | ||
| name='downvoteSpamRating' | ||
| onPress={() => handleSpamPredictionDownvote()} | ||
| > | ||
| <p>{spamPredictionUserRatingData?.spamPredictionUserDownvotes}</p> | ||
| {spamPredictionUserRatingData?.isDownvotedByUser ? <BsHandThumbsDownFill /> : <BsHandThumbsDown />} | ||
| </Button> | ||
| </div> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/frontend-nextjs/hooks/mutations/useRateSpamPrediction.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { useMutation, useQueryClient } from '@tanstack/react-query'; | ||
|
|
||
| import { handleSpamPredictionDownvote, handleSpamPredictionUpvote } from '@/services/SpamPredictionVotingService'; | ||
| import { QUERY_KEYS } from '@/enums/queryKeys'; | ||
|
|
||
| export function useRateSpamPrediction(postId: string) { | ||
| const queryClient = useQueryClient(); | ||
|
|
||
| const handleUpvoteMutation = useMutation({ | ||
| mutationFn: () => handleSpamPredictionUpvote(postId), | ||
| onSuccess: () => { | ||
| queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.spam_prediction_user_rating, postId] }); | ||
| }, | ||
| }); | ||
|
|
||
| const handleDownvoteMutation = useMutation({ | ||
| mutationFn: () => handleSpamPredictionDownvote(postId), | ||
| onSuccess: () => { | ||
| queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.spam_prediction_user_rating, postId] }); | ||
| }, | ||
| }); | ||
|
|
||
| return { | ||
| handleSpamPredictionUpvote: handleUpvoteMutation.mutate, | ||
| handleSpamPredictionDownvote: handleDownvoteMutation.mutate, | ||
| }; | ||
| } |
31 changes: 31 additions & 0 deletions
31
src/frontend-nextjs/hooks/queries/useSpamPredictionUserRating.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import path from 'path'; | ||
|
|
||
| import { useQuery } from '@tanstack/react-query'; | ||
|
|
||
| import { QUERY_KEYS } from '@/enums/queryKeys'; | ||
| import { BASE_PATH } from '@/constants'; | ||
|
|
||
| type SpamPredictionUserRating = { | ||
| spamPredictionUserUpvotes: number; | ||
| spamPredictionUserDownvotes: boolean; | ||
LauraRandl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| isUpvotedByUser?: boolean; | ||
| isDownvotedByUser?: boolean; | ||
| }; | ||
|
|
||
| async function fetchSpamPredictionUserRatings(postId: string): Promise<SpamPredictionUserRating> { | ||
| const res = await fetch(path.join(BASE_PATH, `/api/post/${postId}/spam-prediction-user-rating/`)); | ||
|
|
||
| if (!res.ok) { | ||
| throw new Error('Failed to fetch spam prediction user ratings'); | ||
| } | ||
|
|
||
| return await res.json(); | ||
| } | ||
|
|
||
| export function useSpamPredictionUserRating(postId: string) { | ||
| return useQuery({ | ||
| queryKey: [QUERY_KEYS.spam_prediction_user_rating, postId], | ||
| queryFn: () => fetchSpamPredictionUserRatings(postId), | ||
| throwOnError: true, | ||
| }); | ||
| } | ||
17 changes: 17 additions & 0 deletions
17
src/frontend-nextjs/services/SpamPredictionVotingService.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import path from 'path'; | ||
|
|
||
| import { BASE_PATH } from '@/constants'; | ||
|
|
||
| export async function handleSpamPredictionDownvote(postId: string): Promise<Response> { | ||
| return await fetch(path.join(BASE_PATH, `/api/post/${postId}/spam-prediction-user-rating/downvote/`), { | ||
| method: 'POST', | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| }); | ||
| } | ||
|
|
||
| export async function handleSpamPredictionUpvote(postId: string): Promise<Response> { | ||
| return await fetch(path.join(BASE_PATH, `/api/post/${postId}/spam-prediction-user-rating/upvote/`), { | ||
| method: 'POST', | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| }); | ||
| } |
47 changes: 47 additions & 0 deletions
47
src/frontend-nextjs/services/api/SpamPredictionVotesService.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { AxiosHeaders, AxiosRequestConfig } from 'axios'; | ||
|
|
||
| import { getMicroblogApi } from '@/axios'; | ||
| import { getJwtFromCookie } from '@/services/api/AuthService'; | ||
|
|
||
| async function withJwtCookie(): Promise<AxiosRequestConfig> { | ||
| const jwt = await getJwtFromCookie(); | ||
|
|
||
| const headers = new AxiosHeaders(); | ||
|
|
||
| headers.set('Cookie', `jwt=${jwt}`); | ||
|
|
||
| return { headers }; | ||
| } | ||
|
|
||
| export async function fetchSpamPredictionUserRating(postId: string): Promise<any> { | ||
| return await getMicroblogApi() | ||
| .get(`/spam-prediction-user-rating/${postId}`, await withJwtCookie()) | ||
| .then((response) => { | ||
| return response; | ||
| }) | ||
| .catch((error) => { | ||
| return error.response; | ||
| }); | ||
| } | ||
|
|
||
| export async function addSpamPredictionUserRatingUpvote(postId: string): Promise<any> { | ||
| return await getMicroblogApi() | ||
| .post(`/spam-prediction-user-rating/${postId}/upvote/`, {}, await withJwtCookie()) | ||
LauraRandl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .then((response) => { | ||
| return response; | ||
| }) | ||
| .catch((error) => { | ||
| return error.response; | ||
| }); | ||
| } | ||
|
|
||
| export async function addSpamPredictionUserRatingDownvote(postId: string): Promise<any> { | ||
| return await getMicroblogApi() | ||
| .post(`/spam-prediction-user-rating/${postId}/downvote/`, {}, await withJwtCookie()) | ||
LauraRandl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .then((response) => { | ||
| return response; | ||
| }) | ||
| .catch((error) => { | ||
| return error.response; | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.