Skip to content

Commit

Permalink
resolve conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
sedeve committed Oct 30, 2023
2 parents 6c1b2a4 + 75212df commit 1fda0c0
Show file tree
Hide file tree
Showing 39 changed files with 1,121 additions and 471 deletions.
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
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
78 changes: 60 additions & 18 deletions backend/code/src/rooms/rooms.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export class RoomsService {
if (room.ownerId !== userId)
throw new UnauthorizedException('You are not the owner of this room');
if (!member)
throw new UnauthorizedException('new owner is not a member of this room');
throw new UnauthorizedException('user is not a member of this room');
await this.prisma.room.update({
where: { id: roomData.roomId },
data: { owner: { connect: { userId: roomData.memberId } } },
Expand Down Expand Up @@ -251,7 +251,7 @@ export class RoomsService {
if (room.ownerId !== userId)
throw new UnauthorizedException('You are not the owner of this room');
if (!user)
throw new UnauthorizedException('new admin is not a member of this room');
throw new UnauthorizedException('user is not a member of this room');
if (user.is_admin || room.ownerId === roomData.memberId)
throw new UnauthorizedException(
'new admin is already an admin of this room',
Expand Down Expand Up @@ -334,6 +334,11 @@ export class RoomsService {
roomId: roomData.roomId,
},
},
select: {
room: true,
is_mueted: true,
userId: true,
},
});
if (!room) throw new HttpException('room not found', HttpStatus.NOT_FOUND);
if (!member)
Expand All @@ -342,6 +347,8 @@ export class RoomsService {
throw new UnauthorizedException('You are not admin of this room');
if (member.is_mueted)
throw new UnauthorizedException('member is already muted');
if (member.room.ownerId === roomData.memberId)
throw new UnauthorizedException('You cannot mute the owner');
if (member.userId === userId)
throw new UnauthorizedException('You can not mute yourself');
const afterFiveMin = new Date(Date.now() + 5 * 60 * 1000);
Expand Down Expand Up @@ -382,16 +389,13 @@ export class RoomsService {
const user = await this.prisma.roomMember.findUnique({
where: { unique_user_room: { userId: userId, roomId: roomId } },
});

if (!user)
throw new UnauthorizedException('You are not a member of this room');

const members = await this.prisma.roomMember.findMany({
skip: offset,
take: limit,
where: {
roomId: roomId,
is_banned: false,
},
select: {
user: {
Expand All @@ -402,22 +406,25 @@ export class RoomsService {
avatar: true,
},
},
is_banned: true,
},
});
return members.map((member) => {
const avatar: PICTURE = {
thumbnail: `https://res.cloudinary.com/trandandan/image/upload/c_thumb,h_48,w_48/${member.user.avatar}`,
medium: `https://res.cloudinary.com/trandandan/image/upload/c_thumb,h_72,w_72/${member.user.avatar}`,
large: `https://res.cloudinary.com/trandandan/image/upload/c_thumb,h_128,w_128/${member.user.avatar}`,
};
return {
id: member.user.userId,
name: { first: member.user.firstName, last: member.user.lastName },
avatar,
};
if (!member.is_banned || user.is_admin) {
const avatar: PICTURE = {
thumbnail: `https://res.cloudinary.com/trandandan/image/upload/c_thumb,h_48,w_48/${member.user.avatar}`,
medium: `https://res.cloudinary.com/trandandan/image/upload/c_thumb,h_72,w_72/${member.user.avatar}`,
large: `https://res.cloudinary.com/trandandan/image/upload/c_thumb,h_128,w_128/${member.user.avatar}`,
};
return {
id: member.user.userId,
firstname: member.user.firstName,
lastname: member.user.lastName,
avatar: avatar,
};
}
});
}

async banMember(memberData: ChangeOwnerDto, userId: string) {
const user = await this.prisma.roomMember.findUnique({
where: {
Expand All @@ -433,8 +440,8 @@ export class RoomsService {
if (userId == memberData.memberId)
throw new UnauthorizedException('You cannot ban yourself');
if (ownerId == memberData.memberId)
throw new UnauthorizedException('You cannot ban the Owner of thi Room');
return await this.prisma.roomMember.update({
throw new UnauthorizedException('You cannot ban the Owner of this Room');
await this.prisma.roomMember.update({
where: {
unique_user_room: {
userId: memberData.memberId,
Expand All @@ -446,6 +453,41 @@ export class RoomsService {
bannedAt: new Date(Date.now()),
},
});
return { message: 'member banned successfully' };
}

async unbanMember(memberData: ChangeOwnerDto, userId: string) {
const user = await this.prisma.roomMember.findUnique({
where: {
unique_user_room: { userId: userId, roomId: memberData.roomId },
},
});
const member = await this.prisma.roomMember.findUnique({
where: {
unique_user_room: {
userId: memberData.memberId,
roomId: memberData.roomId,
},
},
});
if (!member)
throw new HttpException('user not found', HttpStatus.NOT_FOUND);
if (!member.is_banned)
throw new HttpException('member is not banned', HttpStatus.BAD_REQUEST);
if (!user.is_admin)
throw new UnauthorizedException('You are not admin of this room');
await this.prisma.roomMember.update({
where: {
unique_user_room: {
userId: memberData.memberId,
roomId: memberData.roomId,
},
},
data: {
is_banned: false,
},
});
return { message: 'member unbanned successfully' };
}

async addMember(memberData: ChangeOwnerDto, userId: string) {
Expand Down
Binary file modified frontend/.DS_Store
Binary file not shown.
Loading

0 comments on commit 1fda0c0

Please sign in to comment.