Skip to content

Commit

Permalink
tentative de faire du front
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Jaros committed Apr 7, 2023
1 parent f41e60b commit ed6eeb5
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 34 deletions.
5 changes: 0 additions & 5 deletions back/nest_project/src/chat/channel/channel.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export class ChannelService {
}

async listChannelsWhereUserIsNot(user: UserEntity): Promise<ChannelEntity[]> {
//console.log("listchannelwhere....", user.id);
const allChannels = await this.listChannelsWithUsers();
let channListToReturn: ChannelEntity[] = [];
allChannels.forEach(channel => {
Expand Down Expand Up @@ -137,7 +136,6 @@ export class ChannelService {
toReturn.push({user: user, status: "normal", connected: false});
}
)
console.log(toReturn)
return toReturn;
}

Expand All @@ -151,8 +149,6 @@ export class ChannelService {
async getUserInChannel(channelId: string, userId: string): Promise<{user: UserEntity, status: string}> {
const usersArray = await this.listUsersInChannel(channelId, false);
for (let elt of usersArray) {
console.log(elt.user.id)
console.log(userId)
if (elt.user.id == userId)
return ({user: elt.user, status: elt.status});
}
Expand Down Expand Up @@ -219,7 +215,6 @@ export class ChannelService {
.where("channel.id = :id", { id: channelId })
.orderBy("messages.date", "ASC")
.getRawMany();
console.log("msgs: ", msgs);
return msgs;
}

Expand Down
22 changes: 11 additions & 11 deletions back/nest_project/src/chat/chat.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,16 @@ export class ChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa
})
}

@SubscribeMessage('inviteUser')
handleInviteUser(@MessageBody() data: inviteUserDto, @ConnectedSocket() client: Socket) {
this.tokenChecker(client)
.then((user) => {
if (user != null)
this.chatService.inviteUserEvent(client, user.login, this.chatRoomHandler, this.logger, data.userToInvite, data.channelId);
else
client.emit('notice', 'Your token is invalid, please log out then sign in');
})
}
// @SubscribeMessage('inviteUser')
// handleInviteUser(@MessageBody() data: inviteUserDto, @ConnectedSocket() client: Socket) {
// this.tokenChecker(client)
// .then((user) => {
// if (user != null)
// this.chatService.inviteUserEvent(client, user.login, this.chatRoomHandler, this.logger, data.userToInvite, data.channelId);
// else
// client.emit('notice', 'Your token is invalid, please log out then sign in');
// })
// }

@SubscribeMessage('createChannel')
handleCreateChannel(@MessageBody() data: createChannelDto, @ConnectedSocket() client: Socket) {
Expand Down Expand Up @@ -180,7 +180,7 @@ export class ChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa
this.tokenChecker(client)
.then((user) => {
if (user != null)
this.chatService.kickUserEvent(client, user.login, this.chatRoomHandler, this.logger, data.userToKick, data.channelId);
this.chatService.kickUserEvent(client, user.id, this.chatRoomHandler, this.logger, data.userToKick, data.channelId);
else
client.emit('notice', 'Your token is invalid, please log out then sign in');
})
Expand Down
14 changes: 6 additions & 8 deletions back/nest_project/src/chat/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ export class ChatService {

private async delUserFromChannel(userId: string, channelId: string, roomHandler: UserRoomHandler) {
await this.channelService.delUser(userId, channelId);
let room = roomHandler.roomMap.of(channelId)
if (room != undefined)
room.emit("userLeaveChannel", userId);
let socketMap = roomHandler.userMap.get(userId);
if (socketMap != undefined) {
let channel: IChannelToEmit = await this.channelService.getOneById(channelId);
Expand All @@ -52,9 +55,6 @@ export class ChatService {
}
});
}
// let chan: ChannelEntity = await this.channelService.getOneById(channelId);
// if (chan == null)
// roomHandler.roomKill(channelId);
}

