Browse Source

divers things

master
nicolas-arnaud 2 years ago
parent
commit
02bad3b07a
  1. 25
      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

25
back/volume/src/chat/chat.controller.ts

@ -44,26 +44,15 @@ export class ChatController {
} }
const dms = channels.filter((channel: Channel) => { const dms = channels.filter((channel: Channel) => {
return ( return (
(channel.name === user.ftId + '&' + other.ftId || (channel.name === `${user.ftId}&${other.ftId}` ||
channel.name === other.ftId + '&' + user.ftId) && channel.name === `${other.ftId}&${user.ftId}`) &&
channel.isPrivate && (channel.password === undefined || channel.password === '') &&
(channel.password === undefined || channel.password === '') channel.isPrivate
) )
}) })
if (dms && dms.length === 0) { if (dms.length === 0) {
throw new BadRequestException('No DMS found') 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 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) const channel = await this.chatService.getFullChannel(msg.chan)
if ( if (
channel.owner.id !== msg.from && 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') 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) => { channel.banned = channel.banned.filter((data) => {
return Date.now() - data[1] > 0 return Date.now() - data[1] > 0
}) })
void this.update( channel) void this.update(channel)
} }
} }
async addUserToChannel (channel: Channel, user: User): Promise<Channel> { async addUserToChannel (channel: Channel, user: User): Promise<Channel> {
channel.users.push(user) channel.users.push(user)
return await this.ChannelRepository.save(channel) await this.save(channel)
return channel
} }
async getChannel (id: number): Promise<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() const msg = new Message()
msg.text = message.text msg.text = message.text
msg.channel = await this.channelService.getChannel(message.ChannelId) 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) return await this.MessageRepository.save(msg)
} }
@ -30,15 +31,16 @@ export class MessageService {
channel: Channel, channel: Channel,
user: User user: User
): Promise<Message[]> { ): Promise<Message[]> {
console.log('findMessagesInChannelForUser', channel.id, user.ftId)
const blockeds = user.blocked.map((u) => +u.ftId) const blockeds = user.blocked.map((u) => +u.ftId)
console.log(JSON.stringify(blockeds))
const messages = await this.MessageRepository.createQueryBuilder('message') const messages = await this.MessageRepository.createQueryBuilder('message')
.innerJoin('message.channel', 'channel') .innerJoin('message.channel', 'channel')
.where('channel.id = :chanId', { chanId: channel.id }) .where('channel.id = :chanId', { chanId: channel.id })
.leftJoinAndSelect('message.author', 'author') .leftJoinAndSelect('message.author', 'author')
.orderBy('message.created_at', 'ASC') .orderBy('message.created_at', 'ASC')
.getMany() .getMany()
messages.forEach((msg) => {
msg.author.socketKey = ''
})
return messages.filter((m) => !blockeds.includes(m.author.ftId)) 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) await this.usersRepository.save(user)
} }
async update (user: User, changes: UserDto): Promise<void> {
this.usersRepository.update({id: user.id}, changes)
}
async findUsers (): Promise<User[]> { async findUsers (): Promise<User[]> {
const users = await this.usersRepository.find({}) const users = await this.usersRepository.find({})
users.forEach((usr) => (usr.socketKey = '')) users.forEach((usr) => (usr.socketKey = ''))
return users 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> { async findUserByName (username: string): Promise<User> {
const user = await this.usersRepository.findOne({ const user = await this.usersRepository.findOne({
where: { username }, where: { username },
@ -41,7 +47,7 @@ export class UsersService {
if (Date.now() - usr.lastAccess > 60000) { if (Date.now() - usr.lastAccess > 60000) {
usr.isVerified = false usr.isVerified = false
usr.status = 'offline' usr.status = 'offline'
this.usersRepository.save(usr).catch((err) => { this.update(usr, usr).catch((err) => {
console.log(err) console.log(err)
}) })
} }
@ -49,12 +55,12 @@ export class UsersService {
await this.getLeaderboard() await this.getLeaderboard()
} }
async findUser (ftId: number): Promise<User | null> { async findUser (ftId: number): Promise<User> {
const user = await this.usersRepository.findOneBy({ ftId }) 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() user.lastAccess = Date.now()
if (user.status === 'offline') user.status = 'online' if (user.status === 'offline') user.status = 'online'
await this.usersRepository.save(user) await this.update.(user)
return user return user
} }
@ -94,11 +100,6 @@ export class UsersService {
.getMany() .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> { async addAvatar (ftId: number, filename: string): Promise<void> {
await this.usersRepository.update({ ftId }, { avatar: filename }) await this.usersRepository.update({ ftId }, { avatar: filename })
} }

7
front/volume/src/App.svelte

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

3
front/volume/src/components/Channels.svelte

@ -70,6 +70,7 @@
socket.emit("joinChannel", { socket.emit("joinChannel", {
UserId: $store.ftId, UserId: $store.ftId,
ChannelId: id, ChannelId: id,
}); });
}; };
@ -95,6 +96,8 @@
export let onSelectChannel: (channel: ChannelsType) => void; export let onSelectChannel: (channel: ChannelsType) => void;
export const selectChat = (id: number) => { export const selectChat = (id: number) => {
console.log("channel: ", id) console.log("channel: ", id)
popup.set(bind(Alert, {message:"Did not find channel"}))
getChannels().then(() => { getChannels().then(() => {
const channel = channels.find((c) => c.id === id); const channel = channels.find((c) => c.id === id);
if (channel) { if (channel) {

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

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

Loading…
Cancel
Save