From 61de7b937b7f99aebd6a25377c22f44a748a1aac Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Thu, 3 Feb 2022 21:01:53 +0100 Subject: [PATCH 1/9] improve chat --- .../group-messages/group-messages.page.ts | 218 ++++++++- src/app/pages/chat/messages/messages.page.ts | 311 ++++++++----- src/app/services/attachments.service.ts | 41 ++ src/app/services/camera.service.spec.ts | 16 + src/app/services/camera.service.ts | 36 ++ src/app/services/chat/message.service.ts | 44 +- src/app/services/chat/room.service.ts | 88 +++- .../services/chat/ws-chat-methods.service.ts | 2 +- src/app/services/file-system.service.spec.ts | 16 + src/app/services/file-system.service.ts | 145 ++++++ src/app/services/functions/file.service.ts | 429 +----------------- .../group-messages/group-messages.page.ts | 226 ++++++++- src/app/shared/chat/messages/messages.page.ts | 243 +++++++--- 13 files changed, 1147 insertions(+), 668 deletions(-) create mode 100644 src/app/services/camera.service.spec.ts create mode 100644 src/app/services/camera.service.ts create mode 100644 src/app/services/file-system.service.spec.ts create mode 100644 src/app/services/file-system.service.ts 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 385b64a06..d0de49f31 100644 --- a/src/app/pages/chat/group-messages/group-messages.page.ts +++ b/src/app/pages/chat/group-messages/group-messages.page.ts @@ -26,6 +26,13 @@ import { ViewEventPage } from 'src/app/modals/view-event/view-event.page'; import { HttpEventType } from '@angular/common/http'; import { SqliteService } from 'src/app/services/sqlite.service'; import { WsChatMethodsService } from 'src/app/services/chat/ws-chat-methods.service'; +import { AttachmentsService } from 'src/app/services/attachments.service'; +import { FileType } from 'src/app/models/fileType'; +import { Storage } from '@ionic/storage'; +import { FileSystemService } from 'src/app/services/file-system.service'; +import { CameraService } from 'src/app/services/camera.service'; +import { SearchPage } from 'src/app/pages/search/search.page'; +import { ProcessesService } from 'src/app/services/processes.service'; @Component({ selector: 'app-group-messages', @@ -87,7 +94,12 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { private changeDetectorRef: ChangeDetectorRef, private sqlservice: SqliteService, private platform: Platform, - public wsChatMethodsService: WsChatMethodsService + public wsChatMethodsService: WsChatMethodsService, + private AttachmentsService: AttachmentsService, + private storage: Storage, + private processesService: ProcessesService, + private FileSystemService: FileSystemService, + private CameraService: CameraService, ) { this.loggedUserChat = authService.ValidatedUserChat['data']; this.isGroupCreated = true; @@ -481,8 +493,150 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { } } + + + async takePicture() { + const roomId = this.roomId + + const image = await this.CameraService.takePicture(); + await this.FileSystemService.saveImage(image, roomId) + const lastphoto: any = await this.FileSystemService.loadFiles(roomId); + const { capturedImage, capturedImageTitle} = await this.FileSystemService.loadFileData(lastphoto, roomId); + + const base64 = await fetch(capturedImage); + const blob = await base64.blob(); + const formData = new FormData(); + formData.append("blobFile", blob); + console.log('ALL IMAGE', formData) + let guid: any = await this.AttachmentsService.uploadFile(formData).toPromise() + console.log(guid.path); + + this.AttachmentsService.downloadFile(guid.path).subscribe(async (event) => { + + if (event.type === HttpEventType.DownloadProgress) { + //this.downloadProgess = Math.round((100 * event.loaded) / event.total); + //console.log('FILE TYPE 33', msg.file.type) + } else if (event.type === HttpEventType.Response) { + var fileImage = 'data:image/jpeg;base64,' + btoa(new Uint8Array(event.body).reduce((data, byte) => data + String.fromCharCode(byte), '')); + console.log('add picture to chat',fileImage); + await this.storage.set(guid.path, fileImage).then(() => { + console.log('add picture to chat IMAGE SAVED') + + this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ + file: { + "type": "application/img", + "guid": guid.path, + "image_url": fileImage + }, + attachments: [{ + //"title": this.capturedImageTitle , + //"text": "description", + "title_link_download": false, + //"image_url": this.capturedImage, + }] + }) + + }); + } + + }); + + } + + async addImage() { + this.addFileToChat(['image/apng', 'image/jpeg', 'image/png']) + } + + async addFile() { + this.addFileToChat(['.doc', '.docx', '.pdf']) + } + + async addFileWebtrix() { + const modal = await this.modalController.create({ + component: SearchPage, + cssClass: 'group-messages modal-desktop search-modal search-modal-to-desktop', + componentProps: { + type: 'AccoesPresidenciais & ArquivoDespachoElect', + select: true, + showSearchInput: true, + } + }); + await modal.present(); + modal.onDidDismiss().then(async res=>{ + const data = res.data; + + if(data.selected){ + const loader = this.toastService.loading(); + + let url = await this.processesService.GetDocumentUrl(res.data.selected.Id, res.data.selected.ApplicationType).toPromise(); + let url_no_options: string = url.replace("webTRIX.Viewer","webTRIX.Viewer.Branch1"); + console.log(url_no_options); + + this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ + alias: "documento", + file:{ + "name": res.data.selected.Assunto, + "type": "application/webtrix", + "ApplicationId": res.data.selected.ApplicationType, + "DocId": res.data.selected.Id, + "Assunto": res.data.selected.Assunto, + }, + attachments: [{ + "title": res.data.selected.Assunto, + "description": res.data.selected.DocTypeDesc, + "title_link": url_no_options, + "title_link_download": true, + //"thumb_url": "assets/images/webtrix-logo.png", + "message_link": url_no_options, + "type": "webtrix" + }] + }) + loader.remove(); + + } + }); + } + + + async addFileToChat(types: typeof FileType[] ) { + const file = await this.fileService.getFileFromDevice(types); + const formData = new FormData(); + formData.append("blobFile", file); + let guid: any = await this.AttachmentsService.uploadFile(formData).toPromise() + this.AttachmentsService.downloadFile(guid.path).subscribe(async (event) => { + + if (event.type === HttpEventType.DownloadProgress) { + //this.downloadProgess = Math.round((100 * event.loaded) / event.total); + //console.log('FILE TYPE 33', msg.file.type) + } else if (event.type === HttpEventType.Response) { + var fileImage = 'data:image/jpeg;base64,' + btoa(new Uint8Array(event.body).reduce((data, byte) => data + String.fromCharCode(byte), '')); + console.log('add picture to chat',fileImage); + await this.storage.set(guid.path, fileImage).then(() => { + console.log('add picture to chat IMAGE SAVED') + + this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ + file: { + "type": "application/img", + "guid": guid.path, + "image_url": fileImage + }, + attachments: [{ + //"title": this.capturedImageTitle , + //"text": "description", + "title_link_download": false, + //"image_url": this.capturedImage, + }] + }) + + }); + } + + }); + } + async openChatOptions(ev?: any) { console.log(this.members); + const roomId = this.roomId; const popover = await this.popoverController.create({ component: ChatOptionsPopoverPage, @@ -496,27 +650,65 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { translucent: true }); await popover.present(); - await popover.onDidDismiss().then((res)=>{ + await popover.onDidDismiss().then( async(res)=>{ console.log(res['data']); if(res['data'] == 'meeting'){ this.bookMeeting(); } - else if(res['data'] == 'take-picture'){ - this.fileService.addCameraPictureToChat(this.roomId); - //this.loadPicture(); + else if(res['data'] == 'take-picture') { + const image = await this.CameraService.takePicture(); + await this.FileSystemService.saveImage(image, roomId) + const lastphoto: any = await this.FileSystemService.loadFiles(roomId); + const { capturedImage, capturedImageTitle} = await this.FileSystemService.loadFileData(lastphoto, roomId); + + const base64 = await fetch(capturedImage); + const blob = await base64.blob(); + const formData = new FormData(); + formData.append("blobFile", blob); + console.log('ALL IMAGE', formData) + let guid: any = await this.AttachmentsService.uploadFile(formData).toPromise() + console.log(guid.path); + + this.AttachmentsService.downloadFile(guid.path).subscribe(async (event) => { + + if (event.type === HttpEventType.DownloadProgress) { + //this.downloadProgess = Math.round((100 * event.loaded) / event.total); + //console.log('FILE TYPE 33', msg.file.type) + } else if (event.type === HttpEventType.Response) { + var fileImage = 'data:image/jpeg;base64,' + btoa(new Uint8Array(event.body).reduce((data, byte) => data + String.fromCharCode(byte), '')); + console.log('add picture to chat',fileImage); + await this.storage.set(guid.path, fileImage).then(() => { + console.log('add picture to chat IMAGE SAVED') + + this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ + file: { + "type": "application/img", + "guid": guid.path, + "image_url": fileImage + }, + attachments: [{ + //"title": this.capturedImageTitle , + //"text": "description", + "title_link_download": false, + //"image_url": this.capturedImage, + }] + }) + + }); + } + + }); } else if(res['data'] == 'add-picture'){ - this.fileService.addPictureToChatMobile(this.roomId); - //this.loadPicture(); + + this.addImage() + } else if(res['data'] == 'add-document'){ - this.fileService.addDocumentToChat(this.roomId); - //this.loadDocument(); + this.addFile() } else if(res['data'] == 'documentoGestaoDocumental'){ - - this.fileService.addDocGestaoDocumentalToChat(this.roomId); - //this.addDocGestaoDocumental(); + this.addFileWebtrix() } this.loadGroupMessages(this.roomId); }); @@ -742,7 +934,7 @@ downloadFileMsg(msg) { console.log('FILE TYPE', msg.file.type) this.downloadFile = ""; if (msg.file.type == "application/img") { - this.fileService.downloadFile(msg.file.guid).subscribe(async (event) => { + this.AttachmentsService.downloadFile(msg.file.guid).subscribe(async (event) => { console.log('FILE TYPE 22', msg.file.guid) var name = msg.file.guid; diff --git a/src/app/pages/chat/messages/messages.page.ts b/src/app/pages/chat/messages/messages.page.ts index 605f83532..9568bcd3b 100644 --- a/src/app/pages/chat/messages/messages.page.ts +++ b/src/app/pages/chat/messages/messages.page.ts @@ -31,6 +31,13 @@ import { HttpEventType } from '@angular/common/http'; import { ViewEventPage } from 'src/app/modals/view-event/view-event.page'; import { WsChatMethodsService } from 'src/app/services/chat/ws-chat-methods.service' import { MessageService } from 'src/app/services/chat/message.service'; +import { AttachmentsService } from 'src/app/services/attachments.service'; +import { FileSystemService } from 'src/app/services/file-system.service'; +import { CameraService } from 'src/app/services/camera.service'; +import { element } from 'protractor'; +import { FileType } from 'src/app/models/fileType'; +import { SearchPage } from 'src/app/pages/search/search.page'; +import { Storage } from '@ionic/storage'; const IMAGE_DIR = 'stored-images'; @@ -98,7 +105,12 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { private changeDetectorRef: ChangeDetectorRef, private platform: Platform, private sqlservice: SqliteService, - public wsChatMethodsService: WsChatMethodsService + public wsChatMethodsService: WsChatMethodsService, + private AttachmentsService: AttachmentsService, + private FileSystemService: FileSystemService, + private CameraService: CameraService, + private processesService: ProcessesService, + private storage: Storage, ) { this.loggedUser = authService.ValidatedUserChat['data']; this.roomId = this.navParams.get('roomId'); @@ -121,8 +133,6 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { } ngOnInit() { - //this.load(); - this.setStatus('online'); //this.loadFiles(); VoiceRecorder.requestAudioRecordingPermission(); @@ -253,15 +263,6 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { this.selectedMsgId = ""; } - setStatus(status: string) { - let body = { - message: '', - status: status, - } - this.chatService.setUserStatus(body).subscribe(res => { - console.log(res); - }) - } notImplemented() { this.alertService.presentAlert('Funcionalidade em desenvolvimento'); @@ -272,7 +273,6 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { } load() { - this.serverLongPull(); this.getChatMembers(); } @@ -515,6 +515,146 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { } } + + async takePicture() { + const roomId = this.roomId + + const image = await this.CameraService.takePicture(); + await this.FileSystemService.saveImage(image, roomId) + const lastphoto: any = await this.FileSystemService.loadFiles(roomId); + const { capturedImage, capturedImageTitle} = await this.FileSystemService.loadFileData(lastphoto, roomId); + + const base64 = await fetch(capturedImage); + const blob = await base64.blob(); + const formData = new FormData(); + formData.append("blobFile", blob); + console.log('ALL IMAGE', formData) + let guid: any = await this.AttachmentsService.uploadFile(formData).toPromise() + console.log(guid.path); + + this.AttachmentsService.downloadFile(guid.path).subscribe(async (event) => { + + if (event.type === HttpEventType.DownloadProgress) { + //this.downloadProgess = Math.round((100 * event.loaded) / event.total); + //console.log('FILE TYPE 33', msg.file.type) + } else if (event.type === HttpEventType.Response) { + var fileImage = 'data:image/jpeg;base64,' + btoa(new Uint8Array(event.body).reduce((data, byte) => data + String.fromCharCode(byte), '')); + console.log('add picture to chat',fileImage); + await this.storage.set(guid.path, fileImage).then(() => { + console.log('add picture to chat IMAGE SAVED') + + this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ + file: { + "type": "application/img", + "guid": guid.path, + "image_url": fileImage + }, + attachments: [{ + //"title": this.capturedImageTitle , + //"text": "description", + "title_link_download": false, + //"image_url": this.capturedImage, + }] + }) + + }); + } + + }); + + } + + async addImage() { + this.addFileToChat(['image/apng', 'image/jpeg', 'image/png']) + } + + async addFile() { + this.addFileToChat(['.doc', '.docx', '.pdf']) + } + + async addFileWebtrix() { + const modal = await this.modalController.create({ + component: SearchPage, + cssClass: 'group-messages modal-desktop search-modal search-modal-to-desktop', + componentProps: { + type: 'AccoesPresidenciais & ArquivoDespachoElect', + select: true, + showSearchInput: true, + } + }); + await modal.present(); + modal.onDidDismiss().then(async res=>{ + const data = res.data; + + if(data.selected){ + const loader = this.toastService.loading(); + + let url = await this.processesService.GetDocumentUrl(res.data.selected.Id, res.data.selected.ApplicationType).toPromise(); + let url_no_options: string = url.replace("webTRIX.Viewer","webTRIX.Viewer.Branch1"); + console.log(url_no_options); + + this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ + alias: "documento", + file:{ + "name": res.data.selected.Assunto, + "type": "application/webtrix", + "ApplicationId": res.data.selected.ApplicationType, + "DocId": res.data.selected.Id, + "Assunto": res.data.selected.Assunto, + }, + attachments: [{ + "title": res.data.selected.Assunto, + "description": res.data.selected.DocTypeDesc, + "title_link": url_no_options, + "title_link_download": true, + //"thumb_url": "assets/images/webtrix-logo.png", + "message_link": url_no_options, + "type": "webtrix" + }] + }) + loader.remove(); + + } + }); + } + + + async addFileToChat(types: typeof FileType[] ) { + const file = await this.fileService.getFileFromDevice(types); + const formData = new FormData(); + formData.append("blobFile", file); + let guid: any = await this.AttachmentsService.uploadFile(formData).toPromise() + this.AttachmentsService.downloadFile(guid.path).subscribe(async (event) => { + + if (event.type === HttpEventType.DownloadProgress) { + //this.downloadProgess = Math.round((100 * event.loaded) / event.total); + //console.log('FILE TYPE 33', msg.file.type) + } else if (event.type === HttpEventType.Response) { + var fileImage = 'data:image/jpeg;base64,' + btoa(new Uint8Array(event.body).reduce((data, byte) => data + String.fromCharCode(byte), '')); + console.log('add picture to chat',fileImage); + await this.storage.set(guid.path, fileImage).then(() => { + console.log('add picture to chat IMAGE SAVED') + + this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ + file: { + "type": "application/img", + "guid": guid.path, + "image_url": fileImage + }, + attachments: [{ + //"title": this.capturedImageTitle , + //"text": "description", + "title_link_download": false, + //"image_url": this.capturedImage, + }] + }) + + }); + } + + }); + } + async openChatOptions(ev?: any) { const roomId = this.roomId console.log(this.members); @@ -531,66 +671,71 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { translucent: true }); await popover.present(); - popover.onDidDismiss().then((res) => { + popover.onDidDismiss().then( async (res) => { console.log(res['data']); if (res['data'] == 'meeting') { this.bookMeeting(); } else if (res['data'] == 'take-picture') { - this.fileService.addCameraPictureToChat(roomId); - //this.loadPicture(); + const image = await this.CameraService.takePicture(); + await this.FileSystemService.saveImage(image, roomId) + const lastphoto: any = await this.FileSystemService.loadFiles(roomId); + const { capturedImage, capturedImageTitle} = await this.FileSystemService.loadFileData(lastphoto, roomId); + + const base64 = await fetch(capturedImage); + const blob = await base64.blob(); + const formData = new FormData(); + formData.append("blobFile", blob); + console.log('ALL IMAGE', formData) + let guid: any = await this.AttachmentsService.uploadFile(formData).toPromise() + console.log(guid.path); + + this.AttachmentsService.downloadFile(guid.path).subscribe(async (event) => { + + if (event.type === HttpEventType.DownloadProgress) { + //this.downloadProgess = Math.round((100 * event.loaded) / event.total); + //console.log('FILE TYPE 33', msg.file.type) + } else if (event.type === HttpEventType.Response) { + var fileImage = 'data:image/jpeg;base64,' + btoa(new Uint8Array(event.body).reduce((data, byte) => data + String.fromCharCode(byte), '')); + console.log('add picture to chat',fileImage); + await this.storage.set(guid.path, fileImage).then(() => { + console.log('add picture to chat IMAGE SAVED') + + this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ + file: { + "type": "application/img", + "guid": guid.path, + "image_url": fileImage + }, + attachments: [{ + //"title": this.capturedImageTitle , + //"text": "description", + "title_link_download": false, + //"image_url": this.capturedImage, + }] + }) + + }); + } + + }); } else if (res['data'] == 'add-picture') { - this.fileService.addPictureToChatMobile(roomId); - //this.loadPicture(); + this.addImage() + } else if (res['data'] == 'add-document') { - this.fileService.addDocumentToChat(this.roomId); - //this.loadDocument(); + this.addFile() } else if (res['data'] == 'documentoGestaoDocumental') { - this.fileService.addDocGestaoDocumentalToChat(this.roomId); - //this.addDocGestaoDocumental(); + this.addFileWebtrix() } }); } - /* - this.fileService.downloadFile(element.file.guid).subscribe(async (event) => { - var name = element.file.guid; - if (event.type === HttpEventType.DownloadProgress) { - - } else if (event.type === HttpEventType.Response) { - var base64 = btoa(new Uint8Array(event.body).reduce((data, byte) => data + String.fromCharCode(byte), '') - ); - - console.log('TRY ARRAY BUFFER NAME', name); - console.log('TRY ARRAY BUFFER', base64); - - await Filesystem.writeFile({ - path: `${IMAGE_DIR}/${name}`, - data: base64, - directory: Directory.Data - }).then((foo) => { - - console.log('LSKE FS FILE SAVED', foo) - - }).catch((error) => { - console.log('error LAKE FS FILE', error) - }); - - - - const readFile = await Filesystem.readFile({ - path: `${IMAGE_DIR}/${name}`, - directory: Directory.Data, - }); - - - });*/ getRoomMessageDB(roomId) { if (this.platform.is('desktop') || this.platform.is('mobileweb')) { @@ -666,60 +811,6 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { } - async serverLongPull() { - - const roomId = this.roomId - - /* this.chatService.getRoomMessages(roomId).subscribe(res=>{ - console.log(res); - - }) */ - - - this.chatService.getRoomMessages(roomId).subscribe(async res => { - console.log("Chat message", res) - - this.transformDataMSG(res['messages']) - this.getRoomMessageDB(this.roomId); - - - if (res == 502) { - // Connection timeout - // happens when the synchro was pending for too long - // let's reconnect - await this.serverLongPull(); - } - else if (res != 200) { - // Show Error - //showMessage(response.statusText); - //this.loadMessages() - if (this.platform.is('desktop') || this.platform.is('mobileweb')) { - - } - - - //console.log(this.messages); - // Reconnect in one second - if (this.route.url != "/home/chat") { - console.log("Timer message stop") - } else { - //Check if modal is opened - if (document.querySelector('.isMessagesChatOpened')) { - await new Promise(resolve => setTimeout(resolve, 5000)); - await this.serverLongPull(); - //console.log('Timer message running') - } - } - - } else { - // Got message - //let message = await response.text(); - //this.loadMessages() - await this.serverLongPull(); - } - }); - } - sliderOpts = { zoom: false, @@ -767,7 +858,7 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { console.log('FILE TYPE', msg.file.type) this.downloadFile = ""; if (msg.file.type == "application/img") { - this.fileService.downloadFile(msg.file.guid).subscribe(async (event) => { + this.AttachmentsService.downloadFile(msg.file.guid).subscribe(async (event) => { console.log('FILE TYPE 22', msg.file.guid) var name = msg.file.guid; diff --git a/src/app/services/attachments.service.ts b/src/app/services/attachments.service.ts index fa0be2ba1..1c20db0dd 100644 --- a/src/app/services/attachments.service.ts +++ b/src/app/services/attachments.service.ts @@ -20,6 +20,47 @@ export class AttachmentsService { this.headers = this.headers.set('Authorization', this.loggeduser.BasicAuthKey); } + uploadFile(formData:any) { + + console.log('UPLOAD file', formData) + + //const geturl = environment.apiURL + 'Tasks/DelegateTask'; + const geturl = environment.apiURL + 'ObjectServer/UploadFiles'; + + let options = { + headers: this.headers + }; + + return this.http.post(`${geturl}`, formData, options); + } + + getFile(guid:any) { + const geturl = environment.apiURL + 'lakefs/StreamFile'; + let params = new HttpParams(); + + params = params.set("path", guid); + + this.headers = this.headers.set('responseType', 'blob'); + this.headers = this.headers.set('Content-Type', 'application/octet-stream'); + + let options = { + headers: this.headers, + params: params + }; + return this.http.get(`${geturl}`, options); + } + + downloadFile(guid:any) { + + let downloadUrl = environment.apiURL +'objectserver/streamfiles?path='+guid; + var name = new Date().getTime(); + return this.http.get(downloadUrl, { + responseType: "arraybuffer", + reportProgress: true, observe: 'events' + }) + + } + getAttachmentsBySerial(serialNumber: string): Observable{ let geturl = environment.apiURL + 'attachments/GetAttachments'; let params = new HttpParams(); diff --git a/src/app/services/camera.service.spec.ts b/src/app/services/camera.service.spec.ts new file mode 100644 index 000000000..73b57d3a5 --- /dev/null +++ b/src/app/services/camera.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { CameraService } from './camera.service'; + +describe('CameraService', () => { + let service: CameraService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(CameraService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/camera.service.ts b/src/app/services/camera.service.ts new file mode 100644 index 000000000..b99fd6956 --- /dev/null +++ b/src/app/services/camera.service.ts @@ -0,0 +1,36 @@ +import { Injectable } from '@angular/core'; +import { Camera, CameraResultType, CameraSource, Photo} from '@capacitor/camera'; + +@Injectable({ + providedIn: 'root' +}) +export class CameraService { + + constructor() { } + + + async takePicture(){ + + return new Promise(async (resolve, reject)=>{ + + console.log('add camera to picture') + + const image = await Camera.getPhoto({ + quality: 50, + allowEditing: false, + resultType: CameraResultType.Uri, + source: CameraSource.Camera // Camera, Photos or Prompt! + }); + + if (image) { + resolve(image) + + } else { + reject('Error saving image') + } + + //this.capturedImage = this.capturedImage; + }) + + } +} diff --git a/src/app/services/chat/message.service.ts b/src/app/services/chat/message.service.ts index 43c8f74be..758e296a5 100644 --- a/src/app/services/chat/message.service.ts +++ b/src/app/services/chat/message.service.ts @@ -2,6 +2,8 @@ import { Injectable } from '@angular/core'; import { Message } from 'src/app/models/chatMethod'; import { chatHistory, ChatMessage, File } from 'src/app/models/chatMethod' import { Storage } from '@ionic/storage'; +import { SessionStore } from 'src/app/store/session.service'; +import { capitalizeTxt } from 'src/plugin/text' @Injectable({ providedIn: 'root' @@ -17,28 +19,34 @@ export class MessageService { u = {} t = '' _id ='' - _updatedAt = '' + _updatedAt file attachments - offline = false + offline = true constructor(private storage: Storage) { - } + } setData({customFields, channels, mentions, msg ,rid ,ts, u, t, _id, _updatedAt, file, attachments}:Message) { - this.customFields = customFields - this.channels = channels - this.mentions = mentions - this.msg = msg + this.customFields = customFields + this.channels = channels || [] + this.mentions = mentions || [] + this.msg = msg || "" this.rid = rid this.ts = ts - this.u = u + this.u = u || { name: this.usernameToDisplayName(SessionStore.user.RochetChatUser), username: SessionStore.user.RochetChatUser, _id: ""} this.t = t this._id = _id - this._updatedAt = _updatedAt + this._updatedAt = _updatedAt || new Date().getTime() this.file = file this.attachments = attachments + if(!this.ts) { + this.offline = true + } else { + this.offline = false + } + if (this.file) { if (this.file.guid) { @@ -49,22 +57,22 @@ export class MessageService { } } - - if(this.rid == 'offline') { - this.offline = true - } else { - this.offline = false - } - } delete() {} showDateDuration() {} - - + resend() { } + + private usernameToDisplayName(username) { + + const firstName = capitalizeTxt(username.split('.')[0]) + const lastName = capitalizeTxt(username.split('.')[1]) + return firstName + ' ' + lastName + } + } diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index a3c58eaed..1112c4a23 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -14,6 +14,7 @@ import { capitalizeTxt } from 'src/plugin/text' import { SortService } from '../functions/sort.service'; import { chatUser } from 'src/app/models/chatMethod'; import { environment } from 'src/environments/environment'; +import { ChatService } from 'src/app/services/chat.service'; @Injectable({ providedIn: 'root' @@ -62,6 +63,7 @@ export class RoomService { private sqlservice: SqliteService, private NativeNotificationService: NativeNotificationService, private sortService: SortService, + private chatService: ChatService, ) { this.NativeNotificationService.askForPermission() } @@ -88,9 +90,10 @@ export class RoomService { this.id, "stream-room-messages", (ChatMessage) => { + ChatMessage = ChatMessage.fields.args[0] if(environment.chatOffline == false || this.isSenderIsNotMe(ChatMessage)) { - ChatMessage = ChatMessage.fields.args[0] + ChatMessage = this.fix_updatedAt(ChatMessage) console.log('recivemessage', ChatMessage) @@ -211,15 +214,15 @@ export class RoomService { const offlineChatMessage = { channels: [], mentions: [], - rid: 'offline', + rid: this.id, msg: this.message, - ts: { $date: new Date().getTime() }, u: { name: this.usernameToDisplayName(SessionStore.user.RochetChatUser), username: SessionStore.user.RochetChatUser, _id: "" }, - _updatedAt: new Date().getTime() + _updatedAt: new Date().getTime(), + offline: true } this.addMessageDB(offlineChatMessage) @@ -231,31 +234,60 @@ export class RoomService { this.lastMessage = message - this.redefinedMessage(message) + + this.WsChatService.send(this.id, message.msg).then((data: any) => { + let ChatMessage = data.result + this.redefinedMessage(message, ChatMessage) + }) this.calDateDuration(message._updatedAt) this.sortRoomList() - this.message= '' } } - redefinedMessage = (message: MessageService) => { - this.WsChatService.send(this.id, message.msg).then((data: any) => { - let ChatMessage = data.result + sendFile({file, attachments, alias = 'document'}) { + let offlineChatMessage = { + rid: this.id, + msg: this.message, + attachments, + file, + } + + this.addMessageDB(offlineChatMessage) + const message: MessageService = this.prepareMessage(offlineChatMessage) - ChatMessage = this.fix_updatedAt(ChatMessage) + setTimeout(() => { + this.scrollDown() + }, 150) - message.setData(ChatMessage) + this.lastMessage = message - if( new Date(this.lastMessage._updatedAt).getTime() < new Date(message._updatedAt).getTime()) { - this.lastMessage = message - this.calDateDuration(message._updatedAt) - } - this.sortRoomList() - }) + this.calDateDuration(message._updatedAt) + this.sortRoomList() + + this.chatService.sendMessage({message:offlineChatMessage}).subscribe((res:any) => { + let ChatMessage = res.message + console.log('ChatMessage', ChatMessage) + this.redefinedMessage(message, ChatMessage) + }, (error) => { + }); + + } + + redefinedMessage (message: MessageService, ChatMessage) { + + ChatMessage = this.fix_updatedAt(ChatMessage) + + message.setData(ChatMessage) + + if( new Date(this.lastMessage._updatedAt).getTime() < new Date(message._updatedAt).getTime()) { + this.lastMessage = message + this.calDateDuration(message._updatedAt) + } + this.sortRoomList() } @@ -330,11 +362,15 @@ export class RoomService { let localMessages = [] - messages.forEach((message = []) => { - const wewMessage = this.prepareMessage(message) + messages.forEach((ChatMessage, index) => { + const wewMessage = this.prepareMessage(ChatMessage) - if(wewMessage.rid == 'offline') { - this.redefinedMessage(wewMessage) + if(wewMessage.offline == true) { + this.WsChatService.send(this.id, wewMessage.msg).then((data: any) => { + let _ChatMessage = data.result + this.redefinedMessage(wewMessage, _ChatMessage) + this.storage.set('chatmsg' + this.id, messages) + }) } localMessages.push(wewMessage) @@ -381,7 +417,7 @@ export class RoomService { - prepareMessage(message) { + prepareMessage(message): MessageService { message = this.fix_updatedAt(message) const wewMessage = new MessageService(this.storage) wewMessage.setData(message) @@ -412,9 +448,11 @@ export class RoomService { if (message.result) { //console.log('FIX UPDATE ', message.result) message.result._updatedAt = message.result._updatedAt['$date'] - } else if(message._updatedAt.hasOwnProperty('$date')) { - // console.log('FIX UPDATE 11', message) - message._updatedAt = message._updatedAt['$date'] + } else if(message._updatedAt) { + if(message._updatedAt.hasOwnProperty('$date')) { + // console.log('FIX UPDATE 11', message) + message._updatedAt = message._updatedAt['$date'] + } } return message } diff --git a/src/app/services/chat/ws-chat-methods.service.ts b/src/app/services/chat/ws-chat-methods.service.ts index 985041e86..1129e85a9 100644 --- a/src/app/services/chat/ws-chat-methods.service.ts +++ b/src/app/services/chat/ws-chat-methods.service.ts @@ -193,7 +193,7 @@ export class WsChatMethodsService { prepareRoom(roomData) { let room:RoomService; - room = new RoomService(this.WsChatService, new MessageService(this.storage), this.storage, this.platform, this.sqlservice, this.NativeNotificationService, this.sortService) + room = new RoomService(this.WsChatService, new MessageService(this.storage), this.storage, this.platform, this.sqlservice, this.NativeNotificationService, this.sortService, this.ChatService) room.setData({ customFields: roomData.customFields, diff --git a/src/app/services/file-system.service.spec.ts b/src/app/services/file-system.service.spec.ts new file mode 100644 index 000000000..f6d5285f4 --- /dev/null +++ b/src/app/services/file-system.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { FileSystemService } from './file-system.service'; + +describe('FileSystemService', () => { + let service: FileSystemService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(FileSystemService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/file-system.service.ts b/src/app/services/file-system.service.ts new file mode 100644 index 000000000..668cede8a --- /dev/null +++ b/src/app/services/file-system.service.ts @@ -0,0 +1,145 @@ +import { Injectable } from '@angular/core'; +import { ModalController, Platform,LoadingController } from '@ionic/angular'; +import { Filesystem, Directory } from '@capacitor/filesystem'; +import { Camera, CameraResultType, CameraSource, Photo} from '@capacitor/camera'; +import { ChatService } from './chat.service'; +import { AttachmentsService } from 'src/app/services/attachments.service'; +import { ToastService } from './toast.service'; +import { environment } from 'src/environments/environment'; +import { HttpClient, HttpEventType, HttpHeaders, HttpParams } from '@angular/common/http'; +import { Storage } from '@ionic/storage'; + + +const IMAGE_DIR = 'stored-images'; +interface LocalFile { + name: string; + path: string; + data: string; +} + +@Injectable({ + providedIn: 'root' +}) +export class FileSystemService { + + images: LocalFile[] = []; + + + constructor( + private platform: Platform, + private loadingCtrl: LoadingController, + private chatService: ChatService, + private AttachmentsService: AttachmentsService, + private toastService: ToastService, + private storage: Storage,) { } + + + async saveImage(photo: Photo, roomid: any) { + const base64Data = await this.readAsBase64(photo); + + const fileName = new Date().getTime() + '.jpeg'; + const savedFile = await Filesystem.writeFile({ + path: `${IMAGE_DIR}/${fileName}`, + data: base64Data, + directory: Directory.Data + }); + + } + + //new method 3 + async loadFiles(roomid) { + this.images = []; + + const loading = await this.loadingCtrl.create({ + message: 'Loading data...', + }); + await loading.present(); + + return new Promise((resolve, reject)=>{ + Filesystem.readdir({ + path: IMAGE_DIR, + directory: Directory.Data, + }).then(result => { + console.log('ALL RESULTS', result.files[0]) + let lastphoto = result.files[result.files.length - 1] + resolve(lastphoto) + }, + async (err) => { + console.log('ERROR FILE DOSENT EXIST', err) + reject('ERROR FILE DOSENT EXIST') + // Folder does not yet exists! + await Filesystem.mkdir({ + path: IMAGE_DIR, + directory: Directory.Data, + recursive: true + }); + } + ).then(_ => { + loading.dismiss(); + }); + }) + } + + + //new method 2 + private async readAsBase64(photo: Photo) { + if (this.platform.is('hybrid')) { + const file = await Filesystem.readFile({ + path: photo.path + }); + + return file.data; + } + else { + // Fetch the photo, read as a blob, then convert to base64 format + const response = await fetch(photo.webPath); + const blob = await response.blob(); + + return await this.convertBlobToBase64(blob) as string; + } + } + + + convertBlobToBase64 = (blob: Blob) => new Promise((resolve, reject) => { + const reader = new FileReader; + reader.onerror = reject; + reader.onload = () => { + resolve(reader.result); + }; + reader.readAsDataURL(blob); + }); + + async readFile(filePath) { + return await Filesystem.readFile({ + path: filePath, + directory: Directory.Data, + }); + } + + //new method 4 + async loadFileData(fileName: string, roomid: any) { + console.log('ALL PHOTOT FILE', fileName) + // for (let f of fileNames) { + const filePath = `${IMAGE_DIR}/${fileName}`; + + const readFile = await this.readFile(filePath) + + this.images.push({ + name: fileName, + path: filePath, + data: `data:image/jpeg;base64,${readFile.data}`, + }); + + console.log('ALL IMAGE', this.images) + + const capturedImage = this.images[0].data + + const capturedImageTitle = new Date().getTime() + '.jpeg'; + + + return { capturedImage, capturedImageTitle} + + } + + +} diff --git a/src/app/services/functions/file.service.ts b/src/app/services/functions/file.service.ts index f864b398f..b279a0f7d 100644 --- a/src/app/services/functions/file.service.ts +++ b/src/app/services/functions/file.service.ts @@ -2,18 +2,14 @@ import { Injectable } from '@angular/core'; import { FileLoaderService } from '../file/file-loader.service'; import { FileToBase64Service } from '../file/file-to-base64.service'; import { InAppBrowser } from '@ionic-native/in-app-browser/ngx'; - import { ChatService } from '../chat.service'; -import { ModalController, Platform,LoadingController } from '@ionic/angular'; +import { ModalController } from '@ionic/angular'; import { SearchPage } from 'src/app/pages/search/search.page'; -import { SearchList } from 'src/app/models/search-document'; import { ProcessesService } from '../processes.service'; import { ToastService } from '../toast.service'; -import { Camera, CameraResultType, CameraSource, Photo} from '@capacitor/camera'; -import { Filesystem, Directory } from '@capacitor/filesystem'; -import { environment } from 'src/environments/environment'; -import { HttpClient, HttpEventType, HttpHeaders, HttpParams } from '@angular/common/http'; -import { Storage } from '@ionic/storage'; +import { Camera, CameraResultType, CameraSource} from '@capacitor/camera'; +import { FileType } from 'src/app/models/fileType'; +import { FileSystemService } from 'src/app/services/file-system.service'; const IMAGE_DIR = 'stored-images'; @@ -31,14 +27,12 @@ export class FileService { images: LocalFile[] = []; capturedImage:any; capturedImageTitle:any; - documents:SearchList[] = []; showLoader: boolean; files: Set; photos: any[] = []; idroom: any; - headers: HttpHeaders; downloadProgess = 0; downloadFilename: any; @@ -50,54 +44,9 @@ export class FileService { private modalController: ModalController, private processesService: ProcessesService, private toastService: ToastService, - private platform: Platform, - private loadingCtrl: LoadingController, - private http: HttpClient, - private storage: Storage - ) { - this.headers = new HttpHeaders(); - } + private FileSystemService: FileSystemService + ) {} - uploadFile(formData:any){ - - console.log('UPLOAD file', formData) - - //const geturl = environment.apiURL + 'Tasks/DelegateTask'; - const geturl = environment.apiURL + 'ObjectServer/UploadFiles'; - - let options = { - headers: this.headers - }; - - return this.http.post(`${geturl}`, formData, options); - } - - getFile(guid:any){ - const geturl = environment.apiURL + 'lakefs/StreamFile'; - let params = new HttpParams(); - - params = params.set("path", guid); - - this.headers = this.headers.set('responseType', 'blob'); - this.headers = this.headers.set('Content-Type', 'application/octet-stream'); - - let options = { - headers: this.headers, - params: params - }; - return this.http.get(`${geturl}`, options); - } - - downloadFile(guid:any) { - - let downloadUrl = environment.apiURL +'objectserver/streamfiles?path='+guid; - var name = new Date().getTime(); - return this.http.get(downloadUrl, { - responseType: "arraybuffer", - reportProgress: true, observe: 'events' - }) - - } _arrayBufferToBase64( buffer ) { var binary = ''; @@ -110,32 +59,6 @@ export class FileService { } - async takePicture() { - const capturedImage = await Camera.getPhoto({ - quality: 90, - // allowEditing: true, - resultType: CameraResultType.Uri, - source: CameraSource.Camera - - }); - const response = await fetch(capturedImage.webPath!); - const blob = await response.blob(); - - this.photos.unshift({ - filepath: "soon...", - webviewPath: capturedImage.webPath - }); - - this.capturedImage = await this.convertBlobToBase64(blob); - this.capturedImageTitle = new Date().getTime() + '.jpeg'; - - let data = { - image:this.capturedImage, - name: this.capturedImageTitle - } - - return data; - } convertBlobToBase64 = (blob: Blob) => new Promise((resolve, reject) => { const reader = new FileReader; @@ -169,166 +92,6 @@ export class FileService { }; } - //new method1 - async saveImage(photo: Photo, roomid: any) { - const base64Data = await this.readAsBase64(photo); - - const fileName = new Date().getTime() + '.jpeg'; - const savedFile = await Filesystem.writeFile({ - path: `${IMAGE_DIR}/${fileName}`, - data: base64Data, - directory: Directory.Data - }); - - this.loadFiles(roomid); - } - - //new method 2 - private async readAsBase64(photo: Photo) { - if (this.platform.is('hybrid')) { - const file = await Filesystem.readFile({ - path: photo.path - }); - - return file.data; - } - else { - // Fetch the photo, read as a blob, then convert to base64 format - const response = await fetch(photo.webPath); - const blob = await response.blob(); - - return await this.convertBlobToBase64(blob) as string; - } - } - - //new method 3 - async loadFiles(roomid) { - this.images = []; - - const loading = await this.loadingCtrl.create({ - message: 'Loading data...', - }); - await loading.present(); - - Filesystem.readdir({ - path: IMAGE_DIR, - directory: Directory.Data, - }).then(result => { - console.log('ALL RESULTS', result.files[0]) - let lastphoto = result.files[result.files.length - 1] - this.loadFileData(lastphoto,roomid); - }, - async (err) => { - console.log('ERROR FILE DOSENT EXIST', err) - // Folder does not yet exists! - await Filesystem.mkdir({ - path: IMAGE_DIR, - directory: Directory.Data, - recursive: true - }); - } - ).then(_ => { - loading.dismiss(); - }); - } - - //new method 4 - async loadFileData(fileName: string, roomid: any) { - console.log('ALL PHOTOT FILE', fileName) - // for (let f of fileNames) { - const filePath = `${IMAGE_DIR}/${fileName}`; - - const readFile = await Filesystem.readFile({ - path: filePath, - directory: Directory.Data, - }); - - this.images.push({ - name: fileName, - path: filePath, - data: `data:image/jpeg;base64,${readFile.data}`, - }); - - console.log('ALL IMAGE', this.images) - - this.capturedImage = this.images[0].data - - this.capturedImageTitle = new Date().getTime() + '.jpeg'; - const base64 = await fetch(this.capturedImage); - const blob = await base64.blob(); - const formData = new FormData(); - formData.append("blobFile", blob); - console.log('ALL IMAGE', formData) - let guid: any = await this.uploadFile(formData).toPromise() - console.log(guid.path); - - - this.downloadFile(guid.path).subscribe(async (event) => { - - if (event.type === HttpEventType.DownloadProgress) { - //this.downloadProgess = Math.round((100 * event.loaded) / event.total); - //console.log('FILE TYPE 33', msg.file.type) - } else if (event.type === HttpEventType.Response) { - var fileImage = 'data:image/jpeg;base64,' + btoa(new Uint8Array(event.body).reduce((data, byte) => data + String.fromCharCode(byte), '')); - console.log('add picture to chat',fileImage); - await this.storage.set(guid.path, fileImage).then(() => { - console.log('add picture to chat IMAGE SAVED') - let body = { - "message": - { - "rid": roomid, - "msg": "", - "attachments": [{ - "title": this.capturedImageTitle, - "title_link_download": false, - "image_url": fileImage, - }], - "file":{ - "type": "application/img", - "guid": guid.path, - "image_url": fileImage - } - } - } - console.log('BODY TAKE PICTURE CHAT', body) - const loader = this.toastService.loading(); - this.chatService.sendMessage(body).subscribe(res=> { - console.log(res); - loader.remove(); - },(error) => { - loader.remove(); - this.toastService.badRequest("Não foi possível adicionar a fotografia!"); - }); - - }); - } - - }); - } - - - - async addCameraPictureToChat(roomId){ - - console.log('add camera to picture') - - const image = await Camera.getPhoto({ - quality: 50, - allowEditing: false, - resultType: CameraResultType.Uri, - source: CameraSource.Camera // Camera, Photos or Prompt! - }); - - if (image) { - await this.saveImage(image,roomId) - } else { - console.log('Error saving image') - } - - //this.capturedImage = this.capturedImage; - - - } async addPictureToChatMobile(roomId) { @@ -341,115 +104,11 @@ export class FileService { }); if (capturedImage) { - await this.saveImage(capturedImage,roomId) + await this.FileSystemService.saveImage(capturedImage,roomId) } + } - /* const response = await fetch(capturedImage.webPath!); - const blob = await response.blob(); - - this.photos.unshift({ - filepath: "soon...", - webviewPath: capturedImage.webPath - }); - - this.capturedImage = await this.convertBlobToBase64(blob); - this.capturedImageTitle = new Date().getTime() + '.jpeg'; - - //const loader = this.toastService.loading(); - - let body = { - "message": - { - "rid": roomId, - "msg": "", - "attachments": [{ - //"title": this.capturedImageTitle , - //"text": "description", - "title_link_download": false, - "image_url": this.capturedImage, - }] - } - } - this.chatService.sendMessage(body).subscribe(res=> { - //loader.remove(); - //console.log(res); - },(error) => { - //loader.remove(); - }); - */ } - addPictureToChat(roomId) { - - console.log('add picture to chat') - - const input = this.fileLoaderService.createInput({ - accept: ['image/apng', 'image/jpeg', 'image/png'] - }) - - input.onchange = async () => { - - //alert('Onchange AQUI') - - const file = this.fileLoaderService.getFirstFile(input) - - console.log('first file',file); - - - const formData = new FormData(); - formData.append("blobFile", file); - let guid: any = await this.uploadFile(formData).toPromise() - console.log('ADD IMAGE FORM DATA', formData) - console.log('add picture to chat', guid.path); - this.downloadFile(guid.path).subscribe(async (event) => { - - if (event.type === HttpEventType.DownloadProgress) { - //this.downloadProgess = Math.round((100 * event.loaded) / event.total); - //console.log('FILE TYPE 33', msg.file.type) - } else if (event.type === HttpEventType.Response) { - var fileImage = 'data:image/jpeg;base64,' + btoa(new Uint8Array(event.body).reduce((data, byte) => data + String.fromCharCode(byte), '')); - console.log('add picture to chat',fileImage); - await this.storage.set(guid.path, fileImage).then(() => { - console.log('add picture to chat IMAGE SAVED') - - let body = { - "message": - { - "rid": roomId, - "msg": "", - "attachments": [{ - //"title": this.capturedImageTitle , - //"text": "description", - "title_link_download": false, - //"image_url": this.capturedImage, - }], - "file": { - "type": "application/img", - "guid": guid.path, - "image_url": fileImage - } - } - } - - console.log('SELECT PICTURE GALLERY', body) - console.log(this.capturedImage) - - this.chatService.sendMessage(body).subscribe(res => { - - console.log('Msg after send image', res); - }, (error) => { - console.log('Msg after send image error', error); - }); - }); - } - - }); - - /* const imageData = await this.fileToBase64Service.convert(file) - this.capturedImage = imageData; */ - - //console.log(this.capturedImage) - }; -} addDocumentToChat(roomId:string) { const input = this.fileLoaderService.createInput({ @@ -475,67 +134,23 @@ export class FileService { }; } - async addDocGestaoDocumentalToChat(roomId:string){ - const modal = await this.modalController.create({ - component: SearchPage, - cssClass: 'group-messages modal-desktop search-modal search-modal-to-desktop', - componentProps: { - type: 'AccoesPresidenciais & ArquivoDespachoElect', - select: true, - showSearchInput: true, - } - }); - await modal.present(); - modal.onDidDismiss().then(async res=>{ - const data = res.data; - if(data.selected){ - const loader = this.toastService.loading(); - - this.documents.push(data.selected); - console.log(res.data.selected); - console.log(res.data.selected.Id); - console.log(res.data.selected.ApplicationType); - - let url = await this.processesService.GetDocumentUrl(res.data.selected.Id, res.data.selected.ApplicationType).toPromise(); - let url_no_options: string = url.replace("webTRIX.Viewer","webTRIX.Viewer.Branch1"); - console.log(url_no_options); - - let body = { - "message": - { - "rid": roomId, - "msg": "", - "alias": "documento", - "attachments": [{ - "title": res.data.selected.Assunto, - "description": res.data.selected.DocTypeDesc, - "title_link": url_no_options, - "title_link_download": true, - //"thumb_url": "assets/images/webtrix-logo.png", - "message_link": url_no_options, - "type": "webtrix" - }], - "file":{ - "name": res.data.selected.Assunto, - "type": "application/webtrix", - "ApplicationId": res.data.selected.ApplicationType, - "DocId": res.data.selected.Id, - "Assunto": res.data.selected.Assunto, - } - } - } - this.chatService.sendMessage(body).subscribe(res=> { - loader.remove(); - console.log(res); - },(error) => { - loader.remove(); - }); - loader.remove(); - } - }); + + getFileFromDevice(types: typeof FileType[]) { + return new Promise((resolve, reject)=>{ + const input = this.fileLoaderService.createInput({ + accept: types + }) + + input.onchange = async () => { + const file = this.fileLoaderService.getFirstFile(input) + + resolve(file) + }; + }) } + viewDocumentByUrl(url) { const browser = this.iab.create(url,"_parent"); browser.show(); 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 5747db9e1..f61c9c520 100644 --- a/src/app/shared/chat/group-messages/group-messages.page.ts +++ b/src/app/shared/chat/group-messages/group-messages.page.ts @@ -26,6 +26,12 @@ import { HttpEventType } from '@angular/common/http'; import { Storage } from '@ionic/storage'; import { WsChatMethodsService } from 'src/app/services/chat/ws-chat-methods.service'; import { MessageService } from 'src/app/services/chat/message.service'; +import { AttachmentsService } from 'src/app/services/attachments.service'; +import { FileSystemService } from 'src/app/services/file-system.service'; +import { CameraService } from 'src/app/services/camera.service'; +import { element } from 'protractor'; +import { FileType } from 'src/app/models/fileType'; +import { ToastService } from 'src/app/services/toast.service'; /* import * as pdfjsLib from 'pdfjs-dist'; @@ -96,6 +102,10 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe public ThemeService: ThemeService, private changeDetectorRef: ChangeDetectorRef, private storage: Storage, + private AttachmentsService: AttachmentsService, + private FileSystemService: FileSystemService, + private CameraService: CameraService, + private toastService: ToastService, ) { console.log('OnCONSTRUCTOR'); @@ -656,18 +666,145 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe await modal.present(); } - takePicture() { - this.fileService.addCameraPictureToChat(this.roomId); + async takePicture() { + const roomId = this.roomId + + const image = await this.CameraService.takePicture(); + await this.FileSystemService.saveImage(image, roomId) + const lastphoto: any = await this.FileSystemService.loadFiles(roomId); + const { capturedImage, capturedImageTitle} = await this.FileSystemService.loadFileData(lastphoto, roomId); + + const base64 = await fetch(capturedImage); + const blob = await base64.blob(); + const formData = new FormData(); + formData.append("blobFile", blob); + console.log('ALL IMAGE', formData) + let guid: any = await this.AttachmentsService.uploadFile(formData).toPromise() + console.log(guid.path); + + this.AttachmentsService.downloadFile(guid.path).subscribe(async (event) => { + + if (event.type === HttpEventType.DownloadProgress) { + //this.downloadProgess = Math.round((100 * event.loaded) / event.total); + //console.log('FILE TYPE 33', msg.file.type) + } else if (event.type === HttpEventType.Response) { + var fileImage = 'data:image/jpeg;base64,' + btoa(new Uint8Array(event.body).reduce((data, byte) => data + String.fromCharCode(byte), '')); + console.log('add picture to chat',fileImage); + await this.storage.set(guid.path, fileImage).then(() => { + console.log('add picture to chat IMAGE SAVED') + + this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ + file: { + "type": "application/img", + "guid": guid.path, + "image_url": fileImage + }, + attachments: [{ + //"title": this.capturedImageTitle , + //"text": "description", + "title_link_download": false, + //"image_url": this.capturedImage, + }] + }) + + }); + } + + }); + } - addImage() { - this.fileService.addPictureToChat(this.roomId); + + async addImage() { + this.addFileToChat(['image/apng', 'image/jpeg', 'image/png']) } - addFile() { - this.fileService.addDocumentToChat(this.roomId); + + async addFile() { + this.addFileToChat(['.doc', '.docx', '.pdf']) } - addFileWebtrix() { - this.fileService.addDocGestaoDocumentalToChat(this.roomId); + + async addFileWebtrix() { + const modal = await this.modalController.create({ + component: SearchPage, + cssClass: 'group-messages modal-desktop search-modal search-modal-to-desktop', + componentProps: { + type: 'AccoesPresidenciais & ArquivoDespachoElect', + select: true, + showSearchInput: true, + } + }); + await modal.present(); + modal.onDidDismiss().then(async res=>{ + const data = res.data; + + if(data.selected){ + const loader = this.toastService.loading(); + + let url = await this.processesService.GetDocumentUrl(res.data.selected.Id, res.data.selected.ApplicationType).toPromise(); + let url_no_options: string = url.replace("webTRIX.Viewer","webTRIX.Viewer.Branch1"); + console.log(url_no_options); + + this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ + alias: "documento", + file:{ + "name": res.data.selected.Assunto, + "type": "application/webtrix", + "ApplicationId": res.data.selected.ApplicationType, + "DocId": res.data.selected.Id, + "Assunto": res.data.selected.Assunto, + }, + attachments: [{ + "title": res.data.selected.Assunto, + "description": res.data.selected.DocTypeDesc, + "title_link": url_no_options, + "title_link_download": true, + //"thumb_url": "assets/images/webtrix-logo.png", + "message_link": url_no_options, + "type": "webtrix" + }] + }) + loader.remove(); + + } + }); } + + + async addFileToChat(types: typeof FileType[] ) { + const file = await this.fileService.getFileFromDevice(types); + const formData = new FormData(); + formData.append("blobFile", file); + let guid: any = await this.AttachmentsService.uploadFile(formData).toPromise() + this.AttachmentsService.downloadFile(guid.path).subscribe(async (event) => { + + if (event.type === HttpEventType.DownloadProgress) { + //this.downloadProgess = Math.round((100 * event.loaded) / event.total); + //console.log('FILE TYPE 33', msg.file.type) + } else if (event.type === HttpEventType.Response) { + var fileImage = 'data:image/jpeg;base64,' + btoa(new Uint8Array(event.body).reduce((data, byte) => data + String.fromCharCode(byte), '')); + console.log('add picture to chat',fileImage); + await this.storage.set(guid.path, fileImage).then(() => { + console.log('add picture to chat IMAGE SAVED') + + this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ + file: { + "type": "application/img", + "guid": guid.path, + "image_url": fileImage + }, + attachments: [{ + //"title": this.capturedImageTitle , + //"text": "description", + "title_link_download": false, + //"image_url": this.capturedImage, + }] + }) + + }); + } + + }); + } + bookMeeting() { let data = { roomId: this.roomId, @@ -693,7 +830,7 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe return this.animationController.create() .addElement(baseEl) .easing('ease-out') - .duration(5000) + .duration(500) .addAnimation([backdropAnimation, wrapperAnimation]); } @@ -711,10 +848,11 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe members: this.members, } }); - await modal.present(); - modal.onDidDismiss().then((res) => { - //console.log(res['data']); + modal.onDidDismiss().then( async (res) => { + console.log(res['data']); + const roomId = this.roomId; + if (res['data'] == 'meeting') { //this.closeAllDesktopComponents.emit(); let data = { @@ -724,21 +862,67 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe this.openNewEventPage.emit(data); } else if (res['data'] == 'take-picture') { - this.fileService.addCameraPictureToChat(this.roomId); - //this.loadPicture(); + + const image = await this.CameraService.takePicture(); + await this.FileSystemService.saveImage(image, roomId) + const lastphoto: any = await this.FileSystemService.loadFiles(roomId); + const { capturedImage, capturedImageTitle} = await this.FileSystemService.loadFileData(lastphoto, roomId); + + const base64 = await fetch(capturedImage); + const blob = await base64.blob(); + const formData = new FormData(); + formData.append("blobFile", blob); + console.log('ALL IMAGE', formData) + let guid: any = await this.AttachmentsService.uploadFile(formData).toPromise() + console.log(guid.path); + + this.AttachmentsService.downloadFile(guid.path).subscribe(async (event) => { + + if (event.type === HttpEventType.DownloadProgress) { + //this.downloadProgess = Math.round((100 * event.loaded) / event.total); + //console.log('FILE TYPE 33', msg.file.type) + } else if (event.type === HttpEventType.Response) { + var fileImage = 'data:image/jpeg;base64,' + btoa(new Uint8Array(event.body).reduce((data, byte) => data + String.fromCharCode(byte), '')); + console.log('add picture to chat',fileImage); + await this.storage.set(guid.path, fileImage).then(() => { + console.log('add picture to chat IMAGE SAVED') + + this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ + file: { + "type": "application/img", + "guid": guid.path, + "image_url": fileImage + }, + attachments: [{ + //"title": this.capturedImageTitle , + //"text": "description", + "title_link_download": false, + //"image_url": this.capturedImage, + }] + }) + + }); + } + + }); + } else if (res['data'] == 'add-picture') { - this.fileService.addPictureToChat(this.roomId); - //this.loadPicture(); + + this.addImage() + } else if (res['data'] == 'add-document') { - this.loadDocument(); + + this.addFile() + } else if (res['data'] == 'documentoGestaoDocumental') { - this.fileService.addDocGestaoDocumentalToChat(this.roomId); - //this.addDocGestaoDocumental(); + + this.addFileWebtrix() + + this.showLoader = false; } - this.loadGroupMessages(this.roomId); }); } @@ -790,7 +974,7 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe console.log('FILE TYPE', msg.file.type) this.downloadFile = ""; if (msg.file.type == "application/img") { - this.fileService.downloadFile(msg.file.guid).subscribe(async (event) => { + this.AttachmentsService.downloadFile(msg.file.guid).subscribe(async (event) => { console.log('FILE TYPE 22', msg.file.guid) var name = msg.file.guid; diff --git a/src/app/shared/chat/messages/messages.page.ts b/src/app/shared/chat/messages/messages.page.ts index f799c6d64..5f093b9e0 100644 --- a/src/app/shared/chat/messages/messages.page.ts +++ b/src/app/shared/chat/messages/messages.page.ts @@ -26,7 +26,12 @@ import { Storage } from '@ionic/storage'; import { WsChatMethodsService } from 'src/app/services/chat/ws-chat-methods.service' import { WsChatService } from 'src/app/services/chat/ws-chat.service' import { MessageService } from 'src/app/services/chat/message.service'; -import { element } from 'protractor'; +import { AttachmentsService } from 'src/app/services/attachments.service'; +import { FileSystemService } from 'src/app/services/file-system.service'; +import { CameraService } from 'src/app/services/camera.service'; +import { FileType } from 'src/app/models/fileType'; +import { SearchPage } from 'src/app/pages/search/search.page'; +import { ProcessesService } from 'src/app/services/processes.service'; const IMAGE_DIR = 'stored-images'; @Component({ @@ -92,7 +97,11 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy private router: Router, private storage: Storage, public wsChatMethodsService: WsChatMethodsService, - public WsChatService: WsChatService + public WsChatService: WsChatService, + private AttachmentsService: AttachmentsService, + private FileSystemService: FileSystemService, + private CameraService: CameraService, + private processesService: ProcessesService, ) { this.loggedUser = authService.ValidatedUserChat['data']; @@ -113,7 +122,6 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy ngOnInit() { this.scrollToBottom(); - this.setStatus('online'); } @@ -133,15 +141,6 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy }); } - setStatus(status: string) { - let body = { - message: '', - status: status, - } - this.chatService.setUserStatus(body).subscribe(res => { - console.log(res); - }) - } notImplemented() { this.alertService.presentAlert('Funcionalidade em desenvolvimento'); @@ -149,7 +148,6 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy load = () => { this.checktimeOut = true; - this.serverLongPull(); this.getChatMembers(); } @@ -204,7 +202,6 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy ngOnDestroy() { this.checktimeOut = false; - this.setStatus('away'); window.removeEventListener('scroll', this.scrollChangeCallback, true); } @@ -250,7 +247,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy async viewDocument(msg: any, url?: string) { if (msg.file.type == "application/img") { - let response: any = await this.fileService.getFile(msg.file.guid).toPromise(); + let response: any = await this.AttachmentsService.getFile(msg.file.guid).toPromise(); console.log(response); alert(response); @@ -266,7 +263,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy //fullUrl = "http://www.africau.edu/images/default/sample.pdf"; this.frameUrl = fullUrl; - //this.fileService.viewDocumentByUrl(fullUrl); + this.chatService.getDocumentDetails(fullUrl); } } @@ -438,22 +435,147 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy - takePicture() { + async takePicture() { const roomId = this.roomId - this.fileService.addCameraPictureToChat(roomId); + + const image = await this.CameraService.takePicture(); + await this.FileSystemService.saveImage(image, roomId) + const lastphoto: any = await this.FileSystemService.loadFiles(roomId); + const { capturedImage, capturedImageTitle} = await this.FileSystemService.loadFileData(lastphoto, roomId); + + const base64 = await fetch(capturedImage); + const blob = await base64.blob(); + const formData = new FormData(); + formData.append("blobFile", blob); + console.log('ALL IMAGE', formData) + let guid: any = await this.AttachmentsService.uploadFile(formData).toPromise() + console.log(guid.path); + + this.AttachmentsService.downloadFile(guid.path).subscribe(async (event) => { + + if (event.type === HttpEventType.DownloadProgress) { + //this.downloadProgess = Math.round((100 * event.loaded) / event.total); + //console.log('FILE TYPE 33', msg.file.type) + } else if (event.type === HttpEventType.Response) { + var fileImage = 'data:image/jpeg;base64,' + btoa(new Uint8Array(event.body).reduce((data, byte) => data + String.fromCharCode(byte), '')); + console.log('add picture to chat',fileImage); + await this.storage.set(guid.path, fileImage).then(() => { + console.log('add picture to chat IMAGE SAVED') + }); + + this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ + alias: 'document', + file: { + "type": "application/img", + "guid": guid.path, + "image_url": fileImage + }, + attachments: [{ + //"title": this.capturedImageTitle , + //"text": "description", + "title_link_download": false, + //"image_url": this.capturedImage, + }] + }) + + } + + }); + } - addImage() { - const roomId = this.roomId; - this.fileService.addPictureToChat(roomId); - //this.fileService.loadPicture(); - //this.fileService.addPictureToChat(roomId); + + async addImage() { + this.addFileToChat(['image/apng', 'image/jpeg', 'image/png']) } - addFile() { - this.fileService.addDocumentToChat(this.roomId); + + async addFile() { + this.addFileToChat(['.doc', '.docx', '.pdf']) } - addFileWebtrix() { - this.fileService.addDocGestaoDocumentalToChat(this.roomId); + + async addFileWebtrix() { + const modal = await this.modalController.create({ + component: SearchPage, + cssClass: 'group-messages modal-desktop search-modal search-modal-to-desktop', + componentProps: { + type: 'AccoesPresidenciais & ArquivoDespachoElect', + select: true, + showSearchInput: true, + } + }); + await modal.present(); + modal.onDidDismiss().then(async res=>{ + const data = res.data; + + if(data.selected){ + const loader = this.toastService.loading(); + + let url = await this.processesService.GetDocumentUrl(res.data.selected.Id, res.data.selected.ApplicationType).toPromise(); + let url_no_options: string = url.replace("webTRIX.Viewer","webTRIX.Viewer.Branch1"); + console.log(url_no_options); + + this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ + alias: "documento", + file:{ + "name": res.data.selected.Assunto, + "type": "application/webtrix", + "ApplicationId": res.data.selected.ApplicationType, + "DocId": res.data.selected.Id, + "Assunto": res.data.selected.Assunto, + }, + attachments: [{ + "title": res.data.selected.Assunto, + "description": res.data.selected.DocTypeDesc, + "title_link": url_no_options, + "title_link_download": true, + //"thumb_url": "assets/images/webtrix-logo.png", + "message_link": url_no_options, + "type": "webtrix" + }] + }) + loader.remove(); + + } + }); } + + + async addFileToChat(types: typeof FileType[] ) { + const file = await this.fileService.getFileFromDevice(types); + const formData = new FormData(); + formData.append("blobFile", file); + let guid: any = await this.AttachmentsService.uploadFile(formData).toPromise() + this.AttachmentsService.downloadFile(guid.path).subscribe(async (event) => { + + if (event.type === HttpEventType.DownloadProgress) { + //this.downloadProgess = Math.round((100 * event.loaded) / event.total); + //console.log('FILE TYPE 33', msg.file.type) + } else if (event.type === HttpEventType.Response) { + var fileImage = 'data:image/jpeg;base64,' + btoa(new Uint8Array(event.body).reduce((data, byte) => data + String.fromCharCode(byte), '')); + console.log('add picture to chat',fileImage); + await this.storage.set(guid.path, fileImage).then(() => { + console.log('add picture to chat IMAGE SAVED') + + this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ + file: { + "type": "application/img", + "guid": guid.path, + "image_url": fileImage + }, + attachments: [{ + //"title": this.capturedImageTitle , + //"text": "description", + "title_link_download": false, + //"image_url": this.capturedImage, + }] + }) + + }); + } + + }); + } + + bookMeeting() { let data = { roomId: this.roomId, @@ -463,7 +585,13 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy } + chatSendFile() { + + } + async _openChatOptions() { + const roomId = this.roomId; + const enterAnimation = (baseEl: any) => { const backdropAnimation = this.animationController.create() @@ -499,8 +627,10 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy } }); await modal.present(); - modal.onDidDismiss().then((res) => { + modal.onDidDismiss().then( async (res) => { console.log(res['data']); + + if (res['data'] == 'meeting') { //this.closeAllDesktopComponents.emit(); let data = { @@ -510,63 +640,30 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy this.openNewEventPage.emit(data); } else if (res['data'] == 'take-picture') { - this.fileService.addCameraPictureToChat(this.roomId); - //this.loadPicture(); + + this.takePicture() + } else if (res['data'] == 'add-picture') { - this.fileService.addPictureToChat(this.roomId); - //this.loadPicture(); + + this.addImage() + } else if (res['data'] == 'add-document') { - this.fileService.addDocumentToChat(this.roomId); - //this.loadDocument(); + + this.addFile() + } else if (res['data'] == 'documentoGestaoDocumental') { - this.fileService.addDocGestaoDocumentalToChat(this.roomId); + this.addFileWebtrix() + this.showLoader = false; - //this.addDocGestaoDocumental(); } }); } - async serverLongPull() { - - const roomId = this.roomId; - - this.chatService.getRoomMessages(this.roomId).subscribe(async res => { - console.log('serverLongPull', res['success']); - - - if (res['success'] == true) { - // Show Error - //showMessage(response.statusText); - /* this.messages = res['messages'].reverse(); - this.chatMessageStore.add(roomId, this.messages) */ - console.log('MSG FROM ROCKET ', res['messages'].reverse()) - /* this.transformData(res['messages'].reverse()); - this.getMessageDB(); */ - - //console.log(this.messages); - // Reconnect in one second - if (this.route.url != "/home/chat") { - console.log("Timer message stop") - } - else { - if (document.querySelector('app-messages')) { - await new Promise(resolve => setTimeout(resolve, 5000)); - // await this.serverLongPull(); - this.getDirectMessages.emit(); - console.log('Timer message running') - } - } - } - }, (error) => { - console.log(error); - }); - - } sliderOpts = { zoom: false, @@ -614,7 +711,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy console.log('FILE TYPE', msg.file.type) this.downloadFile = ""; if (msg.file.type == "application/img") { - this.fileService.downloadFile(msg.file.guid).subscribe(async (event) => { + this.AttachmentsService.downloadFile(msg.file.guid).subscribe(async (event) => { console.log('FILE TYPE 22', msg.file.guid) var name = msg.file.guid; From 0f04ad98c235b3f83a07adbde7fa15f3638c4ca8 Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Fri, 4 Feb 2022 00:22:35 +0100 Subject: [PATCH 2/9] offline messagens --- .../group-messages/group-messages.page.html | 2 +- .../group-messages/group-messages.page.ts | 118 +++++++++++------ .../pages/chat/messages/messages.page.html | 2 +- src/app/pages/chat/messages/messages.page.ts | 120 +++++++++++------ src/app/services/chat/message.service.ts | 27 +++- src/app/services/chat/room.service.ts | 82 +++++------- src/app/services/chat/ws-chat.service.ts | 23 ++-- .../group-messages/group-messages.page.html | 2 +- .../group-messages/group-messages.page.ts | 118 +++++++++++------ .../shared/chat/messages/messages.page.html | 2 +- src/app/shared/chat/messages/messages.page.ts | 124 ++++++++++++------ 11 files changed, 381 insertions(+), 239 deletions(-) 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 cdfd4646b..c4a428958 100644 --- a/src/app/pages/chat/group-messages/group-messages.page.html +++ b/src/app/pages/chat/group-messages/group-messages.page.html @@ -121,7 +121,7 @@ {{file.description}} - {{msg.file.type.replace('application/','').toUpperCase()}} + {{msg.displayType}} 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 d0de49f31..b6c4dca14 100644 --- a/src/app/pages/chat/group-messages/group-messages.page.ts +++ b/src/app/pages/chat/group-messages/group-messages.page.ts @@ -339,7 +339,7 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { sendMessage() { - this.wsChatMethodsService.getGroupRoom(this.roomId).send() + this.wsChatMethodsService.getGroupRoom(this.roomId).send({}) } async openOptions() { @@ -503,13 +503,36 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { const lastphoto: any = await this.FileSystemService.loadFiles(roomId); const { capturedImage, capturedImageTitle} = await this.FileSystemService.loadFileData(lastphoto, roomId); + const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({ + file: { + "type": "application/img", + "guid": '', + "image_url": capturedImage + }, + attachments: [{ + //"title": this.capturedImageTitle , + //"text": "description", + "title_link_download": false, + //"image_url": this.capturedImage, + }] + }) + + const base64 = await fetch(capturedImage); const blob = await base64.blob(); const formData = new FormData(); formData.append("blobFile", blob); console.log('ALL IMAGE', formData) + + let guid: any = await this.AttachmentsService.uploadFile(formData).toPromise() - console.log(guid.path); + updateMessage({ + file: { + type: "application/img", + guid: guid.path, + image_url: capturedImage + } + }) this.AttachmentsService.downloadFile(guid.path).subscribe(async (event) => { @@ -518,25 +541,12 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { //console.log('FILE TYPE 33', msg.file.type) } else if (event.type === HttpEventType.Response) { var fileImage = 'data:image/jpeg;base64,' + btoa(new Uint8Array(event.body).reduce((data, byte) => data + String.fromCharCode(byte), '')); - console.log('add picture to chat',fileImage); - await this.storage.set(guid.path, fileImage).then(() => { + console.log('add picture to chat', capturedImage); + await this.storage.set(guid.path, capturedImage).then(() => { console.log('add picture to chat IMAGE SAVED') - - this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ - file: { - "type": "application/img", - "guid": guid.path, - "image_url": fileImage - }, - attachments: [{ - //"title": this.capturedImageTitle , - //"text": "description", - "title_link_download": false, - //"image_url": this.capturedImage, - }] - }) - + message.getFileFromDb() }); + } }); @@ -565,15 +575,10 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { modal.onDidDismiss().then(async res=>{ const data = res.data; - if(data.selected){ + if(data.selected) { const loader = this.toastService.loading(); - let url = await this.processesService.GetDocumentUrl(res.data.selected.Id, res.data.selected.ApplicationType).toPromise(); - let url_no_options: string = url.replace("webTRIX.Viewer","webTRIX.Viewer.Branch1"); - console.log(url_no_options); - - this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ - alias: "documento", + const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({ file:{ "name": res.data.selected.Assunto, "type": "application/webtrix", @@ -581,6 +586,22 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { "DocId": res.data.selected.Id, "Assunto": res.data.selected.Assunto, }, + attachments: [{ + "title": res.data.selected.Assunto, + "description": res.data.selected.DocTypeDesc, + // "title_link": url_no_options, + "title_link_download": true, + //"thumb_url": "assets/images/webtrix-logo.png", + // "message_link": url_no_options, + "type": "webtrix" + }] + }) + + let url = await this.processesService.GetDocumentUrl(res.data.selected.Id, res.data.selected.ApplicationType).toPromise(); + let url_no_options: string = url.replace("webTRIX.Viewer","webTRIX.Viewer.Branch1"); + console.log(url_no_options); + + updateMessage({ attachments: [{ "title": res.data.selected.Assunto, "description": res.data.selected.DocTypeDesc, @@ -591,6 +612,7 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { "type": "webtrix" }] }) + loader.remove(); } @@ -598,11 +620,37 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { } + async addFileToChat(types: typeof FileType[] ) { const file = await this.fileService.getFileFromDevice(types); + const imageData = await this.fileToBase64Service.convert(file) + const formData = new FormData(); formData.append("blobFile", file); + + const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({ + file: { + "type": "application/img", + "guid": '', + "image_url": imageData + }, + attachments: [{ + //"title": this.capturedImageTitle , + //"text": "description", + "title_link_download": false, + //"image_url": this.capturedImage, + }] + }) + let guid: any = await this.AttachmentsService.uploadFile(formData).toPromise() + updateMessage({ + file: { + type: "application/img", + guid: guid.path, + image_url: imageData + } + }) + this.AttachmentsService.downloadFile(guid.path).subscribe(async (event) => { if (event.type === HttpEventType.DownloadProgress) { @@ -613,21 +661,7 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { console.log('add picture to chat',fileImage); await this.storage.set(guid.path, fileImage).then(() => { console.log('add picture to chat IMAGE SAVED') - - this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ - file: { - "type": "application/img", - "guid": guid.path, - "image_url": fileImage - }, - attachments: [{ - //"title": this.capturedImageTitle , - //"text": "description", - "title_link_download": false, - //"image_url": this.capturedImage, - }] - }) - + message.getFileFromDb() }); } @@ -680,7 +714,7 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { await this.storage.set(guid.path, fileImage).then(() => { console.log('add picture to chat IMAGE SAVED') - this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ + this.wsChatMethodsService.getDmRoom(this.roomId).send({ file: { "type": "application/img", "guid": guid.path, diff --git a/src/app/pages/chat/messages/messages.page.html b/src/app/pages/chat/messages/messages.page.html index ee9612b12..dabd8c88d 100644 --- a/src/app/pages/chat/messages/messages.page.html +++ b/src/app/pages/chat/messages/messages.page.html @@ -102,7 +102,7 @@ {{file.description}} - {{msg.file.type.replace('application/','').toUpperCase()}} + {{msg.displayType}} diff --git a/src/app/pages/chat/messages/messages.page.ts b/src/app/pages/chat/messages/messages.page.ts index 9568bcd3b..3c4da98de 100644 --- a/src/app/pages/chat/messages/messages.page.ts +++ b/src/app/pages/chat/messages/messages.page.ts @@ -38,6 +38,7 @@ import { element } from 'protractor'; import { FileType } from 'src/app/models/fileType'; import { SearchPage } from 'src/app/pages/search/search.page'; import { Storage } from '@ionic/storage'; +import { FileToBase64Service } from 'src/app/services/file/file-to-base64.service'; const IMAGE_DIR = 'stored-images'; @@ -111,6 +112,7 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { private CameraService: CameraService, private processesService: ProcessesService, private storage: Storage, + private fileToBase64Service: FileToBase64Service, ) { this.loggedUser = authService.ValidatedUserChat['data']; this.roomId = this.navParams.get('roomId'); @@ -347,7 +349,7 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { } sendMessage() { - this.wsChatMethodsService.getDmRoom(this.roomId).send() + this.wsChatMethodsService.getDmRoom(this.roomId).send({}) } @@ -524,13 +526,36 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { const lastphoto: any = await this.FileSystemService.loadFiles(roomId); const { capturedImage, capturedImageTitle} = await this.FileSystemService.loadFileData(lastphoto, roomId); + const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({ + file: { + "type": "application/img", + "guid": '', + "image_url": capturedImage + }, + attachments: [{ + //"title": this.capturedImageTitle , + //"text": "description", + "title_link_download": false, + //"image_url": this.capturedImage, + }] + }) + + const base64 = await fetch(capturedImage); const blob = await base64.blob(); const formData = new FormData(); formData.append("blobFile", blob); console.log('ALL IMAGE', formData) + + let guid: any = await this.AttachmentsService.uploadFile(formData).toPromise() - console.log(guid.path); + updateMessage({ + file: { + type: "application/img", + guid: guid.path, + image_url: capturedImage + } + }) this.AttachmentsService.downloadFile(guid.path).subscribe(async (event) => { @@ -539,25 +564,12 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { //console.log('FILE TYPE 33', msg.file.type) } else if (event.type === HttpEventType.Response) { var fileImage = 'data:image/jpeg;base64,' + btoa(new Uint8Array(event.body).reduce((data, byte) => data + String.fromCharCode(byte), '')); - console.log('add picture to chat',fileImage); - await this.storage.set(guid.path, fileImage).then(() => { + console.log('add picture to chat', capturedImage); + await this.storage.set(guid.path, capturedImage).then(() => { console.log('add picture to chat IMAGE SAVED') - - this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ - file: { - "type": "application/img", - "guid": guid.path, - "image_url": fileImage - }, - attachments: [{ - //"title": this.capturedImageTitle , - //"text": "description", - "title_link_download": false, - //"image_url": this.capturedImage, - }] - }) - + message.getFileFromDb() }); + } }); @@ -586,15 +598,10 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { modal.onDidDismiss().then(async res=>{ const data = res.data; - if(data.selected){ + if(data.selected) { const loader = this.toastService.loading(); - let url = await this.processesService.GetDocumentUrl(res.data.selected.Id, res.data.selected.ApplicationType).toPromise(); - let url_no_options: string = url.replace("webTRIX.Viewer","webTRIX.Viewer.Branch1"); - console.log(url_no_options); - - this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ - alias: "documento", + const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({ file:{ "name": res.data.selected.Assunto, "type": "application/webtrix", @@ -602,6 +609,22 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { "DocId": res.data.selected.Id, "Assunto": res.data.selected.Assunto, }, + attachments: [{ + "title": res.data.selected.Assunto, + "description": res.data.selected.DocTypeDesc, + // "title_link": url_no_options, + "title_link_download": true, + //"thumb_url": "assets/images/webtrix-logo.png", + // "message_link": url_no_options, + "type": "webtrix" + }] + }) + + let url = await this.processesService.GetDocumentUrl(res.data.selected.Id, res.data.selected.ApplicationType).toPromise(); + let url_no_options: string = url.replace("webTRIX.Viewer","webTRIX.Viewer.Branch1"); + console.log(url_no_options); + + updateMessage({ attachments: [{ "title": res.data.selected.Assunto, "description": res.data.selected.DocTypeDesc, @@ -612,6 +635,7 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { "type": "webtrix" }] }) + loader.remove(); } @@ -619,11 +643,37 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { } + async addFileToChat(types: typeof FileType[] ) { const file = await this.fileService.getFileFromDevice(types); + const imageData = await this.fileToBase64Service.convert(file) + const formData = new FormData(); formData.append("blobFile", file); + + const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({ + file: { + "type": "application/img", + "guid": '', + "image_url": imageData + }, + attachments: [{ + //"title": this.capturedImageTitle , + //"text": "description", + "title_link_download": false, + //"image_url": this.capturedImage, + }] + }) + let guid: any = await this.AttachmentsService.uploadFile(formData).toPromise() + updateMessage({ + file: { + type: "application/img", + guid: guid.path, + image_url: imageData + } + }) + this.AttachmentsService.downloadFile(guid.path).subscribe(async (event) => { if (event.type === HttpEventType.DownloadProgress) { @@ -634,21 +684,7 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { console.log('add picture to chat',fileImage); await this.storage.set(guid.path, fileImage).then(() => { console.log('add picture to chat IMAGE SAVED') - - this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ - file: { - "type": "application/img", - "guid": guid.path, - "image_url": fileImage - }, - attachments: [{ - //"title": this.capturedImageTitle , - //"text": "description", - "title_link_download": false, - //"image_url": this.capturedImage, - }] - }) - + message.getFileFromDb() }); } @@ -701,7 +737,7 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { await this.storage.set(guid.path, fileImage).then(() => { console.log('add picture to chat IMAGE SAVED') - this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ + this.wsChatMethodsService.getDmRoom(this.roomId).send({ file: { "type": "application/img", "guid": guid.path, diff --git a/src/app/services/chat/message.service.ts b/src/app/services/chat/message.service.ts index 758e296a5..6eb0f3015 100644 --- a/src/app/services/chat/message.service.ts +++ b/src/app/services/chat/message.service.ts @@ -23,6 +23,7 @@ export class MessageService { file attachments offline = true + displayType = '' constructor(private storage: Storage) { } @@ -49,14 +50,16 @@ export class MessageService { if (this.file) { - if (this.file.guid) { - this.storage.get(this.file.guid).then((image) => { - // console.log('IMAGE FROM STORAGE', image) - this.file.image_url = image - }); + this.getFileFromDb() + if(this.file.type) { + if(this.file.type != 'application/webtrix' && typeof(this.file.type) == 'string') { + this.displayType = this.file.type.replace('application/','').toUpperCase() + } } + } + } delete() {} @@ -75,4 +78,18 @@ export class MessageService { return firstName + ' ' + lastName } + + getFileFromDb() { + if (this.file) { + if (this.file.guid) { + this.storage.get(this.file.guid).then((image) => { + if(image != null) { + this.file.image_url = image + } + + }); + } + } + } + } diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index 1112c4a23..7a32579ed 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -54,6 +54,7 @@ export class RoomService { } sortRoomList = () => {} + uploadAttachment = (formData) => {} constructor( public WsChatService: WsChatService, @@ -204,51 +205,9 @@ export class RoomService { /** * @description sen text message */ - send() { - - if(environment.chatOffline == false) { - this.WsChatService.send(this.id, this.message) - this.message= '' - } else { - - const offlineChatMessage = { - channels: [], - mentions: [], - rid: this.id, - msg: this.message, - u: { - name: this.usernameToDisplayName(SessionStore.user.RochetChatUser), - username: SessionStore.user.RochetChatUser, - _id: "" - }, - _updatedAt: new Date().getTime(), - offline: true - } - - this.addMessageDB(offlineChatMessage) - const message: MessageService = this.prepareMessage(offlineChatMessage) - - setTimeout(() => { - this.scrollDown() - }, 150) - - this.lastMessage = message + send({file = null, attachments = null}) { - this.WsChatService.send(this.id, message.msg).then((data: any) => { - let ChatMessage = data.result - this.redefinedMessage(message, ChatMessage) - }) - - this.calDateDuration(message._updatedAt) - this.sortRoomList() - - this.message= '' - } - - } - - sendFile({file, attachments, alias = 'document'}) { let offlineChatMessage = { rid: this.id, msg: this.message, @@ -265,16 +224,35 @@ 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') + return { + message: message, + updateMessage: (update)=> { + offlineChatMessage = Object.assign(offlineChatMessage, update) + + 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) + }) + + } + } + } + + this.calDateDuration(message._updatedAt) this.sortRoomList() - - this.chatService.sendMessage({message:offlineChatMessage}).subscribe((res:any) => { - let ChatMessage = res.message - console.log('ChatMessage', ChatMessage) - this.redefinedMessage(message, ChatMessage) - }, (error) => { - }); + this.message= '' } redefinedMessage (message: MessageService, ChatMessage) { @@ -362,11 +340,13 @@ export class RoomService { let localMessages = [] + if(messages==null) messages = [] + messages.forEach((ChatMessage, index) => { const wewMessage = this.prepareMessage(ChatMessage) if(wewMessage.offline == true) { - this.WsChatService.send(this.id, wewMessage.msg).then((data: any) => { + 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) this.storage.set('chatmsg' + this.id, messages) diff --git a/src/app/services/chat/ws-chat.service.ts b/src/app/services/chat/ws-chat.service.ts index 4b94ad019..593b79276 100644 --- a/src/app/services/chat/ws-chat.service.ts +++ b/src/app/services/chat/ws-chat.service.ts @@ -162,7 +162,7 @@ export class WsChatService { } // send message to room - send(roomId, msg) { + send({roomId, msg, attachments = null, file = null}) { const requestId = uuidv4() @@ -171,9 +171,10 @@ export class WsChatService { method: "sendMessage", id: requestId, params: [{ - _id: uuidv4(), rid: roomId, - msg: msg + msg: msg, + attachments, + file }] } @@ -181,7 +182,7 @@ export class WsChatService { return new Promise((resolve, reject) => { this.ws.registerCallback({type:'Onmessage', funx:(message)=>{ - if(message.id == requestId || deepFind(message,'result.id') == requestId) { // same request send + if(message.id == requestId ) { // same request send resolve(message) return true } @@ -206,7 +207,7 @@ export class WsChatService { return new Promise((resolve, reject) => { this.ws.registerCallback({type:'Onmessage', funx:(message)=>{ - if(message.id == requestId || deepFind(message,'result.id') == requestId) { // same request send + if(message.id == requestId ) { // same request send resolve(message) return true } @@ -232,7 +233,7 @@ export class WsChatService { return new Promise((resolve, reject) => { this.ws.registerCallback({type:'Onmessage', funx:(message)=>{ - if(message.id == requestId || deepFind(message,'result.id') == requestId) { // same request send + if(message.id == requestId ) { // same request send resolve(message) return true } @@ -255,7 +256,7 @@ export class WsChatService { return new Promise((resolve, reject) => { this.ws.registerCallback({type:'Onmessage', funx:(message)=>{ - if(message.id == requestId || deepFind(message,'result.id') == requestId) { // same request send + if(message.id == requestId ) { // same request send resolve(message) return true } @@ -310,7 +311,7 @@ export class WsChatService { return new Promise((resolve, reject) => { this.ws.registerCallback({type:'Onmessage', funx:(message)=>{ - if(message.id == requestId || deepFind(message,'result.id') == requestId) { // same request send + if(message.id == requestId ) { // same request send resolve(message) return true } @@ -337,7 +338,7 @@ export class WsChatService { return new Promise((resolve, reject) => { this.ws.registerCallback({type:'Onmessage', funx:(message)=>{ - if(message.id == requestId || deepFind(message,'result.id') == requestId) { // same request send + if(message.id == requestId ) { // same request send resolve(message) return true } @@ -565,7 +566,7 @@ updateRoomEventss(roomId, collection:string, funx: Function, ) { return new Promise((resolve, reject) => { this.ws.registerCallback({type:'Onmessage', funx:(message)=>{ - if(message.id == requestId || deepFind(message,'result.id') == requestId) { // same request send + if(message.id == requestId ) { // same request send resolve(message) return true } @@ -592,7 +593,7 @@ updateRoomEventss(roomId, collection:string, funx: Function, ) { return new Promise((resolve, reject) => { this.ws.registerCallback({type:'Onmessage', funx:(message)=>{ - if(message.id == requestId || deepFind(message,'result.id') == requestId) { // same request send + if(message.id == requestId ) { // same request send resolve(message) return true } 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 2bbb6b651..7538b7768 100644 --- a/src/app/shared/chat/group-messages/group-messages.page.html +++ b/src/app/shared/chat/group-messages/group-messages.page.html @@ -97,7 +97,7 @@ {{file.description}} - {{msg.file.type.replace('application/','').toUpperCase()}} + {{msg.displayType}} 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 f61c9c520..870e8b7f6 100644 --- a/src/app/shared/chat/group-messages/group-messages.page.ts +++ b/src/app/shared/chat/group-messages/group-messages.page.ts @@ -317,7 +317,7 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe } sendMessage() { - this.wsChatMethodsService.getGroupRoom(this.roomId).send() + this.wsChatMethodsService.getGroupRoom(this.roomId).send({}) } deleteMessage(msgId: string, room:any) { @@ -674,13 +674,36 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe const lastphoto: any = await this.FileSystemService.loadFiles(roomId); const { capturedImage, capturedImageTitle} = await this.FileSystemService.loadFileData(lastphoto, roomId); + const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({ + file: { + "type": "application/img", + "guid": '', + "image_url": capturedImage + }, + attachments: [{ + //"title": this.capturedImageTitle , + //"text": "description", + "title_link_download": false, + //"image_url": this.capturedImage, + }] + }) + + const base64 = await fetch(capturedImage); const blob = await base64.blob(); const formData = new FormData(); formData.append("blobFile", blob); console.log('ALL IMAGE', formData) + + let guid: any = await this.AttachmentsService.uploadFile(formData).toPromise() - console.log(guid.path); + updateMessage({ + file: { + type: "application/img", + guid: guid.path, + image_url: capturedImage + } + }) this.AttachmentsService.downloadFile(guid.path).subscribe(async (event) => { @@ -689,25 +712,12 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe //console.log('FILE TYPE 33', msg.file.type) } else if (event.type === HttpEventType.Response) { var fileImage = 'data:image/jpeg;base64,' + btoa(new Uint8Array(event.body).reduce((data, byte) => data + String.fromCharCode(byte), '')); - console.log('add picture to chat',fileImage); - await this.storage.set(guid.path, fileImage).then(() => { + console.log('add picture to chat', capturedImage); + await this.storage.set(guid.path, capturedImage).then(() => { console.log('add picture to chat IMAGE SAVED') - - this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ - file: { - "type": "application/img", - "guid": guid.path, - "image_url": fileImage - }, - attachments: [{ - //"title": this.capturedImageTitle , - //"text": "description", - "title_link_download": false, - //"image_url": this.capturedImage, - }] - }) - + message.getFileFromDb() }); + } }); @@ -736,15 +746,10 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe modal.onDidDismiss().then(async res=>{ const data = res.data; - if(data.selected){ + if(data.selected) { const loader = this.toastService.loading(); - let url = await this.processesService.GetDocumentUrl(res.data.selected.Id, res.data.selected.ApplicationType).toPromise(); - let url_no_options: string = url.replace("webTRIX.Viewer","webTRIX.Viewer.Branch1"); - console.log(url_no_options); - - this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ - alias: "documento", + const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({ file:{ "name": res.data.selected.Assunto, "type": "application/webtrix", @@ -752,6 +757,22 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe "DocId": res.data.selected.Id, "Assunto": res.data.selected.Assunto, }, + attachments: [{ + "title": res.data.selected.Assunto, + "description": res.data.selected.DocTypeDesc, + // "title_link": url_no_options, + "title_link_download": true, + //"thumb_url": "assets/images/webtrix-logo.png", + // "message_link": url_no_options, + "type": "webtrix" + }] + }) + + let url = await this.processesService.GetDocumentUrl(res.data.selected.Id, res.data.selected.ApplicationType).toPromise(); + let url_no_options: string = url.replace("webTRIX.Viewer","webTRIX.Viewer.Branch1"); + console.log(url_no_options); + + updateMessage({ attachments: [{ "title": res.data.selected.Assunto, "description": res.data.selected.DocTypeDesc, @@ -762,6 +783,7 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe "type": "webtrix" }] }) + loader.remove(); } @@ -769,11 +791,37 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe } + async addFileToChat(types: typeof FileType[] ) { const file = await this.fileService.getFileFromDevice(types); + const imageData = await this.fileToBase64Service.convert(file) + const formData = new FormData(); formData.append("blobFile", file); + + const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({ + file: { + "type": "application/img", + "guid": '', + "image_url": imageData + }, + attachments: [{ + //"title": this.capturedImageTitle , + //"text": "description", + "title_link_download": false, + //"image_url": this.capturedImage, + }] + }) + let guid: any = await this.AttachmentsService.uploadFile(formData).toPromise() + updateMessage({ + file: { + type: "application/img", + guid: guid.path, + image_url: imageData + } + }) + this.AttachmentsService.downloadFile(guid.path).subscribe(async (event) => { if (event.type === HttpEventType.DownloadProgress) { @@ -784,21 +832,7 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe console.log('add picture to chat',fileImage); await this.storage.set(guid.path, fileImage).then(() => { console.log('add picture to chat IMAGE SAVED') - - this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ - file: { - "type": "application/img", - "guid": guid.path, - "image_url": fileImage - }, - attachments: [{ - //"title": this.capturedImageTitle , - //"text": "description", - "title_link_download": false, - //"image_url": this.capturedImage, - }] - }) - + message.getFileFromDb() }); } @@ -887,7 +921,7 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe await this.storage.set(guid.path, fileImage).then(() => { console.log('add picture to chat IMAGE SAVED') - this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ + this.wsChatMethodsService.getDmRoom(this.roomId).send({ file: { "type": "application/img", "guid": guid.path, diff --git a/src/app/shared/chat/messages/messages.page.html b/src/app/shared/chat/messages/messages.page.html index b132f662f..16c0804e9 100644 --- a/src/app/shared/chat/messages/messages.page.html +++ b/src/app/shared/chat/messages/messages.page.html @@ -97,7 +97,7 @@ {{file.description}} {{msg.file.type.replace('application/','').toUpperCase()}} + *ngIf="msg.file.type != 'application/webtrix'">{{msg.displayType}} diff --git a/src/app/shared/chat/messages/messages.page.ts b/src/app/shared/chat/messages/messages.page.ts index 5f093b9e0..a9ad0190f 100644 --- a/src/app/shared/chat/messages/messages.page.ts +++ b/src/app/shared/chat/messages/messages.page.ts @@ -32,6 +32,7 @@ import { CameraService } from 'src/app/services/camera.service'; import { FileType } from 'src/app/models/fileType'; import { SearchPage } from 'src/app/pages/search/search.page'; import { ProcessesService } from 'src/app/services/processes.service'; +import { FileToBase64Service } from 'src/app/services/file/file-to-base64.service'; const IMAGE_DIR = 'stored-images'; @Component({ @@ -102,6 +103,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy private FileSystemService: FileSystemService, private CameraService: CameraService, private processesService: ProcessesService, + private fileToBase64Service: FileToBase64Service, ) { this.loggedUser = authService.ValidatedUserChat['data']; @@ -113,6 +115,11 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy this.wsChatMethodsService.getDmRoom(this.roomId).scrollDown = this.scrollToBottomClicked this.wsChatMethodsService.openRoom(this.roomId) + + this.wsChatMethodsService.getDmRoom(this.roomId).uploadAttachment = async (formData) => { + let guid: any = await this.AttachmentsService.uploadFile(formData).toPromise() + } + setTimeout(() => { this.scrollToBottomClicked() }, 150) @@ -238,7 +245,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy } sendMessage() { - this.wsChatMethodsService.getDmRoom(this.roomId).send() + this.wsChatMethodsService.getDmRoom(this.roomId).send({}) } deleteMessage(msgId: string, room:any) { @@ -443,13 +450,36 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy const lastphoto: any = await this.FileSystemService.loadFiles(roomId); const { capturedImage, capturedImageTitle} = await this.FileSystemService.loadFileData(lastphoto, roomId); + const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({ + file: { + "type": "application/img", + "guid": '', + "image_url": capturedImage + }, + attachments: [{ + //"title": this.capturedImageTitle , + //"text": "description", + "title_link_download": false, + //"image_url": this.capturedImage, + }] + }) + + const base64 = await fetch(capturedImage); const blob = await base64.blob(); const formData = new FormData(); formData.append("blobFile", blob); console.log('ALL IMAGE', formData) + + let guid: any = await this.AttachmentsService.uploadFile(formData).toPromise() - console.log(guid.path); + updateMessage({ + file: { + type: "application/img", + guid: guid.path, + image_url: capturedImage + } + }) this.AttachmentsService.downloadFile(guid.path).subscribe(async (event) => { @@ -458,26 +488,12 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy //console.log('FILE TYPE 33', msg.file.type) } else if (event.type === HttpEventType.Response) { var fileImage = 'data:image/jpeg;base64,' + btoa(new Uint8Array(event.body).reduce((data, byte) => data + String.fromCharCode(byte), '')); - console.log('add picture to chat',fileImage); - await this.storage.set(guid.path, fileImage).then(() => { + console.log('add picture to chat', capturedImage); + await this.storage.set(guid.path, capturedImage).then(() => { console.log('add picture to chat IMAGE SAVED') + message.getFileFromDb() }); - this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ - alias: 'document', - file: { - "type": "application/img", - "guid": guid.path, - "image_url": fileImage - }, - attachments: [{ - //"title": this.capturedImageTitle , - //"text": "description", - "title_link_download": false, - //"image_url": this.capturedImage, - }] - }) - } }); @@ -506,15 +522,10 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy modal.onDidDismiss().then(async res=>{ const data = res.data; - if(data.selected){ + if(data.selected) { const loader = this.toastService.loading(); - let url = await this.processesService.GetDocumentUrl(res.data.selected.Id, res.data.selected.ApplicationType).toPromise(); - let url_no_options: string = url.replace("webTRIX.Viewer","webTRIX.Viewer.Branch1"); - console.log(url_no_options); - - this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ - alias: "documento", + const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({ file:{ "name": res.data.selected.Assunto, "type": "application/webtrix", @@ -522,6 +533,22 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy "DocId": res.data.selected.Id, "Assunto": res.data.selected.Assunto, }, + attachments: [{ + "title": res.data.selected.Assunto, + "description": res.data.selected.DocTypeDesc, + // "title_link": url_no_options, + "title_link_download": true, + //"thumb_url": "assets/images/webtrix-logo.png", + // "message_link": url_no_options, + "type": "webtrix" + }] + }) + + let url = await this.processesService.GetDocumentUrl(res.data.selected.Id, res.data.selected.ApplicationType).toPromise(); + let url_no_options: string = url.replace("webTRIX.Viewer","webTRIX.Viewer.Branch1"); + console.log(url_no_options); + + updateMessage({ attachments: [{ "title": res.data.selected.Assunto, "description": res.data.selected.DocTypeDesc, @@ -532,6 +559,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy "type": "webtrix" }] }) + loader.remove(); } @@ -539,11 +567,37 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy } + async addFileToChat(types: typeof FileType[] ) { const file = await this.fileService.getFileFromDevice(types); + const imageData = await this.fileToBase64Service.convert(file) + const formData = new FormData(); formData.append("blobFile", file); + + const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({ + file: { + "type": "application/img", + "guid": '', + "image_url": imageData + }, + attachments: [{ + //"title": this.capturedImageTitle , + //"text": "description", + "title_link_download": false, + //"image_url": this.capturedImage, + }] + }) + let guid: any = await this.AttachmentsService.uploadFile(formData).toPromise() + updateMessage({ + file: { + type: "application/img", + guid: guid.path, + image_url: imageData + } + }) + this.AttachmentsService.downloadFile(guid.path).subscribe(async (event) => { if (event.type === HttpEventType.DownloadProgress) { @@ -554,21 +608,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy console.log('add picture to chat',fileImage); await this.storage.set(guid.path, fileImage).then(() => { console.log('add picture to chat IMAGE SAVED') - - this.wsChatMethodsService.getDmRoom(this.roomId).sendFile({ - file: { - "type": "application/img", - "guid": guid.path, - "image_url": fileImage - }, - attachments: [{ - //"title": this.capturedImageTitle , - //"text": "description", - "title_link_download": false, - //"image_url": this.capturedImage, - }] - }) - + message.getFileFromDb() }); } @@ -585,7 +625,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy } - chatSendFile() { + chatsend() { } From 3b6e519adb23542276b26847734d6eb53f1465a1 Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Fri, 4 Feb 2022 00:54:41 +0100 Subject: [PATCH 3/9] fix --- .../group-messages/group-messages.page.ts | 30 ++++++++++++++----- src/app/pages/chat/messages/messages.page.ts | 11 ++++--- src/app/shared/chat/messages/messages.page.ts | 10 +++---- 3 files changed, 31 insertions(+), 20 deletions(-) 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 b6c4dca14..3a98cd9a3 100644 --- a/src/app/pages/chat/group-messages/group-messages.page.ts +++ b/src/app/pages/chat/group-messages/group-messages.page.ts @@ -638,18 +638,15 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { //"title": this.capturedImageTitle , //"text": "description", "title_link_download": false, - //"image_url": this.capturedImage, + "type": "application/img", + "guid": '', + "image_url": imageData }] }) let guid: any = await this.AttachmentsService.uploadFile(formData).toPromise() - updateMessage({ - file: { - type: "application/img", - guid: guid.path, - image_url: imageData - } - }) + message.file.guid = guid.path + this.AttachmentsService.downloadFile(guid.path).subscribe(async (event) => { @@ -659,6 +656,23 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { } else if (event.type === HttpEventType.Response) { var fileImage = 'data:image/jpeg;base64,' + btoa(new Uint8Array(event.body).reduce((data, byte) => data + String.fromCharCode(byte), '')); console.log('add picture to chat',fileImage); + + updateMessage({ + file: { + type: "application/img", + guid: guid.path, + image_url: fileImage + }, + attachments: [{ + //"title": this.capturedImageTitle , + //"text": "description", + "title_link_download": false, + "type": "application/img", + guid: guid.path, + "image_url": fileImage + }] + }) + await this.storage.set(guid.path, fileImage).then(() => { console.log('add picture to chat IMAGE SAVED') message.getFileFromDb() diff --git a/src/app/pages/chat/messages/messages.page.ts b/src/app/pages/chat/messages/messages.page.ts index 3c4da98de..d4cf829be 100644 --- a/src/app/pages/chat/messages/messages.page.ts +++ b/src/app/pages/chat/messages/messages.page.ts @@ -658,10 +658,9 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { "image_url": imageData }, attachments: [{ - //"title": this.capturedImageTitle , - //"text": "description", + "title": file.name, "title_link_download": false, - //"image_url": this.capturedImage, + "image_url": imageData, }] }) @@ -744,10 +743,10 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { "image_url": fileImage }, attachments: [{ - //"title": this.capturedImageTitle , - //"text": "description", + "title": capturedImageTitle , + "text": "description", "title_link_download": false, - //"image_url": this.capturedImage, + "image_url": capturedImage, }] }) diff --git a/src/app/shared/chat/messages/messages.page.ts b/src/app/shared/chat/messages/messages.page.ts index a9ad0190f..cb4ec06fb 100644 --- a/src/app/shared/chat/messages/messages.page.ts +++ b/src/app/shared/chat/messages/messages.page.ts @@ -457,10 +457,9 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy "image_url": capturedImage }, attachments: [{ - //"title": this.capturedImageTitle , - //"text": "description", + "title": capturedImageTitle , + "text": "description", "title_link_download": false, - //"image_url": this.capturedImage, }] }) @@ -582,10 +581,9 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy "image_url": imageData }, attachments: [{ - //"title": this.capturedImageTitle , - //"text": "description", + "title": file.name , + "text": "description", "title_link_download": false, - //"image_url": this.capturedImage, }] }) From 7ee2289ca01a469e7185b051a329838f3ce15696 Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Fri, 4 Feb 2022 00:57:46 +0100 Subject: [PATCH 4/9] fix --- .../group-messages/group-messages.page.ts | 38 ++++++------------- src/app/pages/chat/messages/messages.page.ts | 9 ++--- .../group-messages/group-messages.page.ts | 10 ++--- 3 files changed, 19 insertions(+), 38 deletions(-) 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 3a98cd9a3..3e8d02d58 100644 --- a/src/app/pages/chat/group-messages/group-messages.page.ts +++ b/src/app/pages/chat/group-messages/group-messages.page.ts @@ -510,10 +510,9 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { "image_url": capturedImage }, attachments: [{ - //"title": this.capturedImageTitle , - //"text": "description", + "title": capturedImageTitle , + "text": "description", "title_link_download": false, - //"image_url": this.capturedImage, }] }) @@ -635,18 +634,20 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { "image_url": imageData }, attachments: [{ - //"title": this.capturedImageTitle , - //"text": "description", + "title": file.name , + "text": "description", "title_link_download": false, - "type": "application/img", - "guid": '', - "image_url": imageData }] }) let guid: any = await this.AttachmentsService.uploadFile(formData).toPromise() - message.file.guid = guid.path - + updateMessage({ + file: { + type: "application/img", + guid: guid.path, + image_url: imageData + } + }) this.AttachmentsService.downloadFile(guid.path).subscribe(async (event) => { @@ -656,23 +657,6 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { } else if (event.type === HttpEventType.Response) { var fileImage = 'data:image/jpeg;base64,' + btoa(new Uint8Array(event.body).reduce((data, byte) => data + String.fromCharCode(byte), '')); console.log('add picture to chat',fileImage); - - updateMessage({ - file: { - type: "application/img", - guid: guid.path, - image_url: fileImage - }, - attachments: [{ - //"title": this.capturedImageTitle , - //"text": "description", - "title_link_download": false, - "type": "application/img", - guid: guid.path, - "image_url": fileImage - }] - }) - await this.storage.set(guid.path, fileImage).then(() => { console.log('add picture to chat IMAGE SAVED') message.getFileFromDb() diff --git a/src/app/pages/chat/messages/messages.page.ts b/src/app/pages/chat/messages/messages.page.ts index d4cf829be..b47923aa5 100644 --- a/src/app/pages/chat/messages/messages.page.ts +++ b/src/app/pages/chat/messages/messages.page.ts @@ -533,10 +533,9 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { "image_url": capturedImage }, attachments: [{ - //"title": this.capturedImageTitle , - //"text": "description", + "title": capturedImageTitle , + "text": "description", "title_link_download": false, - //"image_url": this.capturedImage, }] }) @@ -658,9 +657,9 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { "image_url": imageData }, attachments: [{ - "title": file.name, + "title": file.name , + "text": "description", "title_link_download": false, - "image_url": imageData, }] }) 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 870e8b7f6..76b20d77c 100644 --- a/src/app/shared/chat/group-messages/group-messages.page.ts +++ b/src/app/shared/chat/group-messages/group-messages.page.ts @@ -681,10 +681,9 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe "image_url": capturedImage }, attachments: [{ - //"title": this.capturedImageTitle , - //"text": "description", + "title": capturedImageTitle , + "text": "description", "title_link_download": false, - //"image_url": this.capturedImage, }] }) @@ -806,10 +805,9 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe "image_url": imageData }, attachments: [{ - //"title": this.capturedImageTitle , - //"text": "description", + "title": file.name , + "text": "description", "title_link_download": false, - //"image_url": this.capturedImage, }] }) From 1ffeb3df5c945d44c5490e67d6acbf41e23fbf28 Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Fri, 4 Feb 2022 07:42:43 +0100 Subject: [PATCH 5/9] update --- .../group-messages/group-messages.page.ts | 16 +- src/app/pages/chat/messages/messages.page.ts | 16 +- src/app/services/file-system.service.spec.ts | 16 -- src/app/services/file-system.service.ts | 145 -------------- src/app/services/functions/file.service.ts | 188 ++++++++++-------- .../group-messages/group-messages.page.ts | 16 +- src/app/shared/chat/messages/messages.page.ts | 10 +- 7 files changed, 135 insertions(+), 272 deletions(-) delete mode 100644 src/app/services/file-system.service.spec.ts delete mode 100644 src/app/services/file-system.service.ts 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 3e8d02d58..ffb265c32 100644 --- a/src/app/pages/chat/group-messages/group-messages.page.ts +++ b/src/app/pages/chat/group-messages/group-messages.page.ts @@ -29,7 +29,7 @@ import { WsChatMethodsService } from 'src/app/services/chat/ws-chat-methods.serv import { AttachmentsService } from 'src/app/services/attachments.service'; import { FileType } from 'src/app/models/fileType'; import { Storage } from '@ionic/storage'; -import { FileSystemService } from 'src/app/services/file-system.service'; + import { CameraService } from 'src/app/services/camera.service'; import { SearchPage } from 'src/app/pages/search/search.page'; import { ProcessesService } from 'src/app/services/processes.service'; @@ -98,7 +98,7 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { private AttachmentsService: AttachmentsService, private storage: Storage, private processesService: ProcessesService, - private FileSystemService: FileSystemService, + private CameraService: CameraService, ) { this.loggedUserChat = authService.ValidatedUserChat['data']; @@ -499,9 +499,9 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { const roomId = this.roomId const image = await this.CameraService.takePicture(); - await this.FileSystemService.saveImage(image, roomId) - const lastphoto: any = await this.FileSystemService.loadFiles(roomId); - const { capturedImage, capturedImageTitle} = await this.FileSystemService.loadFileData(lastphoto, roomId); + await this.fileService.saveImage(image) + const lastphoto: any = await this.fileService.loadFiles(); + const { capturedImage, capturedImageTitle} = await this.fileService.loadFileData(lastphoto); const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({ file: { @@ -689,9 +689,9 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { } else if(res['data'] == 'take-picture') { const image = await this.CameraService.takePicture(); - await this.FileSystemService.saveImage(image, roomId) - const lastphoto: any = await this.FileSystemService.loadFiles(roomId); - const { capturedImage, capturedImageTitle} = await this.FileSystemService.loadFileData(lastphoto, roomId); + await this.fileService.saveImage(image) + const lastphoto: any = await this.fileService.loadFiles(); + const { capturedImage, capturedImageTitle} = await this.fileService.loadFileData(lastphoto); const base64 = await fetch(capturedImage); const blob = await base64.blob(); diff --git a/src/app/pages/chat/messages/messages.page.ts b/src/app/pages/chat/messages/messages.page.ts index b47923aa5..8b03a1439 100644 --- a/src/app/pages/chat/messages/messages.page.ts +++ b/src/app/pages/chat/messages/messages.page.ts @@ -32,7 +32,7 @@ import { ViewEventPage } from 'src/app/modals/view-event/view-event.page'; import { WsChatMethodsService } from 'src/app/services/chat/ws-chat-methods.service' import { MessageService } from 'src/app/services/chat/message.service'; import { AttachmentsService } from 'src/app/services/attachments.service'; -import { FileSystemService } from 'src/app/services/file-system.service'; + import { CameraService } from 'src/app/services/camera.service'; import { element } from 'protractor'; import { FileType } from 'src/app/models/fileType'; @@ -108,7 +108,7 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { private sqlservice: SqliteService, public wsChatMethodsService: WsChatMethodsService, private AttachmentsService: AttachmentsService, - private FileSystemService: FileSystemService, + private CameraService: CameraService, private processesService: ProcessesService, private storage: Storage, @@ -522,9 +522,9 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { const roomId = this.roomId const image = await this.CameraService.takePicture(); - await this.FileSystemService.saveImage(image, roomId) - const lastphoto: any = await this.FileSystemService.loadFiles(roomId); - const { capturedImage, capturedImageTitle} = await this.FileSystemService.loadFileData(lastphoto, roomId); + await this.fileService.saveImage(image) + const lastphoto: any = await this.fileService.loadFiles(); + const { capturedImage, capturedImageTitle} = await this.fileService.loadFileData(lastphoto); const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({ file: { @@ -712,9 +712,9 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { } else if (res['data'] == 'take-picture') { const image = await this.CameraService.takePicture(); - await this.FileSystemService.saveImage(image, roomId) - const lastphoto: any = await this.FileSystemService.loadFiles(roomId); - const { capturedImage, capturedImageTitle} = await this.FileSystemService.loadFileData(lastphoto, roomId); + await this.fileService.saveImage(image) + const lastphoto: any = await this.fileService.loadFiles(); + const { capturedImage, capturedImageTitle} = await this.fileService.loadFileData(lastphoto); const base64 = await fetch(capturedImage); const blob = await base64.blob(); diff --git a/src/app/services/file-system.service.spec.ts b/src/app/services/file-system.service.spec.ts deleted file mode 100644 index f6d5285f4..000000000 --- a/src/app/services/file-system.service.spec.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { TestBed } from '@angular/core/testing'; - -import { FileSystemService } from './file-system.service'; - -describe('FileSystemService', () => { - let service: FileSystemService; - - beforeEach(() => { - TestBed.configureTestingModule({}); - service = TestBed.inject(FileSystemService); - }); - - it('should be created', () => { - expect(service).toBeTruthy(); - }); -}); diff --git a/src/app/services/file-system.service.ts b/src/app/services/file-system.service.ts deleted file mode 100644 index 668cede8a..000000000 --- a/src/app/services/file-system.service.ts +++ /dev/null @@ -1,145 +0,0 @@ -import { Injectable } from '@angular/core'; -import { ModalController, Platform,LoadingController } from '@ionic/angular'; -import { Filesystem, Directory } from '@capacitor/filesystem'; -import { Camera, CameraResultType, CameraSource, Photo} from '@capacitor/camera'; -import { ChatService } from './chat.service'; -import { AttachmentsService } from 'src/app/services/attachments.service'; -import { ToastService } from './toast.service'; -import { environment } from 'src/environments/environment'; -import { HttpClient, HttpEventType, HttpHeaders, HttpParams } from '@angular/common/http'; -import { Storage } from '@ionic/storage'; - - -const IMAGE_DIR = 'stored-images'; -interface LocalFile { - name: string; - path: string; - data: string; -} - -@Injectable({ - providedIn: 'root' -}) -export class FileSystemService { - - images: LocalFile[] = []; - - - constructor( - private platform: Platform, - private loadingCtrl: LoadingController, - private chatService: ChatService, - private AttachmentsService: AttachmentsService, - private toastService: ToastService, - private storage: Storage,) { } - - - async saveImage(photo: Photo, roomid: any) { - const base64Data = await this.readAsBase64(photo); - - const fileName = new Date().getTime() + '.jpeg'; - const savedFile = await Filesystem.writeFile({ - path: `${IMAGE_DIR}/${fileName}`, - data: base64Data, - directory: Directory.Data - }); - - } - - //new method 3 - async loadFiles(roomid) { - this.images = []; - - const loading = await this.loadingCtrl.create({ - message: 'Loading data...', - }); - await loading.present(); - - return new Promise((resolve, reject)=>{ - Filesystem.readdir({ - path: IMAGE_DIR, - directory: Directory.Data, - }).then(result => { - console.log('ALL RESULTS', result.files[0]) - let lastphoto = result.files[result.files.length - 1] - resolve(lastphoto) - }, - async (err) => { - console.log('ERROR FILE DOSENT EXIST', err) - reject('ERROR FILE DOSENT EXIST') - // Folder does not yet exists! - await Filesystem.mkdir({ - path: IMAGE_DIR, - directory: Directory.Data, - recursive: true - }); - } - ).then(_ => { - loading.dismiss(); - }); - }) - } - - - //new method 2 - private async readAsBase64(photo: Photo) { - if (this.platform.is('hybrid')) { - const file = await Filesystem.readFile({ - path: photo.path - }); - - return file.data; - } - else { - // Fetch the photo, read as a blob, then convert to base64 format - const response = await fetch(photo.webPath); - const blob = await response.blob(); - - return await this.convertBlobToBase64(blob) as string; - } - } - - - convertBlobToBase64 = (blob: Blob) => new Promise((resolve, reject) => { - const reader = new FileReader; - reader.onerror = reject; - reader.onload = () => { - resolve(reader.result); - }; - reader.readAsDataURL(blob); - }); - - async readFile(filePath) { - return await Filesystem.readFile({ - path: filePath, - directory: Directory.Data, - }); - } - - //new method 4 - async loadFileData(fileName: string, roomid: any) { - console.log('ALL PHOTOT FILE', fileName) - // for (let f of fileNames) { - const filePath = `${IMAGE_DIR}/${fileName}`; - - const readFile = await this.readFile(filePath) - - this.images.push({ - name: fileName, - path: filePath, - data: `data:image/jpeg;base64,${readFile.data}`, - }); - - console.log('ALL IMAGE', this.images) - - const capturedImage = this.images[0].data - - const capturedImageTitle = new Date().getTime() + '.jpeg'; - - - return { capturedImage, capturedImageTitle} - - } - - -} diff --git a/src/app/services/functions/file.service.ts b/src/app/services/functions/file.service.ts index b279a0f7d..56c42904d 100644 --- a/src/app/services/functions/file.service.ts +++ b/src/app/services/functions/file.service.ts @@ -3,13 +3,14 @@ import { FileLoaderService } from '../file/file-loader.service'; import { FileToBase64Service } from '../file/file-to-base64.service'; import { InAppBrowser } from '@ionic-native/in-app-browser/ngx'; import { ChatService } from '../chat.service'; -import { ModalController } from '@ionic/angular'; +import { LoadingController, ModalController, Platform } from '@ionic/angular'; import { SearchPage } from 'src/app/pages/search/search.page'; import { ProcessesService } from '../processes.service'; import { ToastService } from '../toast.service'; -import { Camera, CameraResultType, CameraSource} from '@capacitor/camera'; +import { Camera, CameraResultType, CameraSource, Photo} from '@capacitor/camera'; import { FileType } from 'src/app/models/fileType'; -import { FileSystemService } from 'src/app/services/file-system.service'; + +import { Filesystem, Directory } from '@capacitor/filesystem'; const IMAGE_DIR = 'stored-images'; @@ -44,22 +45,12 @@ export class FileService { private modalController: ModalController, private processesService: ProcessesService, private toastService: ToastService, - private FileSystemService: FileSystemService + + private loadingCtrl: LoadingController, + private platform: Platform, ) {} - _arrayBufferToBase64( buffer ) { - var binary = ''; - var bytes = new Uint8Array( buffer ); - var len = bytes.byteLength; - for (var i = 0; i < len; i++) { - binary += String.fromCharCode( bytes[ i ] ); - } - return window.btoa( binary ); -} - - - convertBlobToBase64 = (blob: Blob) => new Promise((resolve, reject) => { const reader = new FileReader; reader.onerror = reject; @@ -69,73 +60,7 @@ export class FileService { reader.readAsDataURL(blob); }); - async loadPicture() { - const input = this.fileLoaderService.createInput({ - accept: ['image/apng', 'image/jpeg', 'image/png', '.pdf'] - }) - input.onchange = async () => { - const file = this.fileLoaderService.getFirstFile(input) - - console.log(file); - - const imageData = await this.fileToBase64Service.convert(file) - this.capturedImage = imageData; - this.capturedImageTitle = file.name; - - let data = { - image:this.capturedImage, - name: this.capturedImageTitle - } - - return data; - }; - } - - - async addPictureToChatMobile(roomId) { - - const capturedImage = await Camera.getPhoto({ - quality: 50, - // allowEditing: true, - resultType: CameraResultType.Uri, - source: CameraSource.Camera - - }); - - if (capturedImage) { - await this.FileSystemService.saveImage(capturedImage,roomId) - } - - } - - - addDocumentToChat(roomId:string) { - const input = this.fileLoaderService.createInput({ - accept: ['.doc', '.docx', '.pdf'] - }) - - input.onchange = async () => { - const loader = this.toastService.loading(); - const file = this.fileLoaderService.getFirstFile(input) - - console.log(file); - - const formData = new FormData(); - formData.append('file', file, file.name); - - this.chatService.uploadFile(formData, roomId).subscribe(res=> { - console.log(res); - loader.remove(); - },(error) => { - loader.remove(); - }); - //console.log(this.capturedImage) - }; - } - - - getFileFromDevice(types: typeof FileType[]) { return new Promise((resolve, reject)=>{ const input = this.fileLoaderService.createInput({ @@ -155,4 +80,103 @@ export class FileService { const browser = this.iab.create(url,"_parent"); browser.show(); } + + + async saveImage(photo: Photo) { + const base64Data = await this.readAsBase64(photo); + + const fileName = new Date().getTime() + '.jpeg'; + const savedFile = await Filesystem.writeFile({ + path: `${IMAGE_DIR}/${fileName}`, + data: base64Data, + directory: Directory.Data + }); + + } + + //new method 3 + async loadFiles() { + + const loading = await this.loadingCtrl.create({ + message: 'Loading data...', + }); + await loading.present(); + + return new Promise((resolve, reject)=>{ + Filesystem.readdir({ + path: IMAGE_DIR, + directory: Directory.Data, + }).then(result => { + console.log('ALL RESULTS', result.files[0]) + let lastphoto = result.files[result.files.length - 1] + resolve(lastphoto) + }, + async (err) => { + console.log('ERROR FILE DOSENT EXIST', err) + reject('ERROR FILE DOSENT EXIST') + // Folder does not yet exists! + await Filesystem.mkdir({ + path: IMAGE_DIR, + directory: Directory.Data, + recursive: true + }); + } + ).then(_ => { + loading.dismiss(); + }); + }) + } + + + //new method 2 + private async readAsBase64(photo: Photo) { + if (this.platform.is('hybrid')) { + const file = await Filesystem.readFile({ + path: photo.path + }); + + return file.data; + } + else { + // Fetch the photo, read as a blob, then convert to base64 format + const response = await fetch(photo.webPath); + const blob = await response.blob(); + + return await this.convertBlobToBase64(blob) as string; + } + } + + + async readFile(filePath) { + return await Filesystem.readFile({ + path: filePath, + directory: Directory.Data, + }); + } + + //new method 4 + async loadFileData(fileName: string) { + console.log('ALL PHOTOT FILE', fileName) + // for (let f of fileNames) { + const filePath = `${IMAGE_DIR}/${fileName}`; + + const readFile = await this.readFile(filePath) + + const image ={ + name: fileName, + path: filePath, + data: `data:image/jpeg;base64,${readFile.data}`, + }; + + console.log('ALL IMAGE', image) + + const capturedImage = image.data + + const capturedImageTitle = new Date().getTime() + '.jpeg'; + + + return { capturedImage, capturedImageTitle} + + } + } 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 76b20d77c..d25537edb 100644 --- a/src/app/shared/chat/group-messages/group-messages.page.ts +++ b/src/app/shared/chat/group-messages/group-messages.page.ts @@ -27,7 +27,7 @@ import { Storage } from '@ionic/storage'; import { WsChatMethodsService } from 'src/app/services/chat/ws-chat-methods.service'; import { MessageService } from 'src/app/services/chat/message.service'; import { AttachmentsService } from 'src/app/services/attachments.service'; -import { FileSystemService } from 'src/app/services/file-system.service'; + import { CameraService } from 'src/app/services/camera.service'; import { element } from 'protractor'; import { FileType } from 'src/app/models/fileType'; @@ -103,7 +103,7 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe private changeDetectorRef: ChangeDetectorRef, private storage: Storage, private AttachmentsService: AttachmentsService, - private FileSystemService: FileSystemService, + private CameraService: CameraService, private toastService: ToastService, @@ -670,9 +670,9 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe const roomId = this.roomId const image = await this.CameraService.takePicture(); - await this.FileSystemService.saveImage(image, roomId) - const lastphoto: any = await this.FileSystemService.loadFiles(roomId); - const { capturedImage, capturedImageTitle} = await this.FileSystemService.loadFileData(lastphoto, roomId); + await this.fileService.saveImage(image) + const lastphoto: any = await this.fileService.loadFiles(); + const { capturedImage, capturedImageTitle} = await this.fileService.loadFileData(lastphoto); const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({ file: { @@ -896,9 +896,9 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe else if (res['data'] == 'take-picture') { const image = await this.CameraService.takePicture(); - await this.FileSystemService.saveImage(image, roomId) - const lastphoto: any = await this.FileSystemService.loadFiles(roomId); - const { capturedImage, capturedImageTitle} = await this.FileSystemService.loadFileData(lastphoto, roomId); + await this.fileService.saveImage(image) + const lastphoto: any = await this.fileService.loadFiles(); + const { capturedImage, capturedImageTitle} = await this.fileService.loadFileData(lastphoto); const base64 = await fetch(capturedImage); const blob = await base64.blob(); diff --git a/src/app/shared/chat/messages/messages.page.ts b/src/app/shared/chat/messages/messages.page.ts index cb4ec06fb..21675134e 100644 --- a/src/app/shared/chat/messages/messages.page.ts +++ b/src/app/shared/chat/messages/messages.page.ts @@ -27,7 +27,7 @@ import { WsChatMethodsService } from 'src/app/services/chat/ws-chat-methods.serv import { WsChatService } from 'src/app/services/chat/ws-chat.service' import { MessageService } from 'src/app/services/chat/message.service'; import { AttachmentsService } from 'src/app/services/attachments.service'; -import { FileSystemService } from 'src/app/services/file-system.service'; + import { CameraService } from 'src/app/services/camera.service'; import { FileType } from 'src/app/models/fileType'; import { SearchPage } from 'src/app/pages/search/search.page'; @@ -100,7 +100,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy public wsChatMethodsService: WsChatMethodsService, public WsChatService: WsChatService, private AttachmentsService: AttachmentsService, - private FileSystemService: FileSystemService, + private CameraService: CameraService, private processesService: ProcessesService, private fileToBase64Service: FileToBase64Service, @@ -446,9 +446,9 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy const roomId = this.roomId const image = await this.CameraService.takePicture(); - await this.FileSystemService.saveImage(image, roomId) - const lastphoto: any = await this.FileSystemService.loadFiles(roomId); - const { capturedImage, capturedImageTitle} = await this.FileSystemService.loadFileData(lastphoto, roomId); + await this.fileService.saveImage(image) + const lastphoto: any = await this.fileService.loadFiles(); + const { capturedImage, capturedImageTitle} = await this.fileService.loadFileData(lastphoto); const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({ file: { From 80aaf352b61d724e12074bf3ab8e50fc658a35da Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Fri, 4 Feb 2022 08:00:10 +0100 Subject: [PATCH 6/9] improve --- .../group-messages/group-messages.page.ts | 119 ++----------- src/app/pages/chat/messages/messages.page.ts | 49 +----- .../group-messages/group-messages.page.ts | 162 +----------------- src/app/shared/chat/messages/messages.page.ts | 4 +- 4 files changed, 24 insertions(+), 310 deletions(-) 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 ffb265c32..b6023c121 100644 --- a/src/app/pages/chat/group-messages/group-messages.page.ts +++ b/src/app/pages/chat/group-messages/group-messages.page.ts @@ -249,7 +249,6 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { this.roomCountDownDate = this.timeService.countDownDateTimer(this.room.customFields.countDownDate, this.room._id); } this.getGroupContacts(this.room); - this.loadGroupMessages(this.room); this.showLoader = false; }); } @@ -298,27 +297,6 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { } } - loadGroupMessages(room:any){ - this.showLoader = true; - //If group is private call getGroupMembers - if(this.room.t === 'p'){ - this.chatService.getPrivateGroupMessages(this.roomId).subscribe(res=>{ - console.log(res); - let msgOnly = res['messages'].filter(data => data.t != 'au'); - //this.messages = msgOnly.reverse(); - this.transformDataMSG(msgOnly.reverse()); - this.getRoomMessageDB(this.roomId); - this.showLoader = false; - }); - } - //Otherwise call getChannelMembers for públic groups - /* else{ - this.chatService.getPublicGroupMessages(this.roomId).subscribe(res=>{ - console.log(res); - this.messages = res['messages'].reverse(); - }); - } */ - } showDateDuration(start:any){ return this.timeService.showDateDuration(start); @@ -366,42 +344,6 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { }); } - loadPicture() { - const input = this.fileLoaderService.createInput({ - accept: ['image/apng', 'image/jpeg', 'image/png'] - }) - - input.onchange = async () => { - const file = this.fileLoaderService.getFirstFile(input) - - console.log(file); - - const imageData = await this.fileToBase64Service.convert(file) - this.capturedImage = imageData; - this.capturedImageTitle = file.name; - - let body = { - "message": - { - "rid": this.roomId, - "msg": "", - "attachments": [{ - //"title": this.capturedImageTitle , - //"text": "description", - "title_link_download": false, - "image_url": this.capturedImage, - }] - } - } - - this.chatService.sendMessage(body).subscribe(res=> { - console.log(res); - },(error) => { - - }); - //console.log(this.capturedImage) - }; - } viewDocument(file:any, url?:string){ @@ -608,7 +550,9 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { "title_link_download": true, //"thumb_url": "assets/images/webtrix-logo.png", "message_link": url_no_options, - "type": "webtrix" + "type": "webtrix", + "text": res.data.selected.DocTypeDesc, + "thumb_url": "https://static.ichimura.ed.jp/uploads/2017/10/pdf-icon.png", }] }) @@ -619,7 +563,6 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { } - async addFileToChat(types: typeof FileType[] ) { const file = await this.fileService.getFileFromDevice(types); const imageData = await this.fileToBase64Service.convert(file) @@ -684,65 +627,23 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { await popover.present(); await popover.onDidDismiss().then( async(res)=>{ console.log(res['data']); - if(res['data'] == 'meeting'){ + if (res['data'] == 'meeting') { this.bookMeeting(); } - else if(res['data'] == 'take-picture') { - const image = await this.CameraService.takePicture(); - await this.fileService.saveImage(image) - const lastphoto: any = await this.fileService.loadFiles(); - const { capturedImage, capturedImageTitle} = await this.fileService.loadFileData(lastphoto); - - const base64 = await fetch(capturedImage); - const blob = await base64.blob(); - const formData = new FormData(); - formData.append("blobFile", blob); - console.log('ALL IMAGE', formData) - let guid: any = await this.AttachmentsService.uploadFile(formData).toPromise() - console.log(guid.path); - - this.AttachmentsService.downloadFile(guid.path).subscribe(async (event) => { - - if (event.type === HttpEventType.DownloadProgress) { - //this.downloadProgess = Math.round((100 * event.loaded) / event.total); - //console.log('FILE TYPE 33', msg.file.type) - } else if (event.type === HttpEventType.Response) { - var fileImage = 'data:image/jpeg;base64,' + btoa(new Uint8Array(event.body).reduce((data, byte) => data + String.fromCharCode(byte), '')); - console.log('add picture to chat',fileImage); - await this.storage.set(guid.path, fileImage).then(() => { - console.log('add picture to chat IMAGE SAVED') - - this.wsChatMethodsService.getDmRoom(this.roomId).send({ - file: { - "type": "application/img", - "guid": guid.path, - "image_url": fileImage - }, - attachments: [{ - //"title": this.capturedImageTitle , - //"text": "description", - "title_link_download": false, - //"image_url": this.capturedImage, - }] - }) - - }); - } - - }); + else if (res['data'] == 'take-picture') { + this.takePicture() } - else if(res['data'] == 'add-picture'){ - + else if (res['data'] == 'add-picture') { this.addImage() } - else if(res['data'] == 'add-document'){ + else if (res['data'] == 'add-document') { this.addFile() } - else if(res['data'] == 'documentoGestaoDocumental'){ + else if (res['data'] == 'documentoGestaoDocumental') { + this.addFileWebtrix() } - this.loadGroupMessages(this.roomId); }); } diff --git a/src/app/pages/chat/messages/messages.page.ts b/src/app/pages/chat/messages/messages.page.ts index 8b03a1439..1bbcebd38 100644 --- a/src/app/pages/chat/messages/messages.page.ts +++ b/src/app/pages/chat/messages/messages.page.ts @@ -526,7 +526,7 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { const lastphoto: any = await this.fileService.loadFiles(); const { capturedImage, capturedImageTitle} = await this.fileService.loadFileData(lastphoto); - const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({ + const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(roomId).send({ file: { "type": "application/img", "guid": '', @@ -631,7 +631,9 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { "title_link_download": true, //"thumb_url": "assets/images/webtrix-logo.png", "message_link": url_no_options, - "type": "webtrix" + "type": "webtrix", + "text": res.data.selected.DocTypeDesc, + "thumb_url": "https://static.ichimura.ed.jp/uploads/2017/10/pdf-icon.png", }] }) @@ -711,48 +713,7 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { this.bookMeeting(); } else if (res['data'] == 'take-picture') { - const image = await this.CameraService.takePicture(); - await this.fileService.saveImage(image) - const lastphoto: any = await this.fileService.loadFiles(); - const { capturedImage, capturedImageTitle} = await this.fileService.loadFileData(lastphoto); - - const base64 = await fetch(capturedImage); - const blob = await base64.blob(); - const formData = new FormData(); - formData.append("blobFile", blob); - console.log('ALL IMAGE', formData) - let guid: any = await this.AttachmentsService.uploadFile(formData).toPromise() - console.log(guid.path); - - this.AttachmentsService.downloadFile(guid.path).subscribe(async (event) => { - - if (event.type === HttpEventType.DownloadProgress) { - //this.downloadProgess = Math.round((100 * event.loaded) / event.total); - //console.log('FILE TYPE 33', msg.file.type) - } else if (event.type === HttpEventType.Response) { - var fileImage = 'data:image/jpeg;base64,' + btoa(new Uint8Array(event.body).reduce((data, byte) => data + String.fromCharCode(byte), '')); - console.log('add picture to chat',fileImage); - await this.storage.set(guid.path, fileImage).then(() => { - console.log('add picture to chat IMAGE SAVED') - - this.wsChatMethodsService.getDmRoom(this.roomId).send({ - file: { - "type": "application/img", - "guid": guid.path, - "image_url": fileImage - }, - attachments: [{ - "title": capturedImageTitle , - "text": "description", - "title_link_download": false, - "image_url": capturedImage, - }] - }) - - }); - } - - }); + this.takePicture() } else if (res['data'] == 'add-picture') { this.addImage() 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 d25537edb..fbff28265 100644 --- a/src/app/shared/chat/group-messages/group-messages.page.ts +++ b/src/app/shared/chat/group-messages/group-messages.page.ts @@ -398,10 +398,6 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe else { this.roomName = res.data.name.split('-').join(' '); console.log(this.roomName); - - //this.getRoomInfo(); - this.loadGroupMessages(this.roomId); - //this.modalController.dismiss(); }; }); @@ -478,85 +474,6 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe }); } - loadPicture() { - const input = this.fileLoaderService.createInput({ - accept: ['image/apng', 'image/jpeg', 'image/png'] - }) - - input.onchange = async () => { - const file = this.fileLoaderService.getFirstFile(input) - - console.log(file); - - const imageData = await this.fileToBase64Service.convert(file) - this.capturedImage = imageData; - this.capturedImageTitle = file.name; - - let body = { - "message": - { - "rid": this.roomId, - "msg": "", - "attachments": [{ - //"title": this.capturedImageTitle , - //"text": "description", - "title_link_download": false, - "image_url": this.capturedImage, - }] - } - } - - this.chatService.sendMessage(body).subscribe(res => { - console.log(res); - }, (error) => { - - }); - //console.log(this.capturedImage) - }; - } - - loadDocument() { - const input = this.fileLoaderService.createInput({ - accept: ['.doc', '.docx', '.pdf'] - }) - - input.onchange = async () => { - const file = this.fileLoaderService.getFirstFile(input) - - console.log(file); - - const imageData = await this.fileToBase64Service.convert(file) - this.capturedImage = imageData; - this.capturedImageTitle = file.name; - - let body = { - "message": - { - "rid": this.roomId, - "msg": "", - "attachments": [{ - "title": this.capturedImageTitle, - "text": "", - "title_link": this.capturedImage, - "title_link_download": true, - "thumb_url": "https://static.ichimura.ed.jp/uploads/2017/10/pdf-icon.png", - "message_link": this.capturedImage, - }], - "file": { - "name": this.capturedImageTitle, - "type": "application/pdf", - } - } - } - - this.chatService.sendMessage(body).subscribe(res => { - //console.log(res); - }, (error) => { - - }); - //console.log(this.capturedImage) - }; - } async addDocGestaoDocumental() { const modal = await this.modalController.create({ @@ -573,36 +490,8 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe if (res) { const data = res.data; this.documents.push(data.selected); - console.log(res.data.selected); - console.log(res.data.selected.Id); - console.log(res.data.selected.ApplicationType); - - let url = await this.processesService.GetDocumentUrl(res.data.selected.Id, res.data.selected.ApplicationType).toPromise(); - let url_no_options: string = url.replace("webTRIX.Viewer", "webTRIX.Viewer.Branch1"); - console.log(url_no_options); - //console.log('Oie'); - - let body = { - "message": - { - "rid": this.roomId, - "msg": "", - "attachments": [{ - "title": res.data.selected.Assunto, - "text": res.data.selected.DocTypeDesc, - "title_link": url_no_options, - "title_link_download": true, - "thumb_url": "https://static.ichimura.ed.jp/uploads/2017/10/pdf-icon.png", - "message_link": url_no_options, - }] - } - } - this.chatService.sendMessage(body).subscribe(res => { - console.log(res); - }, (error) => { - - }); - + + this.addFileWebtrix() } }); } @@ -779,7 +668,9 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe "title_link_download": true, //"thumb_url": "assets/images/webtrix-logo.png", "message_link": url_no_options, - "type": "webtrix" + "type": "webtrix", + "text": res.data.selected.DocTypeDesc, + "thumb_url": "https://static.ichimura.ed.jp/uploads/2017/10/pdf-icon.png", }] }) @@ -895,48 +786,7 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe } else if (res['data'] == 'take-picture') { - const image = await this.CameraService.takePicture(); - await this.fileService.saveImage(image) - const lastphoto: any = await this.fileService.loadFiles(); - const { capturedImage, capturedImageTitle} = await this.fileService.loadFileData(lastphoto); - - const base64 = await fetch(capturedImage); - const blob = await base64.blob(); - const formData = new FormData(); - formData.append("blobFile", blob); - console.log('ALL IMAGE', formData) - let guid: any = await this.AttachmentsService.uploadFile(formData).toPromise() - console.log(guid.path); - - this.AttachmentsService.downloadFile(guid.path).subscribe(async (event) => { - - if (event.type === HttpEventType.DownloadProgress) { - //this.downloadProgess = Math.round((100 * event.loaded) / event.total); - //console.log('FILE TYPE 33', msg.file.type) - } else if (event.type === HttpEventType.Response) { - var fileImage = 'data:image/jpeg;base64,' + btoa(new Uint8Array(event.body).reduce((data, byte) => data + String.fromCharCode(byte), '')); - console.log('add picture to chat',fileImage); - await this.storage.set(guid.path, fileImage).then(() => { - console.log('add picture to chat IMAGE SAVED') - - this.wsChatMethodsService.getDmRoom(this.roomId).send({ - file: { - "type": "application/img", - "guid": guid.path, - "image_url": fileImage - }, - attachments: [{ - //"title": this.capturedImageTitle , - //"text": "description", - "title_link_download": false, - //"image_url": this.capturedImage, - }] - }) - - }); - } - - }); + this.takePicture() } else if (res['data'] == 'add-picture') { diff --git a/src/app/shared/chat/messages/messages.page.ts b/src/app/shared/chat/messages/messages.page.ts index 21675134e..8faa411ec 100644 --- a/src/app/shared/chat/messages/messages.page.ts +++ b/src/app/shared/chat/messages/messages.page.ts @@ -555,7 +555,9 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy "title_link_download": true, //"thumb_url": "assets/images/webtrix-logo.png", "message_link": url_no_options, - "type": "webtrix" + "type": "webtrix", + "text": res.data.selected.DocTypeDesc, + "thumb_url": "https://static.ichimura.ed.jp/uploads/2017/10/pdf-icon.png", }] }) From 91ed23cb3a64cb5051a353affa4ac8ef48b5bc26 Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Fri, 4 Feb 2022 08:11:49 +0100 Subject: [PATCH 7/9] improve --- src/app/pages/chat/chat.page.ts | 5 -- src/app/pages/chat/messages/messages.page.ts | 49 ++------------ .../group-messages/group-messages.page.ts | 65 ++----------------- src/app/shared/chat/messages/messages.page.ts | 49 ++------------ 4 files changed, 18 insertions(+), 150 deletions(-) diff --git a/src/app/pages/chat/chat.page.ts b/src/app/pages/chat/chat.page.ts index b5963e605..916f0f0cb 100644 --- a/src/app/pages/chat/chat.page.ts +++ b/src/app/pages/chat/chat.page.ts @@ -159,11 +159,6 @@ export class ChatPage implements OnInit { let t = this.showDateDuration(new Date()); - this.setStatus('away'); - /* if(this.dataService.get("newGroup")){ - this.openNewGroupPage(); - } */ - this.router.events.forEach((event) => { if (event instanceof NavigationStart && event.url.startsWith('/home/chat')) { if (this.dataService.get("newGroup")) { diff --git a/src/app/pages/chat/messages/messages.page.ts b/src/app/pages/chat/messages/messages.page.ts index 1bbcebd38..b57bfadb8 100644 --- a/src/app/pages/chat/messages/messages.page.ts +++ b/src/app/pages/chat/messages/messages.page.ts @@ -596,11 +596,12 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { await modal.present(); modal.onDidDismiss().then(async res=>{ const data = res.data; + const roomId = this.roomId if(data.selected) { const loader = this.toastService.loading(); - const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({ + const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(roomId).send({ file:{ "name": res.data.selected.Assunto, "type": "application/webtrix", @@ -646,13 +647,15 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { async addFileToChat(types: typeof FileType[] ) { + const roomId = this.roomId + const file = await this.fileService.getFileFromDevice(types); const imageData = await this.fileToBase64Service.convert(file) const formData = new FormData(); formData.append("blobFile", file); - const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({ + const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(roomId).send({ file: { "type": "application/img", "guid": '', @@ -807,48 +810,6 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { } - sliderOpts = { - zoom: false, - slidesPerView: 1.5, - spaceBetween: 20, - centeredSlides: true - }; - zoomActive = false; - zoomScale = 1; - - sliderZoomOpts = { - allowSlidePrev: false, - allowSlideNext: false, - zoom: { - maxRatio: 5 - }, - on: { - zoomChange: (scale, imageEl, slideEl) => { - this.zoomActive = true; - this.zoomScale = scale / 5; - this.changeDetectorRef.detectChanges(); - } - } - } - - async touchEnd(zoomslides: IonSlides, card) { - // Zoom back to normal - const slider = await zoomslides.getSwiper(); - const zoom = slider.zoom; - zoom.out(); - - // Card back to normal - card.el.style['z-index'] = 9; - - this.zoomActive = false; - this.changeDetectorRef.detectChanges(); - } - - touchStart(card) { - // Make card appear above backdrop - card.el.style['z-index'] = 11; - } - downloadFileMsg(msg: MessageService) { console.log('FILE TYPE', msg.file.type) this.downloadFile = ""; 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 fbff28265..0a02d3063 100644 --- a/src/app/shared/chat/group-messages/group-messages.page.ts +++ b/src/app/shared/chat/group-messages/group-messages.page.ts @@ -139,7 +139,6 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe setTimeout(() => { this.getRoomInfo(); }, 1000); - this.setStatus('online'); this.getChatMembers(); //this.getMessageDB(); @@ -226,7 +225,6 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe } ngOnDestroy() { - this.setStatus('away'); window.removeEventListener('scroll', this.scrollChangeCallback, true); } @@ -307,15 +305,6 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe } } - loadGroupMessages(roomId) { - this.showLoader = true; - this.chatService.getPrivateGroupMessages(this.roomId).subscribe(res => { - console.log(res); - let msgOnly = res['messages'].filter(data => data.t != 'au'); - this.showLoader = false; - }); - } - sendMessage() { this.wsChatMethodsService.getGroupRoom(this.roomId).send({}) } @@ -429,7 +418,6 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe popover.onDidDismiss().then(res => { console.log(res); if (res.data) { - this.loadGroupMessages(this.roomId); //this.getRoomInfo(); //this.modalController.dismiss(); }; @@ -470,7 +458,6 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe modal.onDidDismiss().then(() => { //this.getRoomInfo(); - this.loadGroupMessages(this.roomId) }); } @@ -563,7 +550,7 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe const lastphoto: any = await this.fileService.loadFiles(); const { capturedImage, capturedImageTitle} = await this.fileService.loadFileData(lastphoto); - const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({ + const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(roomId).send({ file: { "type": "application/img", "guid": '', @@ -621,6 +608,8 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe } async addFileWebtrix() { + const roomId = this.roomId + const modal = await this.modalController.create({ component: SearchPage, cssClass: 'group-messages modal-desktop search-modal search-modal-to-desktop', @@ -637,7 +626,7 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe if(data.selected) { const loader = this.toastService.loading(); - const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({ + const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(roomId).send({ file:{ "name": res.data.selected.Assunto, "type": "application/webtrix", @@ -683,13 +672,15 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe async addFileToChat(types: typeof FileType[] ) { + const roomId = this.roomId + const file = await this.fileService.getFileFromDevice(types); const imageData = await this.fileToBase64Service.convert(file) const formData = new FormData(); formData.append("blobFile", file); - const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({ + const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(roomId).send({ file: { "type": "application/img", "guid": '', @@ -810,48 +801,6 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe } - sliderOpts = { - zoom: false, - slidesPerView: 1.5, - spaceBetween: 20, - centeredSlides: true - }; - zoomActive = false; - zoomScale = 1; - - sliderZoomOpts = { - allowSlidePrev: false, - allowSlideNext: false, - zoom: { - maxRatio: 5 - }, - on: { - zoomChange: (scale, imageEl, slideEl) => { - this.zoomActive = true; - this.zoomScale = scale / 5; - this.changeDetectorRef.detectChanges(); - } - } - } - - async touchEnd(zoomslides: IonSlides, card) { - // Zoom back to normal - const slider = await zoomslides.getSwiper(); - const zoom = slider.zoom; - zoom.out(); - - // Card back to normal - card.el.style['z-index'] = 9; - - this.zoomActive = false; - this.changeDetectorRef.detectChanges(); - } - - touchStart(card) { - // Make card appear above backdrop - card.el.style['z-index'] = 11; - } - downloadFileMsg(msg: MessageService) { console.log('FILE TYPE', msg.file.type) this.downloadFile = ""; diff --git a/src/app/shared/chat/messages/messages.page.ts b/src/app/shared/chat/messages/messages.page.ts index 8faa411ec..97651efad 100644 --- a/src/app/shared/chat/messages/messages.page.ts +++ b/src/app/shared/chat/messages/messages.page.ts @@ -450,7 +450,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy const lastphoto: any = await this.fileService.loadFiles(); const { capturedImage, capturedImageTitle} = await this.fileService.loadFileData(lastphoto); - const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({ + const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(roomId).send({ file: { "type": "application/img", "guid": '', @@ -520,11 +520,12 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy await modal.present(); modal.onDidDismiss().then(async res=>{ const data = res.data; + const roomId = this.roomId if(data.selected) { const loader = this.toastService.loading(); - const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({ + const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(roomId).send({ file:{ "name": res.data.selected.Assunto, "type": "application/webtrix", @@ -570,13 +571,15 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy async addFileToChat(types: typeof FileType[] ) { + const roomId = this.roomId + const file = await this.fileService.getFileFromDevice(types); const imageData = await this.fileToBase64Service.convert(file) const formData = new FormData(); formData.append("blobFile", file); - const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(this.roomId).send({ + const { message, updateMessage} = this.wsChatMethodsService.getDmRoom(roomId).send({ file: { "type": "application/img", "guid": '', @@ -705,47 +708,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy } - sliderOpts = { - zoom: false, - slidesPerView: 1.5, - spaceBetween: 20, - centeredSlides: true - }; - zoomActive = false; - zoomScale = 1; - sliderZoomOpts = { - allowSlidePrev: false, - allowSlideNext: false, - zoom: { - maxRatio: 5 - }, - on: { - zoomChange: (scale, imageEl, slideEl) => { - this.zoomActive = true; - this.zoomScale = scale / 5; - this.changeDetectorRef.detectChanges(); - } - } - } - - async touchEnd(zoomslides: IonSlides, card) { - // Zoom back to normal - const slider = await zoomslides.getSwiper(); - const zoom = slider.zoom; - zoom.out(); - - // Card back to normal - card.el.style['z-index'] = 9; - - this.zoomActive = false; - this.changeDetectorRef.detectChanges(); - } - - touchStart(card) { - // Make card appear above backdrop - card.el.style['z-index'] = 11; - } downloadFileMsg(msg: MessageService) { console.log('FILE TYPE', msg.file.type) From 88a810a7bba863288d5c97f5d3c11c173a73aa31 Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Fri, 4 Feb 2022 08:17:39 +0100 Subject: [PATCH 8/9] improve --- src/app/services/functions/file.service.ts | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/src/app/services/functions/file.service.ts b/src/app/services/functions/file.service.ts index 56c42904d..01b42f718 100644 --- a/src/app/services/functions/file.service.ts +++ b/src/app/services/functions/file.service.ts @@ -1,19 +1,12 @@ import { Injectable } from '@angular/core'; import { FileLoaderService } from '../file/file-loader.service'; -import { FileToBase64Service } from '../file/file-to-base64.service'; import { InAppBrowser } from '@ionic-native/in-app-browser/ngx'; -import { ChatService } from '../chat.service'; -import { LoadingController, ModalController, Platform } from '@ionic/angular'; -import { SearchPage } from 'src/app/pages/search/search.page'; -import { ProcessesService } from '../processes.service'; -import { ToastService } from '../toast.service'; -import { Camera, CameraResultType, CameraSource, Photo} from '@capacitor/camera'; +import { LoadingController, Platform } from '@ionic/angular'; +import {Photo} from '@capacitor/camera'; import { FileType } from 'src/app/models/fileType'; - import { Filesystem, Directory } from '@capacitor/filesystem'; const IMAGE_DIR = 'stored-images'; - interface LocalFile { name: string; path: string; @@ -39,13 +32,7 @@ export class FileService { constructor( private fileLoaderService: FileLoaderService, - private fileToBase64Service: FileToBase64Service, private iab: InAppBrowser, - private chatService: ChatService, - private modalController: ModalController, - private processesService: ProcessesService, - private toastService: ToastService, - private loadingCtrl: LoadingController, private platform: Platform, ) {} From a3f1da77b92f31c146a246e265b01915d251c25e Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Mon, 7 Feb 2022 13:24:24 +0100 Subject: [PATCH 9/9] rever array --- src/app/services/chat/room.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index 7a32579ed..325172ac2 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -342,7 +342,7 @@ export class RoomService { if(messages==null) messages = [] - messages.forEach((ChatMessage, index) => { + messages.reverse().forEach((ChatMessage, index) => { const wewMessage = this.prepareMessage(ChatMessage) if(wewMessage.offline == true) {