mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 04:57:52 +00:00
code refactor
This commit is contained in:
@@ -1,11 +0,0 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class MessageRepositorySyncService {
|
||||
|
||||
constructor() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { UserTypingLiveDataSourceService } from '../../data-source/userTyping/user-typing-live-data-source.service';
|
||||
import { UserTypingLocalDataSourceService } from '../../data-source/userTyping/user-typing-local-data-source.service';
|
||||
import { SignalRService } from '../../../infra/socket/signal-r.service';
|
||||
import { interval, Subject, timer } from 'rxjs';
|
||||
import { switchMap } from 'rxjs/operators';
|
||||
import { addUserTyping, removeUserTyping, TypingState } from '../../data-source/userTyping/user-typing-memory-data-source.service';
|
||||
import { Store } from '@ngrx/store';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class UserTypingAsyncService {
|
||||
|
||||
typingCallback: {[key: string]: Subject<any> } = {}
|
||||
|
||||
constructor(
|
||||
private localDataSource: UserTypingLocalDataSourceService,
|
||||
private liveDataSource: UserTypingLiveDataSourceService,
|
||||
private memoryDataSource: Store<TypingState>,
|
||||
private signalR: SignalRService,
|
||||
) {
|
||||
|
||||
this.signalR.getTyping().subscribe(async (e) => {
|
||||
if(e?.roomId) {
|
||||
|
||||
this.memoryDataSource.dispatch(removeUserTyping({data: {...e} as any}))
|
||||
this.memoryDataSource.dispatch(addUserTyping({data: {...e} as any}))
|
||||
//
|
||||
const value = await this.localDataSource.addUserTyping(e);
|
||||
|
||||
const id = e.roomId + '@' + e.userName
|
||||
if(!this.typingCallback[id]) {
|
||||
this.typingCallback[id] = new Subject()
|
||||
this.typingCallback[id].pipe(
|
||||
switchMap(() => timer(2000)),
|
||||
).subscribe(() => {
|
||||
console.log('111111==============')
|
||||
this.memoryDataSource.dispatch(removeUserTyping({data: {...e} as any}))
|
||||
this.localDataSource.removeUserTyping(e)
|
||||
})
|
||||
} else {
|
||||
this.typingCallback[id].next()
|
||||
}
|
||||
|
||||
} else {
|
||||
console.log('e--', e)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { err, ok } from 'neverthrow';
|
||||
import { WebSocketMessage, WebSocketService } from '../../../infra/socket/socket';
|
||||
import { SignalRService } from '../../../infra/socket/signal-r.service';
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class MessageLiveDataSourceService {
|
||||
|
||||
constructor(
|
||||
// public socket: WebSocketService,
|
||||
private signalR: SignalRService) {}
|
||||
|
||||
// async sendMessage(data: WebSocketMessage) {
|
||||
|
||||
// // try {
|
||||
|
||||
// // const result = await this.socket.sendMessage(data).toPromise()
|
||||
|
||||
// // return ok(result)
|
||||
// // } catch (e) {
|
||||
// // return err(e)
|
||||
// // }
|
||||
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class MessageMemoryDataSourceService {
|
||||
|
||||
messages: {[roomId: string]: string[]} = {}
|
||||
|
||||
constructor() {}
|
||||
}
|
||||
-70
@@ -1,70 +0,0 @@
|
||||
import { createAction, createFeatureSelector, createReducer, createSelector, on, props } from '@ngrx/store';
|
||||
import { TypingTable } from '../../../infra/database/dexie/schema/typing';
|
||||
|
||||
|
||||
export const addUserTyping = createAction(
|
||||
'[Typing] Add User Typing',
|
||||
props<{ data: TypingTable }>()
|
||||
);
|
||||
|
||||
export const removeUserTyping = createAction(
|
||||
'[Typing] Remove User Typing',
|
||||
props<{ data: TypingTable }>()
|
||||
);
|
||||
|
||||
export const loadUserTyping = createAction('[Typing] Load User Typing');
|
||||
|
||||
export const loadUserTypingSuccess = createAction(
|
||||
'[Typing] Load User Typing Success',
|
||||
props<{ data: TypingTable[] }>()
|
||||
);
|
||||
|
||||
export const loadUserTypingFailure = createAction(
|
||||
'[Typing] Load User Typing Failure',
|
||||
props<{ error: any }>()
|
||||
);
|
||||
|
||||
|
||||
export interface TypingState {
|
||||
typingList: TypingTable[];
|
||||
error: any;
|
||||
}
|
||||
|
||||
export const initialState: TypingState = {
|
||||
typingList: [],
|
||||
error: null
|
||||
};
|
||||
|
||||
export const typingReducer = createReducer(
|
||||
initialState,
|
||||
on(loadUserTypingSuccess, (state, { data }) => ({
|
||||
...state,
|
||||
typingList: data
|
||||
})),
|
||||
on(loadUserTypingFailure, (state, { error }) => ({
|
||||
...state,
|
||||
error
|
||||
})),
|
||||
on(addUserTyping, (state, { data }) => ({
|
||||
...state,
|
||||
typingList: [...state.typingList, data]
|
||||
})),
|
||||
on(removeUserTyping, (state, { data }) => ({
|
||||
...state,
|
||||
typingList: state.typingList.filter(
|
||||
typing => typing.chatRoomId !== data.chatRoomId || typing.userName !== data.userName
|
||||
)
|
||||
}))
|
||||
);
|
||||
|
||||
export const selectCalendarState = createFeatureSelector<TypingState>('userTyping');
|
||||
|
||||
export const selectAllUserSource = createSelector(
|
||||
selectCalendarState,
|
||||
(state: TypingState) => state.typingList
|
||||
);
|
||||
|
||||
export const selectUserTypingTable = () => createSelector(
|
||||
selectAllUserSource,
|
||||
(typingList) => typingList
|
||||
);
|
||||
+2
-2
@@ -1,8 +1,8 @@
|
||||
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';
|
||||
import { chatDatabase } from '../../infra/database/dexie/service';
|
||||
import { AttachmentTable, AttachmentTableSchema } from '../../infra/database/dexie/schema/attachment';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
+2
-2
@@ -1,8 +1,8 @@
|
||||
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';
|
||||
import { MessageOutPutDTO } from '../dto/message/messageOutputDTO';
|
||||
import { MessageAttachmentByMessageIdInput } from '../../domain/use-case/message-attachment-by-message-id.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { err, ok } from 'neverthrow';
|
||||
import { SignalRService } from '../../../infra/socket/signal-r.service';
|
||||
import { SignalRService } from '../../infra/socket/signal-r.service';
|
||||
|
||||
interface msgObj {
|
||||
roomId: string;
|
||||
+2
-2
@@ -3,10 +3,10 @@ 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 { 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 { chatDatabase } from '../../infra/database/dexie/service';
|
||||
import { Observable as DexieObservable, PromiseExtended } from 'Dexie';
|
||||
|
||||
@Injectable({
|
||||
+43
-3
@@ -1,10 +1,16 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpService } from 'src/app/services/http.service';
|
||||
import { MessageInputDTO, MessageInputDTOSchema } from '../../dto/message/messageInputDtO';
|
||||
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 { 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'
|
||||
@@ -13,7 +19,10 @@ export class MessageRemoteDataSourceService {
|
||||
|
||||
private baseUrl = 'https://gdapi-dev.dyndns.info/stage/api/v2/Chat'; // Your base URL
|
||||
|
||||
constructor(private httpService: HttpService) {}
|
||||
constructor(
|
||||
private httpService: HttpService,
|
||||
private socket: SignalRService,
|
||||
) {}
|
||||
|
||||
@APIReturn(MessageOutPutDTOSchema, 'post/Messages')
|
||||
@ValidateSchema(MessageInputDTOSchema)
|
||||
@@ -37,4 +46,35 @@ export class MessageRemoteDataSourceService {
|
||||
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)
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -1,11 +1,11 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { SignalRService } from '../../../infra/socket/signal-r.service';
|
||||
import { SignalRService } from '../../infra/socket/signal-r.service';
|
||||
import { SessionStore } from 'src/app/store/session.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class UserTypingLiveDataSourceService {
|
||||
export class UserTypingRemoteRepositoryService {
|
||||
|
||||
constructor(
|
||||
private SignalRLiveDataSourceService: SignalRService
|
||||
+3
-3
@@ -2,15 +2,15 @@ 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';
|
||||
import { chatDatabase } from '../../infra/database/dexie/service';
|
||||
import { TypingTable } from '../../infra/database/dexie/schema/typing';
|
||||
|
||||
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class UserTypingLocalDataSourceService {
|
||||
export class UserTypingLocalRepository {
|
||||
|
||||
constructor() {
|
||||
this.clear();
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { SignalRService } from '../../../infra/socket/signal-r.service';
|
||||
import { MessageLiveDataSourceService } from '../../data-source/message/message-live-data-source.service';
|
||||
import { MessageLocalDataSourceService } from '../../data-source/message/message-local-data-source.service';
|
||||
import { MessageRemoteDataSourceService } from '../../data-source/message/message-remote-data-source.service';
|
||||
import { InstanceId } from '../../../domain/chat-service.service';
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
import { MessageMapper } from 'src/app/module/chat/domain/mapper/messageMapper'
|
||||
import { MessageOutPutDataDTO } from '../../dto/message/messageOutputDTO';
|
||||
import { MessageTable } from 'src/app/module/chat/infra/database/dexie/schema/message';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class SyncMessageRepositoryService {
|
||||
|
||||
constructor(
|
||||
private messageRemoteDataSourceService: MessageRemoteDataSourceService,
|
||||
private messageLiveDataSourceService: MessageLiveDataSourceService,
|
||||
private messageLiveSignalRDataSourceService: SignalRService,
|
||||
private messageLocalDataSourceService: MessageLocalDataSourceService
|
||||
) {
|
||||
|
||||
// this.messageLocalDataSourceService.setAllSenderToFalse();
|
||||
}
|
||||
|
||||
async sendLocalMessages() {
|
||||
const messages = await this.messageLocalDataSourceService.getOfflineMessages()
|
||||
|
||||
if(messages.length >= 1) {
|
||||
|
||||
for(const message of messages) {
|
||||
|
||||
console.log('to upload', messages)
|
||||
const requestId = InstanceId +'@'+ uuidv4();
|
||||
const DTO = MessageMapper.fromDomain(message, requestId)
|
||||
|
||||
await this.messageLocalDataSourceService.update(message.$id, { sending: true })
|
||||
const sendMessageResult = await this.messageLiveSignalRDataSourceService.sendMessage<MessageOutPutDataDTO>(DTO)
|
||||
|
||||
if(sendMessageResult.isOk()) {
|
||||
|
||||
if(sendMessageResult.value.sender == undefined || sendMessageResult.value.sender == null) {
|
||||
|
||||
delete sendMessageResult.value.sender
|
||||
}
|
||||
|
||||
let clone: MessageTable = {
|
||||
...sendMessageResult.value,
|
||||
id: sendMessageResult.value.id,
|
||||
$id : message.$id
|
||||
}
|
||||
|
||||
console.log('send message local '+ messages.length)
|
||||
|
||||
this.messageLocalDataSourceService.update(message.$id, {...clone, sending: false, roomId: message.roomId})
|
||||
} else {
|
||||
|
||||
console.log('erro send message')
|
||||
this.messageLocalDataSourceService.update(message.$id, {sending: false})
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user