load messages

This commit is contained in:
Peter Maquiran
2024-08-15 10:26:20 +01:00
parent ea4ca5c34c
commit 9697290bb7
11 changed files with 102 additions and 73 deletions
+22 -5
View File
@@ -3,6 +3,8 @@ import { SignalRService } from 'src/app/module/chat/infra/socket/signal-r.servic
import { SyncMessageRepositoryService } from './data/service/sync-repository/sync-message-repository.service';
import { UserTypingAsyncService } from 'src/app/module/chat/data/async/socket/user-typing-async.service'
import { ChatServiceService } from 'src/app/module/chat/domain/chat-service.service'
import { skip } from 'rxjs/operators';
import { SessionStore } from 'src/app/store/session.service';
@NgModule({
imports: [
@@ -21,21 +23,36 @@ export class ChatModule {
private message: SyncMessageRepositoryService,
private SignalRService: SignalRService,
private UserTypingAsyncService: UserTypingAsyncService,
private ChatServiceService: ChatServiceService
private ChatServiceService: ChatServiceService
) {
this.triggerToSendOfflineMessages()
this.syncMessage()
}
triggerToSendOfflineMessages() {
const result = this.SignalRService.getConnectionState()
syncMessage() {
const connection = this.SignalRService.getConnectionState()
result.subscribe((value) => {
connection.pipe(
skip(1) // Skip the first value
).subscribe((value)=> {
if(value) {
// on reconnect
this.ChatServiceService.asyncAllRoomMessage();
}
});
connection.subscribe((value) => {
if(value) {
// on connect
console.log('send local image')
this.message.sendLocalMessages()
}
})
// on page reload sync
if(!(!SessionStore.user.Inactivity || !SessionStore.exist)) {
this.ChatServiceService.asyncAllRoomMessage();
}
}
}
@@ -18,6 +18,8 @@ export class MessageLocalDataSourceService extends DexieRepository<MessageTable>
constructor() {
super(chatDatabase.message, MessageTableSchema)
this.setAllSenderToFalse();
}
async setAllSenderToFalse() {
@@ -195,6 +197,10 @@ export class MessageLocalDataSourceService extends DexieRepository<MessageTable>
.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);
@@ -26,7 +26,7 @@ export class MessageRemoteDataSourceService {
}
@APIReturn(MessageOutPutDTOSchema, 'get/Messages')
// @APIReturn(MessageOutPutDTOSchema, 'get/Messages')
async getMessagesFromRoom(id: string): DataSourceReturn<MessageOutPutDTO> {
return await this.httpService.get(`${this.baseUrl}/Room/${id}/Messages`);
}
@@ -51,8 +51,12 @@ export class SyncMessageRepositoryService {
$id : message.$id
}
console.log('send message local')
this.messageLocalDataSourceService.update(message.$id, {...clone, sending: false, roomId: message.roomId})
} else {
console.log('erro send message')
this.messageLocalDataSourceService.update(message.$id, {sending: false})
}
}
@@ -14,10 +14,12 @@ export class SyncAllRoomMessagesService {
) { }
async execute() {
await this.RoomRepositoryService.list()
const allRooms: RoomTable[] = await this.RoomRepositoryService.getRoomList()
if(allRooms) {
console.log('allRooms', allRooms)
for(const room of allRooms) {
this.MessageRepositoryService.listAllMessagesByRoomId(room.id)
}
+1 -1
View File
@@ -45,10 +45,10 @@ export class SignalRConnection {
hubConnection
.start()
.then(() => {
this.hubConnection = hubConnection
this.hasConnectOnce = true
console.log('Connection started');
this.connectionStateSubject.next(true);
this.hubConnection = hubConnection
this.join()
this.addMessageListener()
resolve(ok(hubConnection))
+47 -6
View File
@@ -107,6 +107,8 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
downloadFile: any;
files: any[] = [];
@ViewChild('filechooser') fileChooserElementRef: ElementRef;
@ViewChild('array') myInputRef!: ElementRef;
//items: File[] = [];
fileSelected?: Blob;
pdfUrl?: string;
@@ -172,6 +174,7 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
this.roomData$ = this.roomRepositoryService.getItemByIdLive(this.roomId)
this.getMessages();
this.listenToIncomingMessage();
this.listenToDeleteMessage();
this.listenToUpdateMessage();
@@ -206,15 +209,52 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
})
//this.userTyping$ = this.userTypingMemoryDataSource.select(state => state) as any
this.userTypingServiceRepository.getUserTypingLive().subscribe((e) => {
const arrayNames = e.map(e => e.userName)
this.userTyping$ = e as any
// let a = this.userTypingMemoryDataSource.select(state => state).subscribe((e) => {
// this.userTyping$ = e as any
// })
const uniqueArray = [...new Set(arrayNames)];
(this.myInputRef.nativeElement as HTMLDivElement).innerHTML = '::'+ uniqueArray
})
}
async getMessages() {
// dont remove this line
this.messages1[this.roomId] = []
let messages = await this.messageRepositoryService.getItems(this.roomId)
this.messages1[this.roomId] = []
this.messages1[this.roomId] = messages
this.loadAttachment()
setTimeout(() => {
this.scrollToBottomClicked()
}, 100)
}
async loadAttachment() {
for(const message of this.messages1[this.roomId]) {
if(message.hasAttachment) {
const result = await this.chatServiceService.getMessageAttachmentByMessageId({
$messageId: message.$id,
id: message.attachments[0].id
})
if(result.isOk()) {
message.attachments[0].safeFile = result.value
}
}
}
}
ngOnInit() {
try {
@@ -830,10 +870,11 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
}
message.attachments = [{
file: compressedImage.value,
file: compressedImage.value.split(',')[1],
fileName: "foto",
source: MessageAttachmentSource.Device,
fileType: MessageAttachmentFileType.Image
fileType: MessageAttachmentFileType.Image,
mimeType: 'image/'+picture.value.format
}]
this.chatServiceService.sendMessage(message)
-22
View File
@@ -11,7 +11,6 @@ import { ClearStoreService } from 'src/app/services/clear-store.service';
import { ChangeProfileService } from 'src/app/services/change-profile.service';
import { ThemeService } from 'src/app/services/theme.service';
import { PermissionService } from 'src/app/services/permission.service';
import { ChatService } from 'src/app/services/chat.service';
import { NotificationHolderService } from 'src/app/store/notification-holder.service';
import { Platform } from '@ionic/angular';
import { Storage } from '@ionic/storage';
@@ -51,7 +50,6 @@ export class LoginPage implements OnInit {
public ThemeService: ThemeService,
public p: PermissionService,
// public ChatSystemService: ChatSystemService,
private ChatService: ChatService,
private platform: Platform,
private storage: Storage,
private storageService: StorageService,
@@ -137,20 +135,6 @@ export class LoginPage implements OnInit {
await this.authService.SetSession(attempt, this.userattempt);
this.changeProfileService.run();
if (attempt.ChatData) {
try {
this.NotificationHolderService.clear()
await this.authService.loginToChatWs();
this.ChatService.setheader()
} catch(error) {
console.log("faild to clear chat")
}
}
this.changeProfileService.runLogin();
this.ChatServiceService.asyncAllRoomMessage()
this.getToken();
@@ -172,12 +156,6 @@ export class LoginPage implements OnInit {
await this.authService.SetSession(attempt, this.userattempt);
this.changeProfileService.run();
if (attempt.ChatData) {
await this.authService.loginToChatWs();
this.ChatService.setheader();
}
this.storageService.remove("Notifications")
this.ChatServiceService.asyncAllRoomMessage()
+10 -10
View File
@@ -166,12 +166,14 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
}
ngOnChanges(changes: SimpleChanges): void {
this.roomData$ = this.roomRepositoryService.getItemByIdLive(this.roomId)
this.getMessages();
this.listenToIncomingMessage();
this.listenToDeleteMessage();
this.listenToUpdateMessage();
this.listenToSendMessage();
this.listenToSendMessage()
this.roomMessage$ = this.messageRepositoryService.getItemsLive(this.roomId)
this.roomMembers$ = this.roomRepositoryService.getRoomMemberByIdLive(this.roomId) as any
@@ -193,17 +195,17 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
async getMessages() {
// dont remove this line
this.messages1[this.roomId] = []
let messages = await this.messageRepositoryService.getItems(this.roomId)
this.messages1[this.roomId] = []
this.messages1[this.roomId] = messages
this.messages1[this.roomId] = this.sortBySentAt(this.messages1[this.roomId])
this.loadAttachment()
setTimeout(() => {
this.scrollToBottomClicked()
}, 100)
}, 200)
}
@@ -217,14 +219,12 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
})
if(result.isOk()) {
message.attachments[0].safeFile = result.value
}
}
}
}
// Sorting function
@@ -818,11 +818,11 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
message.sentAt = new Date().toISOString()
message.attachments = [{
file: compressedImage.value,
file: compressedImage.value.split(',')[1],
fileName: "foto",
source: MessageAttachmentSource.Device,
fileType: MessageAttachmentFileType.Image,
mimeType: picture.value.format
mimeType: 'image/'+picture.value.format
}]
this.messages1[this.roomId].push(message)