2024-08-07 15:23:23 +01:00
|
|
|
import { NgModule } from '@angular/core';
|
2024-08-27 11:00:32 +01:00
|
|
|
import { SignalRService } from 'src/app/infra/socket/signalR/signal-r.service';
|
2024-08-14 15:29:16 +01:00
|
|
|
import { ChatServiceService } from 'src/app/module/chat/domain/chat-service.service'
|
2024-08-18 15:40:43 +01:00
|
|
|
import { skip, switchMap } from 'rxjs/operators';
|
2024-08-15 10:26:20 +01:00
|
|
|
import { SessionStore } from 'src/app/store/session.service';
|
2024-08-18 15:40:43 +01:00
|
|
|
import { Subject, timer } from 'rxjs';
|
2024-08-23 11:10:52 +01:00
|
|
|
import { UserTypingLocalRepository } from './data/repository/typing/user-typing-local-data-source.service';
|
|
|
|
|
import { UserTypingRemoteRepositoryService } from './data/repository/typing/user-typing-live-data-source.service';
|
2024-08-21 20:14:48 +01:00
|
|
|
import { RoomService } from 'src/app/module/chat/domain/service/room.service'
|
2024-08-29 12:13:15 +01:00
|
|
|
import { HttpListenToMessageLoadHistoryAdapter } from './domain/adapter';
|
2024-08-27 15:42:11 +01:00
|
|
|
import { ISignalRService } from 'src/app/infra/socket/adapter';
|
|
|
|
|
import { HttpModule } from 'src/app/infra/http/http.module';
|
|
|
|
|
import { HttpListenToMessageLoadHistoryUseCase } from 'src/app/core/chat/usecase/message/http-listen-to-message-load-history-use-case';
|
2024-08-29 12:13:15 +01:00
|
|
|
import { IMessageLocalRepository } from 'src/app/core/chat/repository/message/message-local-repository';
|
|
|
|
|
import { MessageLocalDataSourceService } from './data/repository/message/message-local-data-source.service';
|
|
|
|
|
import { MessageRemoteDataSourceService } from './data/repository/message/message-remote-data-source.service';
|
|
|
|
|
import { IMessageRemoteRepository } from 'src/app/core/chat/repository/message/message-remote-repository';
|
|
|
|
|
import { IMessageSocketRepository } from 'src/app/core/chat/repository/message/message-socket-repository';
|
|
|
|
|
import { MessageSocketRepositoryService } from './data/repository/message/message-live-signalr-data-source.service';
|
|
|
|
|
import { MemberListLocalRepository } from './data/repository/member/member-list-local-repository.service';
|
|
|
|
|
import { IMemberLocalRepository } from 'src/app/core/chat/repository/member/member-local-repository';
|
|
|
|
|
import { MemberListRemoteRepository } from './data/repository/member/member-list-remote-repository.service';
|
|
|
|
|
import { IMemberRemoteRepository } from 'src/app/core/chat/repository/member/member-remote-repository';
|
|
|
|
|
import { IRoomLocalRepository } from 'src/app/core/chat/repository/room/room-local-repository';
|
|
|
|
|
import { RoomLocalRepository } from './data/repository/room/room-local-repository.service';
|
|
|
|
|
import { RoomRemoteDataSourceService } from './data/repository/room/room-remote-repository.service';
|
|
|
|
|
import { IRoomRemoteRepository } from 'src/app/core/chat/repository/room/room-remote-repository';
|
|
|
|
|
import { RoomSocketRepositoryService } from './data/repository/room/room-socket-repository.service';
|
|
|
|
|
import { IRoomSocketRepository } from 'src/app/core/chat/repository/room/room-socket-repository';
|
|
|
|
|
import { IAttachmentLocalRepository } from 'src/app/core/chat/repository/typing/typing-local-repository';
|
|
|
|
|
import { AttachmentLocalDataSource } from './data/repository/attachment/attachment-local-repository.service';
|
|
|
|
|
import { IAttachmentRemoteRepository } from 'src/app/core/chat/repository/attachment/attachment-remote-repository';
|
|
|
|
|
import { AttachmentRemoteDataSourceService } from './data/repository/attachment/attachment-remote-repository.service';
|
2024-08-07 15:23:23 +01:00
|
|
|
@NgModule({
|
2024-08-27 15:42:11 +01:00
|
|
|
imports: [HttpModule],
|
|
|
|
|
providers: [
|
|
|
|
|
{
|
|
|
|
|
provide: ISignalRService,
|
|
|
|
|
useClass: SignalRService, // or MockDataService
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
provide: HttpListenToMessageLoadHistoryAdapter,
|
|
|
|
|
useClass: HttpListenToMessageLoadHistoryUseCase, // or MockDataService
|
2024-08-29 12:13:15 +01:00
|
|
|
},
|
|
|
|
|
// message repository
|
|
|
|
|
{
|
|
|
|
|
provide: IMessageLocalRepository,
|
|
|
|
|
useClass: MessageLocalDataSourceService
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
provide: IMessageRemoteRepository,
|
|
|
|
|
useClass: MessageRemoteDataSourceService
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
provide: IMessageSocketRepository,
|
|
|
|
|
useClass: MessageSocketRepositoryService
|
|
|
|
|
},
|
|
|
|
|
// member repository
|
|
|
|
|
{
|
|
|
|
|
provide: IMemberLocalRepository,
|
|
|
|
|
useClass: MemberListLocalRepository
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
provide: IMemberRemoteRepository,
|
|
|
|
|
useClass: MemberListRemoteRepository
|
|
|
|
|
},
|
|
|
|
|
// room repository
|
|
|
|
|
{
|
|
|
|
|
provide: IRoomLocalRepository,
|
|
|
|
|
useClass: RoomLocalRepository
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
provide: IRoomRemoteRepository,
|
|
|
|
|
useClass: RoomRemoteDataSourceService
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
provide: IRoomSocketRepository,
|
|
|
|
|
useClass: RoomSocketRepositoryService
|
|
|
|
|
},
|
|
|
|
|
// attachment
|
|
|
|
|
{
|
|
|
|
|
provide: IAttachmentLocalRepository,
|
|
|
|
|
useClass: AttachmentLocalDataSource
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
provide: IAttachmentRemoteRepository,
|
|
|
|
|
useClass: AttachmentRemoteDataSourceService
|
2024-08-27 15:42:11 +01:00
|
|
|
},
|
|
|
|
|
],
|
2024-08-07 15:23:23 +01:00
|
|
|
declarations: [],
|
|
|
|
|
schemas: [],
|
|
|
|
|
entryComponents: []
|
|
|
|
|
})
|
|
|
|
|
export class ChatModule {
|
|
|
|
|
|
2024-08-18 15:40:43 +01:00
|
|
|
typingCallback: {[key: string]: Subject<any> } = {}
|
|
|
|
|
|
2024-08-07 15:23:23 +01:00
|
|
|
constructor(
|
2024-08-13 10:52:35 +01:00
|
|
|
private SignalRService: SignalRService,
|
2024-08-18 15:40:43 +01:00
|
|
|
private ChatServiceService: ChatServiceService,
|
|
|
|
|
private signalR: SignalRService,
|
|
|
|
|
private localDataSource: UserTypingLocalRepository,
|
2024-08-23 11:10:52 +01:00
|
|
|
private UserTypingRemoteRepositoryService: UserTypingRemoteRepositoryService,
|
2024-08-21 20:14:48 +01:00
|
|
|
private RoomService: RoomService
|
2024-08-07 15:23:23 +01:00
|
|
|
) {
|
|
|
|
|
|
2024-08-21 20:14:48 +01:00
|
|
|
this.RoomService.init()
|
2024-08-15 10:26:20 +01:00
|
|
|
this.syncMessage()
|
2024-08-18 15:40:43 +01:00
|
|
|
this.listenToTyping()
|
2024-08-07 15:23:23 +01:00
|
|
|
}
|
|
|
|
|
|
2024-08-18 15:40:43 +01:00
|
|
|
async listenToTyping() {
|
2024-08-23 11:10:52 +01:00
|
|
|
this.UserTypingRemoteRepositoryService.listenToTyping().subscribe(async(e) => {
|
2024-08-18 15:40:43 +01:00
|
|
|
|
|
|
|
|
// 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(() => {
|
2024-08-20 16:34:47 +01:00
|
|
|
// console.log('111111==============')
|
2024-08-18 15:40:43 +01:00
|
|
|
// this.memoryDataSource.dispatch(removeUserTyping({data: {...e} as any}))
|
|
|
|
|
this.localDataSource.removeUserTyping(e)
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
this.typingCallback[id].next()
|
|
|
|
|
}
|
|
|
|
|
})
|
2024-08-26 14:47:03 +01:00
|
|
|
|
2024-08-18 15:40:43 +01:00
|
|
|
}
|
2024-08-07 15:23:23 +01:00
|
|
|
|
2024-08-18 13:27:57 +01:00
|
|
|
async syncMessage() {
|
2024-08-15 10:26:20 +01:00
|
|
|
const connection = this.SignalRService.getConnectionState()
|
2024-08-07 15:23:23 +01:00
|
|
|
|
2024-08-15 10:26:20 +01:00
|
|
|
connection.pipe(
|
|
|
|
|
skip(1) // Skip the first value
|
2024-08-23 11:10:52 +01:00
|
|
|
).subscribe((value: boolean)=> {
|
2024-08-07 15:23:23 +01:00
|
|
|
if(value) {
|
2024-08-15 10:26:20 +01:00
|
|
|
// on reconnect
|
2024-08-21 10:40:54 +01:00
|
|
|
this.ChatServiceService.chatSync();
|
2024-08-15 10:26:20 +01:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2024-08-23 11:10:52 +01:00
|
|
|
connection.subscribe((value: boolean) => {
|
2024-08-15 10:26:20 +01:00
|
|
|
if(value) {
|
|
|
|
|
// on connect
|
2024-08-26 14:47:03 +01:00
|
|
|
this.ChatServiceService.sendLocalMessages()
|
2024-08-07 15:23:23 +01:00
|
|
|
}
|
|
|
|
|
})
|
2024-08-15 10:26:20 +01:00
|
|
|
|
|
|
|
|
// on page reload sync
|
|
|
|
|
if(!(!SessionStore.user.Inactivity || !SessionStore.exist)) {
|
2024-08-18 13:27:57 +01:00
|
|
|
this.ChatServiceService.start();
|
2024-08-15 10:26:20 +01:00
|
|
|
}
|
2024-08-07 15:23:23 +01:00
|
|
|
}
|
|
|
|
|
}
|