diff --git a/back/volume/src/chat/chat.controller.ts b/back/volume/src/chat/chat.controller.ts index a8e760b..ba39ccf 100644 --- a/back/volume/src/chat/chat.controller.ts +++ b/back/volume/src/chat/chat.controller.ts @@ -44,26 +44,15 @@ export class ChatController { } const dms = channels.filter((channel: Channel) => { return ( - (channel.name === user.ftId + '&' + other.ftId || - channel.name === other.ftId + '&' + user.ftId) && - channel.isPrivate && - (channel.password === undefined || channel.password === '') + (channel.name === `${user.ftId}&${other.ftId}` || + channel.name === `${other.ftId}&${user.ftId}`) && + (channel.password === undefined || channel.password === '') && + channel.isPrivate ) }) - if (dms && dms.length === 0) { - throw new BadRequestException('No DMS found') - } - dms.forEach((c) => { - if (c.users) { - c.users.forEach((u) => (u.socketKey = '')) - } - if (c.admins) { - c.admins.forEach((u) => (u.socketKey = '')) - } - if (c.owner) { - c.owner.socketKey = '' - } - }) + if (dms.length === 0) { + throw new BadRequestException('No DMS found') + } return dms } diff --git a/back/volume/src/chat/chat.gateway.ts b/back/volume/src/chat/chat.gateway.ts index b4b5454..94534be 100644 --- a/back/volume/src/chat/chat.gateway.ts +++ b/back/volume/src/chat/chat.gateway.ts @@ -106,7 +106,7 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { const channel = await this.chatService.getFullChannel(msg.chan) if ( channel.owner.id !== msg.from && - channel.admins.find((e) => e.id == msg.from) == null + channel.admins.findIndex((usr) => usr.id === msg.from) === -1 ) { throw new WsException('You do not have the required privileges') } diff --git a/back/volume/src/chat/chat.service.ts b/back/volume/src/chat/chat.service.ts index d2d9a26..479abea 100644 --- a/back/volume/src/chat/chat.service.ts +++ b/back/volume/src/chat/chat.service.ts @@ -125,13 +125,14 @@ export class ChatService { channel.banned = channel.banned.filter((data) => { return Date.now() - data[1] > 0 }) - void this.update( channel) + void this.update(channel) } } async addUserToChannel (channel: Channel, user: User): Promise { channel.users.push(user) - return await this.ChannelRepository.save(channel) + await this.save(channel) + return channel } async getChannel (id: number): Promise { diff --git a/back/volume/src/chat/message.service.ts b/back/volume/src/chat/message.service.ts index ad3d81f..c2d5661 100644 --- a/back/volume/src/chat/message.service.ts +++ b/back/volume/src/chat/message.service.ts @@ -22,7 +22,8 @@ export class MessageService { const msg = new Message() msg.text = message.text msg.channel = await this.channelService.getChannel(message.ChannelId) - msg.author = (await this.usersService.findUser(message.UserId)) as User + msg.author = await this.usersService.findUser(message.UserId) + msg.author.socketKey = '' return await this.MessageRepository.save(msg) } @@ -30,15 +31,16 @@ export class MessageService { channel: Channel, user: User ): Promise { - console.log('findMessagesInChannelForUser', channel.id, user.ftId) const blockeds = user.blocked.map((u) => +u.ftId) - console.log(JSON.stringify(blockeds)) const messages = await this.MessageRepository.createQueryBuilder('message') .innerJoin('message.channel', 'channel') .where('channel.id = :chanId', { chanId: channel.id }) .leftJoinAndSelect('message.author', 'author') .orderBy('message.created_at', 'ASC') .getMany() + messages.forEach((msg) => { + msg.author.socketKey = '' + }) return messages.filter((m) => !blockeds.includes(m.author.ftId)) } } diff --git a/back/volume/src/users/users.service.ts b/back/volume/src/users/users.service.ts index 2edf18e..f17fa53 100644 --- a/back/volume/src/users/users.service.ts +++ b/back/volume/src/users/users.service.ts @@ -19,12 +19,18 @@ export class UsersService { await this.usersRepository.save(user) } + async update (user: User, changes: UserDto): Promise { + this.usersRepository.update({id: user.id}, changes) + } + async findUsers (): Promise { const users = await this.usersRepository.find({}) users.forEach((usr) => (usr.socketKey = '')) return users } + // WARNING: socketKey isn't removed here. it must be done before + // any return from it in a route. async findUserByName (username: string): Promise { const user = await this.usersRepository.findOne({ where: { username }, @@ -41,7 +47,7 @@ export class UsersService { if (Date.now() - usr.lastAccess > 60000) { usr.isVerified = false usr.status = 'offline' - this.usersRepository.save(usr).catch((err) => { + this.update(usr, usr).catch((err) => { console.log(err) }) } @@ -49,12 +55,12 @@ export class UsersService { await this.getLeaderboard() } - async findUser (ftId: number): Promise { + async findUser (ftId: number): Promise { const user = await this.usersRepository.findOneBy({ ftId }) - if (user == null) return null + if (user == null) throw new BadRequestException("User not exist") user.lastAccess = Date.now() if (user.status === 'offline') user.status = 'online' - await this.usersRepository.save(user) + await this.update.(user) return user } @@ -94,11 +100,6 @@ export class UsersService { .getMany() } - async update (user: User, changes: UserDto): Promise { - this.usersRepository.merge(user, changes) - return await this.usersRepository.save(user) - } - async addAvatar (ftId: number, filename: string): Promise { await this.usersRepository.update({ ftId }, { avatar: filename }) } diff --git a/front/volume/src/App.svelte b/front/volume/src/App.svelte index 8ab0fa2..99a9e47 100644 --- a/front/volume/src/App.svelte +++ b/front/volume/src/App.svelte @@ -214,9 +214,8 @@ {clickChannels} {clickLeaderboard} /> - {#if appState.includes(APPSTATE.CHANNELS)} + {#if appState.includes(`${APPSTATE.CHANNELS}#`)}
setAppState(APPSTATE.CHANNELS)} on:keydown={() => setAppState(APPSTATE.CHANNELS)} > @@ -228,8 +227,10 @@ on:send-message={openDirectChat} />
+ {/if} + {#if appState.includes(APPSTATE.CHANNELS)}
diff --git a/front/volume/src/components/Channels.svelte b/front/volume/src/components/Channels.svelte index 974f138..470fbf9 100644 --- a/front/volume/src/components/Channels.svelte +++ b/front/volume/src/components/Channels.svelte @@ -70,6 +70,7 @@ socket.emit("joinChannel", { UserId: $store.ftId, ChannelId: id, + }); }; @@ -95,6 +96,8 @@ export let onSelectChannel: (channel: ChannelsType) => void; export const selectChat = (id: number) => { console.log("channel: ", id) + + popup.set(bind(Alert, {message:"Did not find channel"})) getChannels().then(() => { const channel = channels.find((c) => c.id === id); if (channel) { diff --git a/front/volume/src/components/Chat.svelte b/front/volume/src/components/Chat.svelte index 5a476e1..30fb69e 100644 --- a/front/volume/src/components/Chat.svelte +++ b/front/volume/src/components/Chat.svelte @@ -25,7 +25,7 @@ getMembers(); usersInterval = setInterval(async () => { getMembers(); - }, 1000); + }, 5000); }); socket.on("messages", (msgs: Array) => {