send offline message

This commit is contained in:
Peter Maquiran
2024-08-07 15:23:23 +01:00
parent b0a334c9dd
commit bbacc35b08
9 changed files with 172 additions and 4 deletions
+4 -1
View File
@@ -97,6 +97,7 @@ import { DiplomaOptionsPage } from './shared/popover/deploma-options/deploma-opt
import { ImageCropperModule } from 'ngx-image-cropper';
import { createAction, createReducer, on, StoreModule } from '@ngrx/store';
import { typingReducer } from './module/chat/data/data-source/userTyping/user-typing-memory-data-source.service';
import { ChatModule } from './module/chat/chat.module';
// import { ServiceWorkerModule } from '@angular/service-worker';
// import { AngularFireModule } from '@angular/fire';
// import { AngularFireMessagingModule } from '@angular/fire/messaging';
@@ -210,7 +211,9 @@ export function counterReducer(state, action) {
// options
DeplomaOptionsPageModule,
CreateProcessPageModule,
ImageCropperModule
ImageCropperModule,
// module
ChatModule
],
entryComponents: [
DiplomaOptionsPage,
+36
View File
@@ -0,0 +1,36 @@
import { NgModule } from '@angular/core';
import { SignalRService } from 'src/app/module/chat/infra/socket/signal-r.service'
import { SyncMessageRepositoryService } from './data/service/sync-repository/sync-message-repository.service';
@NgModule({
imports: [
],
providers: [
SyncMessageRepositoryService
],
declarations: [],
schemas: [],
entryComponents: []
})
export class ChatModule {
constructor(
private message: SyncMessageRepositoryService,
private SignalRService: SignalRService
) {
this.triggerToSendOfflineMessages()
}
triggerToSendOfflineMessages() {
const result = this.SignalRService.getConnectionState()
result.subscribe((value) => {
if(value) {
this.message.sendLocalMessages()
}
})
}
}
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { MessageRepositoryAyncService } from './message-repository-aync.service';
describe('MessageRepositoryAyncService', () => {
let service: MessageRepositoryAyncService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(MessageRepositoryAyncService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
@@ -0,0 +1,12 @@
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class MessageRepositoryAyncService {
constructor() {
// alert('hellor')
}
}
@@ -235,5 +235,18 @@ export class MessageLocalDataSourceService {
)
)
}
async getOfflineMessages () {
try {
const allMessages = await messageDataSource.message
.filter(msg => typeof msg.id !== 'string' && msg.sending == false)
.toArray();
return allMessages as MessageEntity[];
} catch (error) {
console.error('Error fetching messages:', error);
}
}
}
@@ -0,0 +1,51 @@
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, TableMessage } 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'
@Injectable({
providedIn: 'root'
})
export class SyncMessageRepositoryService {
constructor(
private messageRemoteDataSourceService: MessageRemoteDataSourceService,
private messageLiveDataSourceService: MessageLiveDataSourceService,
private messageLiveSignalRDataSourceService: SignalRService,
private messageLocalDataSourceService: MessageLocalDataSourceService
) { }
async sendLocalMessages() {
const messages = await this.messageLocalDataSourceService.getOfflineMessages()
if(messages.length >= 1) {
console.log('to send '+ messages.length)
for(const message of messages) {
const requestId = InstanceId +'@'+ uuidv4();
const DTO = MessageMapper.fromDomain(message, requestId)
const sendMessageResult = await this.messageLiveSignalRDataSourceService.sendMessage(DTO)
if(sendMessageResult.isOk()) {
if(sendMessageResult.value.sender == undefined || sendMessageResult.value.sender == null) {
delete sendMessageResult.value.sender
}
let clone: TableMessage = {
...sendMessageResult.value,
id: sendMessageResult.value.id,
$id : message.$id
}
return this.messageLocalDataSourceService.update({...clone, sending: false, roomId: message.roomId})
}
}
}
}
}
@@ -0,0 +1,16 @@
import { Injectable } from '@angular/core';
import { SyncMessageRepositoryService } from 'src/app/module/chat/data/service/sync-repository/sync-message-repository.service'
@Injectable({
providedIn: 'root'
})
export class SyncLocalMessageService {
constructor(
private SyncMessageRepositoryService: SyncMessageRepositoryService
) { }
async execute() {
return this.SyncMessageRepositoryService.sendLocalMessages()
}
}
@@ -1,5 +1,5 @@
import { Injectable } from '@angular/core';
import { BehaviorSubject, Subject, timer } from 'rxjs';
import { BehaviorSubject, Observable, Subject, timer } from 'rxjs';
import { Platform } from '@ionic/angular';
import { SignalRConnection } from './signalR';
import { Plugins } from '@capacitor/core';
@@ -102,6 +102,16 @@ export class SignalRService {
this.sendDataSubject.next(data)
this.deadConnectionBackGround.next()
})
this.connection.getConnectionState().subscribe((data) => {
this.connectingSubject.next(data)
})
} else {
setTimeout(() => {
this.establishConnection()
}, 2000)
}
}
@@ -131,6 +141,9 @@ export class SignalRService {
getData() {
return this.sendDataSubject.asObservable()
}
public getConnectionState(): Observable<boolean> {
return this.connectingSubject.asObservable();
}
async sendMessage(data: Object) {
return await this.connection.sendMessage(data as any)
+10 -2
View File
@@ -27,6 +27,7 @@ export class SignalRConnection {
private sendDataSubject: BehaviorSubject<{method: string, data: any}> = new BehaviorSubject<{method: string, data: any}>(null);
private pendingRequests: Map<string, { resolve: Function; reject: Function }> = new Map();
url: string
private hasConnectOnce = false
constructor({url}) {
this.url = url
@@ -44,6 +45,7 @@ export class SignalRConnection {
hubConnection
.start()
.then(() => {
this.hasConnectOnce = true
console.log('Connection started');
this.connectionStateSubject.next(true);
this.hubConnection = hubConnection
@@ -54,7 +56,13 @@ export class SignalRConnection {
.catch(error => {
console.log('Error while starting connection: ' + error);
this.connectionStateSubject.next(false);
reject(err(false))
if(this.hasConnectOnce) {
setTimeout(()=> {
this.attempReconnect();
}, 2000)
}
resolve(err(false))
});
hubConnection.onclose(() => {
@@ -64,7 +72,7 @@ export class SignalRConnection {
this.pendingRequests.forEach((_, requestId) => {
const { reject } = this.pendingRequests.get(requestId);
reject(err('Connection closed================!s'));
reject(err(false));
this.pendingRequests.delete(requestId);
});