Files
doneit-web/src/app/ui/chat/modal/messages/messages.page.ts
T

1064 lines
29 KiB
TypeScript
Raw Normal View History

2024-09-10 16:01:51 +01:00
import { AfterViewInit, Component, ElementRef, HostListener, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { GestureController, ModalController, NavParams, PopoverController, Platform } from '@ionic/angular';
import { ViewDocumentPage } from 'src/app/modals/view-document/view-document.page';
2021-04-13 14:14:55 +01:00
import { AlertService } from 'src/app/services/alert.service';
2021-07-12 11:13:29 +01:00
import { ToastService } from 'src/app/services/toast.service';
2021-10-23 09:53:21 +01:00
import { ThemeService } from 'src/app/services/theme.service'
2022-02-25 15:10:10 +01:00
import { VoiceRecorder, GenericResponse } from 'capacitor-voice-recorder';
2021-11-05 18:19:27 +01:00
import { Haptics, ImpactStyle } from '@capacitor/haptics';
2022-02-03 21:01:53 +01:00
import { SearchPage } from 'src/app/pages/search/search.page';
import { Storage } from '@ionic/storage';
2024-08-17 22:05:57 +01:00
import { CameraResultType } from '@capacitor/camera';
2022-03-14 08:09:33 +01:00
import { DomSanitizer } from '@angular/platform-browser';
2022-04-18 15:12:27 +01:00
import { SessionStore } from 'src/app/store/session.service';
import { ViewMediaPage } from 'src/app/modals/view-media/view-media.page';
2024-08-17 22:05:57 +01:00
2024-08-08 11:44:41 +01:00
//======
2024-09-18 19:02:45 +01:00
import { Subscription } from 'rxjs';
2024-08-29 15:23:11 +01:00
import { EditMessagePage } from 'src/app/ui/chat/modal/edit-message/edit-message.page';
2024-09-09 10:28:53 +01:00
import { IMessageType, MessageAttachmentFileType, MessageAttachmentSource, MessageEntity } from 'src/app/core/chat/entity/message';
2024-08-13 10:52:35 +01:00
import { compressImageBase64 } from 'src/app/utils/imageCompressore';
import { RecordingData } from 'capacitor-voice-recorder';
2024-08-16 17:45:45 +01:00
import { ChatOptionsPopoverPage } from '../chat-options-popover/chat-options-popover.page';
2024-08-17 22:05:57 +01:00
import { CameraService } from 'src/app/infra/camera/camera.service'
import { FilePickerMobileService } from 'src/app/infra/file-picker/mobile/file-picker-mobile.service'
import { FilePickerWebService } from 'src/app/infra/file-picker/web/file-picker-web.service'
import { allowedDocExtension } from 'src/app/utils/allowedDocExtension';
2024-10-25 12:23:58 +01:00
import { JSFileToDataUrl } from 'src/app/utils/ToBase64';
2024-08-26 14:47:03 +01:00
import { RoomType } from "src/app/core/chat/entity/group";
2024-09-06 14:38:30 +01:00
import { MessageViewModal } from '../../store/model/message';
import { ChatPopoverPage } from '../chat-popover/chat-popover.page';
2024-09-09 10:28:53 +01:00
import { ViewOncesImagePageInput, ViewOncesImagePage } from '../view-onces/view-onces.page';
import { LastMessage } from '../../utils/lastMessage';
2024-09-12 14:50:10 +01:00
import { File } from '@awesome-cordova-plugins/file/ngx';
2024-09-12 15:19:14 +01:00
import { FileSystemMobileService } from 'src/app/infra/file-system/mobile/file-system-mobile.service';
2024-09-18 11:47:23 +01:00
import { RoomViewModel } from '../../store/model/room';
2024-09-18 19:02:45 +01:00
import { RoomStore } from '../../store/roomStore'
2024-09-19 23:36:59 +01:00
import { EditGroupPage } from '../edit-group/edit-group.page';
2024-09-20 11:45:11 +01:00
import { imageMimeTypes } from 'src/app/utils/allowedImageExtension';
2024-10-07 13:27:49 +01:00
import { GroupContactsPage, IGroupContactsPageOutPutSchema } from '../group-contacts/group-contacts.page';
2024-08-18 15:40:43 +01:00
2020-12-30 11:13:50 +01:00
@Component({
selector: 'app-messages',
templateUrl: './messages.page.html',
styleUrls: ['./messages.page.scss'],
})
2021-08-23 16:31:06 +01:00
export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
2021-01-27 16:01:49 +01:00
showLoader: boolean;
2021-01-13 10:02:30 +01:00
@ViewChild('scrollMe') private myScrollContainer: ElementRef;
@ViewChild('rectangle') private rectangle: ElementRef;
2021-01-13 10:02:30 +01:00
2021-11-23 10:57:07 +01:00
canvas: any
ctx: any
2021-01-13 10:02:30 +01:00
loggedUser: any;
userPresence = '';
dmUsers: any;
roomId: string;
el: any;
scrollingOnce: boolean = true;
2021-07-23 14:43:51 +01:00
2021-08-23 16:31:06 +01:00
private scrollChangeCallback: () => void;
currentPosition: any;
startPosition: number;
2021-09-24 15:39:25 +01:00
scrollToBottomBtn = false;
2021-09-30 10:41:40 +01:00
showMessageOptions = false;
selectedMsgId: string;
2021-01-13 10:02:30 +01:00
LoadedDocument: any = null;
2021-10-09 16:41:18 +01:00
2021-11-05 18:19:27 +01:00
recording = false;
2022-03-04 18:46:56 +01:00
allowTyping = true;
2022-03-04 11:52:43 +01:00
lastAudioRecorded = '';
2022-04-04 00:37:00 +01:00
audioRecorded: any = "";
audioDownloaded: any = "";
2021-11-05 18:19:27 +01:00
durationDisplay = '';
duration = 0;
@ViewChild('recordbtn', { read: ElementRef }) recordBtn: ElementRef;
myAudio: any;
2021-12-16 16:36:39 +01:00
downloadfile: any;
2021-12-17 17:20:43 +01:00
downloadFile: any;
2022-03-27 18:54:08 +01:00
files: any[] = [];
@ViewChild('filechooser') fileChooserElementRef: ElementRef;
2024-08-15 10:26:20 +01:00
@ViewChild('array') myInputRef!: ElementRef;
2022-03-09 09:23:27 +01:00
downloadProgess: number;
downloadLoader: boolean;
2021-11-05 18:19:27 +01:00
2022-07-06 17:02:42 +01:00
audioPermissionStatus: 'granted' | 'denied' | 'prompt' | null = null
2022-04-18 15:12:27 +01:00
sessionStore = SessionStore
2024-09-06 14:38:30 +01:00
SessionStore = SessionStore
2022-04-02 09:40:09 +01:00
2024-08-08 11:44:41 +01:00
selectedMessage: any = null;
emojis: string[] = ['😊', '😂', '❤️', '👍', '😢']; // Add more emojis as needed
textField = ''
2024-08-14 15:29:16 +01:00
MessageAttachmentFileType = MessageAttachmentFileType
MessageAttachmentFileSource = MessageAttachmentSource
2024-09-06 14:38:30 +01:00
messageOnReconnectSubject: Subscription
RoomTypeEnum = RoomType
IMessageType = IMessageType
2024-09-10 17:07:26 +01:00
handleClickActive = false
2024-09-18 11:47:23 +01:00
room!: RoomViewModel
2020-12-30 11:13:50 +01:00
constructor(
public popoverController: PopoverController,
private modalController: ModalController,
2021-03-12 11:56:54 +01:00
private navParams: NavParams,
2021-04-13 14:14:55 +01:00
private alertService: AlertService,
2021-07-12 11:13:29 +01:00
private toastService: ToastService,
private gestureController: GestureController,
2021-11-17 15:34:15 +01:00
public ThemeService: ThemeService,
2021-12-08 19:36:41 +01:00
private platform: Platform,
2022-02-03 21:01:53 +01:00
private storage: Storage,
2022-03-14 08:09:33 +01:00
private sanitiser: DomSanitizer,
2024-08-17 22:05:57 +01:00
private CameraService: CameraService,
private FilePickerMobileService: FilePickerMobileService,
2024-08-18 13:27:57 +01:00
private FilePickerWebService: FilePickerWebService,
2024-09-12 14:50:10 +01:00
private file: File,
2024-09-18 19:02:45 +01:00
private fileSystemMobileService: FileSystemMobileService,
2024-09-20 11:45:11 +01:00
public RoomStore: RoomStore,
2021-07-23 14:43:51 +01:00
) {
2022-07-06 17:02:42 +01:00
2024-09-18 11:47:23 +01:00
this.room = this.navParams.get('room');
2024-09-18 19:02:45 +01:00
this.RoomStore.start(this.room)
2024-08-15 10:26:20 +01:00
}
2024-08-08 11:44:41 +01:00
2024-09-19 23:36:59 +01:00
2024-09-10 16:01:51 +01:00
@HostListener('document:click', ['$event'])
2024-10-17 12:17:24 +01:00
@HostListener('document:touchstart', ['$event'])
2024-09-10 16:01:51 +01:00
handleClickOutside(event: Event) {
2024-10-17 12:17:24 +01:00
2024-09-10 16:01:51 +01:00
if (!this.handleClickActive) return;
const clickedInside = (event.target as HTMLElement).closest('.mat-menu-content');
2024-10-17 12:17:24 +01:00
setTimeout(()=> {
if (!clickedInside) {
this.selectedMessage = null;
}
}, 100);
}
2024-09-10 16:01:51 +01:00
2024-09-06 14:38:30 +01:00
messageStatus(message: MessageViewModal) {
if(this.allViewed(message)) {
return 'allViewed'
} else if(this.allReceived(message)) {
return 'allReceived'
} else if (message.messageStatus == 'send') {
return 'enviado'
} else {
return 'enviar'
}
}
allReceived(message: MessageViewModal) {
2024-09-18 19:02:45 +01:00
return message.info.filter(e => typeof e.deliverAt == 'string').length == this.RoomStore.totalMembers
2024-08-15 10:26:20 +01:00
}
2020-12-30 11:13:50 +01:00
2024-09-06 14:38:30 +01:00
allViewed(message: MessageViewModal) {
2024-09-18 19:02:45 +01:00
const totalMembers = this.RoomStore.members.filter((e) => message.sender.wxUserId != e.wxUserId ).length
2024-09-06 14:38:30 +01:00
return message.info.filter(e => typeof e.readAt == 'string' && message.sender.wxUserId != e.memberId ).length == totalMembers
}
2024-08-16 17:17:32 +01:00
ngOnInit() {}
2023-09-29 16:40:50 +01:00
2021-07-23 14:43:51 +01:00
2021-11-05 18:19:27 +01:00
ngAfterViewInit() {
this.scrollChangeCallback = () => this.onContentScrolled(event);
window.addEventListener('scroll', this.scrollChangeCallback, true);
2021-11-05 18:19:27 +01:00
const longpress = this.gestureController.create({
el: this.recordBtn.nativeElement,
threshold: 0,
gestureName: 'long-press',
onStart: ev => {
Haptics.impact({ style: ImpactStyle.Light })
this.startRecording();
2022-03-04 18:46:56 +01:00
//this.calculateDuration();
2021-11-05 18:19:27 +01:00
},
onEnd: ev => {
2021-11-05 18:19:27 +01:00
Haptics.impact({ style: ImpactStyle.Light })
this.stopRecording();
}
}, true);
longpress.enable();
}
2023-10-19 15:03:12 +01:00
onDragOver(e?) { }
onDragLeave(e?) { }
2023-04-28 12:56:45 +01:00
calculateDuration() {
if (!this.recording) {
2021-11-05 18:19:27 +01:00
this.duration = 0;
this.durationDisplay = '';
return;
}
this.duration += 1;
const minutes = Math.floor(this.duration / 60);
const seconds = (this.duration % 60).toString().padStart(2, '0');
2021-11-05 18:19:27 +01:00
this.durationDisplay = `${minutes}:${seconds}`;
setTimeout(() => {
2021-11-05 18:19:27 +01:00
this.calculateDuration();
}, 1000)
}
2024-08-14 15:29:16 +01:00
async loadFiles() {
2022-03-18 11:45:38 +01:00
this.storage.get('fileName').then((fileName) => {
this.lastAudioRecorded = fileName;
2021-11-05 18:19:27 +01:00
})
2022-03-18 11:45:38 +01:00
2022-03-22 14:23:38 +01:00
try {
this.storage.get('recordData').then((recordData) => {
2022-07-06 17:02:42 +01:00
2022-04-04 00:37:00 +01:00
if (recordData?.value?.recordDataBase64.includes('data:audio')) {
2022-03-24 17:40:14 +01:00
this.audioRecorded = this.sanitiser.bypassSecurityTrustResourceUrl(recordData?.value?.recordDataBase64);
2022-03-22 14:23:38 +01:00
}
2022-07-06 17:02:42 +01:00
else if (recordData?.value?.mimeType && recordData?.value?.recordDataBase64) {
2022-03-24 17:40:14 +01:00
this.audioRecorded = this.sanitiser.bypassSecurityTrustResourceUrl(`data:${recordData.value.mimeType};base64,${recordData?.value?.recordDataBase64}`);
2022-03-22 14:23:38 +01:00
}
});
2022-04-04 00:37:00 +01:00
} catch (error) { }
2022-03-22 14:23:38 +01:00
}
2022-04-07 15:22:25 +01:00
async startRecording() {
VoiceRecorder.requestAudioRecordingPermission();
2022-07-06 17:02:42 +01:00
if (await VoiceRecorder.canDeviceVoiceRecord().then((result: GenericResponse) => { return result.value })) {
if (await VoiceRecorder.requestAudioRecordingPermission().then((result: GenericResponse) => { return result.value })) {
2022-04-07 15:22:25 +01:00
//if(await this.hasAudioRecordingPermission()){
2022-07-06 17:02:42 +01:00
if (this.recording) {
return;
}
this.recording = true;
VoiceRecorder.startRecording();
this.calculateDuration();
2022-04-07 15:22:25 +01:00
//}
}
2022-07-06 17:02:42 +01:00
else {
2022-04-07 15:22:25 +01:00
this.toastService._badRequest('Para gravar uma mensagem de voz, permita o acesso do Gabinete Digital ao seu microfone.');
}
}
2022-07-06 17:02:42 +01:00
else {
2022-04-07 15:22:25 +01:00
this.toastService._badRequest('Este dispositivo não tem capacidade para gravação de áudio!');
2021-11-05 18:19:27 +01:00
}
}
stopRecording() {
2022-03-18 11:45:38 +01:00
this.deleteRecording();
2022-03-04 18:46:56 +01:00
this.allowTyping = false;
2022-07-06 17:02:42 +01:00
if (!this.recording) {
2021-11-05 18:19:27 +01:00
return;
}
this.recording = false;
VoiceRecorder.stopRecording().then(async (result: RecordingData) => {
2022-07-06 17:02:42 +01:00
2021-11-05 18:19:27 +01:00
this.recording = false;
if (result.value && result.value.recordDataBase64) {
2021-11-05 18:19:27 +01:00
const recordData = result.value.recordDataBase64;
2022-04-18 15:27:16 +01:00
//
2022-03-18 16:42:59 +01:00
const fileName = new Date().getTime() + ".mp3";
2022-03-18 11:45:38 +01:00
//Save file
2022-04-04 00:37:00 +01:00
this.storage.set('fileName', fileName);
this.storage.set('recordData', result).then(() => {
2022-07-06 17:02:42 +01:00
2021-11-05 18:19:27 +01:00
})
}
})
2022-03-10 16:26:43 +01:00
setTimeout(async () => {
2022-03-04 18:46:56 +01:00
this.loadFiles();
2022-03-18 11:45:38 +01:00
}, 1000);
2021-11-05 18:19:27 +01:00
}
2022-04-04 00:37:00 +01:00
async deleteRecording() {
2022-03-18 11:45:38 +01:00
this.storage.remove('fileName');
this.storage.remove('recordData');
2021-10-08 15:37:24 +01:00
2022-03-04 18:46:56 +01:00
this.allowTyping = true;
2022-03-04 11:52:43 +01:00
this.lastAudioRecorded = '';
this.loadFiles();
}
2022-03-14 08:09:33 +01:00
handlePress(id?: string) {
2021-09-30 10:41:40 +01:00
this.selectedMsgId = id;
this.showMessageOptions = true;
}
handleClick() {
this.showMessageOptions = false;
2021-09-30 10:41:40 +01:00
this.selectedMsgId = "";
}
2024-08-16 17:17:32 +01:00
deleteMessage(msgId: string) {}
2021-08-23 16:31:06 +01:00
2021-04-13 14:14:55 +01:00
notImplemented() {
2021-04-13 14:14:55 +01:00
this.alertService.presentAlert('Funcionalidade em desenvolvimento');
}
close() {
2021-03-12 11:56:54 +01:00
this.modalController.dismiss();
2022-03-18 11:45:38 +01:00
this.deleteRecording();
2021-03-12 11:56:54 +01:00
}
2021-07-26 09:55:57 +01:00
2024-09-19 09:16:14 +01:00
2021-07-26 09:55:57 +01:00
doRefresh(ev: any) {
2021-01-27 16:01:49 +01:00
ev.target.complete();
}
2021-07-23 14:43:51 +01:00
scrollToBottom(): void {
2021-01-13 10:02:30 +01:00
try {
if (this.scrollingOnce) {
2021-08-23 16:31:06 +01:00
this.myScrollContainer.nativeElement.scrollTop = this.myScrollContainer.nativeElement.scrollHeight;
//this.scrollingOnce = false;
}
} catch (err) { }
2021-01-13 10:02:30 +01:00
}
2022-01-14 14:50:47 +01:00
scrollToBottomClicked = () => {
2021-09-24 15:39:25 +01:00
try {
this.myScrollContainer.nativeElement.scrollTop = this.myScrollContainer.nativeElement.scrollHeight;
} catch (err) { }
2021-09-24 15:39:25 +01:00
}
2021-08-23 16:31:06 +01:00
2021-09-24 15:39:25 +01:00
onContentScrolled(e) {
2021-08-23 16:31:06 +01:00
this.startPosition = e.srcElement.scrollTop;
let scroll = e.srcElement.scrollTop;
2021-09-24 15:39:25 +01:00
let windowHeight = e.srcElement.scrollHeight;
let containerHeight = windowHeight - e.srcElement.clientHeight;
2021-09-24 15:39:25 +01:00
2021-08-23 16:31:06 +01:00
if (scroll > this.currentPosition) {
} else {
this.scrollingOnce = false;
}
if ((containerHeight - 100) > scroll) {
2021-09-24 15:39:25 +01:00
this.scrollToBottomBtn = true;
}
else {
2021-09-24 15:39:25 +01:00
this.scrollToBottomBtn = false;
}
2021-08-23 16:31:06 +01:00
this.currentPosition = scroll;
2021-09-24 15:39:25 +01:00
2021-08-23 16:31:06 +01:00
}
ngOnDestroy() {
2024-09-19 23:36:59 +01:00
this.RoomStore.stop()
2021-08-23 16:31:06 +01:00
window.removeEventListener('scroll', this.scrollChangeCallback, true);
2024-09-10 16:01:51 +01:00
this.handleClickActive = false; // Disable the listener before component destruction
2021-08-23 16:31:06 +01:00
}
2024-09-06 14:38:30 +01:00
async sendMessage() {
2024-09-18 19:02:45 +01:00
this.RoomStore.sendMessage(this.textField)
2024-09-06 14:38:30 +01:00
this.textField = ''
setTimeout(() => {
this.scrollToBottomClicked()
}, 100)
2023-02-02 18:24:26 +01:00
}
2022-03-04 14:51:08 +01:00
async sendAudio(fileName) {
2024-09-18 19:02:45 +01:00
const roomId = this.room.id
2022-03-21 21:09:05 +01:00
let audioFile;
this.storage.get('recordData').then(async (recordData:RecordingData) => {
2022-03-29 00:03:54 +01:00
2022-03-21 21:09:05 +01:00
audioFile = recordData;
2022-04-04 00:37:00 +01:00
if (recordData?.value?.recordDataBase64.includes('data:audio')) {
2022-03-24 17:40:14 +01:00
this.audioRecorded = recordData?.value?.recordDataBase64;
2022-03-18 11:45:38 +01:00
}
2022-07-06 17:02:42 +01:00
else if (recordData?.value?.mimeType && recordData?.value?.recordDataBase64) {
2022-03-24 17:40:14 +01:00
this.audioRecorded = `data:${recordData.value.mimeType};base64,${recordData?.value?.recordDataBase64}`;
2022-03-18 11:45:38 +01:00
}
2022-03-04 14:51:08 +01:00
const audioMimeType: string = recordData.value.mimeType
2024-08-13 10:52:35 +01:00
2024-09-18 19:02:45 +01:00
this.RoomStore.sendMessage('', [{
2024-08-21 20:14:48 +01:00
file: this.audioRecorded.split(',')[1],
2024-08-13 10:52:35 +01:00
fileName: "audio",
source: MessageAttachmentSource.Device,
fileType: MessageAttachmentFileType.Audio,
2024-09-06 14:38:30 +01:00
mimeType: audioMimeType, // 'audio/webm',
2024-08-16 17:17:32 +01:00
safeFile: this.sanitiser.bypassSecurityTrustResourceUrl(this.audioRecorded)
2024-09-18 19:02:45 +01:00
}])
2024-09-06 14:38:30 +01:00
2024-08-16 17:17:32 +01:00
setTimeout(() => {
this.scrollToBottomClicked()
}, 100)
2022-03-04 14:51:08 +01:00
2024-09-06 14:38:30 +01:00
this.deleteRecording();
2022-04-04 00:37:00 +01:00
});
2022-03-21 21:09:05 +01:00
this.deleteRecording();
2022-03-04 14:51:08 +01:00
}
viewDocument(file: any, url?: string) {
2022-07-06 17:02:42 +01:00
2024-09-13 14:03:51 +01:00
this.openViewDocumentModal(file);
}
2022-03-14 08:09:33 +01:00
2021-10-09 16:41:18 +01:00
async openViewDocumentModal(file: any) {
2021-10-09 20:24:34 +01:00
let task = {
serialNumber: '',
taskStartDate: '',
isEvent: true,
workflowInstanceDataFields: {
FolderID: '',
Subject: file.Assunto,
SourceSecFsID: file.ApplicationId,
2022-02-25 15:10:10 +01:00
SourceType: 'PDF',
2021-10-09 20:24:34 +01:00
SourceID: file.DocId,
DispatchNumber: ''
2021-10-09 16:41:18 +01:00
}
2021-10-09 20:24:34 +01:00
}
2021-10-09 16:41:18 +01:00
2021-10-09 20:24:34 +01:00
let doc = {
"Id": "",
"ParentId": "",
"Source": 1,
"ApplicationId": file.ApplicationId,
"CreateDate": "",
"Data": null,
"Description": "",
"Link": null,
"SourceId": file.DocId,
"SourceName": file.Assunto,
"Stakeholders": "",
2021-10-09 20:24:34 +01:00
}
2021-10-09 16:41:18 +01:00
2021-10-09 20:24:34 +01:00
const modal = await this.modalController.create({
component: ViewDocumentPage,
componentProps: {
trustedUrl: '',
file: {
title: file.Assunto,
url: '',
title_link: '',
2021-10-09 16:41:18 +01:00
},
2021-10-09 20:24:34 +01:00
Document: doc,
applicationId: file.ApplicationId,
docId: file.DocId,
folderId: '',
task: task
},
cssClass: 'modal modal-desktop'
});
2021-10-09 20:24:34 +01:00
await modal.present();
2021-09-21 14:05:59 +01:00
}
2024-09-19 09:16:14 +01:00
2020-12-30 11:13:50 +01:00
showDateDuration(start: any) {
2021-08-20 17:00:48 +01:00
let end;
end = new Date();
start = new Date(start);
let customizedDate;
const totalSeconds = Math.floor((end - (start)) / 1000);;
const totalMinutes = Math.floor(totalSeconds / 60);
const totalHours = Math.floor(totalMinutes / 60);
const totalDays = Math.floor(totalHours / 24);
2021-08-20 17:00:48 +01:00
const hours = totalHours - (totalDays * 24);
const minutes = totalMinutes - (totalDays * 24 * 60) - (hours * 60);
const seconds = totalSeconds - (totalDays * 24 * 60 * 60) - (hours * 60 * 60) - (minutes * 60);
2021-08-20 17:00:48 +01:00
if (totalDays == 0) {
if (start.getDate() == new Date().getDate()) {
2021-08-20 17:00:48 +01:00
let time = start.getHours() + ":" + this.addZero(start.getUTCMinutes());
return time;
}
else {
2021-08-20 17:00:48 +01:00
return 'Ontem';
}
}
else {
let date = start.getDate() + "/" + (start.getMonth() + 1) + "/" + start.getFullYear();
2021-08-20 17:00:48 +01:00
return date;
}
}
addZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
2021-06-03 17:07:29 +01:00
async openMessagesOptions(ev?: any) {
2020-12-30 11:13:50 +01:00
const popover = await this.popoverController.create({
2024-09-06 14:38:30 +01:00
component: ChatPopoverPage,
2021-01-13 10:02:30 +01:00
componentProps: {
2024-09-18 19:02:45 +01:00
roomId: this.room.id,
2024-09-06 14:38:30 +01:00
members: [],
2024-09-18 19:02:45 +01:00
isAdmin: this.RoomStore.isAdmin,
2024-09-19 23:36:59 +01:00
roomType: this.RoomStore.room.roomType
2021-01-13 10:02:30 +01:00
},
2020-12-30 11:13:50 +01:00
cssClass: 'messages-options',
event: ev,
2021-01-13 10:02:30 +01:00
translucent: true,
2020-12-30 11:13:50 +01:00
});
2024-09-19 23:36:59 +01:00
await popover.present();
popover.onDidDismiss().then(res => {
if (res.data == 'leave') {
this.modalController.dismiss()
}
else if (res.data == 'delete') {
this.modalController.dismiss()
}
else if (res.data == 'cancel') {
this.modalController.dismiss()
}
else if (res.data == 'edit') {
//this.closeAllDesktopComponents.emit();
this.editGroup()
} else if (res.data == 'addUser') {
this.addContacts();
}
else {}
});
2020-12-30 11:13:50 +01:00
}
async addContacts() {
2024-09-19 23:36:59 +01:00
2020-12-30 11:13:50 +01:00
const modal = await this.modalController.create({
2024-09-19 23:36:59 +01:00
component: GroupContactsPage,
componentProps: {
roomId: this.RoomStore.room.id,
},
2020-12-30 11:13:50 +01:00
cssClass: 'contacts',
backdropDismiss: false
});
2023-07-15 11:01:09 +01:00
2024-09-19 23:36:59 +01:00
modal.onDidDismiss<IGroupContactsPageOutPutSchema>().then(result => {
// this.modalController.dismiss({roomId})
});
await modal.present();
}
2020-12-30 11:13:50 +01:00
2024-09-19 23:36:59 +01:00
async editGroup() {
const modal = await this.modalController.create({
component: EditGroupPage,
cssClass: 'modal modal-desktop',
componentProps: {
roomId: this.RoomStore.room.id,
},
});
2023-07-15 11:01:09 +01:00
await modal.present();
2024-09-19 23:36:59 +01:00
modal.onDidDismiss().then((res) => {
this.RoomStore.updateRoomDetails()
// this.getRoomInfo();
//this.modalController.dismiss(res.data);
});
2020-12-30 11:13:50 +01:00
}
2021-09-21 14:05:59 +01:00
2024-09-09 10:28:53 +01:00
async onImageError() {}
async onImageLoad(message: MessageViewModal, index:number) {
if(message.attachments[0].fileName == LastMessage.attachments[0].fileName) {
this.scrollToBottom()
setTimeout(() => {
this.scrollToBottom();
}, 100)
2024-09-18 19:02:45 +01:00
// this.messages1[this.room.id].splice(index, 1);
2024-09-09 10:28:53 +01:00
}
}
2024-09-10 16:01:51 +01:00
2022-02-03 21:01:53 +01:00
2024-08-13 10:52:35 +01:00
async takePictureMobile() {
2022-09-28 21:05:28 +01:00
2024-08-17 22:05:57 +01:00
const picture = await this.CameraService.takePicture({
cameraResultType: CameraResultType.DataUrl,
quality: 90
})
2023-09-29 16:40:50 +01:00
2024-08-17 22:05:57 +01:00
if(picture.isOk()) {
const file = picture.value
2022-02-04 00:22:35 +01:00
2024-08-17 22:05:57 +01:00
const compressedImage = await compressImageBase64(
file.dataUrl,
800, // maxWidth
800, // maxHeight
0.9 // quality
)
if(compressedImage.isOk()) {
2024-09-18 19:02:45 +01:00
this.RoomStore.sendMessage('', [{
2024-08-17 22:05:57 +01:00
file: compressedImage.value.split(',')[1],
fileName: "foto",
source: MessageAttachmentSource.Device,
fileType: MessageAttachmentFileType.Image,
mimeType: 'image/'+picture.value.format
2024-09-18 19:02:45 +01:00
}])
2023-09-29 16:40:50 +01:00
2024-08-17 22:05:57 +01:00
}
2023-09-29 16:40:50 +01:00
2023-03-14 09:50:40 +01:00
}
2023-09-29 16:40:50 +01:00
2022-02-03 21:01:53 +01:00
}
2022-02-08 15:45:09 +01:00
2022-02-11 15:08:27 +01:00
2022-02-03 21:01:53 +01:00
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();
2022-03-03 08:21:22 +01:00
modal.onDidDismiss().then(async res => {
2022-02-03 21:01:53 +01:00
const data = res.data;
2022-03-03 08:21:22 +01:00
if (data.selected) {
2022-02-03 21:01:53 +01:00
2024-09-18 19:02:45 +01:00
this.RoomStore.sendMessage('', [{
2024-08-13 10:52:35 +01:00
fileName: res.data.selected.Assunto,
source: MessageAttachmentSource.Webtrix,
fileType: MessageAttachmentFileType.Doc,
applicationId: res.data.selected.ApplicationType,
2024-09-11 12:16:05 +01:00
docId: parseInt(res.data.selected.Id),
2024-09-06 14:38:30 +01:00
description: res.data.selected.Assunto
2024-09-18 19:02:45 +01:00
}])
2022-02-04 00:22:35 +01:00
2022-02-03 21:01:53 +01:00
}
});
}
2024-08-13 10:52:35 +01:00
async pickPicture() {
2022-02-08 15:45:09 +01:00
2024-09-20 11:45:11 +01:00
const result = await this.FilePickerMobileService.getFile({
types: imageMimeTypes,
multiple: false,
readData: true,
2024-08-13 10:52:35 +01:00
})
2022-07-06 17:02:42 +01:00
2024-09-20 11:45:11 +01:00
if(result.isOk()) {
const file = result.value.files[0]
if(file) {
let resultUrl = decodeURIComponent(file.path);
const base64 = await this.fileSystemMobileService.readFile({resultUrl})
2022-02-08 15:45:09 +01:00
2024-09-20 11:45:11 +01:00
if(base64.isOk()) {
// console.log('1', base64.value)
// console.log('base64.value.data', base64.value.data)
// console.log('file', file)
// console.log('3',createDataURL(base64.value.data, file.mimeType))
2024-08-13 10:52:35 +01:00
2024-09-18 19:02:45 +01:00
this.RoomStore.sendMessage('', [{
2024-09-20 11:45:11 +01:00
file: base64.value.data,
2024-08-13 10:52:35 +01:00
fileName: "foto",
source: MessageAttachmentSource.Device,
fileType: MessageAttachmentFileType.Image,
2024-09-20 11:45:11 +01:00
mimeType: file.mimeType
2024-09-18 19:02:45 +01:00
}])
2024-09-20 11:45:11 +01:00
} else {
console.log(base64.error)
2024-08-13 10:52:35 +01:00
}
2024-09-20 11:45:11 +01:00
} else {
console.log('no file')
2024-08-17 22:05:57 +01:00
}
2024-09-20 11:45:11 +01:00
} else {
console.log('error', result.error)
2024-08-13 10:52:35 +01:00
}
2022-02-08 15:45:09 +01:00
2024-09-20 11:45:11 +01:00
// const file = await this.FilePickerService.getPicture({
// cameraResultType: CameraResultType.Base64
// })
// if(file.isOk()) {
// var base64 = 'data:image/jpeg;base64,' + file.value.base64String
// if (file.value.format == "jpeg" || file.value.format == "png" || file.value.format == "gif") {
// const compressedImage = await compressImageBase64(
// base64,
// 800, // maxWidth
// 800, // maxHeight
// 0.9 // quality
// )
// if(compressedImage.isOk()) {
// this.RoomStore.sendMessage('', [{
// file: compressedImage.value,
// fileName: "foto",
// source: MessageAttachmentSource.Device,
// fileType: MessageAttachmentFileType.Image,
// mimeType: 'image/'+file.value.format
// }])
// }
// }
// } else {
// if(file.error.type == 'PERMISSION_DENIED') {
// this.toastService._badRequest("Sem acesso a camera")
// }
// Logger.error('failed to pick picture from the device', {
// error: file.error
// })
// }
2022-02-08 15:45:09 +01:00
}
2024-09-09 10:28:53 +01:00
async viewOnce(event: Event, message: MessageViewModal, index:number) {
const params: ViewOncesImagePageInput = {
imageDataUrl: message.attachments[index].safeFile as any,
}
const modal = await this.modalController.create({
component: ViewOncesImagePage,
cssClass: '',
componentProps: params
});
modal.present()
return modal.onDidDismiss().then((res) => {
this.messageDelete(message)
});
}
2023-08-16 08:01:45 +01:00
2024-08-13 10:52:35 +01:00
async addFileToChat(types) {
2024-03-11 11:18:32 +01:00
console.log('add file ')
2024-09-18 19:02:45 +01:00
const roomId = this.room.id
2022-03-27 18:54:08 +01:00
2024-03-12 16:45:08 +01:00
if (this.platform.is('ios')) {
2024-03-11 11:18:32 +01:00
console.log('ios add file ')
2024-08-17 22:05:57 +01:00
const result = await this.FilePickerMobileService.getFile({
2024-03-18 11:13:34 +01:00
types: ['application/pdf', 'application/doc', 'application/docx','application/xls', 'application/xlsx', 'application/ppt',
'application/pptx', 'application/txt'],
2024-03-11 11:18:32 +01:00
multiple: false,
readData: true,
2024-08-17 22:05:57 +01:00
})
2024-03-11 11:18:32 +01:00
2024-08-17 22:05:57 +01:00
if(result.isOk()) {
console.log('RESULT', result.value.files[0].data)
2024-03-11 11:18:32 +01:00
2024-09-18 19:02:45 +01:00
this.RoomStore.sendMessage('', [{
2024-08-17 22:05:57 +01:00
file: result.value.files[0].data,
fileName: result.value.files[0].name,
source: MessageAttachmentSource.Device,
2024-09-10 16:01:51 +01:00
fileType: MessageAttachmentFileType.Doc,
description: result.value.files[0].name
2024-09-18 19:02:45 +01:00
}])
2024-08-17 22:05:57 +01:00
return
}
2024-08-13 10:52:35 +01:00
2024-03-12 16:45:08 +01:00
2024-03-11 11:18:32 +01:00
}
2024-08-17 22:05:57 +01:00
const file = await this.FilePickerWebService.getFileFromDevice(types)
2022-04-04 00:37:00 +01:00
2024-08-17 22:05:57 +01:00
if(file.isOk()) {
2022-07-06 17:02:42 +01:00
2024-08-17 22:05:57 +01:00
console.log(file)
2023-08-31 13:07:52 +01:00
2022-03-27 18:54:08 +01:00
2024-08-17 22:05:57 +01:00
const fileName = file.value.name
2022-02-04 00:22:35 +01:00
2024-09-19 23:36:59 +01:00
const validation = await allowedDocExtension(file.value.type)
2022-03-25 09:25:05 +01:00
2024-08-17 22:05:57 +01:00
if (validation.isOk()) {
2024-08-13 10:52:35 +01:00
2024-08-17 22:05:57 +01:00
let fileBase64 = await JSFileToDataUrl(file.value);
2024-08-13 10:52:35 +01:00
2024-08-17 22:05:57 +01:00
if(fileBase64.isOk()) {
2024-08-13 10:52:35 +01:00
2024-09-18 19:02:45 +01:00
this.RoomStore.sendMessage('', [{
2024-08-17 22:05:57 +01:00
file: fileBase64.value.split(',')[1],
fileName: file.value.name,
source: MessageAttachmentSource.Device,
fileType: MessageAttachmentFileType.Doc,
2024-09-19 23:36:59 +01:00
mimeType: file.value.type,
description: file.value.name
2024-09-18 19:02:45 +01:00
}])
2024-08-17 22:05:57 +01:00
}
2022-02-04 00:22:35 +01:00
2022-07-06 17:02:42 +01:00
2024-08-17 22:05:57 +01:00
} else {
2024-09-19 23:36:59 +01:00
console.log({fileName})
2024-08-17 22:05:57 +01:00
this.toastService._badRequest("Ficheiro inválido")
}
}
2022-04-04 00:37:00 +01:00
}
2022-03-27 18:54:08 +01:00
2021-08-18 18:58:02 +01:00
async openChatOptions(ev?: any) {
2024-09-18 19:02:45 +01:00
const roomId = this.room.id
2022-07-06 17:02:42 +01:00
2021-08-18 18:58:02 +01:00
2020-12-30 11:13:50 +01:00
const popover = await this.popoverController.create({
component: ChatOptionsPopoverPage,
cssClass: 'chat-options-popover',
event: ev,
2021-08-18 18:58:02 +01:00
componentProps: {
2024-09-18 19:02:45 +01:00
room: this.room.id,
members: this.RoomStore.members,
2021-08-18 18:58:02 +01:00
eventSelectedDate: new Date(),
},
2020-12-30 11:13:50 +01:00
translucent: true
});
2021-09-21 14:05:59 +01:00
await popover.present();
2022-03-03 08:21:22 +01:00
popover.onDidDismiss().then(async (res) => {
2022-07-06 17:02:42 +01:00
if (res['data'] == 'take-picture') {
2024-08-13 10:52:35 +01:00
this.takePictureMobile()
2021-09-21 14:05:59 +01:00
}
else if (res['data'] == 'add-picture') {
console.log('add-picture')
2024-08-13 10:52:35 +01:00
this.pickPicture()
2021-09-21 14:05:59 +01:00
}
else if (res['data'] == 'add-document') {
2022-02-03 21:01:53 +01:00
this.addFile()
2021-09-21 14:05:59 +01:00
}
else if (res['data'] == 'documentoGestaoDocumental') {
2021-09-21 14:05:59 +01:00
2022-02-03 21:01:53 +01:00
this.addFileWebtrix()
2021-09-21 14:05:59 +01:00
}
2022-01-29 20:39:35 +01:00
2021-09-21 14:05:59 +01:00
});
2020-12-30 11:13:50 +01:00
}
2021-12-08 19:36:41 +01:00
isJson(str) {
try {
JSON.parse(str);
2021-12-08 19:36:41 +01:00
} catch (e) {
return "";
2021-12-08 19:36:41 +01:00
}
return JSON.parse(str);
}
2021-12-08 19:36:41 +01:00
2024-09-12 14:50:10 +01:00
async fetchBlobFromURL(blobUrl: string): Promise<Blob> {
try {
const response = await fetch(blobUrl);
if (!response.ok) {
throw new Error(`Failed to fetch blob: ${response.statusText}`);
}
return await response.blob();
} catch (error) {
console.error('Error fetching blob from URL:', error);
throw error;
}
}
2024-03-14 09:10:17 +01:00
2024-09-12 14:50:10 +01:00
convertBlobToDataURL(blob: Blob): Promise<string> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => {
if (reader.result) {
resolve(reader.result as string);
} else {
reject('Conversion failed');
}
};
reader.onerror = (error) => reject(error);
reader.readAsDataURL(blob);
2024-03-14 09:10:17 +01:00
});
2024-09-12 14:50:10 +01:00
}
async openFile(blobUrl:string, filename, fileType) {
let pathFile = ''
const fileName = filename
const contentFile = await this.fetchBlobFromURL(blobUrl)
if (this.platform.is('ios')) {
pathFile = this.file.documentsDirectory
} else {
pathFile = this.file.cacheDirectory
}
console.log(pathFile)
console.log(contentFile)
2024-09-12 15:19:14 +01:00
const writeResult = await this.fileSystemMobileService.writeFile(pathFile, fileName, contentFile, { replace: true })
if(writeResult.isOk()) {
const openResult = await this.fileSystemMobileService.fileOpener(pathFile + fileName, fileType)
}
2022-03-09 09:23:27 +01:00
}
2024-09-18 19:02:45 +01:00
2024-03-12 16:45:08 +01:00
2024-09-11 12:38:25 +01:00
downloadFileFromBrowser(fileName: string, msg: MessageViewModal): void {
if(msg.attachments[0]?.blobURl) {
// Create a temporary URL for the Blob
const url = msg.attachments[0].safeFile;
// Create an <a> element with the download attribute
const a = document.createElement('a');
a.href = url;
a.download = fileName; // Set the desired file name
a.click();
} else {
const link = document.createElement("a")
link.href = `data:${msg.attachments[0].mimeType}';base64,${msg.attachments[0].safeFile}`;
link.download = fileName
link.click()
link.remove()
}
}
2022-04-04 00:37:00 +01:00
2024-09-11 12:38:25 +01:00
async openPreview(msg: MessageViewModal) {
2022-01-21 16:55:05 +01:00
2024-09-11 12:38:25 +01:00
if (msg.attachments[0].source === MessageAttachmentSource.Webtrix) {
this.viewDocument(msg)
2022-01-21 16:55:05 +01:00
} else {
2024-09-11 12:38:25 +01:00
if (!msg.attachments[0].safeFile || msg.attachments[0].safeFile === null || msg.attachments[0].safeFile === '') {
2022-04-07 15:22:25 +01:00
} else {
2022-04-07 15:22:25 +01:00
if (this.platform.is('desktop') || this.platform.is('mobileweb')) {
2022-04-07 15:22:25 +01:00
2024-09-11 12:38:25 +01:00
if (msg.attachments[0].mimeType.includes('image')) {
const modal = await this.modalController.create({
component: ViewMediaPage,
cssClass: 'modal modal-desktop',
componentProps: {
2024-09-11 12:38:25 +01:00
image: msg.attachments[0].safeFile,
type: msg.attachments[0].mimeType,
username: msg.attachments[0].description,
_updatedAt: msg.sentAt
}
});
modal.present();
} else {
2022-09-23 11:23:24 +01:00
2024-09-11 12:38:25 +01:00
this.downloadFileFromBrowser(msg.attachments[0].description, msg)
}
2022-04-07 15:22:25 +01:00
} else {
2024-09-11 12:38:25 +01:00
if (msg.attachments[0].mimeType.includes('image')) {
2022-07-06 17:02:42 +01:00
const modal = await this.modalController.create({
component: ViewMediaPage,
cssClass: 'modal modal-desktop',
componentProps: {
2024-09-11 12:38:25 +01:00
image: msg.attachments[0].safeFile,
type: msg.attachments[0].mimeType,
username: msg.attachments[0].description,
_updatedAt: msg.sentAt
2022-07-06 17:02:42 +01:00
}
});
modal.present();
} else {
2024-09-19 23:36:59 +01:00
this.openFile(msg.attachments[0].safeFile, msg.attachments[0].fileName || msg.attachments[0].description, msg.attachments[0].mimeType);
2022-07-06 17:02:42 +01:00
}
}
2022-04-07 15:22:25 +01:00
}
2021-12-17 17:20:43 +01:00
}
2022-01-21 16:55:05 +01:00
2021-11-17 15:34:15 +01:00
}
2021-11-22 13:53:37 +01:00
imageSize(img) {
2021-11-22 13:53:37 +01:00
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
canvas.width = 300
canvas.height = 234
2021-11-22 13:53:37 +01:00
ctx.drawImage(img.attachments[0].image_url, 0, 0, 300, 234);
document.body.appendChild(canvas);
}
2021-11-23 10:57:07 +01:00
getPicture(img) {
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
canvas.width = 300
canvas.height = 234
ctx.drawImage(img.attachments[0].image_url, 0, 0, 300, 234);
document.body.appendChild(canvas);
2021-11-23 10:57:07 +01:00
}
2021-11-30 12:30:58 +01:00
2024-09-06 14:38:30 +01:00
messageDelete(message: MessageViewModal) {
2024-09-18 19:02:45 +01:00
this.RoomStore.messageDelete(message)
2024-08-08 11:44:41 +01:00
}
async editMessage(message: any) {
2024-09-10 16:01:51 +01:00
const modal = await this.popoverController.create({
2024-08-08 11:44:41 +01:00
component: EditMessagePage,
2024-09-10 16:01:51 +01:00
cssClass: 'edit-message',
2024-08-08 11:44:41 +01:00
componentProps: {
message: message.message,
2024-09-18 19:02:45 +01:00
roomId: this.room.id,
2024-08-08 11:44:41 +01:00
}
});
modal.present()
return modal.onDidDismiss().then((res) => {
2024-09-10 21:45:46 +01:00
if(res.data) {
2024-09-18 19:02:45 +01:00
this.RoomStore.updateMessage({
messageId:message.id,
message: res.data.message
2024-09-10 21:45:46 +01:00
})
}
2024-08-08 11:44:41 +01:00
});
}
2024-09-10 16:01:51 +01:00
toggleEmojiPicker(message: MessageViewModal) {
2024-08-08 11:44:41 +01:00
if (this.selectedMessage === message) {
this.selectedMessage = null; // Close the picker if it's already open
} else {
2024-09-10 17:07:26 +01:00
this.handleClickActive = true
2024-09-10 16:01:51 +01:00
this.selectedMessage = message; // Open the picker for the selected message
2024-08-08 11:44:41 +01:00
}
}
addReaction(message: any, emoji: string) {
2024-10-17 12:17:24 +01:00
console.log('reaction==')
2024-08-08 11:44:41 +01:00
// Logic to add reaction to the message
this.selectedMessage = null; // Close the picker after adding reaction
2024-09-18 19:02:45 +01:00
this.RoomStore.addReaction(message, emoji)
2024-08-08 11:44:41 +01:00
}
sendTyping() {
2024-09-18 19:02:45 +01:00
this.RoomStore.sendTyping()
2024-08-08 11:44:41 +01:00
}
2021-07-23 14:43:51 +01:00
}