diff --git a/src/app/pages/chat/group-messages/group-messages.page.html b/src/app/pages/chat/group-messages/group-messages.page.html index 00a341ca9..07b81b08a 100644 --- a/src/app/pages/chat/group-messages/group-messages.page.html +++ b/src/app/pages/chat/group-messages/group-messages.page.html @@ -83,7 +83,7 @@
File - image + image
@@ -102,8 +102,8 @@
- image - + image +
diff --git a/src/app/pages/chat/group-messages/group-messages.page.ts b/src/app/pages/chat/group-messages/group-messages.page.ts index eb3bcce11..e9d318a5a 100644 --- a/src/app/pages/chat/group-messages/group-messages.page.ts +++ b/src/app/pages/chat/group-messages/group-messages.page.ts @@ -454,8 +454,7 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { this.wsChatMethodsService.getDmRoom(roomId).send({ file: { "type": "application/img", - "guid": '', - "image_url": capturedImage + "guid": '' }, temporaryData: formData, attachments: [{ @@ -533,8 +532,7 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { this.wsChatMethodsService.getDmRoom(this.roomId).send({ file: { "type": "application/img", - "guid": '', - "image_url": imageData + "guid": '' }, temporaryData: formData, attachments: [{ diff --git a/src/app/pages/chat/messages/messages.page.html b/src/app/pages/chat/messages/messages.page.html index dabd8c88d..8b0b8ccfc 100644 --- a/src/app/pages/chat/messages/messages.page.html +++ b/src/app/pages/chat/messages/messages.page.html @@ -83,8 +83,8 @@
- image - + image +
diff --git a/src/app/pages/chat/messages/messages.page.ts b/src/app/pages/chat/messages/messages.page.ts index 52db3b6ea..eec98fa08 100644 --- a/src/app/pages/chat/messages/messages.page.ts +++ b/src/app/pages/chat/messages/messages.page.ts @@ -539,8 +539,7 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { this.wsChatMethodsService.getDmRoom(roomId).send({ file: { "type": "application/img", - "guid": '', - "image_url": capturedImage + "guid": '' }, attachments: [{ "image_url": capturedImage, @@ -620,7 +619,6 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { file: { "type": "application/img", "guid": '', - "image_url": imageData }, temporaryData: formData, attachments: [{ diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts index 6620a316c..69f04318d 100644 --- a/src/app/services/auth.service.ts +++ b/src/app/services/auth.service.ts @@ -1,6 +1,6 @@ import { Injectable } from '@angular/core'; import { StorageService } from './storage.service'; -import { HttpClient, HttpHeaders } from '@angular/common/http'; +import { HttpClient, HttpHeaders, HttpEventType } from '@angular/common/http'; import { LoginUserRespose, UserForm, UserSession } from '../models/user.model'; import { environment } from 'src/environments/environment'; import { HttpService } from './http.service'; @@ -18,6 +18,7 @@ import { ProcessesService } from 'src/app/services/processes.service'; import { AttachmentsService } from 'src/app/services/attachments.service'; import { RoomService } from './chat/room.service'; import { Storage } from '@ionic/storage'; + @Injectable({ providedIn: 'root' }) @@ -187,6 +188,43 @@ export class AuthService { return false } + + + this.NfService.downloadFileMsg = async (message: MessageService, room?: RoomService) => { + + console.log('FILE TYPE', message.file.type) + let downloadFile = ""; + if (message.file.type == "application/img") { + const event: any = await this.AttachmentsService.downloadFile(message.file.guid).toPromise(); + + console.log('FILE TYPE 22', message.file.guid) + + if (event.type === HttpEventType.DownloadProgress) { + //this.downloadProgess = Math.round((100 * event.loaded) / event.total); + console.log('FILE TYPE 33', message.file.type) + return true + } else if (event.type === HttpEventType.Response) { + downloadFile = 'data:image/jpeg;base64,' + btoa(new Uint8Array(event.body).reduce((data, byte) => data + String.fromCharCode(byte), '')); + + message.file = { + guid: message.file.guid, + image_url: downloadFile, + type: message.file.type + } + + await this.storage.set(message.file.guid, downloadFile).then(() => { + console.log('IMAGE SAVED') + }); + return true + } + + return false + + } + }; + + + }, 1) } diff --git a/src/app/services/chat/chat-methods.service.ts b/src/app/services/chat/chat-methods.service.ts index e22f6ddc0..d92cb2198 100644 --- a/src/app/services/chat/chat-methods.service.ts +++ b/src/app/services/chat/chat-methods.service.ts @@ -6,7 +6,8 @@ import { ChatService } from '../chat.service'; }) export class ChatMethodsService { - constructor(private chatService: ChatService) { + constructor( + private chatService: ChatService) { } sendMessage(roomId:string, data:any) { diff --git a/src/app/services/chat/message.service.ts b/src/app/services/chat/message.service.ts index 976a81e21..142619315 100644 --- a/src/app/services/chat/message.service.ts +++ b/src/app/services/chat/message.service.ts @@ -5,6 +5,7 @@ import { SessionStore } from 'src/app/store/session.service'; import { capitalizeTxt } from 'src/plugin/text' import { NfService } from 'src/app/services/chat/nf.service' import { WsChatService } from 'src/app/services/chat/ws-chat.service'; +import { type } from 'os'; @Injectable({ providedIn: 'root' }) @@ -27,6 +28,9 @@ export class MessageService { temporaryData: any = {} hasFile = false hasSendAttachment = false + sendAttempt = 0 + uploadingFile = false + errorUploadingAttachment = false constructor(private storage: Storage, private NfService: NfService, @@ -90,34 +94,71 @@ export class MessageService { } } - sendFile() { - if(this.file == null && this.attachments == null) { + async send() { + + this.sendAttempt++; + + if(!this.hasFile) { console.log('simple send') this.WsChatService.send({roomId:this.rid, msg:this.msg}).then((data: any) => { let ChatMessage = data.result this.redefinedMessage(this, ChatMessage) + this.offline = false }) } else { console.log('complex send') - const result = this.NfService.beforeSendAttachment(this) - + this.uploadingFile = true - if(result) { + let uploadSuccessfully = false + if(this.hasSendAttachment == false) { + uploadSuccessfully = await this.NfService.beforeSendAttachment(this) + } + + this.uploadingFile = false + + if(uploadSuccessfully || this.hasSendAttachment == false) { this.hasSendAttachment = true + this.errorUploadingAttachment = false + this.temporaryData = {} this.WsChatService.send({roomId:this.rid, msg: this.msg, attachments: this.attachments, file: this.file}).then((data: any) => { console.log('send sucees', data.result) let ChatMessage = data.result this.redefinedMessage(this, ChatMessage) + this.offline = false }) + } else if(this.WsChatService.isLogin == false) { + alert('registerCallback') + + this.WsChatService.registerCallback({ + type: 'reConnect', + funx:()=> { + alert('reConnect') + // this.send() + return true + } + }) + + } else if(uploadSuccessfully == false) { + alert('this.WsChatService.isLogin '+ this.WsChatService.isLogin+ '') + this.errorUploadingAttachment = true } } } - redefinedMessage(messagem, ChatMessage){ + redefinedMessage(messagem, ChatMessage) { this.setData(ChatMessage) } + + async downloadFileMsg() { + const result = await this.NfService.beforeSendAttachment(this) + if(result) { + + } + + } + } diff --git a/src/app/services/chat/nf.service.ts b/src/app/services/chat/nf.service.ts index 7bfbd26be..cca0e4807 100644 --- a/src/app/services/chat/nf.service.ts +++ b/src/app/services/chat/nf.service.ts @@ -8,7 +8,7 @@ import { RoomService } from './room.service'; export class NfService { beforeSendAttachment = async (message: MessageService, room?: RoomService): Promise => new Promise ((resolve, reject)=> (resolve(true))) - + downloadFileMsg = async (message: MessageService, room?: RoomService): Promise => new Promise ((resolve, reject)=> (resolve(true))) constructor() { } } diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index c2a61a60a..a68b496f4 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -3,7 +3,7 @@ import { WsChatService } from 'src/app/services/chat/ws-chat.service'; import { MessageService } from 'src/app/services/chat/message.service'; import { showDateDuration } from 'src/plugin/showDateDuration'; import { ToastsService } from '../toast.service'; -import { chatHistory, ChatMessage } from 'src/app/models/chatMethod'; +import { chatHistory } from 'src/app/models/chatMethod'; import { Storage } from '@ionic/storage'; import { Platform } from '@ionic/angular'; import { SqliteService } from 'src/app/services/sqlite.service'; @@ -138,6 +138,8 @@ export class RoomService { addMessageDB(ChatMessage) { this.storage.get('chatmsg' + this.id).then((messages: any = []) => { + if(messages==null) messages = [] + delete ChatMessage.temporaryData messages.push(ChatMessage) @@ -188,6 +190,7 @@ export class RoomService { */ private deleteMessageFromDb(id) { this.storage.get('chatmsg' + this.id).then((messages: any = []) => { + if(messages==null) messages = [] messages.forEach((message, index) => { @@ -228,31 +231,7 @@ export class RoomService { this.lastMessage = message - - if(file == null && attachments == null) { - console.log('simple send') - this.WsChatService.send({roomId:this.id, msg:message.msg}).then((data: any) => { - let ChatMessage = data.result - this.redefinedMessage(message, ChatMessage) - }) - } else { - console.log('complex send') - - const result = await this.NfService.beforeSendAttachment(message, this) - - - if(result) { - message.hasSendAttachment = true - - this.WsChatService.send({roomId:this.id, msg:message.msg, attachments:offlineChatMessage.attachments, file:offlineChatMessage.file}).then((data: any) => { - console.log('send sucees', data.result) - let ChatMessage = data.result - this.redefinedMessage(message, ChatMessage) - }) - } - - } - + message.send() this.calDateDuration(message._updatedAt) this.sortRoomList() @@ -342,24 +321,17 @@ export class RoomService { async restoreMessageFromDB() { await this.storage.get('chatmsg' + this.id).then( async (messages = []) => { - if(messages==null) messages = [] await messages.forEach( async (ChatMessage, index) => { const wewMessage = this.prepareMessage(ChatMessage) if(wewMessage.offline == true) { - // this.WsChatService.send({roomId:this.id, msg:wewMessage.msg, attachments:wewMessage.attachments, file: wewMessage.file}).then((data: any) => { - // let _ChatMessage = data.result - // this.redefinedMessage(wewMessage, _ChatMessage) - // messages[index] = _ChatMessage - // this.storage.set('chatmsg' + this.id, messages) - // }) + wewMessage.send() } }); - this.messages = alasql('SELECT * FROM ? ORDER BY _updatedAt',[ this.messages]); setTimeout(()=> { this.scrollDown() }, 50) @@ -390,8 +362,6 @@ export class RoomService { }); - console.log(chatHistory.result.messages); - }) setTimeout(() => { @@ -408,14 +378,9 @@ export class RoomService { const wewMessage = new MessageService(this.storage, this.NfService, this.WsChatService) wewMessage.setData(message) this.messages.push(wewMessage) - return wewMessage } - updateMeessage(messageID, imgbase64) { - - } - async returnData(res) { return res; diff --git a/src/app/services/chat/ws-chat-methods.service.ts b/src/app/services/chat/ws-chat-methods.service.ts index 6ea061412..616f2e56a 100644 --- a/src/app/services/chat/ws-chat-methods.service.ts +++ b/src/app/services/chat/ws-chat-methods.service.ts @@ -13,13 +13,14 @@ import { NativeNotificationService } from 'src/app/services/native-notification. import { SortService } from '../functions/sort.service'; import { chatUser } from 'src/app/models/chatMethod'; import { NfService } from 'src/app/services/chat/nf.service' + @Injectable({ providedIn: 'root' }) export class WsChatMethodsService { - dm: {[key: string]: RoomService} = {} - group: {[key: string]: RoomService} = {} + dm = {} + group = {} _dm = [] @@ -42,9 +43,10 @@ export class WsChatMethodsService { private NativeNotificationService: NativeNotificationService, private sortService: SortService, private ChatService: ChatService, - private NfService: NfService + private NfService: NfService, ) { (async()=>{ + await this.restoreRooms() await this.getAllRooms(); this.subscribeToRoom() @@ -98,6 +100,24 @@ export class WsChatMethodsService { } + + async restoreRooms() { + + try { + const rooms = await this.storage.get('Rooms'); + + if(rooms) { + await rooms.result.update.forEach( async (roomData: room) => { + await this.prepareRoom(roomData); + }); + + } + + } catch(e){} + + this.sortRoomList() + } + async getAllRooms () { this.loadingWholeList = true //this.getRoomFromDb(); @@ -105,6 +125,13 @@ export class WsChatMethodsService { await this.storage.remove('Rooms'); await this.storage.set('Rooms', rooms); + console.log('rooms', rooms) + + this.dm = {} + this.group = {} + this._dm = [] + this._group = [] + // console.log("ROOMS" + JSON.stringify(rooms)) this.WsChatService.registerCallback({ type:'Onmessage', diff --git a/src/app/services/chat/ws-chat.service.ts b/src/app/services/chat/ws-chat.service.ts index 593b79276..256d713a7 100644 --- a/src/app/services/chat/ws-chat.service.ts +++ b/src/app/services/chat/ws-chat.service.ts @@ -635,7 +635,7 @@ updateRoomEventss(roomId, collection:string, funx: Function, ) { console.log('================== welcome to socket server =====================') this.ws.wsMsgQueue() - + alert('run recoonecte ') if(this.wsReconnect >= 1) { for (const [key, value] of Object.entries(this.wsCallbacks)) { if(value.type== 'reConnect') { diff --git a/src/app/shared/chat/group-messages/group-messages.page.html b/src/app/shared/chat/group-messages/group-messages.page.html index 99162985b..2244dbf40 100644 --- a/src/app/shared/chat/group-messages/group-messages.page.html +++ b/src/app/shared/chat/group-messages/group-messages.page.html @@ -78,8 +78,8 @@
- image - + image +
@@ -133,7 +133,7 @@
- image + image
diff --git a/src/app/shared/chat/group-messages/group-messages.page.ts b/src/app/shared/chat/group-messages/group-messages.page.ts index 7be23ce8a..e703fb4dc 100644 --- a/src/app/shared/chat/group-messages/group-messages.page.ts +++ b/src/app/shared/chat/group-messages/group-messages.page.ts @@ -558,8 +558,7 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe this.wsChatMethodsService.getDmRoom(roomId).send({ file: { "type": "application/img", - "guid": '', - "image_url": capturedImage + "guid": '' }, attachments: [{ "title": capturedImageTitle , @@ -642,8 +641,7 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe this.wsChatMethodsService.getDmRoom(roomId).send({ file: { "type": "application/img", - "guid": '', - "image_url": imageData + "guid": '' }, temporaryData: formData, attachments: [{ diff --git a/src/app/shared/chat/messages/messages.page.html b/src/app/shared/chat/messages/messages.page.html index 16c0804e9..72d828098 100644 --- a/src/app/shared/chat/messages/messages.page.html +++ b/src/app/shared/chat/messages/messages.page.html @@ -48,7 +48,7 @@
- {{msg.u.name}} {{msg.offline}} + {{msg.u.name}} {{msg.offline}} {{ msg.sendAttempt }} {{ msg.uploadingFile }} errorUploadingAttachment:{{ msg.errorUploadingAttachment}} {{showDateDuration(msg._updatedAt)}}
@@ -67,14 +67,14 @@
- {{msg.u.name}} + {{msg.u.name}} {{msg.offline}} {{ msg.sendAttempt }} {{ msg.uploadingFile }} errorUploadingAttachment:{{ msg.errorUploadingAttachment}} {{showDateDuration(msg.duration)}}
- image + image
@@ -142,7 +142,7 @@
File - image + image
diff --git a/src/app/shared/chat/messages/messages.page.ts b/src/app/shared/chat/messages/messages.page.ts index 29e55cbb0..9124bdee1 100644 --- a/src/app/shared/chat/messages/messages.page.ts +++ b/src/app/shared/chat/messages/messages.page.ts @@ -460,8 +460,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy this.wsChatMethodsService.getDmRoom(roomId).send({ file: { "type": "application/img", - "guid": '', - "image_url": capturedImage + "guid": '' }, temporaryData: formData, attachments: [{ @@ -539,7 +538,6 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy file: { "type": "application/img", "guid": '', - "image_url": imageData, // GPR }, attachments: [{ "title": file.name ,