This commit is contained in:
Peter Maquiran
2022-02-08 17:44:15 +01:00
parent cef4c39d6e
commit 6a5a486293
15 changed files with 144 additions and 80 deletions
@@ -83,7 +83,7 @@
<div>
<div (click)="openPreview(msg)">
File
<img *ngIf="msg.file.image_url" src="{{msg.file.image_url}}" alt="image">
<img *ngIf="msg.attachments[0].image_url" src="{{msg.attachments[0].image_url}}" alt="image">
</div>
</div>
</div>
@@ -102,8 +102,8 @@
<div *ngIf="msg.attachments" class="message-attachments">
<div *ngFor="let file of msg.attachments">
<div *ngIf="msg.file.type == 'application/img'" (click)="openPreview(msg)">
<img *ngIf="msg.file.image_url" src="{{msg.file.image_url}}" alt="image" >
<ion-icon *ngIf="msg.file.image_url == null" name="download-outline"></ion-icon>
<img *ngIf="msg.attachments[0].image_url" src="{{msg.attachments[0].image_url}}" alt="image" >
<ion-icon *ngIf="msg.attachments[0].image_url == null" name="download-outline"></ion-icon>
</div>
<div *ngIf="msg.file.type != 'application/img'">
<div class="file">
@@ -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: [{
@@ -83,8 +83,8 @@
<div *ngIf="msg.attachments" class="message-attachments">
<div *ngFor="let file of msg.attachments let i = index">
<div *ngIf="msg.file.type == 'application/img'" (click)="openPreview(msg)">
<img *ngIf="msg.file.image_url" src="{{msg.file.image_url}}" alt="image">
<ion-icon *ngIf="msg.file.image_url == null" name="download-outline"></ion-icon>
<img *ngIf="msg.attachments[0].image_url" src="{{msg.attachments[0].image_url}}" alt="image">
<ion-icon *ngIf="msg.attachments[0].image_url == null" name="download-outline"></ion-icon>
</div>
<div *ngIf="msg.file.type != 'application/img'">
<div class="file">
+1 -3
View File
@@ -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: [{
+39 -1
View File
@@ -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)
}
@@ -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) {
+47 -6
View File
@@ -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) {
}
}
}
+1 -1
View File
@@ -8,7 +8,7 @@ import { RoomService } from './room.service';
export class NfService {
beforeSendAttachment = async (message: MessageService, room?: RoomService): Promise<boolean> => new Promise ((resolve, reject)=> (resolve(true)))
downloadFileMsg = async (message: MessageService, room?: RoomService): Promise<boolean> => new Promise ((resolve, reject)=> (resolve(true)))
constructor() { }
}
+6 -41
View File
@@ -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;
@@ -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',
+1 -1
View File
@@ -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') {
@@ -78,8 +78,8 @@
<div *ngIf="msg.attachments" class="message-attachments">
<div *ngFor="let file of msg.attachments">
<div *ngIf="msg.file.type == 'application/img'" (click)="openPreview(msg)">
<img *ngIf="msg.file.image_url" src="{{msg.file.image_url}}" alt="image">
<ion-icon *ngIf="msg.file.image_url == null" name="download-outline"></ion-icon>
<img *ngIf="msg.attachments[0].image_url" src="{{msg.attachments[0].image_url}}" alt="image">
<ion-icon *ngIf="msg.attachments[0].image_url == null" name="download-outline"></ion-icon>
</div>
<div *ngIf="msg.file.type != 'application/img'">
<div class="file">
@@ -133,7 +133,7 @@
<div *ngIf="msg.file" class="message-attachments">
<div>
<div (click)="openPreview(msg)">
<img *ngIf="msg.file.image_url" src="{{msg.file.image_url}}" alt="image">
<img *ngIf="msg.attachments[0].image_url" src="{{msg.attachments[0].image_url}}" alt="image">
</div>
</div>
</div>
@@ -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: [{
@@ -48,7 +48,7 @@
</mat-menu>
</div>
<div class="title">
<ion-label>{{msg.u.name}} {{msg.offline}}</ion-label>
<ion-label>{{msg.u.name}} {{msg.offline}} {{ msg.sendAttempt }} {{ msg.uploadingFile }} errorUploadingAttachment:{{ msg.errorUploadingAttachment}}</ion-label>
<span class="time">{{showDateDuration(msg._updatedAt)}}</span>
</div>
<div>
@@ -67,14 +67,14 @@
</mat-menu>
</div>
<div class="title">
<ion-label>{{msg.u.name}} </ion-label>
<ion-label>{{msg.u.name}} {{msg.offline}} {{ msg.sendAttempt }} {{ msg.uploadingFile }} errorUploadingAttachment:{{ msg.errorUploadingAttachment}}</ion-label>
<span class="time">{{showDateDuration(msg.duration)}}</span>
</div>
<div>
<div *ngIf="msg.attachments" class="message-attachments">
<div *ngFor="let file of msg.attachments">
<div *ngIf="msg.file.type == 'application/img'" (click)="openPreview(msg)" dfsdvsvs>
<img src={{msg.file.image_url}} alt="image">
<img src={{msg.attachments[0].image_url}} alt="image">
</div>
<div *ngIf="msg.file.type != 'application/img'">
<div class="file">
@@ -142,7 +142,7 @@
<div>
<div (click)="openPreview(msg)">
File
<img *ngIf="msg.file.image_url" src="{{msg.file.image_url}}" alt="image">
<img *ngIf="msg.attachments[0].image_url" src="{{msg.attachments[0].image_url}}" alt="image">
</div>
</div>
</div>
@@ -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 ,