Skip to content

Commit

Permalink
merging
Browse files Browse the repository at this point in the history
  • Loading branch information
sedeve committed Oct 30, 2023
2 parents 6c1b2a4 + 75212df commit 017cd3f
Show file tree
Hide file tree
Showing 41 changed files with 1,173 additions and 471 deletions.
Binary file modified .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ coverage

# nyc test coverage
.nyc_output
.DS_Store

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
Expand Down
Binary file modified backend/.DS_Store
Binary file not shown.
6 changes: 5 additions & 1 deletion backend/code/prisma/dbml/schema.dbml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Table users {
tfaStatus Boolean [not null, default: false]
left_friends friends [not null]
right_friends friends [not null]
matches matches [not null]
participant1 matches [not null]
participant2 matches [not null]
owned_rooms rooms [not null]
roomMember room_members [not null]
blocked_by blocked_friends [not null]
Expand Down Expand Up @@ -66,6 +67,7 @@ Table matches {
score2 Int
gametype String
participant1 users [not null]
participant2 users [not null]
}

Table messages {
Expand Down Expand Up @@ -141,6 +143,8 @@ Ref: blocked_friends.blocked_id > users.userId

Ref: matches.participant1Id > users.userId

Ref: matches.participant2Id > users.userId

Ref: messages.roomId > rooms.id [delete: Cascade]

Ref: messages.authorId > users.userId
Expand Down
52 changes: 52 additions & 0 deletions backend/code/prisma/migrations/20231030183113_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Warnings:
- A unique constraint covering the columns `[createdAt]` on the table `messages` will be added. If there are existing duplicate values, this will fail.
- A unique constraint covering the columns `[Username]` on the table `users` will be added. If there are existing duplicate values, this will fail.
*/
-- CreateEnum
CREATE TYPE "NotifType" AS ENUM ('addFriend');

-- DropForeignKey
ALTER TABLE "messages" DROP CONSTRAINT "messages_roomId_fkey";

-- AlterTable
ALTER TABLE "blocked_friends" ADD COLUMN "dmRoomId" TEXT;

-- AlterTable
ALTER TABLE "room_members" ADD COLUMN "bannedAt" TIMESTAMP(3);

-- AlterTable
ALTER TABLE "users" ADD COLUMN "tfaSecret" TEXT,
ADD COLUMN "tfaStatus" BOOLEAN NOT NULL DEFAULT false;

-- CreateTable
CREATE TABLE "notifications" (
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"id" TEXT NOT NULL,
"recipientId" TEXT NOT NULL,
"content" "NotifType" NOT NULL,
"is_read" BOOLEAN NOT NULL DEFAULT false,
"readAt" TIMESTAMP(3),

CONSTRAINT "notifications_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "notifications_createdAt_key" ON "notifications"("createdAt");

-- CreateIndex
CREATE UNIQUE INDEX "messages_createdAt_key" ON "messages"("createdAt");

-- CreateIndex
CREATE UNIQUE INDEX "users_Username_key" ON "users"("Username");

-- AddForeignKey
ALTER TABLE "matches" ADD CONSTRAINT "matches_participant2Id_fkey" FOREIGN KEY ("participant2Id") REFERENCES "users"("userId") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "messages" ADD CONSTRAINT "messages_roomId_fkey" FOREIGN KEY ("roomId") REFERENCES "rooms"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "messages" ADD CONSTRAINT "messages_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "users"("userId") ON DELETE RESTRICT ON UPDATE CASCADE;
4 changes: 3 additions & 1 deletion backend/code/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ model User {
tfaStatus Boolean @default(false)
left_friends Friend[] @relation("from")
right_friends Friend[] @relation("to")
matches Match[] @relation("participant1")
participant1 Match[] @relation("participant1")
participant2 Match[] @relation("participant2")
owned_rooms Room[] @relation("owner")
roomMember RoomMember[]
blocked_by BlockedUsers[] @relation("blocked_by")
Expand Down Expand Up @@ -86,6 +87,7 @@ model Match {
score2 Int?
gametype String?
participant1 User @relation("participant1", fields: [participant1Id], references: [userId])
participant2 User @relation("participant2", fields: [participant2Id], references: [userId])
@@map("matches")
}
Expand Down
14 changes: 13 additions & 1 deletion backend/code/src/game/game.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Controller, Post } from '@nestjs/common';
import { Controller, Get, Post, Query, UseGuards } from '@nestjs/common';
import { GameService } from './game.service';
import { AtGuard } from 'src/auth/guards/at.guard';
import { GetCurrentUser } from 'src/auth/decorator/get_current_user.decorator';
import { QueryOffsetDto } from 'src/friends/dto/query-ofsset-dto';

@Controller('game')
export class GameController {
Expand All @@ -9,4 +12,13 @@ export class GameController {
startGame() {
// return this.gameService.startGame();
}

@Get('history')
@UseGuards(AtGuard)
getHistory(
@GetCurrentUser('userId') userId: string,
@Query() { offset, limit }: QueryOffsetDto,
) {
return this.gameService.getHistory(userId, offset, limit);
}
}
68 changes: 67 additions & 1 deletion backend/code/src/game/game.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Injectable } from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter';
import { PrismaService } from 'src/prisma/prisma.service';
import { PICTURE } from 'src/profile/dto/profile.dto';

@Injectable()
export class GameService {
constructor() {
constructor(private readonly prisma: PrismaService) {
// this.launchGame();
}

Expand All @@ -25,4 +27,68 @@ export class GameService {
}
}, 1000);
}

async getHistory(userId: string, offset: number, limit: number) {
const matches = await this.prisma.match.findMany({
skip: offset,
take: limit,
where: {
OR: [
{
participant1Id: userId,
},
{
participant2Id: userId,
},
],
},
orderBy: {
createdAt: 'desc',
},
select: {
createdAt: true,
score1: true,
score2: true,
participant1: {
select: {
Username: true,
avatar: true,
},
},
participant2: {
select: {
Username: true,
avatar: true,
},
},
},
});
return matches.map((match) => {
const avatar1: PICTURE = {
thumbnail: `https://res.cloudinary.com/trandandan/image/upload/c_thumb,h_48,w_48/${match.participant1.avatar}`,
medium: `https://res.cloudinary.com/trandandan/image/upload/c_thumb,h_72,w_72/${match.participant1.avatar}`,
large: `https://res.cloudinary.com/trandandan/image/upload/c_thumb,h_128,w_128/${match.participant1.avatar}`,
};
const avatar2: PICTURE = {
thumbnail: `https://res.cloudinary.com/trandandan/image/upload/c_thumb,h_48,w_48/${match.participant2.avatar}`,
medium: `https://res.cloudinary.com/trandandan/image/upload/c_thumb,h_72,w_72/${match.participant2.avatar}`,
large: `https://res.cloudinary.com/trandandan/image/upload/c_thumb,h_128,w_128/${match.participant2.avatar}`,
};
return {
match: {
createdAt: match.createdAt,
Player1: {
username: match.participant1.Username,
score: match.score1,
avatar: avatar1,
},
Player2: {
username: match.participant2.Username,
score: match.score2,
avatar: avatar2,
},
},
};
});
}
}
15 changes: 13 additions & 2 deletions backend/code/src/profile/profile.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Param,
ParseFilePipe,
Post,
Query,
UploadedFile,
UseGuards,
UseInterceptors,
Expand All @@ -29,16 +30,17 @@ import {
ApiTags,
} from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { QueryOffsetDto } from 'src/friends/dto/query-ofsset-dto';

@ApiTags('profile')
@ApiCookieAuth('X-Acces-Token')
@Controller('profile')
export class ProfileController {
constructor(private readonly profileService: ProfileService) {}
constructor(private readonly profileService: ProfileService) { }

@Get('me')
@ApiOkResponse({ type: ProfileDto })
@UseGuards(AtGuard)
@UseGuards(AuthGuard('jwt'))
async getMe(@GetCurrentUser('userId') userId: string): Promise<ProfileDto> {
return await this.profileService.getProfile(userId);
}
Expand Down Expand Up @@ -106,4 +108,13 @@ export class ProfileController {
getAvatar(@Param('id') recourseId: string) {
return this.profileService.getAvatar(recourseId);
}

@Get('notifications')
@UseGuards(AtGuard)
getNotifications(
@GetCurrentUser('userId') userId: string,
@Query() { offset, limit }: QueryOffsetDto,
) {
return this.profileService.getNotifications(userId, offset, limit);
}
}
16 changes: 15 additions & 1 deletion backend/code/src/profile/profile.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ import { UsersService } from 'src/users/users.service';
import { ProfileDto } from './dto/profile.dto';
import { UpdateProfileDto } from './dto/update-profile.dto';
import { UploadApiResponse, v2 as cloudinary } from 'cloudinary';
import { PrismaService } from 'src/prisma/prisma.service';
import { Readable } from 'stream';

@Injectable()
export class ProfileService {
constructor(private usersService: UsersService) {}
constructor(
private usersService: UsersService,
private readonly prisma: PrismaService,
) {}

async getProfile(userId: string): Promise<ProfileDto> {
const user = await this.usersService.getUserById(userId);
Expand Down Expand Up @@ -98,4 +102,14 @@ export class ProfileService {
});
return result;
}

async getNotifications(userId: string, offset: number, limit: number) {
return await this.prisma.notification.findMany({
skip: offset,
take: limit,
where: {
recipientId: userId,
},
});
}
}
10 changes: 10 additions & 0 deletions backend/code/src/rooms/rooms.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ export class RoomsController {
return await this.roomsService.banMember(memberData, userId);
}

@Post('unban')
@HttpCode(HttpStatus.OK)
@UseGuards(AtGuard)
async unbanMember(
@Body() memberData: ChangeOwnerDto,
@GetCurrentUser('userId') userId: string,
) {
return await this.roomsService.unbanMember(memberData, userId);
}

@Post('add')
@HttpCode(HttpStatus.OK)
@UseGuards(AtGuard)
Expand Down
Loading

0 comments on commit 017cd3f

Please sign in to comment.