code refactor

This commit is contained in:
Peter Maquiran
2024-08-18 15:40:43 +01:00
parent ef12ff439d
commit 29d0a9b55e
39 changed files with 191 additions and 464 deletions
@@ -0,0 +1,19 @@
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { DexieRepository } from 'src/app/infra/repository/dexie/dexie-repository.service';
import { chatDatabase } from '../../infra/database/dexie/service';
import { AttachmentTable, AttachmentTableSchema } from '../../infra/database/dexie/schema/attachment';
@Injectable({
providedIn: 'root'
})
export class AttachmentLocalDataSource extends DexieRepository<AttachmentTable> {
messageSubject = new Subject();
constructor() {
super(chatDatabase.attachment, AttachmentTableSchema)
}
}
@@ -0,0 +1,21 @@
import { Injectable, Input } from '@angular/core';
import { HttpService } from 'src/app/services/http.service';
import { DataSourceReturn } from 'src/app/services/Repositorys/type';
import { MessageOutPutDTO } from '../dto/message/messageOutputDTO';
import { MessageAttachmentByMessageIdInput } from '../../domain/use-case/message-attachment-by-message-id.service';
@Injectable({
providedIn: 'root'
})
export class AttachmentRemoteDataSourceService {
private baseUrl = 'https://gdapi-dev.dyndns.info/stage/api/v2/Chat'; // Your base URL
constructor(
private httpService: HttpService
) { }
async getAttachment(id: string | number): DataSourceReturn<Blob> {
return await this.httpService.get(`${this.baseUrl}/attachment/${id}`, { responseType: 'blob' });
}
}
@@ -1,31 +0,0 @@
import { Injectable } from '@angular/core';
import { AttachmentLocalDataSource } from 'src/app/module/chat/data/data-source/attachment/attachment-local-data-source.service'
import { AttachmentTable } from '../../infra/database/dexie/schema/attachment';
@Injectable({
providedIn: 'root'
})
export class AttachmentRepositoryService {
constructor(
private AttachmentLocalDataSourceService: AttachmentLocalDataSource
) { }
create(data: AttachmentTable) {
return this.AttachmentLocalDataSourceService.insert(data)
}
get findOne() {
return this.AttachmentLocalDataSourceService.findOne
}
get insert() {
return this.AttachmentLocalDataSourceService.insert
}
get update() {
return this.AttachmentLocalDataSourceService.update
}
}
@@ -0,0 +1,51 @@
import { Injectable } from '@angular/core';
import { err, ok } from 'neverthrow';
import { SignalRService } from '../../infra/socket/signal-r.service';
interface msgObj {
roomId: string;
senderId: string;
message:string;
messageType:1;
canEdit:Boolean;
oneShot:Boolean;
requestId: string;
}
@Injectable({
providedIn: 'root'
})
export class MessageLiveDataSourceService {
constructor(
private messageLiveSignalRDataSourceService: SignalRService
) {}
async sendMessage(data: msgObj) {
try {
const result = await this.messageLiveSignalRDataSourceService.sendMessage(data)
return ok(result)
} catch (e) {
return err(e)
}
}
listenToMessages() {
return this.messageLiveSignalRDataSourceService.getMessage()
}
listenToDeleteMessages() {
return this.messageLiveSignalRDataSourceService.getMessageDelete()
}
listenToUpdateMessages() {
return this.messageLiveSignalRDataSourceService.getMessageUpdate()
}
}
@@ -0,0 +1,210 @@
import { Injectable } from '@angular/core';
import { liveQuery } from 'Dexie';
import { err, ok, Result } from 'neverthrow';
import { Observable, Subject } from 'rxjs';
import { filter } from 'rxjs/operators';
import { MessageEntity } from '../../domain/entity/message';
import { DexieRepository } from 'src/app/infra/repository/dexie/dexie-repository.service';
import { MessageTable, MessageTableSchema } from 'src/app/module/chat/infra/database/dexie/schema/message';
import { chatDatabase } from '../../infra/database/dexie/service';
import { Observable as DexieObservable, PromiseExtended } from 'Dexie';
@Injectable({
providedIn: 'root'
})
export class MessageLocalDataSourceService extends DexieRepository<MessageTable> {
messageSubject = new Subject();
constructor() {
super(chatDatabase.message, MessageTableSchema)
this.setAllSenderToFalse();
}
async setAllSenderToFalse() {
try {
await chatDatabase.transaction('rw', chatDatabase.message, async () => {
// Perform the update operation within the transaction
await chatDatabase.message.toCollection().modify({ sending: false });
});
console.log('All messages updated successfully.');
} catch (error) {
console.error('Error updating messages:', error);
}
}
async getLastMessageByRoomId(roomId: string): Promise<Result<undefined|MessageTable, any>> {
try {
console.log({roomId})
const lastMessage = await chatDatabase.message
.where('roomId')
.equals(roomId)
.reverse()
.sortBy('id');
return ok(lastMessage[0]); // Get the last message
} catch (error) {
return err(error);
}
}
async deleteByMessageId(id: string): Promise<Result<undefined|MessageTable, any>> {
try {
console.log(id)
const lastMessage = await chatDatabase.message
.where('id')
.equals(id).delete()
return ok(lastMessage[0]); // Get the last message
} catch (error) {
return err(error);
}
}
async sendMessage(data: MessageTable) {
const dataValidation = MessageTableSchema.safeParse(data)
if(dataValidation.success) {
const safeData = dataValidation.data
safeData.sending = true
try {
const result = await chatDatabase.message.add(safeData)
this.messageSubject.next({roomId: safeData.roomId});
return ok(result as number)
} catch (e) {
return err(false)
}
} else {
console.log(dataValidation)
return err(dataValidation)
}
}
// @ValidateSchema(tableSchema)
async createMessage(data: MessageTable) {
try {
const result = await chatDatabase.message.add(data)
this.messageSubject.next({roomId: data.roomId});
return ok(result)
} catch (e) {
return err(false)
}
}
async createManyMessage(data: MessageTable[]) {
try {
const result = await chatDatabase.message.bulkAdd(data)
this.messageSubject.next({roomId: data[0].roomId});
return ok(result)
} catch (e) {
return err(false)
}
}
async messageExist({id}) {
try {
console.log({id});
const existingMessage = await chatDatabase.message
.where('id')
.equals(id)
.first();
if (existingMessage) {
return ok(existingMessage)
} else {
return err(false)
}
} catch (error) {
return err(false);
}
}
// not used
async updateByMessageId(data: MessageTable ) {
try {
const result = await chatDatabase.message.update(data.id as any, data)
return ok(result)
} catch (e) {
return err(false)
}
}
async findOrUpdate(data: MessageTable) {
const findResult = await this.findMessageById(data.id)
if(findResult.isOk()) {
return this.update(findResult.value.$id, data)
} else {
return this.createMessage(data)
}
}
getItems(roomId: string): PromiseExtended<MessageEntity[]> {
return chatDatabase.message.where('roomId').equals(roomId).sortBy('$id') as any
}
getItemsLive(roomId: string): DexieObservable<MessageEntity[]> {
return liveQuery(() => chatDatabase.message.where('roomId').equals(roomId).sortBy('$id') as any)
}
async findMessageById(id: string) {
try {
const a = await chatDatabase.message.where('id').equals(id).first()
if(a) {
return ok(a)
} else {
return err('not found')
}
} catch (e) {
return err('DB error')
}
}
subscribeToNewMessage(roomId: string): Observable<MessageTable> {
return this.messageSubject.pipe(
filter((message: MessageTable) =>
message.roomId === roomId
)
)
}
async getOfflineMessages () {
try {
const allMessages = await chatDatabase.message
.filter(msg => typeof msg.id !== 'string' && msg.sending == false)
.toArray();
console.log("======================================================================")
console.log({localMessage:allMessages})
return allMessages as MessageEntity[];
} catch (error) {
console.error('Error fetching messages:', error);
}
}
}
@@ -0,0 +1,80 @@
import { Injectable } from '@angular/core';
import { HttpService } from 'src/app/services/http.service';
import { MessageInputDTO, MessageInputDTOSchema } from '../dto/message/messageInputDtO';
import { ValidateSchema } from 'src/app/services/decorators/validate-schema.decorator';
import { APIReturn } from 'src/app/services/decorators/api-validate-schema.decorator';
import { MessageOutPutDataDTOSchema, MessageOutPutDTO, MessageOutPutDTOSchema } from '../dto/message/messageOutputDTO';
import { DataSourceReturn } from 'src/app/services/Repositorys/type';
import { SignalRService } from '../../infra/socket/signal-r.service';
import { MessageUpdateInput } from '../../domain/use-case/message-update-by-id-use-case.service';
import { SessionStore } from 'src/app/store/session.service';
import { MessageDeleteInputDTO } from '../dto/message/messageDeleteInputDTO';
import { InstanceId } from '../../domain/chat-service.service';
import { v4 as uuidv4 } from 'uuid'
@Injectable({
providedIn: 'root'
})
export class MessageRemoteDataSourceService {
private baseUrl = 'https://gdapi-dev.dyndns.info/stage/api/v2/Chat'; // Your base URL
constructor(
private httpService: HttpService,
private socket: SignalRService,
) {}
@APIReturn(MessageOutPutDTOSchema, 'post/Messages')
@ValidateSchema(MessageInputDTOSchema)
async sendMessage(data: MessageInputDTO) {
return await this.httpService.post<MessageOutPutDTO>(`${this.baseUrl}/Messages`, data);
}
async reactToMessage(id: string, reaction: any) {
return await this.httpService.post<any>(`${this.baseUrl}/Messages/${id}/React`, reaction);
}
// @APIReturn(MessageOutPutDTOSchema, 'get/Messages')
async getMessagesFromRoom(id: string): DataSourceReturn<MessageOutPutDTO> {
return await this.httpService.get(`${this.baseUrl}/Room/${id}/Messages`);
}
@APIReturn(MessageOutPutDTOSchema, 'get/Messages/attachment')
async getAttachment(id: string): DataSourceReturn<MessageOutPutDTO> {
return await this.httpService.get(`${this.baseUrl}/attachment/${id}`);
}
reactToMessageSocket(data) {
this.socket.sendData({
method: 'ReactMessage',
data
})
}
updateMessage(input: MessageUpdateInput) {
this.socket.sendData({
method: 'EditMessage',
data: input,
})
}
sendTyping(roomId) {
return this.socket.sendTyping({
roomId,
UserName:SessionStore.user.FullName,
userId: SessionStore.user.UserId
})
}
sendMessageDelete(data: MessageDeleteInputDTO) {
data['requestId'] = InstanceId +'@'+ uuidv4();
return this.socket.sendMessageDelete(data)
}
}
@@ -1,86 +0,0 @@
import { Injectable } from '@angular/core';
import { MessageRemoteDataSourceService } from '../data-source/message/message-remote-data-source.service';
import { SessionStore } from 'src/app/store/session.service';
import { SignalRService } from '../../infra/socket/signal-r.service';
import { v4 as uuidv4 } from 'uuid'
import { err, ok } from 'neverthrow';
import { MessageDeleteInputDTO } from '../../domain/use-case/message-delete-by-id-live-use-case.service';
import { MessageUpdateInput } from '../../domain/use-case/message-update-by-id-use-case.service';
import { InstanceId } from '../../domain/chat-service.service';
import { MessageLocalDataSourceService } from '../data-source/message/message-local-data-source.service';
import { MessageLiveDataSourceService } from '../data-source/message/message-live-signalr-data-source.service';
import { AttachmentLocalDataSource } from 'src/app/module/chat/data/data-source/attachment/attachment-local-data-source.service'
@Injectable({
providedIn: 'root'
})
export class MessageRepositoryService {
constructor(
private messageRemoteDataSourceService: MessageRemoteDataSourceService,
private messageLiveDataSourceService: MessageLiveDataSourceService,
private messageLiveSignalRDataSourceService: SignalRService,
private messageLocalDataSourceService: MessageLocalDataSourceService,
private AttachmentLocalDataSourceService: AttachmentLocalDataSource
) {}
sendMessageDelete(data: MessageDeleteInputDTO) {
data['requestId'] = InstanceId +'@'+ uuidv4();
return this.messageLiveSignalRDataSourceService.sendMessageDelete(data)
}
async sendReadAt({roomId}) {
const result = await this.messageLocalDataSourceService.getLastMessageByRoomId(roomId)
if(result.isOk()) {
if(result.value) {
return await this.messageLiveSignalRDataSourceService.sendReadAt({roomId, memberId: SessionStore.user.UserId, chatMessageId: result.value.id})
}
return ok(true)
}
return err(false)
}
reactToMessage(data) {
this.messageLiveSignalRDataSourceService.sendData({
method: 'ReactMessage',
data
})
}
updateMessage(input: MessageUpdateInput) {
this.messageLiveSignalRDataSourceService.sendData({
method: 'EditMessage',
data: input,
})
}
getItemsLive (roomId: string) {
return this.messageLocalDataSourceService.getItemsLive(roomId)
}
getItems (roomId: string) {
return this.messageLocalDataSourceService.getItems(roomId)
}
subscribeToNewMessages(roomId: any) {
return this.messageLocalDataSourceService.subscribeToNewMessage(roomId)
}
sendTyping(roomId) {
return this.messageLiveSignalRDataSourceService.sendTyping({
roomId,
UserName:SessionStore.user.FullName,
userId: SessionStore.user.UserId
})
}
attachment(roomId: string) {
console.log('attachment')
return this.messageRemoteDataSourceService.getAttachment(roomId)
}
}
@@ -0,0 +1,22 @@
import { Injectable } from '@angular/core';
import { SignalRService } from '../../infra/socket/signal-r.service';
import { SessionStore } from 'src/app/store/session.service';
@Injectable({
providedIn: 'root'
})
export class UserTypingRemoteRepositoryService {
constructor(
private SignalRLiveDataSourceService: SignalRService
) { }
sendTyping(roomId, ) {
return this.SignalRLiveDataSourceService.sendTyping({
roomId,
UserName:SessionStore.user.FullName,
userId:SessionStore.user.UserId
})
}
}
@@ -0,0 +1,56 @@
import { Injectable } from '@angular/core';
import { z } from 'zod';
import { Dexie, EntityTable, liveQuery, Observable } from 'Dexie';
import { err, ok } from 'neverthrow';
import { chatDatabase } from '../../infra/database/dexie/service';
import { TypingTable } from '../../infra/database/dexie/schema/typing';
@Injectable({
providedIn: 'root'
})
export class UserTypingLocalRepository {
constructor() {
this.clear();
}
async clear() {
try {
const result = await chatDatabase.typing.clear()
return ok(result)
} catch (e) {
return err(false)
}
}
async addUserTyping(data: TypingTable) {
data.id = data.chatRoomId + '@' + data.userName
try {
const result = await chatDatabase.typing.add(data)
return ok(result)
} catch (e) {
return err(false)
}
}
async removeUserTyping(data: TypingTable) {
const id = data.chatRoomId + '@' + data.userName
try {
const result = await chatDatabase.typing.delete(id)
return ok(result)
} catch (e) {
return err(false)
}
}
getUserTypingLive() {
return liveQuery(() => chatDatabase.typing.toArray());
}
}
@@ -1,28 +0,0 @@
import { Injectable } from '@angular/core';
import { UserTypingLocalDataSourceService } from '../data-source/userTyping/user-typing-local-data-source.service';
import { UserTypingLiveDataSourceService } from '../data-source/userTyping/user-typing-live-data-source.service';
import { TypingTable } from '../../infra/database/dexie/schema/typing';
@Injectable({
providedIn: 'root'
})
export class UserTypingServiceRepository {
constructor(
private localDataSource: UserTypingLocalDataSourceService,
private liveDataSource: UserTypingLiveDataSourceService
) { }
async addUserTyping(ChatRoomId: any) {
return await this.liveDataSource.sendTyping(ChatRoomId)
}
async removeUserTyping(data: TypingTable) {
return await this.localDataSource.removeUserTyping(data)
}
getUserTypingLive() {
return this.localDataSource.getUserTypingLive()
}
}