diff --git a/src/app/pages/chat/messages/messages.page.ts b/src/app/pages/chat/messages/messages.page.ts
index 8f400df01..e9142db52 100644
--- a/src/app/pages/chat/messages/messages.page.ts
+++ b/src/app/pages/chat/messages/messages.page.ts
@@ -540,8 +540,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,
@@ -582,7 +581,6 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
const roomId = this.roomId
if(data.selected) {
- const loader = this.toastService.loading();
this.wsChatMethodsService.getDmRoom(roomId).send({
file:{
@@ -604,8 +602,6 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
}],
})
- loader.remove();
-
}
});
}
@@ -632,8 +628,7 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
this.wsChatMethodsService.getDmRoom(roomId).send({
file: {
"type": "application/img",
- "guid": '',
- "image_url": 'data:image/jpeg;base64,' +file.base64String
+ "guid": ''
},
temporaryData: formData,
attachments: [{
@@ -663,7 +658,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 fb9659c43..9a4e9ab49 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'
})
@@ -173,6 +174,7 @@ export class AuthService {
return true
} catch(e) {
+ console.log(e)
return false
}
@@ -186,6 +188,7 @@ export class AuthService {
return true
} catch(e) {
+ console.log(e)
return false
}
@@ -194,6 +197,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..35c93720a 100644
--- a/src/app/services/chat/message.service.ts
+++ b/src/app/services/chat/message.service.ts
@@ -27,6 +27,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 +93,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) {
+
+
+ this.WsChatService.registerCallback({
+ type: 'reConnect',
+ funx:()=> {
+ alert('reConnect')
+ // this.send()
+ return true
+ }
+ })
+
+ } else if(uploadSuccessfully == false) {
+
+ 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..10b322cdf 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';
@@ -91,31 +91,40 @@ export class RoomService {
this.id,
"stream-room-messages",
(ChatMessage) => {
- ChatMessage = ChatMessage.fields.args[0]
- if(environment.chatOffline == false || this.isSenderIsNotMe(ChatMessage)) {
-
+ setTimeout(()=>{
+ ChatMessage = ChatMessage.fields.args[0]
+
ChatMessage = this.fix_updatedAt(ChatMessage)
- console.log('recivemessage', ChatMessage)
+ // console.log('recivemessage', ChatMessage)
+
+ const messageIsFound = this.messages.find((message) => {
+ return message._id == ChatMessage._id
+ })
+
+ if(!messageIsFound) {
+ console.log('messageIsFound', messageIsFound)
+ const message = this.prepareMessage(ChatMessage)
+
+ this.lastMessage = message
+ if (message.t == 'r') { this.name = message.msg }
+ this.calDateDuration(ChatMessage._updatedAt)
- const message = this.prepareMessage(ChatMessage)
-
- this.lastMessage = message
- if (message.t == 'r') { this.name = message.msg }
- this.calDateDuration(ChatMessage._updatedAt)
+ setTimeout(() => {
+ this.scrollDown()
+ }, 100)
+
+ this.NativeNotificationService.sendNotificationChat({
+ message: message.msg,
+ title: this.name
+ });
- setTimeout(() => {
- this.scrollDown()
- }, 100)
-
- this.NativeNotificationService.sendNotificationChat({
- message: message.msg,
- title: this.name
- });
+ this.addMessageDB(ChatMessage)
+ } else {
+ console.log('have')
+ }
+ }, 150)
- this.addMessageDB(ChatMessage)
-
- }
}
)
@@ -132,12 +141,13 @@ export class RoomService {
} else if (message.fields.eventName == this.id+'/'+'deleteMessage') {}
})
- this.WsChatService.registerCallback
}
addMessageDB(ChatMessage) {
this.storage.get('chatmsg' + this.id).then((messages: any = []) => {
+ if(messages==null) messages = []
+
delete ChatMessage.temporaryData
messages.push(ChatMessage)
@@ -158,7 +168,7 @@ export class RoomService {
}
)
- this.WsChatService.registerCallback
+
}
/**
@@ -188,6 +198,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 +239,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,29 +329,21 @@ 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)
+ const wewMessage = this.prepareMessage(ChatMessage, false)
- 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)
- // })
+ if(wewMessage.offline == false) {
+ this.prepareMessage(ChatMessage)
}
});
- this.messages = alasql('SELECT * FROM ? ORDER BY _updatedAt',[ this.messages]);
setTimeout(()=> {
this.scrollDown()
}, 50)
-
})
}
@@ -373,7 +352,7 @@ export class RoomService {
if (this.hasLoadHistory) { return false }
- await this.restoreMessageFromDB()
+ this.restoreMessageFromDB()
await this.WsChatService.loadHistory(this.id, limit).then( async (chatHistory:chatHistory) => {
console.log('loadHistory', chatHistory)
@@ -381,17 +360,8 @@ export class RoomService {
await chatHistory.result.messages.reverse().forEach( async (message) => {
this.prepareMessage(message)
-
- // const result = alasql(`SELECT * FROM ? WHERE _id = "${message._id}" `,[ this.messages]);
- // if(result.length == 0) {
- // this.prepareMessage(message)
- // this.storage.set('chatmsg' + this.id, chatHistory.result.messages.concat([message]))
- // }
-
});
- console.log(chatHistory.result.messages);
-
})
setTimeout(() => {
@@ -403,19 +373,18 @@ export class RoomService {
- prepareMessage(message): MessageService {
+ prepareMessage(message, save = true): MessageService {
message = this.fix_updatedAt(message)
const wewMessage = new MessageService(this.storage, this.NfService, this.WsChatService)
wewMessage.setData(message)
- this.messages.push(wewMessage)
-
+
+ if (save) {
+ 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 9d3a6268f..509b61c96 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()
@@ -67,6 +69,39 @@ export class WsChatMethodsService {
}
})
+
+ // this.WsChatService.registerCallback({
+ // type:'Onmessage',
+ // funx:(message) => {
+
+ // if(message.msg =='changed' && message.collection == "stream-room-messages") {
+ // if(message.fields.args[0].rid) {
+ // // new message
+ // const ChatMessage = message.fields.args[0]
+ // const messageId = ChatMessage.rid
+
+ // setTimeout(()=>{
+ // this.sortRoomList()
+ // }, 100)
+
+ // }
+ // } else if(message.msg =='changed' && message.collection == "stream-notify-room") {
+ // if(message.fields.eventName.includes('deleteMessage')) {
+ // // delete message
+ // const DeletedMessageId = message.fields.args[0]._id;
+
+ // setTimeout(()=>{
+ // this.sortRoomList()
+ // }, 100)
+
+ // } else if(message.fields.eventName.includes('typing')) {
+
+ // }
+ // }
+ // }
+ // })
+
+
}
getRoomFromDb() {
@@ -98,6 +133,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,31 +158,12 @@ export class WsChatMethodsService {
await this.storage.remove('Rooms');
await this.storage.set('Rooms', rooms);
- // console.log("ROOMS" + JSON.stringify(rooms))
- this.WsChatService.registerCallback({
- type:'Onmessage',
- funx:(message)=>{
+ console.log('rooms', rooms)
- if(message.msg =='changed' && message.collection == "stream-room-messages") {
- if(message.fields.args[0].rid) {
-
- setTimeout(()=>{
- this.sortRoomList()
-
- }, 100)
- }
- }
-
- if(message.msg =='changed' && message.collection == "stream-notify-room") {
- if(message.fields.eventName.includes('deleteMessage')){
- setTimeout(()=>{
-
- this.sortRoomList()
- }, 100)
- }
- }
- }
- })
+ this.dm = {}
+ this.group = {}
+ this._dm = []
+ this._group = []
await rooms.result.update.forEach( async (roomData: room) => {
await this.prepareRoom(roomData);
@@ -249,20 +283,20 @@ export class WsChatMethodsService {
*/
private getUserStatus(id?:string) {
- this.WsChatService.getUserStatus((d) => {
+ // this.WsChatService.getUserStatus((d) => {
- const username = d.fields.args[0][1]
- const statusNum = d.fields.args[0][2]
+ // const username = d.fields.args[0][1]
+ // const statusNum = d.fields.args[0][2]
- const statusText = this.statusNumberToText(statusNum)
+ // const statusText = this.statusNumberToText(statusNum)
- const user = this.getUserByName(username)
+ // const user = this.getUserByName(username)
- if(user) {
- user.status = statusText
- }
+ // if(user) {
+ // user.status = statusText
+ // }
- })
+ // })
}
diff --git a/src/app/services/chat/ws-chat.service.ts b/src/app/services/chat/ws-chat.service.ts
index ef226c27c..dd84856c6 100644
--- a/src/app/services/chat/ws-chat.service.ts
+++ b/src/app/services/chat/ws-chat.service.ts
@@ -15,13 +15,6 @@ export class WsChatService {
loginResponse = {}
constructor() {
-
-
- window.addEventListener('online', ()=>{
- this.connect()
- this.login()
- });
-
}
connect() {
@@ -612,13 +605,15 @@ updateRoomEventss(roomId, collection:string, funx: Function, ) {
private wsCallbacks: {[key: string]: wsCallbacksParams} = {}
private wsReconnect = 0
+ private n = 0
+
private ws = {
connected: false,
registerCallback:(params: wsCallbacksParams) => {
let id = params.requestId || params.key || uuidv4()
this.wsCallbacks[id] = params
-
+ this.n++
return id
},
connect:()=> {
@@ -629,19 +624,19 @@ updateRoomEventss(roomId, collection:string, funx: Function, ) {
this.socket.onclose = this.ws.onclose;
this.socket.onerror = this.ws.onerror;
},
- onopen:()=> {
+ onopen: async ()=> {
this.ws.connected = true
console.log('================== welcome to socket server =====================')
this.ws.wsMsgQueue()
-
if(this.wsReconnect >= 1) {
for (const [key, value] of Object.entries(this.wsCallbacks)) {
if(value.type== 'reConnect') {
- const dontRepeat = value.funx()
+ const dontRepeat = await value.funx()
if(dontRepeat) {
+ this.n--
delete this.wsCallbacks[key]
}
@@ -681,15 +676,16 @@ updateRoomEventss(roomId, collection:string, funx: Function, ) {
return requestId
},
- onmessage:(event: any)=> {
+ onmessage: async (event: any)=> {
const data = JSON.parse(event.data)
for (const [key, value] of Object.entries(this.wsCallbacks)) {
if(value.type== 'Onmessage') {
- const dontRepeat = value.funx(data)
+ const dontRepeat = await value.funx(data)
if(dontRepeat) {
delete this.wsCallbacks[key]
+ this.n--
}
}
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 @@
-

-
+

+
@@ -133,7 +133,7 @@
-

+
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 f4b11c203..99cde18f3 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 52066f057..cdb4c7b2f 100644
--- a/src/app/shared/chat/messages/messages.page.html
+++ b/src/app/shared/chat/messages/messages.page.html
@@ -67,14 +67,14 @@
- {{msg.u.name}}
+ {{msg.u.name}}
{{showDateDuration(msg.duration)}}
-
-

+
+
@@ -142,7 +142,7 @@
File
-

+
diff --git a/src/app/shared/chat/messages/messages.page.ts b/src/app/shared/chat/messages/messages.page.ts
index f80738448..29aca9233 100644
--- a/src/app/shared/chat/messages/messages.page.ts
+++ b/src/app/shared/chat/messages/messages.page.ts
@@ -277,7 +277,6 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
}
}
-
changeInput() {
this.wsChatMethodsService.getDmRoom(this.roomId).typing()
}
@@ -466,7 +465,6 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
file: {
"type": "application/img",
"guid": '',
- "image_url": 'data:image/jpeg;base64,' +file.base64String
},
temporaryData: formData,
attachments: [{
@@ -497,8 +495,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: [{
@@ -583,8 +580,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
this.wsChatMethodsService.getDmRoom(roomId).send({
file: {
"type": "application/img",
- "guid": '',
- "image_url": 'data:image/jpeg;base64,' +file.base64String
+ "guid": ''
},
temporaryData: formData,
attachments: [{
@@ -614,7 +610,6 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
file: {
"type": "application/img",
"guid": '',
- "image_url": imageData, // GPR
},
attachments: [{
"title": file.name ,