Browse Source

divers things

master
nicolas-arnaud 2 years ago
parent
commit
02bad3b07a
  1. 21
      back/volume/src/chat/chat.controller.ts
  2. 2
      back/volume/src/chat/chat.gateway.ts
  3. 5
      back/volume/src/chat/chat.service.ts
  4. 8
      back/volume/src/chat/message.service.ts
  5. 19
      back/volume/src/users/users.service.ts
  6. 7
      front/volume/src/App.svelte
  7. 3
      front/volume/src/components/Channels.svelte
  8. 2
      front/volume/src/components/Chat.svelte

21
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) {
if (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 = ''
}
})
return dms
}

2
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')
}

5
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> {
channel.users.push(user)
return await this.ChannelRepository.save(channel)
await this.save(channel)
return channel
}
async getChannel (id: number): Promise<Channel> {

8
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<Message[]> {
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))
}
}

19
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<void> {
this.usersRepository.update({id: user.id}, changes)
}
async findUsers (): Promise<User[]> {
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<User> {
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<User | null> {
async findUser (ftId: number): Promise<User> {
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<User | null> {
this.usersRepository.merge(user, changes)
return await this.usersRepository.save(user)
}
async addAvatar (ftId: number, filename: string): Promise<void> {
await this.usersRepository.update({ ftId }, { avatar: filename })
}

7
front/volume/src/App.svelte

@ -214,9 +214,8 @@
{clickChannels}
{clickLeaderboard}
/>
{#if appState.includes(APPSTATE.CHANNELS)}
{#if appState.includes(`${APPSTATE.CHANNELS}#`)}
<div
class="{appState.replace(APPSTATE.CHANNELS, "") === "" ? 'hidden' : ''}"
on:click={() => setAppState(APPSTATE.CHANNELS)}
on:keydown={() => setAppState(APPSTATE.CHANNELS)}
>
@ -228,8 +227,10 @@
on:send-message={openDirectChat}
/>
</div>
{/if}
{#if appState.includes(APPSTATE.CHANNELS)}
<div
class="{appState.replace(APPSTATE.CHANNELS, "") !== "" ? 'hidden' : ''}"
class="{appState !== APPSTATE.CHANNELS ? 'hidden' : ''}"
on:click={resetAppState}
on:keydown={resetAppState}
>

3
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) {

2
front/volume/src/components/Chat.svelte

@ -25,7 +25,7 @@
getMembers();
usersInterval = setInterval(async () => {
getMembers();
}, 1000);
}, 5000);
});
socket.on("messages", (msgs: Array<chatMessagesType>) => {

Loading…
Cancel
Save