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

1381 lines
38 KiB
TypeScript
Raw Normal View History

2023-07-06 12:18:15 +01:00
import { AfterViewInit, Component, ElementRef, OnDestroy, OnInit, ViewChild } from '@angular/core';
2024-08-16 11:26:31 +01:00
import { GestureController, ModalController, NavParams, PopoverController, Platform } from '@ionic/angular';
import { ViewDocumentPage } from 'src/app/modals/view-document/view-document.page';
2021-09-21 14:05:59 +01:00
import { EventPerson } from 'src/app/models/eventperson.model';
2021-10-09 16:41:18 +01:00
import { ExpedientTaskModalPageNavParamsTask } from 'src/app/models/ExpedientTaskModalPage';
2024-08-16 14:21:01 +01:00
import { ContactsPage } from 'src/app/ui/chat/modal/messages/contacts/contacts.page';
2021-04-13 14:14:55 +01:00
import { AlertService } from 'src/app/services/alert.service';
2021-09-21 14:05:59 +01:00
import { FileService } from 'src/app/services/functions/file.service';
2021-07-12 11:13:29 +01:00
import { ToastService } from 'src/app/services/toast.service';
2021-08-24 14:07:27 +01:00
import { ChatMessageStore } from 'src/app/store/chat/chat-message.service';
import { ChatUserStorage } from 'src/app/store/chat/chat-user.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
2024-08-16 11:26:31 +01:00
import { VoiceRecorder, GenericResponse } from 'capacitor-voice-recorder';
2021-11-05 18:19:27 +01:00
import { Haptics, ImpactStyle } from '@capacitor/haptics';
import { ViewEventPage } from 'src/app/modals/view-event/view-event.page';
2022-02-03 21:01:53 +01:00
import { SearchPage } from 'src/app/pages/search/search.page';
import { Storage } from '@ionic/storage';
2024-08-16 17:17:32 +01:00
import { Camera, CameraResultType, CameraSource } 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';
2022-07-06 09:59:22 +01:00
import { ViewMediaPage } from 'src/app/modals/view-media/view-media.page';
2022-09-23 11:23:24 +01:00
import { File } from '@awesome-cordova-plugins/file/ngx';
2022-07-06 17:02:42 +01:00
import { FileOpener } from '@awesome-cordova-plugins/file-opener/ngx';
2024-08-16 11:26:31 +01:00
import { Filesystem, Directory } from '@capacitor/filesystem';
import { FileValidatorService } from "src/app/services/file/file-validator.service"
2024-03-11 11:18:32 +01:00
import { FilePicker } from '@capawesome/capacitor-file-picker';
2024-08-08 11:44:41 +01:00
//======
import { Observable as DexieObservable } from 'Dexie';
2024-08-14 15:29:16 +01:00
import { Subscription } from 'rxjs';
2024-08-08 11:44:41 +01:00
import { MessageRepositoryService } from 'src/app/module/chat/data/repository/message-respository.service'
import { RoomRepositoryService } from 'src/app/module/chat/data/repository/room-repository.service'
import { MessageTable } from 'src/app/module/chat/infra/database/dexie/schema/message';
import { RoomListItemOutPutDTO } from 'src/app/module/chat/data/dto/room/roomListOutputDTO';
import { UserTypingServiceRepository } from 'src/app/module/chat/data/repository/user-typing-repository.service';
import { ChatServiceService } from 'src/app/module/chat/domain/chat-service.service';
import { EditMessagePage } from 'src/app/modals/edit-message/edit-message.page';
import { MessageEntity } from 'src/app/module/chat/domain/entity/message';
import { MemberTable } from 'src/app/module/chat/infra/database/dexie/schema/members';
import { TypingTable } from 'src/app/module/chat/infra/database/dexie/schema/typing';
2024-08-13 10:52:35 +01:00
import { MessageAttachmentFileType, MessageAttachmentSource } from 'src/app/module/chat/data/dto/message/messageOutputDTO';
import { compressImageBase64 } from 'src/app/utils/imageCompressore';
import { FilePickerService } from 'src/app/infra/file-picker/file-picker.service'
2024-08-16 11:26:31 +01:00
import { RecordingData } from 'capacitor-voice-recorder';
2024-08-16 12:24:26 +01:00
import { Logger } from 'src/app/services/logger/main/service';
2024-08-16 17:45:45 +01:00
import { MessagesOptionsPage } from '../messages-options/messages-options.page';
import { ChatOptionsPopoverPage } from '../chat-options-popover/chat-options-popover.page';
2021-11-21 19:49:59 +01:00
const IMAGE_DIR = 'stored-images';
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('messageContainer') messageContainer: 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;
members: any;
scrollingOnce: boolean = true;
2021-07-23 14:43:51 +01:00
2021-11-05 18:19:27 +01:00
chatMessageStore = ChatMessageStore;
chatUserStorage = ChatUserStorage;
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-21 14:05:59 +01:00
attendees: EventPerson[] = [];
longPressActive = false;
2021-09-30 10:41:40 +01:00
showMessageOptions = false;
selectedMsgId: string;
2021-01-13 10:02:30 +01:00
2021-10-09 16:41:18 +01:00
dicIndex = 0;
task: ExpedientTaskModalPageNavParamsTask;
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;
2021-11-05 18:19:27 +01:00
storedFileNames = [];
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-27 18:54:08 +01:00
//items: File[] = [];
fileSelected?: Blob;
2022-04-04 00:37:00 +01:00
pdfUrl?: string;
2022-03-27 18:54:08 +01:00
base64File: string;
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
2022-04-02 09:40:09 +01:00
2024-08-08 11:44:41 +01:00
roomData$: DexieObservable<RoomListItemOutPutDTO | undefined>
roomStatus$: DexieObservable<Boolean >
roomMessage$: DexieObservable<MessageTable[]>
roomMembers$: DexieObservable<MemberTable[] | undefined>
//userTyping$: DexieObservable<TypingTable[] | undefined>
userTyping$: TypingTable[] | undefined
newMessagesStream!: Subscription
selectedMessage: any = null;
emojis: string[] = ['😊', '😂', '❤️', '👍', '😢']; // Add more emojis as needed
textField = ''
2024-08-14 15:29:16 +01:00
messageReceiveSubject: Subscription
messageDeleteSubject: Subscription
messageUpdateSubject: Subscription
messageSendSubject: Subscription
messages1: {[key: string]: MessageEntity[]} = {}
2024-08-16 11:26:31 +01:00
MessageAttachmentFileType = MessageAttachmentFileType
MessageAttachmentFileSource = MessageAttachmentSource
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,
2021-09-21 14:05:59 +01:00
private fileService: FileService,
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,
2022-07-06 17:02:42 +01:00
private file: File,
private fileOpener: FileOpener,
2024-03-07 12:02:59 +01:00
private FileValidatorService: FileValidatorService,
2024-08-08 11:44:41 +01:00
private roomRepositoryService: RoomRepositoryService,
private messageRepositoryService: MessageRepositoryService,
private userTypingServiceRepository: UserTypingServiceRepository,
2024-08-13 10:52:35 +01:00
private chatServiceService: ChatServiceService,
private FilePickerService: FilePickerService,
2021-07-23 14:43:51 +01:00
) {
2022-07-06 17:02:42 +01:00
2023-10-19 15:03:12 +01:00
2024-08-08 11:44:41 +01:00
this.roomId = this.navParams.get('roomId');
2021-09-03 17:02:42 +01:00
2023-10-19 15:03:12 +01:00
2024-08-08 11:44:41 +01:00
this.roomData$ = this.roomRepositoryService.getItemByIdLive(this.roomId)
2024-08-15 10:26:20 +01:00
this.getMessages();
2024-08-14 15:29:16 +01:00
this.listenToIncomingMessage();
this.listenToDeleteMessage();
this.listenToUpdateMessage();
this.listenToSendMessage()
2022-01-11 15:43:09 +01:00
2024-08-14 15:29:16 +01:00
this.roomMessage$ = this.messageRepositoryService.getItemsLive(this.roomId)
2024-08-08 11:44:41 +01:00
this.roomMembers$ = this.roomRepositoryService.getRoomMemberByIdLive(this.roomId) as any
this.roomStatus$ = this.roomRepositoryService.getRoomStatus(this.roomId)
this.roomRepositoryService.getRoomById(this.roomId)
this.newMessagesStream?.unsubscribe()
this.newMessagesStream = this.messageRepositoryService.subscribeToNewMessages(this.roomId).subscribe((e) => {
2022-02-11 15:08:27 +01:00
2023-10-19 15:03:12 +01:00
setTimeout(() => {
this.scrollToBottomClicked()
2024-08-08 11:44:41 +01:00
}, 200)
setTimeout(() => {
this.scrollToBottomClicked()
}, 500)
})
2024-08-15 10:26:20 +01:00
this.userTypingServiceRepository.getUserTypingLive().subscribe((e) => {
const arrayNames = e.map(e => e.userName)
this.userTyping$ = e as any
const uniqueArray = [...new Set(arrayNames)];
(this.myInputRef.nativeElement as HTMLDivElement).innerHTML = '::'+ uniqueArray
})
}
2024-08-08 11:44:41 +01:00
2024-08-15 10:26:20 +01:00
async getMessages() {
2024-08-16 11:26:31 +01:00
2024-08-15 10:26:20 +01:00
// dont remove this line
this.messages1[this.roomId] = []
let messages = await this.messageRepositoryService.getItems(this.roomId)
2024-08-16 11:26:31 +01:00
2024-08-15 10:26:20 +01:00
this.messages1[this.roomId] = []
this.messages1[this.roomId] = messages
this.loadAttachment()
setTimeout(() => {
this.scrollToBottomClicked()
}, 100)
2024-08-08 11:44:41 +01:00
2024-03-07 12:02:59 +01:00
}
2022-01-11 15:43:09 +01:00
2024-08-15 10:26:20 +01:00
async loadAttachment() {
for(const message of this.messages1[this.roomId]) {
if(message.hasAttachment) {
const result = await this.chatServiceService.getMessageAttachmentByMessageId({
$messageId: message.$id,
id: message.attachments[0].id
})
if(result.isOk()) {
message.attachments[0].safeFile = result.value
}
}
2024-03-26 12:03:30 +01:00
2024-08-15 10:26:20 +01:00
}
}
2020-12-30 11:13:50 +01:00
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
listenToIncomingMessage() {
this.messageReceiveSubject?.unsubscribe();
this.messageReceiveSubject = this.chatServiceService.listenToIncomingMessage(this.roomId).subscribe(async (message) => {
this.messages1[this.roomId].push(message as MessageEntity)
2024-08-16 11:26:31 +01:00
2024-08-14 15:29:16 +01:00
if(message.hasAttachment) {
const result = await this.chatServiceService.getMessageAttachmentByMessageId({
$messageId: message.$id,
id: message.attachments[0].id
})
if(result.isOk()) {
message.attachments[0].safeFile = result.value
}
}
setTimeout(() => {
this.scrollToBottomClicked()
}, 100)
});
}
listenToDeleteMessage() {
this.messageDeleteSubject?.unsubscribe();
this.messageDeleteSubject = this.chatServiceService.listenToDeleteMessage(this.roomId).subscribe((deleteMessage) => {
const index = this.messages1[this.roomId].findIndex(e => e?.id === deleteMessage.id); // Use triple equals for comparison
if (index !== -1) { // Check if the item was found
this.messages1[this.roomId].splice(index, 1);
// console.log('removed index', index);
} else {
// console.log('message not found');
}
});
}
listenToUpdateMessage() {
this.messageUpdateSubject?.unsubscribe();
this.messageUpdateSubject = this.chatServiceService.listenToUpdateMessage(this.roomId).subscribe((updateMessage) => {
const index = this.messages1[this.roomId].findIndex(e => e?.id === updateMessage.id); // Use triple equals for comparison
if (index !== -1) { // Check if the item was found
this.messages1[this.roomId][index].message = updateMessage.message
this.messages1[this.roomId][index].reactions = updateMessage.reactions
} else {
// console.log('message not found');
}
});
}
listenToSendMessage() {
this.messageSendSubject?.unsubscribe();
this.messageSendSubject = this.chatServiceService.listenToSendMessage(this.roomId).subscribe((updateMessage) => {
const index = this.messages1[this.roomId].findIndex(e => e?.requestId === updateMessage.requestId); // Use triple equals for comparison
if (index !== -1) { // Check if the item was found
this.messages1[this.roomId][index].id = updateMessage.id
let attachmentIndex = 0;
for(const message of updateMessage.attachments) {
this.messages1[this.roomId][index].attachments[attachmentIndex].id = message.id
attachmentIndex++;
}
} else {
// console.log('message not found');
}
});
}
2024-08-16 11:26:31 +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
load() {
2021-01-27 16:01:49 +01:00
this.getChatMembers();
2021-01-13 10:02:30 +01:00
}
2021-07-26 09:55:57 +01:00
doRefresh(ev: any) {
2021-01-27 16:01:49 +01:00
this.load();
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
}
2022-08-03 16:31:03 +01:00
async goToEvent(event: any) {
2023-09-29 16:40:50 +01:00
let classs;
if (window.innerWidth < 701) {
classs = 'modal modal-desktop'
} else {
classs = 'modal modal-desktop showAsideOptions'
}
const modal = await this.modalController.create({
component: ViewEventPage,
componentProps: {
2022-08-03 16:31:03 +01:00
eventId: event.id,
CalendarId: event.calendarId
},
cssClass: classs,
});
await modal.present();
modal.onDidDismiss().then((res) => {
2022-07-06 17:02:42 +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;
2021-09-24 16:10:24 +01:00
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() {
window.removeEventListener('scroll', this.scrollChangeCallback, true);
}
2023-02-02 18:44:19 +01:00
sendMessage() {
2024-08-13 10:52:35 +01:00
const message = new MessageEntity();
message.message = this.textField
message.roomId = this.roomId
message.sender = {
userPhoto: '',
wxeMail: SessionStore.user.Email,
wxFullName: SessionStore.user.FullName,
wxUserId: SessionStore.user.UserId
}
this.chatServiceService.sendMessage(message)
2024-08-16 11:26:31 +01:00
this.messages1[this.roomId].push(message)
2024-08-13 10:52:35 +01:00
this.textField = ''
2023-02-02 18:24:26 +01:00
}
2022-03-04 14:51:08 +01:00
async sendAudio(fileName) {
const roomId = this.roomId
2022-03-21 21:09:05 +01:00
let audioFile;
2024-08-16 11:26:31 +01:00
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
2024-08-16 11:26:31 +01:00
const audioMimeType: string = recordData.value.mimeType
2022-04-04 00:37:00 +01:00
//Converting base64 to blob
const encodedData = btoa(this.audioRecorded);
2022-07-06 17:02:42 +01:00
2024-08-13 10:52:35 +01:00
const message = new MessageEntity();
message.roomId = this.roomId
2024-08-13 10:52:35 +01:00
message.sender = {
userPhoto: '',
wxeMail: SessionStore.user.Email,
wxFullName: SessionStore.user.FullName,
wxUserId: SessionStore.user.UserId
}
message.attachments = [{
file: encodedData,
fileName: "audio",
source: MessageAttachmentSource.Device,
2024-08-16 11:26:31 +01:00
fileType: MessageAttachmentFileType.Audio,
2024-08-16 17:17:32 +01:00
mimeType: audioMimeType,
safeFile: this.sanitiser.bypassSecurityTrustResourceUrl(this.audioRecorded)
2024-08-13 10:52:35 +01:00
}]
2024-08-16 11:26:31 +01:00
this.messages1[this.roomId].push(message)
2024-08-13 10:52:35 +01:00
this.chatServiceService.sendMessage(message)
2024-08-16 17:17:32 +01:00
setTimeout(() => {
this.scrollToBottomClicked()
}, 100)
2022-03-04 14:51:08 +01:00
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
}
2022-04-06 16:25:47 +01:00
viewDocument(file: any, url?: string) {
2022-07-06 17:02:42 +01:00
2022-04-06 16:25:47 +01:00
if (file.type == "application/webtrix") {
this.openViewDocumentModal(file);
2022-02-25 15:10:10 +01:00
}
2022-04-06 16:25:47 +01:00
else {
let fullUrl = "https://www.tabularium.pt" + url;
this.fileService.viewDocumentByUrl(fullUrl);
}
}
2022-03-14 08:09:33 +01:00
docIndex(index: number) {
2021-10-09 16:41:18 +01:00
this.dicIndex = index
}
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-08-16 17:17:32 +01:00
getChatMembers() {}
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({
component: MessagesOptionsPage,
2021-01-13 10:02:30 +01:00
componentProps: {
2021-03-12 11:56:54 +01:00
roomId: this.roomId,
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
});
return await popover.present();
}
async addContacts() {
2020-12-30 11:13:50 +01:00
const modal = await this.modalController.create({
component: ContactsPage,
2021-07-23 14:43:51 +01:00
componentProps: {},
2020-12-30 11:13:50 +01:00
cssClass: 'contacts',
backdropDismiss: false
});
2023-07-15 11:01:09 +01:00
2020-12-30 11:13:50 +01:00
modal.onDidDismiss();
2023-07-15 11:01:09 +01:00
await modal.present();
2020-12-30 11:13:50 +01:00
}
2021-09-21 14:05:59 +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-16 17:17:32 +01:00
this.addFileToChatMobile()
2023-09-29 16:40:50 +01:00
2022-02-04 00:22:35 +01:00
2023-03-14 09:50:40 +01:00
}
2023-10-19 15:03:12 +01:00
dataURItoBlob(dataURI) {
2023-03-14 09:50:40 +01:00
// convert base64 to raw binary data held in a string
// doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
var byteString = atob(dataURI.split(',')[1]);
2023-09-29 16:40:50 +01:00
2023-03-14 09:50:40 +01:00
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
2023-09-29 16:40:50 +01:00
2023-03-14 09:50:40 +01:00
// write the bytes of the string to an ArrayBuffer
var ab = new ArrayBuffer(byteString.length);
2023-09-29 16:40:50 +01:00
2023-03-14 09:50:40 +01:00
// create a view into the buffer
var ia = new Uint8Array(ab);
2023-09-29 16:40:50 +01:00
2023-03-14 09:50:40 +01:00
// set the bytes of the buffer to the correct values
for (var i = 0; i < byteString.length; i++) {
2023-10-19 15:03:12 +01:00
ia[i] = byteString.charCodeAt(i);
2023-03-14 09:50:40 +01:00
}
2023-09-29 16:40:50 +01:00
2023-03-14 09:50:40 +01:00
// write the ArrayBuffer to a blob, and you're done
2023-10-19 15:03:12 +01:00
var blob = new Blob([ab], { type: mimeString });
2023-03-14 09:50:40 +01:00
return blob;
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-02-04 08:11:49 +01:00
const roomId = this.roomId
2022-02-03 21:01:53 +01:00
2022-03-03 08:21:22 +01:00
if (data.selected) {
2022-02-03 21:01:53 +01:00
2024-08-13 10:52:35 +01:00
const message = new MessageEntity();
message.message = this.textField
message.roomId = this.roomId
message.sender = {
userPhoto: '',
wxeMail: SessionStore.user.Email,
wxFullName: SessionStore.user.FullName,
wxUserId: SessionStore.user.UserId
}
message.attachments = [{
fileName: res.data.selected.Assunto,
source: MessageAttachmentSource.Webtrix,
fileType: MessageAttachmentFileType.Doc,
applicationId: res.data.selected.ApplicationType,
docId: res.data.selected.Id,
}]
2024-08-16 11:26:31 +01:00
this.messages1[this.roomId].push(message)
2024-08-13 10:52:35 +01:00
this.chatServiceService.sendMessage(message)
this.textField = ''
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-08-13 10:52:35 +01:00
const file = await this.FilePickerService.getPicture({
cameraResultType: CameraResultType.Base64
})
2022-07-06 17:02:42 +01:00
2024-08-13 10:52:35 +01:00
if(file.isOk()) {
2022-02-08 15:45:09 +01:00
2024-08-13 10:52:35 +01:00
var base64 = 'data:image/jpeg;base64,' + file.value.base64String
if (file.value.format == "jpeg" || file.value.format == "png" || file.value.format == "gif") {
2023-09-29 16:40:50 +01:00
2024-08-13 10:52:35 +01:00
const compressedImage = await compressImageBase64(
base64,
800, // maxWidth
800, // maxHeight
0.9 // quality
)
2023-08-28 16:36:31 +01:00
2024-08-13 10:52:35 +01:00
if(compressedImage.isOk()) {
2022-02-08 15:45:09 +01:00
2024-08-13 10:52:35 +01:00
const message = new MessageEntity();
message.roomId = this.roomId
message.sender = {
userPhoto: '',
wxeMail: SessionStore.user.Email,
wxFullName: SessionStore.user.FullName,
wxUserId: SessionStore.user.UserId
}
message.attachments = [{
file: compressedImage.value,
fileName: "foto",
source: MessageAttachmentSource.Device,
2024-08-16 11:26:31 +01:00
fileType: MessageAttachmentFileType.Image,
mimeType: 'image/'+file.value.format
2024-08-13 10:52:35 +01:00
}]
2024-08-16 11:26:31 +01:00
this.messages1[this.roomId].push(message)
2024-08-13 10:52:35 +01:00
this.chatServiceService.sendMessage(message)
}
}
2024-08-16 11:26:31 +01:00
} else {
2024-08-16 12:24:26 +01:00
Logger.error('failed to pick picture from the device', {
error: file.error
})
2024-08-13 10:52:35 +01:00
}
2022-02-08 15:45:09 +01:00
}
2024-08-16 17:17:32 +01:00
async addFileToChatMobile() {
const roomId = this.roomId
const file = await Camera.getPhoto({
quality: 90,
// allowEditing: true,
resultType: CameraResultType.Base64,
source: CameraSource.Photos
});
//const imageData = await this.fileToBase64Service.convert(file)
//
var imageBase64 = 'data:image/jpeg;base64,' + file.base64String
const compressedImage = await this.compressImageBase64(
imageBase64,
800, // maxWidth
800, // maxHeight
0.9 // quality
).then((picture) => {
imageBase64 = picture
});
//console.log(imageBase64)
const response = await fetch(imageBase64);
const blob = await response.blob();
const formData = new FormData();
//console.log('add file', formData)
formData.append("blobFile", blob);
//console.log('add file', formData)
// this.ChatSystemService.getDmRoom(roomId).send({
// file: {
// "type": "application/img",
// "guid": ''
// },
// temporaryData: formData,
// attachments: [{
// "title": file.path,
// "text": "description",
// "title_link_download": false,
// }],
// attachmentsModelData: {
// fileBase64: imageBase64,
// }
// })
}
2024-08-13 10:52:35 +01:00
2022-03-27 18:54:08 +01:00
getFileReader(): FileReader {
const fileReader = new FileReader();
const zoneOriginalInstance = (fileReader as any)["__zone_symbol__originalInstance"];
return zoneOriginalInstance || fileReader;
2022-04-04 00:37:00 +01:00
}
2022-02-03 21:01:53 +01:00
2023-08-16 08:01:45 +01:00
_getBase64(file) {
2023-10-19 15:03:12 +01:00
return new Promise((resolve, reject) => {
2023-08-16 08:01:45 +01:00
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
resolve(reader.result)
};
reader.onerror = function (error) {
console.log('Error: ', error);
};
})
2023-10-19 15:03:12 +01:00
}
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 ')
2022-02-04 08:11:49 +01:00
const roomId = this.roomId
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 ')
const resultt = await FilePicker.pickFiles({
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-03-12 16:45:08 +01:00
console.log('RESULT', resultt.files[0].data)
2024-03-11 11:18:32 +01:00
2024-08-13 10:52:35 +01:00
const message = new MessageEntity();
message.roomId = this.roomId
message.sender = {
userPhoto: '',
wxeMail: SessionStore.user.Email,
wxFullName: SessionStore.user.FullName,
wxUserId: SessionStore.user.UserId
}
message.attachments = [{
file: resultt.files[0].data,
fileName: resultt.files[0].name,
source: MessageAttachmentSource.Device,
fileType: MessageAttachmentFileType.Doc
}]
2024-08-16 11:26:31 +01:00
this.messages1[this.roomId].push(message)
2024-08-13 10:52:35 +01:00
this.chatServiceService.sendMessage(message)
2024-03-11 11:18:32 +01:00
return
2024-03-12 16:45:08 +01:00
2024-03-11 11:18:32 +01:00
}
2024-08-16 11:26:31 +01:00
const file = await this.fileService.getFileFromDevice(types);
2023-08-30 18:41:13 +01:00
console.log(file)
2022-04-04 00:37:00 +01:00
2022-07-06 17:02:42 +01:00
2024-03-12 16:45:08 +01:00
const fileName = file.name
2023-08-31 13:07:52 +01:00
2024-03-12 16:45:08 +01:00
const validation = this.FileValidatorService.fileNameValidation(fileName)
2022-03-27 18:54:08 +01:00
2024-03-12 16:45:08 +01:00
if (validation.isOk) {
2022-02-04 00:22:35 +01:00
2024-03-12 16:45:08 +01:00
const encodedData = btoa(JSON.stringify(await this.getBase64(file).catch((error) => {
console.error(error);
})));
console.log(encodedData)
const blob = this.fileService.base64toBlob(encodedData, file.type)
2022-03-25 09:25:05 +01:00
2024-03-12 16:45:08 +01:00
const formData = new FormData();
formData.append('blobFile', blob);
2024-08-13 10:52:35 +01:00
const message = new MessageEntity();
message.roomId = this.roomId
message.sender = {
userPhoto: '',
wxeMail: SessionStore.user.Email,
wxFullName: SessionStore.user.FullName,
wxUserId: SessionStore.user.UserId
}
message.attachments = [{
file: encodedData,
2024-08-16 11:26:31 +01:00
fileName: file.name,
2024-08-13 10:52:35 +01:00
source: MessageAttachmentSource.Device,
2024-08-16 11:26:31 +01:00
fileType: MessageAttachmentFileType.Doc,
mimeType: file.type
2024-08-13 10:52:35 +01:00
}]
2024-08-16 11:26:31 +01:00
this.messages1[this.roomId].push(message)
2024-08-13 10:52:35 +01:00
this.chatServiceService.sendMessage(message)
2024-03-12 16:45:08 +01:00
} else {
this.toastService._badRequest("Ficheiro inválido")
}
2022-03-25 09:25:05 +01:00
2022-03-27 18:54:08 +01:00
}
2022-02-04 00:22:35 +01:00
2022-03-27 18:54:08 +01:00
getBase64(file) {
var reader = this.getFileReader();
reader.readAsDataURL(file);
return new Promise(resolve => {
reader.onload = function () {
resolve(reader.result)
};
reader.onerror = function (error) {
2022-07-06 17:02:42 +01:00
2022-03-27 18:54:08 +01:00
};
});
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) {
2021-10-07 15:30:36 +01:00
const roomId = this.roomId
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: {
room: this.roomId,
members: this.members,
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
2024-08-16 11:26:31 +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
b64toBlob(b64Data, contentType) {
2022-04-04 00:37:00 +01:00
contentType = contentType || '';
var sliceSize = 512;
b64Data = b64Data.replace(/^[^,]+,/, '');
b64Data = b64Data.replace(/\s/g, '');
var byteCharacters = window.atob(b64Data);
2022-04-04 00:37:00 +01:00
var byteArrays = [];
2022-04-04 00:37:00 +01:00
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
var slice = byteCharacters.slice(offset, offset + sliceSize);
2022-04-04 00:37:00 +01:00
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
2022-04-04 00:37:00 +01:00
var byteArray = new Uint8Array(byteNumbers);
2022-04-04 00:37:00 +01:00
byteArrays.push(byteArray);
}
2022-04-04 00:37:00 +01:00
var blob = new Blob(byteArrays, { type: contentType });
return blob;
2021-12-17 17:20:43 +01:00
}
2021-11-23 10:57:07 +01:00
2024-03-12 16:45:08 +01:00
blobToBase64(blob) {
return new Promise((resolve, _) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.readAsDataURL(blob);
});
}
2023-08-20 22:05:46 +01:00
async openFile(pdfString, filename, type) {
2024-03-12 16:45:08 +01:00
2024-03-14 09:10:17 +01:00
console.log('url while open ',pdfString)
2024-03-18 11:13:34 +01:00
/* const modal = await this.modalController.create({
2024-03-14 09:10:17 +01:00
component: ViewDocumentSecondOptionsPage,
componentProps: {
fileUrl: pdfString,
filename: filename
},
cssClass: 'modal modal-desktop'
});
await modal.present();
2024-03-26 12:03:30 +01:00
/*
2024-03-18 11:13:34 +01:00
await modal.present(); */
2024-03-12 16:45:08 +01:00
var blob = new Blob([pdfString], { type: 'application/pdf' });
console.log('blob blob', blob)
this.blobToBase64(blob).then((value) => {
console.log(value)
}).catch((error) => {
console.log(error)
})
2022-07-06 17:02:42 +01:00
let pathFile = ''
const fileName = filename
if (this.platform.is('ios')) {
pathFile = this.file.documentsDirectory
} else {
pathFile = this.file.externalRootDirectory
}
2023-09-29 16:40:50 +01:00
2024-03-12 16:45:08 +01:00
console.log('file data', pdfString)
2023-08-20 18:09:10 +01:00
console.log(pathFile)
2023-08-20 22:05:46 +01:00
2024-03-12 16:45:08 +01:00
let removePre = this.removeTextBeforeSlash(pdfString,',')
console.log('file data remove ', removePre)
2023-08-20 22:05:46 +01:00
await Filesystem.writeFile({
path: fileName,
2024-03-12 16:45:08 +01:00
data: removePre,
2024-03-11 13:21:44 +01:00
directory: Directory.Cache,
2023-08-20 22:05:46 +01:00
}).then((dir) => {
console.log('DIR ', dir)
this.fileOpener
2023-10-19 15:03:12 +01:00
.open(dir.uri, type)
.then(() => console.log())
.catch(e => console.error(e))
2024-03-11 13:21:44 +01:00
}).catch((error) => {
console.log('error writing the file', error)
2024-03-18 11:13:34 +01:00
});
2022-03-09 09:23:27 +01:00
}
2024-03-12 16:45:08 +01:00
removeTextBeforeSlash(inputString, controlString) {
if (inputString.includes(controlString)) {
const parts = inputString.split(controlString);
return parts.length > 1 ? parts[1] : inputString;
} else {
return inputString;
}
}
2022-04-06 16:25:47 +01:00
downloadFileFromBrowser(fileName: string, data: any): void {
const linkSource = data;
const downloadLink = document.createElement("a");
downloadLink.href = linkSource;
downloadLink.download = fileName;
downloadLink.click();
}
2022-04-04 00:37:00 +01:00
2021-11-17 15:34:15 +01:00
async openPreview(msg) {
2022-01-21 16:55:05 +01:00
2022-07-06 17:02:42 +01:00
if (msg.file.type === "application/webtrix") {
2022-04-06 16:25:47 +01:00
this.viewDocument(msg.file, msg.attachments.image_url)
2022-01-21 16:55:05 +01:00
} else {
2022-04-06 16:25:47 +01:00
if (!msg.attachments[0].image_url || msg.attachments[0].image_url === null || msg.attachments[0].image_url === '') {
2024-08-16 18:06:27 +01:00
// this.downloadFileMsg(msg)
// this.testDownlod(msg)
2022-04-07 15:22:25 +01:00
} else {
2022-04-06 16:25:47 +01:00
var str = msg.attachments[0].image_url;
str = str.substring(1, ((str.length) - 1));
2022-04-07 15:22:25 +01:00
2022-04-06 16:25:47 +01:00
if (this.platform.is('desktop') || this.platform.is('mobileweb')) {
2022-04-07 15:22:25 +01:00
2022-07-06 09:59:22 +01:00
console.log(msg)
2022-09-23 11:23:24 +01:00
2022-07-06 09:59:22 +01:00
if (msg.file.type == "application/img") {
const modal = await this.modalController.create({
component: ViewMediaPage,
cssClass: 'modal modal-desktop',
componentProps: {
image: msg.attachments[0].image_url,
type: msg.file.type,
username: msg.u.name,
_updatedAt: msg._updatedAt
}
});
modal.present();
} else {
2022-09-23 11:23:24 +01:00
2022-07-06 09:59:22 +01:00
this.downloadFileFromBrowser("file", str)
2023-03-28 16:11:59 +01:00
this.downloadFileFromBrowser(msg.attachments[0].title, str)
2022-09-23 11:23:24 +01:00
2023-03-28 16:11:59 +01:00
this.downloadFileFromBrowser(msg.attachments[0].title, str)
2022-09-23 11:34:38 +01:00
2022-07-06 09:59:22 +01:00
}
2022-04-07 15:22:25 +01:00
2022-04-06 16:25:47 +01:00
} else {
2022-07-06 17:02:42 +01:00
if (msg.file.type == "application/img") {
const modal = await this.modalController.create({
component: ViewMediaPage,
cssClass: 'modal modal-desktop',
componentProps: {
image: msg.attachments[0].image_url,
type: msg.file.type,
username: msg.u.name,
_updatedAt: msg._updatedAt
}
});
modal.present();
} else {
2024-03-14 09:10:17 +01:00
this.openFile(msg.attachments[0].image_url, msg.attachments[0].title, msg.file.type);
2022-07-06 17:02:42 +01:00
}
2022-04-06 16:25:47 +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
2023-08-28 16:36:31 +01:00
async compressImageBase64(base64String: string, maxWidth: number, maxHeight: number, quality: number): Promise<string> {
return new Promise((resolve, reject) => {
const image = new (window as any).Image();
image.src = base64String;
2023-09-29 16:40:50 +01:00
2023-08-28 16:36:31 +01:00
image.onload = async () => {
const canvas = document.createElement('canvas');
let newWidth = image.width;
let newHeight = image.height;
2023-09-29 16:40:50 +01:00
2023-08-28 16:36:31 +01:00
if (newWidth > maxWidth) {
newHeight *= maxWidth / newWidth;
newWidth = maxWidth;
}
2023-09-29 16:40:50 +01:00
2023-08-28 16:36:31 +01:00
if (newHeight > maxHeight) {
newWidth *= maxHeight / newHeight;
newHeight = maxHeight;
}
2023-09-29 16:40:50 +01:00
2023-08-28 16:36:31 +01:00
canvas.width = newWidth;
canvas.height = newHeight;
2023-09-29 16:40:50 +01:00
2023-08-28 16:36:31 +01:00
const context = canvas.getContext('2d');
context?.drawImage(image, 0, 0, newWidth, newHeight);
2023-09-29 16:40:50 +01:00
2023-08-28 16:36:31 +01:00
const compressedBase64 = canvas.toDataURL('image/jpeg', quality);
resolve(compressedBase64);
};
2023-09-29 16:40:50 +01:00
2023-08-28 16:36:31 +01:00
image.onerror = (error) => {
reject(error);
};
});
}
2024-03-11 11:18:32 +01:00
dataURItoBlobIso(dataURI: any) {
const byteString = window.atob(dataURI);
const arrayBuffer = new ArrayBuffer(byteString.length);
const int8Array = new Uint8Array(arrayBuffer);
for (let i = 0; i < byteString.length; i++) {
int8Array[i] = byteString.charCodeAt(i);
}
2024-03-12 16:45:08 +01:00
const blob = new Blob([int8Array], { type: 'application/pdf' });
2024-03-11 11:18:32 +01:00
return blob;
2024-03-12 16:45:08 +01:00
}
2024-03-11 11:18:32 +01:00
2024-08-08 11:44:41 +01:00
2024-08-09 10:50:32 +01:00
messageDelete(message: MessageEntity) {
2024-08-08 11:44:41 +01:00
this.chatServiceService.messageDelete({
2024-08-09 10:50:32 +01:00
messageId: message.id,
2024-08-08 11:44:41 +01:00
roomId: this.roomId,
})
}
async editMessage(message: any) {
const modal = await this.modalController.create({
component: EditMessagePage,
cssClass: '',
componentProps: {
message: message.message,
roomId: this.roomId,
}
});
modal.present()
return modal.onDidDismiss().then((res) => {
this.chatServiceService.updateMessage({
memberId: SessionStore.user.UserId,
message: res.data.message,
messageId: message.id,
requestId: '',
roomId: this.roomId
})
});
}
toggleEmojiPicker(message: any) {
if (this.selectedMessage === message) {
this.selectedMessage = null; // Close the picker if it's already open
} else {
this.selectedMessage = message; // Open the picker for the selected message
}
}
addReaction(message: any, emoji: string) {
// Logic to add reaction to the message
console.log(`Reacting to message ${message.id} with emoji ${emoji.codePointAt(0).toString(16)}`);
this.selectedMessage = null; // Close the picker after adding reaction
this.chatServiceService.reactToMessage({
memberId: SessionStore.user.UserId,
messageId: message.id,
roomId: this.roomId,
reaction: emoji,
requestId: ''
})
}
sendTyping() {
this.userTypingServiceRepository.addUserTyping(this.roomId)
}
2021-07-23 14:43:51 +01:00
}