-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
save chat messages in db and show on reload
- Loading branch information
Showing
22 changed files
with
173 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
20 changes: 20 additions & 0 deletions
20
apps/shelter-gateway/src/chat-messages/chat-messages.controller.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { ChatMessagesController } from './chat-messages.controller'; | ||
import { ChatMessagesService } from './chat-messages.service'; | ||
|
||
describe('ChatMessagesController', () => { | ||
let controller: ChatMessagesController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [ChatMessagesController], | ||
providers: [ChatMessagesService], | ||
}).compile(); | ||
|
||
controller = module.get<ChatMessagesController>(ChatMessagesController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
12 changes: 12 additions & 0 deletions
12
apps/shelter-gateway/src/chat-messages/chat-messages.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Controller, Get, Param } from '@nestjs/common'; | ||
import { ChatMessagesService } from './chat-messages.service'; | ||
|
||
@Controller('lobbies/:lobbyId/chat-messages') | ||
export class ChatMessagesController { | ||
constructor(private readonly chatMessagesService: ChatMessagesService) {} | ||
|
||
@Get('') | ||
getAllByLobbyId(@Param('lobbyId') lobbyId: string) { | ||
return this.chatMessagesService.getAllByLobbyId(lobbyId); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
apps/shelter-gateway/src/chat-messages/chat-messages.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ChatMessagesService } from './chat-messages.service'; | ||
import { ChatMessagesController } from './chat-messages.controller'; | ||
import { DatabaseModule } from '@app/common'; | ||
|
||
@Module({ | ||
imports: [DatabaseModule], | ||
controllers: [ChatMessagesController], | ||
providers: [ChatMessagesService], | ||
}) | ||
export class ChatMessagesModule {} |
18 changes: 18 additions & 0 deletions
18
apps/shelter-gateway/src/chat-messages/chat-messages.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { ChatMessagesService } from './chat-messages.service'; | ||
|
||
describe('ChatMessagesService', () => { | ||
let service: ChatMessagesService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ChatMessagesService], | ||
}).compile(); | ||
|
||
service = module.get<ChatMessagesService>(ChatMessagesService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
46 changes: 46 additions & 0 deletions
46
apps/shelter-gateway/src/chat-messages/chat-messages.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { DatabaseService, constants } from '@app/common'; | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class ChatMessagesService { | ||
constructor(private readonly databaseService: DatabaseService) {} | ||
|
||
// TODO: add caching | ||
async getAllByLobbyId(lobbyId: string) { | ||
const messages = // id, userId, lobbyId, text, mentionId, replyTo, created_at, updated_at | ||
await this.databaseService.getChatMessagesByLobbyId(lobbyId); | ||
|
||
// to avoid useless db calls | ||
const userMap: { [userId: string]: any } = {}; | ||
|
||
const res: any[] = []; | ||
for (const m of messages) { | ||
if (!userMap[m.userId]) { | ||
let user: any = await this.databaseService.getUserByIdOrNull(m.userId); | ||
// means that user is bot | ||
if (user) { | ||
userMap[user.id] = user; | ||
} else { | ||
user = constants.allBots.find((bot) => bot.userId === m.userId); | ||
userMap[user.userId] = user; | ||
} | ||
} | ||
const message = { | ||
sender: userMap[m.userId].displayName, | ||
senderId: m.userId, | ||
message: m.text, | ||
avatar: userMap[m.userId].avatar, | ||
timeSent: parseTimeStr(m.createdAt), | ||
}; | ||
res.push(message); | ||
} | ||
return res; // return in chat message format | ||
} | ||
} | ||
|
||
function parseTimeStr(date: Date) { | ||
const hour = date.getHours().toString().padStart(2, '0'); | ||
const minute = date.getMinutes().toString().padStart(2, '0'); | ||
const timeStr = `${hour}:${minute}`; | ||
return timeStr; | ||
} |
1 change: 1 addition & 0 deletions
1
apps/shelter-gateway/src/chat-messages/dto/create-chat-message.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export class CreateChatMessageDto {} |
4 changes: 4 additions & 0 deletions
4
apps/shelter-gateway/src/chat-messages/dto/update-chat-message.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { PartialType } from '@nestjs/mapped-types'; | ||
import { CreateChatMessageDto } from './create-chat-message.dto'; | ||
|
||
export class UpdateChatMessageDto extends PartialType(CreateChatMessageDto) {} |
1 change: 1 addition & 0 deletions
1
apps/shelter-gateway/src/chat-messages/entities/chat-message.entity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export class ChatMessage {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters