Browse Source
Допрацювання відправки текстових повідомлень. Reviewed-on: #40 Co-authored-by: Yevhen Romanenko <yevhen.romanenko.jetup@gmail.com> Co-committed-by: Yevhen Romanenko <yevhen.romanenko.jetup@gmail.com>pull/41/head
24 changed files with 411 additions and 99 deletions
Binary file not shown.
Binary file not shown.
@ -0,0 +1,123 @@
@@ -0,0 +1,123 @@
|
||||
import { storageService } from '@/services/system' |
||||
import { IChatMessage, StorageKey, appEvents } from '@/shared' |
||||
import _ from 'lodash' |
||||
|
||||
export class FakeMessageManager { |
||||
private FilterMessagesToExcludeDuplicateKeys( |
||||
messages: (Omit<IChatMessage, 'id'> & { id: number })[], |
||||
encodedMessages: IChatMessage[], |
||||
chatId: number, |
||||
) { |
||||
return messages.filter( |
||||
it => |
||||
!_.some( |
||||
it.chatId === chatId && encodedMessages, |
||||
msg => msg.uniqueKey === it.uniqueKey, |
||||
), |
||||
) |
||||
} |
||||
|
||||
public async addProcessingMsgs( |
||||
messages: (Omit<IChatMessage, 'id'> & { id: number })[], |
||||
chatId: number, |
||||
) { |
||||
const processingMsgs = await storageService.get(StorageKey.Messages) |
||||
const encodeProcessingMessages = JSON.parse( |
||||
processingMsgs || '[]', |
||||
) as IChatMessage[] |
||||
|
||||
if (!encodeProcessingMessages || _.isEmpty(encodeProcessingMessages)) { |
||||
return messages |
||||
} |
||||
|
||||
await this.deleteDeliveredMsgs(messages, encodeProcessingMessages) |
||||
|
||||
const updatedMessages = this.FilterMessagesToExcludeDuplicateKeys( |
||||
messages, |
||||
encodeProcessingMessages, |
||||
chatId, |
||||
) |
||||
|
||||
return updatedMessages |
||||
} |
||||
|
||||
public async remainingUndeliveredMsgs( |
||||
messages: IChatMessage[], |
||||
processingMsgs: IChatMessage[], |
||||
chatId: number, |
||||
) { |
||||
const filterMsg = processingMsgs.filter(it => it.chatId === chatId) |
||||
|
||||
const deliveredMsgs = processingMsgs.filter(it => |
||||
_.some(messages, msg => msg.uniqueKey === it.uniqueKey), |
||||
) |
||||
|
||||
await this.deleteDeliveredMsgs(messages, deliveredMsgs) |
||||
|
||||
const remainingMessages = filterMsg.filter( |
||||
it => !deliveredMsgs.some(msg => msg.uniqueKey === it.uniqueKey), |
||||
) |
||||
|
||||
return remainingMessages |
||||
} |
||||
|
||||
protected async deleteDeliveredMsgs( |
||||
messages: IChatMessage[], |
||||
processingMsgs: IChatMessage[], |
||||
) { |
||||
const deliveredMsgs = processingMsgs.filter(it => |
||||
_.some(messages, msg => msg.uniqueKey === it.uniqueKey), |
||||
) |
||||
|
||||
if (_.isEmpty(deliveredMsgs)) { |
||||
return |
||||
} |
||||
|
||||
const restProcessingMsgs = processingMsgs.filter( |
||||
it => !_.some(messages, msg => msg.uniqueKey === it.uniqueKey), |
||||
) |
||||
|
||||
const value = JSON.stringify(restProcessingMsgs) |
||||
await storageService.set(StorageKey.Messages, value) |
||||
} |
||||
|
||||
public async storeFakeMessage(message: IChatMessage) { |
||||
const fakedMsgs = await storageService.get(StorageKey.Messages) |
||||
const encodeFakeMessage = JSON.parse( |
||||
fakedMsgs || '[]', |
||||
) as IChatMessage[] |
||||
|
||||
const mergedMsgs = [..._.defaultTo(encodeFakeMessage, []), message] |
||||
|
||||
const value = JSON.stringify(mergedMsgs) |
||||
await storageService.set(StorageKey.Messages, value) |
||||
|
||||
appEvents.emit('onNewMessage', { message }) |
||||
} |
||||
|
||||
public async deleteFakeMessage(uniqueKey: string) { |
||||
const fakeMsgs = await storageService.get(StorageKey.Messages) |
||||
const processingMsgs: IChatMessage[] = JSON.parse(fakeMsgs || '[]') |
||||
|
||||
if (!processingMsgs || _.isEmpty(processingMsgs)) { |
||||
return |
||||
} |
||||
|
||||
const restMsgs = processingMsgs.filter(it => it.uniqueKey !== uniqueKey) |
||||
|
||||
const value = JSON.stringify(restMsgs) |
||||
await storageService.set(StorageKey.Messages, value) |
||||
|
||||
appEvents.emit('deleteFakeMessage', { uniqueKey }) |
||||
} |
||||
|
||||
static instance: FakeMessageManager |
||||
|
||||
static getInstance() { |
||||
if (!FakeMessageManager.instance) |
||||
FakeMessageManager.instance = new FakeMessageManager() |
||||
return FakeMessageManager.instance |
||||
} |
||||
} |
||||
|
||||
export const fakeMessageManager = FakeMessageManager.getInstance() |
@ -0,0 +1,65 @@
@@ -0,0 +1,65 @@
|
||||
import { DbChatMessageRepository } from '@/repositories/db/chat-message.repository' |
||||
import { MessageType, generateId } from '@/shared' |
||||
import _ from 'lodash' |
||||
|
||||
export interface IFakeTextMessage { |
||||
userId: number |
||||
chatId: string | number |
||||
text: string |
||||
mentionsMessage?: string |
||||
replyToId?: number |
||||
} |
||||
|
||||
export class FakeTextMessageCreator { |
||||
constructor( |
||||
public userId: number, |
||||
public chatId: string | number, |
||||
public text: string, |
||||
public mentionsMessage?: string, |
||||
public replyToId?: number, |
||||
) {} |
||||
|
||||
static async getInstance( |
||||
params: IFakeTextMessage, |
||||
dbCMRepository: DbChatMessageRepository, |
||||
) { |
||||
const { userId, chatId, text, mentionsMessage, replyToId } = params |
||||
|
||||
const uniqueKey = generateId() |
||||
|
||||
const message = { |
||||
id: generateId(), |
||||
chatId, |
||||
userId, |
||||
type: MessageType.Text, |
||||
isPined: false, |
||||
content: { |
||||
message: text, |
||||
mentionsMessage, |
||||
replyToMessage: undefined, |
||||
}, |
||||
createdAt: new Date().toISOString(), |
||||
events: null, |
||||
uniqueKey, |
||||
isFake: true, |
||||
} |
||||
|
||||
if (mentionsMessage) message.content.mentionsMessage = mentionsMessage |
||||
|
||||
if (replyToId) { |
||||
const replyMessage = await dbCMRepository.getMessage( |
||||
replyToId.toString(), |
||||
) |
||||
|
||||
if (replyMessage) |
||||
message.content.replyToMessage = _.pick(replyMessage, [ |
||||
'id', |
||||
'userId', |
||||
'type', |
||||
'content', |
||||
]) |
||||
} |
||||
|
||||
return message |
||||
} |
||||
} |
@ -0,0 +1 @@
@@ -0,0 +1 @@
|
||||
export * from './fake-text-message-creator' |
Loading…
Reference in new issue