public connectEvent(client: Socket, user: UserEntity, chatNamespace: Namespace, roomHandler: UserRoomHandler, logger: Logger) {
Expand Down Expand Up @@ -135,7 +135,6 @@ export class ChatService {
return;
}
else {
console.log("entries: ", user.id, loc);
this.userService.getChannelLink(user.id, loc)
.then(
(found) => {
Expand Down Expand Up @@ -166,7 +165,6 @@ export class ChatService {
(found) => {
if (found != null) {
roomHandler.joinRoom(user.id, client, found.id, false, false, false);
console.log('user currently in room : ', roomHandler.socketMap.sockets.get(client).room, roomHandler.socketMap.sockets.get(client).isChannel)
this.messagePrivateService.findConversation(user.id, found.id)
.then(
(messages) => {
Expand Down Expand Up @@ -221,7 +219,6 @@ export class ChatService {
* @param roomHandler
*/
async listUsersInChannel(client: Socket, channelId: string, roomHandler: UserRoomHandler) {
console.log("blop")
let usersArray: {user: IUserToEmit, status: string, connected: boolean}[] = await this.channelService.listUsersInChannel(channelId, true);
usersArray.forEach((elt) => {
let connected = roomHandler.userMap.get(elt.user.id);
Expand All @@ -245,8 +242,9 @@ export class ChatService {
this.channelService.addNormalUser(user, channel.id)
.then(() => {
let room = roomHandler.roomMap.of(channel.id);
if (room != undefined)
room.emit("newUserInChannel", user.id, user.login);
if (room != undefined) {
room.emit("newUserInChannel", user.id, user.login, true);
}
let channelToEmit: IChannelToEmit = channel;
roomHandler.emitToUserHavingThisSocket(client, "channelJoined", {channel: channelToEmit, status: "normal"});
this.changeLocEvent(client, user, data.channelId, true, roomHandler);
Expand Down
1 change: 0 additions & 1 deletion back/nest_project/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ async function bootstrap() {
app.use(bodyParser.json({ limit: '1000mb' }));
app.use(bodyParser.urlencoded({ limit: '1000mb', extended: true }));
await app.listen(3000);
app.useGlobalPipes(new ValidationPipe());
// additional feature to make sure server shutdown gracefully to try and see
app.enableShutdownHooks();
}
Expand Down
2 changes: 0 additions & 2 deletions back/nest_project/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ export class UserService {
}
// SIGN IN OR DISPLAY ONE USER PROFILE BY ID
async findById(id: string): Promise<UserEntity> {
// console.log(`${id}`)
const toReturn = await this.usersRepository.findOneBy({id: id});
// console.log(`${id} apres ooooooooooooooooooooooooooooooooooooooooooo findById`)
return toReturn
}

Expand Down
9 changes: 6 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ services:
- .env
networks:
- transcendence_network
volumes:
- ./postgres:/var/lib/postgresql/users

########################################## BACK-END NEST JS ##################################
back:
image: nestjs_img
Expand Down Expand Up @@ -67,19 +70,19 @@ volumes:
driver_opts:
type: none
o: bind
device: /home/alexi/Bureau/Dev/42-ft-transcendence/postgres
device: ./postgres
back:
driver: local
driver_opts:
type: none
o: bind
device: /home/alexi/Bureau/Dev/42-ft-transcendence/back
device: ./back
front:
driver: local
driver_opts:
type: none
o: bind
device: /home/alexi/Bureau/Dev/42-ft-transcendence/front
device: ./front
# pgadmin:
# driver: local
# driver_opts:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class SearchElement extends React.Component<{socket: Socket, popupAction: any, h
}
handlerJoinChannel() {
if (!this.props.elt.password) {
console.log("I join");
this.props.socket.emit('joinChannel', {channelId: this.props.elt.id, channelPass: null});
this.props.handleClose();
}
Expand Down
63 changes: 59 additions & 4 deletions front/react_project/src/components/ChatModule/ChatSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export function SidebarChannel(props: {dest: IDest, handleClose: any}) {
const [onClickMembers, setOnClickMembers] = useState<boolean>(false);
const [onClickSettings, setOnClickSettings] = useState<boolean>(false);
const me: JwtPayload = accountService.readPayload()!;
const [myGrade, setGrade] = useState<string>("normal");

const leaveChannel = () => {
// console.log(props.dest.id)
Expand Down Expand Up @@ -98,13 +99,58 @@ export function SidebarChannel(props: {dest: IDest, handleClose: any}) {
}
}

const kickUser = (e: any) => {
socket.emit("kickUser", {userToKick: e.target.value, channelId: props.dest.id});
}

const deOp = () => {
console.log("deOp!");
}

const doOp = () => {
console.log("doOp!");
}

useEffect(() => {
console.log(props.dest.id)
socket.emit('listUsersChann', {channelId: props.dest.id});
socket.on('listUsersChann', (list: {user: {id: string, login: string}, status: string, connected: boolean}[]) => {
console.log("list")
console.log("list", list);
setMembers(list);
})
socket.on("channelLeaved", (userId: string) => {
console.log("user leaved", userId);
let newMembers = members.filter((member) => {
return member.user.id != userId
});
setMembers(newMembers);
})
socket.on("newUserInChannel", (id: string, login: string, connected: boolean) => {
let newMembers = members;
let user: {user: {id: string, login: string}, status: string, connected: boolean} = {
user: {
id: id,
login: login
},
status: "normal",
connected: connected
}
newMembers.push(user);
// newMembers.sort((a, b) => {
// if (a.status == "god" && b.status != "god")
// return (-1)
// if (a.status == "op") {
// if (b.status == "god")
// return (1)
// if (b.status == "normal")
// return (-1)
// }
// if (a.status == "normal" && b.status != "normal")
// return (1)
// return (a.user.login.localeCompare(b.user.login))
// })
setMembers(newMembers);
})
document.addEventListener("mousedown", handleClickOutside);
return () => {
document.removeEventListener("mousedown", handleClickOutside);
Expand All @@ -120,10 +166,19 @@ export function SidebarChannel(props: {dest: IDest, handleClose: any}) {
<ul className="paramMenu">
<li onClick={listMembers}>Members</li>
{onClickMembers && (
<ul className="memberList">{members.map(
<ul className="memberList">{
members.map(
(member, id) => {
if (member.user.login !== me.login)
return (<li key={id} onClick={showUserParam}>{member.user.login}</li>)
if (member.user.login !== me.login) {
if (props.dest.status == "normal")
return (<li key={id}><button onClick={showUserParam}>{member.user.login}</button> </li>)
else if (props.dest.status == "op" && member.status == "normal")
return (<li key={id}><button onClick={showUserParam}>{member.user.login}</button> <button value={member.user.id} onClick={kickUser}>kickUser</button></li>)
else if (props.dest.status == "god")
return (<li key={id}><button onClick={showUserParam}>{member.user.login}</button> <button value={member.user.id} onClick={kickUser}>kickUser</button>
{ member.status == "op" ? (<button onClick={deOp}>deOp</button>) : (<button onClick={doOp}>doOp</button>) }
</li>)
}
else
return (<li key={id}>{member.user.login}<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M288 192h17.1c22.1 38.3 63.5 64 110.9 64c11 0 21.8-1.4 32-4v4 32V480c0 17.7-14.3 32-32 32s-32-14.3-32-32V339.2L248 448h56c17.7 0 32 14.3 32 32s-14.3 32-32 32H160c-53 0-96-43-96-96V192.5c0-16.1-12-29.8-28-31.8l-7.9-1C10.5 157.6-1.9 141.6 .2 124s18.2-30 35.7-27.8l7.9 1c48 6 84.1 46.8 84.1 95.3v85.3c34.4-51.7 93.2-85.8 160-85.8zm160 26.5v0c-10 3.5-20.8 5.5-32 5.5c-28.4 0-54-12.4-71.6-32h0c-3.7-4.1-7-8.5-9.9-13.2C325.3 164 320 146.6 320 128v0V32 12 10.7C320 4.8 324.7 .1 330.6 0h.2c3.3 0 6.4 1.6 8.4 4.2l0 .1L352 21.3l27.2 36.3L384 64h64l4.8-6.4L480 21.3 492.8 4.3l0-.1c2-2.6 5.1-4.2 8.4-4.2h.2C507.3 .1 512 4.8 512 10.7V12 32v96c0 17.3-4.6 33.6-12.6 47.6c-11.3 19.8-29.6 35.2-51.4 42.9zM400 128a16 16 0 1 0 -32 0 16 16 0 1 0 32 0zm48 16a16 16 0 1 0 0-32 16 16 0 1 0 0 32z"/></svg></li>)
}
Expand Down

0 comments on commit ed6eeb5

Please sign in to comment.