Files
doneit-web/src/app/pages/chat/group-messages/group-messages.page.ts
T

1259 lines
33 KiB
TypeScript
Raw Normal View History

2022-04-26 16:14:55 +01:00
import { Component, ElementRef, OnInit, ViewChild, AfterViewInit, OnDestroy, ChangeDetectorRef, } from '@angular/core';
2023-07-14 18:57:07 +01:00
import { ModalController, NavParams, PopoverController, Platform } from '@ionic/angular';
2021-04-13 14:14:55 +01:00
import { AlertService } from 'src/app/services/alert.service';
2021-01-19 16:10:40 +01:00
import { ChatService } from 'src/app/services/chat.service';
2020-12-29 12:40:19 +01:00
import { ChatOptionsPopoverPage } from 'src/app/shared/popover/chat-options-popover/chat-options-popover.page';
2020-12-28 10:11:00 +01:00
import { ChatPopoverPage } from 'src/app/shared/popover/chat-popover/chat-popover.page';
2021-01-21 16:27:04 +01:00
import { GroupContactsPage } from './group-contacts/group-contacts.page';
2022-03-03 08:21:22 +01:00
import { Router } from '@angular/router'
2021-08-17 14:17:19 +01:00
import { EditGroupPage } from '../edit-group/edit-group.page';
2021-09-06 16:53:58 +01:00
import { TimeService } from 'src/app/services/functions/time.service';
2021-09-14 10:30:13 +01:00
import { FileService } from 'src/app/services/functions/file.service';
2021-09-14 10:48:28 +01:00
import { ToastService } from 'src/app/services/toast.service';
2021-09-21 14:05:59 +01:00
import { environment } from 'src/environments/environment';
import { NewEventPage } from '../../agenda/new-event/new-event.page';
import { EventPerson } from 'src/app/models/eventperson.model';
import { ViewDocumentPage } from 'src/app/modals/view-document/view-document.page';
2021-10-23 09:53:21 +01:00
import { ThemeService } from 'src/app/services/theme.service'
import { ViewEventPage } from 'src/app/modals/view-event/view-event.page';
2022-09-30 15:13:36 +01:00
import { ChatSystemService } from 'src/app/services/chat/chat-system.service';
2022-02-03 21:01:53 +01:00
import { FileType } from 'src/app/models/fileType';
import { Storage } from '@ionic/storage';
2022-02-04 07:42:43 +01:00
2022-02-03 21:01:53 +01:00
import { CameraService } from 'src/app/services/camera.service';
import { SearchPage } from 'src/app/pages/search/search.page';
2022-04-26 16:53:10 +01:00
import { VoiceRecorder, RecordingData, GenericResponse } from 'capacitor-voice-recorder';
import { Filesystem, Directory } from '@capacitor/filesystem';
2022-03-18 11:45:38 +01:00
import { DomSanitizer } from '@angular/platform-browser';
2022-03-22 16:11:30 +01:00
import { MessageService } from 'src/app/services/chat/message.service';
import { File } from '@awesome-cordova-plugins/file/ngx';
import { FileOpener } from '@awesome-cordova-plugins/file-opener/ngx';
2022-04-19 14:48:22 +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';
2023-08-16 08:01:45 +01:00
import { Camera, CameraResultType, CameraSource } from '@capacitor/camera';
2023-10-19 15:03:12 +01:00
import { RouteService } from 'src/app/services/route.service';
2020-12-28 10:11:00 +01:00
@Component({
selector: 'app-group-messages',
templateUrl: './group-messages.page.html',
styleUrls: ['./group-messages.page.scss'],
})
2021-08-23 16:31:06 +01:00
export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy {
2021-11-17 15:34:15 +01:00
2021-01-27 16:01:49 +01:00
showLoader: boolean;
2022-03-03 08:21:22 +01:00
isGroupCreated: boolean;
2021-01-19 16:10:40 +01:00
loggedUser: any;
2021-01-21 16:27:04 +01:00
2022-03-03 08:21:22 +01:00
room: any;
roomName: any;
2023-02-14 10:40:05 +01:00
members: any = []
2021-03-12 11:56:54 +01:00
contacts: string[] = [" Ana M.", "Andre F.", "Bruno G.", "Catarina T", "Tiago"];
2022-03-03 08:21:22 +01:00
allUsers: any[] = [];
2020-12-28 10:11:00 +01:00
roomId: string;
2022-03-03 08:21:22 +01:00
loggedUserChat: any;
2021-08-18 09:23:53 +01:00
eventSelectedDate: Date = new Date();
2022-03-03 08:21:22 +01:00
scrollingOnce: boolean = true;
2021-08-23 16:31:06 +01:00
private scrollChangeCallback: () => void;
currentPosition: any;
startPosition: number;
2022-03-03 08:21:22 +01:00
capturedImage: any;
capturedImageTitle: any;
2021-09-21 14:05:59 +01:00
attendees: EventPerson[] = [];
2021-09-24 15:39:25 +01:00
scrollToBottomBtn = false;
2021-09-13 12:37:58 +01:00
2021-09-30 12:23:22 +01:00
longPressActive = false;
showMessageOptions = false;
2022-03-03 08:21:22 +01:00
selectedMsgId: string;
roomCountDownDate: any;
2022-01-13 17:28:35 +01:00
downloadFile: any;
2022-06-29 16:45:39 +01:00
groupNameFormart = "";
2021-09-30 12:23:22 +01:00
2021-07-26 14:09:26 +01:00
@ViewChild('scrollMe') private myScrollContainer: ElementRef;
2022-03-14 17:11:00 +01:00
recording = false;
allowTyping = true;
storedFileNames = [];
lastAudioRecorded = '';
audioRecorded: any = "";
audioDownloaded: any = "";
2022-03-14 17:11:00 +01:00
durationDisplay = '';
duration = 0;
2022-03-18 16:29:10 +01:00
showAvatar = true;
2023-10-11 16:20:42 +01:00
audioPermissionStatus: 'granted' | 'denied' | 'prompt' | null = null
2022-04-19 16:03:59 +01:00
sessionStore = SessionStore
2022-07-01 14:28:57 +01:00
isAdmin = false;
2020-12-28 10:11:00 +01:00
constructor(
2020-12-29 12:40:19 +01:00
private modalController: ModalController,
2020-12-28 10:11:00 +01:00
public popoverController: PopoverController,
2021-01-19 16:10:40 +01:00
private chatService: ChatService,
2021-03-12 11:56:54 +01:00
private navParams: NavParams,
2021-04-13 14:14:55 +01:00
private alertService: AlertService,
2021-09-06 16:53:58 +01:00
private route: Router,
private timeService: TimeService,
2021-09-14 10:30:13 +01:00
private fileService: FileService,
2021-09-14 10:48:28 +01:00
private toastService: ToastService,
2021-11-17 15:34:15 +01:00
public ThemeService: ThemeService,
2022-01-13 17:28:35 +01:00
private changeDetectorRef: ChangeDetectorRef,
private platform: Platform,
2022-09-30 15:13:36 +01:00
public ChatSystemService: ChatSystemService,
2022-02-03 21:01:53 +01:00
private storage: Storage,
private CameraService: CameraService,
2022-03-18 11:45:38 +01:00
private sanitiser: DomSanitizer,
private file: File,
private fileOpener: FileOpener,
2023-10-19 15:03:12 +01:00
public RouteService: RouteService,
2021-01-21 16:27:04 +01:00
) {
2023-09-22 17:25:27 +01:00
this.ChatSystemService.getUser()
2022-10-12 17:01:09 +01:00
this.loggedUserChat = SessionStore.user.ChatData['data'];
2021-01-21 16:27:04 +01:00
this.isGroupCreated = true;
this.roomId = this.navParams.get('roomId');
2021-09-03 17:02:42 +01:00
window.onresize = (event) => {
2022-03-03 08:21:22 +01:00
if (window.innerWidth > 701) {
2021-09-03 17:02:42 +01:00
this.modalController.dismiss();
}
};
2022-01-14 14:58:47 +01:00
2023-10-11 16:20:42 +01:00
this.open();
2022-02-09 15:09:47 +01:00
2022-03-03 08:21:22 +01:00
setTimeout(() => {
2022-01-14 14:58:47 +01:00
this.scrollToBottomClicked()
}, 50)
2022-03-18 16:29:10 +01:00
this.showAvatar = false
setTimeout(() => {
this.scrollToBottomClicked()
this.showAvatar = true
}, 150)
2021-01-19 16:10:40 +01:00
}
2020-12-28 10:11:00 +01:00
2023-10-11 16:20:42 +01:00
open() {
try {
this.ChatSystemService.getGroupRoom(this.roomId).loadHistory({});
this.ChatSystemService.getGroupRoom(this.roomId).scrollDown = this.scrollToBottomClicked;
this.ChatSystemService.openRoom(this.roomId)
this.groupNameFormart = this.ChatSystemService.getGroupRoom(this.roomId).name.split('-').join(' ')
} catch (error) {
setTimeout(() => {
this.open()
}, 3000)
}
}
2020-12-28 10:11:00 +01:00
ngOnInit() {
2023-09-22 17:25:27 +01:00
2022-03-03 08:21:22 +01:00
this.loggedUser = this.loggedUserChat;
this.getRoomInfo();
2021-07-26 14:34:52 +01:00
this.scrollToBottom();
2021-07-26 19:31:19 +01:00
this.serverLongPull();
2021-08-23 16:31:06 +01:00
this.setStatus('online');
2021-09-09 11:47:49 +01:00
this.getChatMembers();
2022-01-13 17:28:35 +01:00
2023-06-11 13:36:27 +01:00
// this.getRoomMessageDB(this.roomId);
2023-02-14 10:40:05 +01:00
this.getGroupContacts({});
2023-09-22 17:25:27 +01:00
2021-08-23 16:31:06 +01:00
}
2022-03-03 08:21:22 +01:00
setStatus(status: string) {
2021-08-23 16:31:06 +01:00
let body = {
message: '',
status: status,
}
2022-04-07 14:40:37 +01:00
// this.chatService.setUserStatus(body).subscribe(res => {
2023-09-22 17:25:27 +01:00
//
2022-04-07 14:40:37 +01:00
// })
2021-08-23 16:31:06 +01:00
}
2022-03-03 08:21:22 +01:00
deleteMessage(msgId: string) {
2022-09-30 15:13:36 +01:00
const room = this.ChatSystemService.getGroupRoom(this.roomId)
2022-03-14 20:44:41 +01:00
this.alertService.confirmDeleteMessage(msgId, room);
2021-09-30 12:23:22 +01:00
}
2021-08-23 16:31:06 +01:00
ngAfterViewInit() {
this.scrollChangeCallback = () => this.onContentScrolled(event);
window.addEventListener('scroll', this.scrollChangeCallback, true);
2022-08-09 17:01:44 +01:00
2023-10-11 16:20:42 +01:00
if (this.room?.customFields?.countDownDate) {
2022-08-09 17:01:44 +01:00
this.roomCountDownDate = this.timeService.countDownDate(this.room.customFields.countDownDate, this.room._id);
}
2021-08-23 16:31:06 +01:00
}
2022-03-03 08:21:22 +01:00
handlePress(id?: string) {
2021-09-30 12:23:22 +01:00
this.selectedMsgId = id;
this.showMessageOptions = true;
}
2022-03-03 08:21:22 +01:00
handleClick() {
2021-09-30 12:23:22 +01:00
this.showMessageOptions = false;
this.selectedMsgId = "";
}
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;
}
2022-03-03 08:21:22 +01:00
if ((containerHeight - 100) > scroll) {
2021-09-24 15:39:25 +01:00
this.scrollToBottomBtn = true;
}
2022-03-03 08:21:22 +01:00
else {
2021-09-24 15:39:25 +01:00
this.scrollToBottomBtn = false;
}
2021-08-23 16:31:06 +01:00
this.currentPosition = scroll;
}
2022-03-14 17:11:00 +01:00
calculateDuration() {
if (!this.recording) {
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');
this.durationDisplay = `${minutes}:${seconds}`;
setTimeout(() => {
this.calculateDuration();
}, 1000)
}
async getFile(fileName?: any) {
2022-03-14 17:11:00 +01:00
const audioFile = await Filesystem.readFile({
path: fileName,
directory: Directory.Data
})
2022-04-28 09:32:27 +01:00
//
2022-03-14 17:11:00 +01:00
const base64sound = audioFile.data;
//Converting base64 to blob
const base64 = await fetch(base64sound);
2022-04-28 09:32:27 +01:00
//
2022-03-14 17:11:00 +01:00
const base64Response = await fetch(`data:audio/ogg;base64,${base64sound}`);
2022-04-28 09:32:27 +01:00
//
2022-03-14 17:11:00 +01:00
this.audioRecorded = base64Response.url;
2023-09-22 17:25:27 +01:00
2022-03-14 17:11:00 +01:00
}
async loadFiles() {
2022-03-18 11:45:38 +01:00
this.storage.get('fileName').then((fileName) => {
this.lastAudioRecorded = fileName;
2022-03-14 17:11:00 +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) => {
2023-09-22 17:25:27 +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
}
2023-10-11 16:20: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
}
});
} catch (error) { }
2022-03-22 14:23:38 +01:00
2022-03-14 17:11:00 +01:00
}
stopRecording() {
2022-03-18 11:45:38 +01:00
this.deleteRecording();
2022-03-14 17:11:00 +01:00
this.allowTyping = false;
2023-09-22 17:25:27 +01:00
2022-03-14 17:11:00 +01:00
if (!this.recording) {
return;
}
this.recording = false;
VoiceRecorder.stopRecording().then(async (result: RecordingData) => {
2023-09-22 17:25:27 +01:00
2022-03-27 15:18:54 +01:00
2022-03-14 17:11:00 +01:00
this.recording = false;
if (result.value && result.value.recordDataBase64) {
const recordData = result.value.recordDataBase64;
2022-04-18 15:27:16 +01:00
//
2022-03-18 16:43:15 +01:00
const fileName = new Date().getTime() + ".mp3";
2022-03-18 11:45:38 +01:00
//Save file
await this.storage.set('fileName', fileName)
this.storage.set('recordData', result).then(() => {
2023-09-22 17:25:27 +01:00
2022-03-24 17:40:14 +01:00
setTimeout(async () => {
this.loadFiles();
}, 1000);
2022-03-14 17:11:00 +01:00
})
}
})
2022-03-27 15:18:54 +01:00
2022-03-14 17:11:00 +01:00
}
2022-04-07 15:22:25 +01:00
async startRecording() {
VoiceRecorder.requestAudioRecordingPermission();
2023-10-11 16:20: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()){
2023-10-11 16:20:42 +01:00
if (this.recording) {
return;
}
this.recording = true;
VoiceRecorder.startRecording();
this.calculateDuration();
2022-04-07 15:22:25 +01:00
//}
}
2023-10-11 16:20: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.');
}
}
2023-10-11 16:20: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!');
2022-03-24 17:40:14 +01:00
}
}
async deleteRecording() {
2022-03-18 11:45:38 +01:00
this.storage.remove('fileName');
this.storage.remove('recordData');
2022-03-14 17:11:00 +01:00
this.allowTyping = true;
this.lastAudioRecorded = '';
this.loadFiles();
}
2022-01-28 16:15:20 +01:00
2022-08-03 16:31:03 +01:00
async goToEvent(event: any) {
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,
});
2023-07-15 11:01:09 +01:00
modal.onDidDismiss().then((res) => {
2023-09-22 17:25:27 +01:00
});
2023-07-15 11:01:09 +01:00
await modal.present();
}
2021-08-23 16:31:06 +01:00
ngOnDestroy() {
window.removeEventListener('scroll', this.scrollChangeCallback, true);
2021-07-26 14:09:26 +01:00
}
scrollToBottom(): void {
try {
2022-03-03 08:21:22 +01:00
if (this.scrollingOnce) {
2021-08-23 16:31:06 +01:00
this.myScrollContainer.nativeElement.scrollTop = this.myScrollContainer.nativeElement.scrollHeight;
//this.scrollingOnce = false;
}
2022-03-03 08:21:22 +01:00
} catch (err) { }
2021-04-13 14:14:55 +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;
2022-03-03 08:21:22 +01:00
} catch (err) { }
2021-09-24 15:39:25 +01:00
}
2022-07-01 14:28:57 +01:00
async getRoomInfo() {
this.showLoader = true;
2022-04-07 14:40:37 +01:00
// this.chatService.getRoomInfo(this.roomId).subscribe(room => {
// this.room = room['room'];
// this.roomName = this.room.name.split('-').join(' ');
// if (this.room.customFields.countDownDate) {
// this.roomCountDownDate = this.timeService.countDownDateTimer(this.room.customFields.countDownDate, this.room._id);
// }
// this.getGroupContacts(this.room);
// this.showLoader = false;
// });
2022-09-30 15:13:36 +01:00
this.ChatSystemService.getGroupRoom(this.roomId).loadHistory({});
2022-07-01 14:28:57 +01:00
let room = await this.chatService.getRoomInfo(this.roomId).toPromise();
// console.log('ROOM',room)
this.room = room['room'];
if (this.room.name) {
2022-10-04 16:50:55 +01:00
try {
this.roomName = this.room.name.split('-').join(' ');
} catch (error) {
this.roomName = this.room.name;
}
2023-09-22 17:25:27 +01:00
2022-07-01 14:28:57 +01:00
}
2023-09-22 17:25:27 +01:00
2022-07-01 14:28:57 +01:00
2023-10-11 16:20:42 +01:00
if (SessionStore.user.ChatData.data.userId == this.room.u._id) {
2022-07-01 14:28:57 +01:00
this.isAdmin = true
} else {
this.isAdmin = false
}
if (this.room.customFields.countDownDate) {
this.roomCountDownDate = this.room.customFields.countDownDate;
}
2021-03-12 11:56:54 +01:00
}
2022-03-03 08:21:22 +01:00
async getChatMembers() {
this.chatService.getAllUsers().subscribe(res => {
2023-09-22 17:25:27 +01:00
2021-09-09 11:47:49 +01:00
2022-04-26 14:34:52 +01:00
this.allUsers = res['users'].filter(data => data.username != SessionStore.user.UserName);
2023-09-22 17:25:27 +01:00
2021-09-09 11:47:49 +01:00
});
}
/* load(){
2021-03-12 11:56:54 +01:00
this.getGroupContacts();
this.loadGroupMessages();
} */
2021-01-26 15:18:03 +01:00
2022-03-03 08:21:22 +01:00
close() {
2021-01-22 15:28:52 +01:00
this.modalController.dismiss();
2022-03-18 11:45:38 +01:00
this.deleteRecording();
2021-01-22 15:28:52 +01:00
}
2021-01-26 15:18:03 +01:00
2022-03-03 08:21:22 +01:00
doRefresh(ev: any) {
this.getRoomInfo();
2021-01-27 16:01:49 +01:00
ev.target.complete();
2021-01-26 15:18:03 +01:00
}
2022-03-03 08:21:22 +01:00
getGroupContacts(room: any) {
2022-09-30 13:24:19 +01:00
// this.showLoader = true;
// //If group is private call getGroupMembers
// if (this.room.t === 'p') {
// this.chatService.getGroupMembers(this.roomId).subscribe(res => {
2023-09-22 17:25:27 +01:00
2022-09-30 13:24:19 +01:00
// this.members = res['members'];
// this.showLoader = false;
// });
// }
// //Otherwise call getChannelMembers for públic groups
// else {
// this.chatService.getChannelMembers(this.roomId).subscribe(res => {
2023-09-22 17:25:27 +01:00
2022-09-30 13:24:19 +01:00
// this.members = res['members'];
// this.showLoader = false;
// });
// }
2022-09-30 15:13:36 +01:00
this.members = this.ChatSystemService.getGroupRoom(this.roomId).members
2021-01-19 16:10:40 +01:00
}
2021-08-19 18:54:50 +01:00
2021-08-20 17:00:48 +01:00
2022-03-03 08:21:22 +01:00
showDateDuration(start: any) {
2021-09-06 16:53:58 +01:00
return this.timeService.showDateDuration(start);
2021-10-28 15:40:41 +01:00
}
2022-03-03 08:21:22 +01:00
countDownDate(date: any, roomId: string) {
2021-11-03 15:35:01 +01:00
this.roomCountDownDate = this.timeService.countDownDate(date, roomId);
return this.timeService.countDownDateTimer(date, roomId);
2021-08-20 17:00:48 +01:00
}
addZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
2022-03-14 17:11:00 +01:00
sendMessage() {
2022-09-30 15:13:36 +01:00
this.ChatSystemService.getGroupRoom(this.roomId).send({})
2022-03-14 17:11:00 +01:00
}
2021-08-20 17:00:48 +01:00
2022-03-21 21:09:05 +01:00
2022-03-14 17:11:00 +01:00
async sendAudio(fileName) {
2022-03-18 11:45:38 +01:00
2022-03-14 17:11:00 +01:00
const roomId = this.roomId
2022-03-21 21:09:05 +01:00
let audioFile;
2023-06-29 16:04:44 +01:00
this.storage.get('recordData').then(async (recordData) => {
2023-09-22 17:25:27 +01:00
2022-03-21 21:09:05 +01:00
audioFile = recordData;
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
}
2023-08-16 08:01:45 +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
}
//Converting base64 to blob
const encodedData = btoa(this.audioRecorded);
2023-06-29 16:04:44 +01:00
const blob = this.fileService.base64toBlob(encodedData, recordData.value.mimeType)
2023-08-16 08:01:45 +01:00
const formData = new FormData();
formData.append("blobFile", blob);
2022-03-14 17:11:00 +01:00
2023-08-16 08:38:09 +01:00
this.ChatSystemService.getGroupRoom(roomId).send({
file: {
"type": "application/audio",
"msDuration": audioFile.value.msDuration,
"mimeType": audioFile.value.mimeType,
},
attachments: [{
"title": fileName,
"title_link_download": true,
"type": "audio"
}],
2023-06-29 16:04:44 +01:00
temporaryData: formData,
attachmentsModelData: {
2023-08-16 08:01:45 +01:00
fileBase64: encodedData,
2023-06-29 16:04:44 +01:00
}
})
2021-08-20 17:00:48 +01:00
});
2022-03-21 21:09:05 +01:00
this.deleteRecording();
2020-12-28 10:11:00 +01:00
}
2022-03-14 17:11:00 +01:00
2021-04-15 23:57:14 +01:00
async openOptions() {
2021-04-21 15:14:43 +01:00
const modal = await this.popoverController.create({
2020-12-28 10:11:00 +01:00
component: ChatPopoverPage,
2023-03-10 11:41:44 +01:00
cssClass: 'popover-bottom',
2021-01-20 13:17:54 +01:00
componentProps: {
2021-04-15 23:57:14 +01:00
roomId: this.roomId,
2022-01-27 14:26:18 +01:00
members: this.members,
2022-07-01 14:28:57 +01:00
isAdmin: this.isAdmin,
2023-03-09 09:09:10 +01:00
isCreated: this.isGroupCreated,
room: this.room,
name: this.room.name,
2021-01-20 13:17:54 +01:00
},
2020-12-28 10:11:00 +01:00
});
2021-04-15 23:57:14 +01:00
await modal.present();
2022-03-03 08:21:22 +01:00
modal.onDidDismiss().then(res => {
if (res.data == 'leave') {
2023-08-08 09:43:26 +01:00
this.close()
this.ChatSystemService.deleteRoom(this.roomId)
2022-09-30 15:13:36 +01:00
//this.ChatSystemService.subscribeToRoomUpdate(this.roomId, this.room);
2021-04-20 17:18:57 +01:00
}
2022-03-03 08:21:22 +01:00
else if (res.data == 'cancel') {
2023-09-22 17:25:27 +01:00
2021-04-21 15:14:43 +01:00
}
2022-03-03 08:21:22 +01:00
else if (res.data == 'edit') {
2021-08-17 14:17:19 +01:00
this.editGroup(this.roomId);
2023-10-11 16:20:42 +01:00
} else if (res.data == 'addUser') {
2023-09-22 17:25:27 +01:00
2023-03-09 16:35:54 +01:00
this.addContacts();
2023-08-08 09:43:26 +01:00
} else if (res.data == 'delete') {
this.close()
this.ChatSystemService.deleteRoom(this.roomId)
2023-09-22 17:25:27 +01:00
}
2021-01-20 16:58:04 +01:00
});
2020-12-28 10:11:00 +01:00
}
2021-04-21 15:14:43 +01:00
2021-09-13 12:37:58 +01:00
2022-03-03 08:21:22 +01:00
viewDocument(file: any, url?: string) {
2021-10-09 20:55:05 +01:00
2022-03-03 08:21:22 +01:00
if (file.type == "application/webtrix") {
2021-10-09 20:55:05 +01:00
this.openViewDocumentModal(file);
}
2022-03-03 08:21:22 +01:00
else {
2021-10-09 20:55:05 +01:00
let fullUrl = "https://www.tabularium.pt" + url;
this.fileService.viewDocumentByUrl(fullUrl);
}
}
2022-03-03 08:21:22 +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,
SourceType: 'DOC',
SourceID: file.DocId,
DispatchNumber: ''
}
}
let doc = {
2022-03-03 08:21:22 +01:00
"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
}
const modal = await this.modalController.create({
component: ViewDocumentPage,
componentProps: {
2021-10-09 20:24:34 +01:00
trustedUrl: '',
file: {
title: file.Assunto,
url: '',
title_link: '',
},
Document: doc,
applicationId: file.ApplicationId,
docId: file.DocId,
folderId: '',
task: task
},
2021-10-09 20:24:34 +01:00
cssClass: 'modal modal-desktop'
});
await modal.present();
2021-10-09 20:24:34 +01:00
2021-09-14 16:34:50 +01:00
}
2021-09-21 14:05:59 +01:00
async bookMeeting() {
2023-02-14 10:40:05 +01:00
this.getGroupContacts({});
2022-03-03 08:21:22 +01:00
this.attendees = this.members.map((val) => {
2021-09-21 14:05:59 +01:00
return {
Name: val.name,
2022-03-03 08:21:22 +01:00
EmailAddress: val.username + "@" + environment.domain,
2021-09-21 14:05:59 +01:00
IsRequired: "true",
}
});
2023-09-22 17:25:27 +01:00
2023-02-14 10:40:05 +01:00
// this.popoverController.dismiss();
const modal = await this.modalController.create({
component: NewEventPage,
componentProps: {
attendees: this.attendees,
},
cssClass: 'modal modal-desktop',
backdropDismiss: false
});
2023-07-15 11:01:09 +01:00
2023-02-14 10:40:05 +01:00
modal.onDidDismiss().then((data) => {
if (data) {
2021-09-21 14:05:59 +01:00
2023-02-14 10:40:05 +01:00
}
});
2023-07-15 11:01:09 +01:00
await modal.present();
2021-09-21 14:05:59 +01:00
}
2022-02-09 15:09:47 +01:00
2022-02-03 21:01:53 +01:00
async takePicture() {
const roomId = this.roomId
2022-02-09 15:09:47 +01:00
2023-08-16 08:01:45 +01:00
const file = await Camera.getPhoto({
quality: 90,
// allowEditing: true,
resultType: CameraResultType.Base64,
source: CameraSource.Camera
});
2023-08-28 16:36:31 +01:00
var imageBase64 = 'data:image/jpeg;base64,' + file.base64String
const compressedImage = await this.compressImageBase64(
imageBase64,
800, // maxWidth
800, // maxHeight
0.9 // quality
).then((picture) => {
2023-09-22 17:34:39 +01:00
// console.log('Selected: ', picture)
2023-08-28 16:36:31 +01:00
imageBase64 = picture
});
2023-08-16 08:01:45 +01:00
const blob = this.dataURItoBlob(imageBase64)
2023-09-22 17:34:39 +01:00
// console.log(imageBase64)
2023-08-16 08:01:45 +01:00
2022-02-07 17:55:00 +01:00
const formData = new FormData();
formData.append("blobFile", blob);
2022-02-03 21:01:53 +01:00
2023-08-16 08:38:09 +01:00
this.ChatSystemService.getGroupRoom(roomId).send({
2022-02-04 00:22:35 +01:00
file: {
"type": "application/img",
2022-02-08 17:44:15 +01:00
"guid": ''
2022-02-04 00:22:35 +01:00
},
attachments: [{
2023-08-16 08:01:45 +01:00
"title": "file.jpg",
2022-02-04 00:57:46 +01:00
"text": "description",
2022-02-04 00:22:35 +01:00
"title_link_download": false,
2023-06-29 16:04:44 +01:00
}],
2023-08-16 08:01:45 +01:00
temporaryData: formData,
2023-06-29 16:04:44 +01:00
attachmentsModelData: {
2023-08-16 08:01:45 +01:00
fileBase64: imageBase64,
2023-06-29 16:04:44 +01:00
}
2022-02-04 00:22:35 +01:00
})
2022-02-03 21:01:53 +01:00
}
2022-02-09 15:09:47 +01:00
2022-02-03 21:01:53 +01:00
async addImage() {
this.addFileToChat(['image/apng', 'image/jpeg', 'image/png'])
}
2022-02-09 15:09:47 +01:00
2022-02-03 21:01:53 +01:00
async addFile() {
this.addFileToChat(['.doc', '.docx', '.pdf'])
}
2023-08-16 08:01:45 +01:00
async addImageMobile() {
this.addFileToChatMobile(['image/apng', 'image/jpeg', 'image/png'])
}
async addFileToChatMobile(types: typeof FileType[]) {
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)
//
2023-08-28 16:36:31 +01:00
var imageBase64 = 'data:image/jpeg;base64,' + file.base64String
const compressedImage = await this.compressImageBase64(
imageBase64,
800, // maxWidth
800, // maxHeight
0.9 // quality
).then((picture) => {
2023-09-22 17:34:39 +01:00
// console.log('Selected: ', picture)
2023-08-28 16:36:31 +01:00
imageBase64 = picture
});
2023-08-16 08:01:45 +01:00
const response = await fetch(imageBase64);
const blob = await response.blob();
console.log(imageBase64)
const formData = new FormData();
formData.append("blobFile", blob);
2023-08-16 08:38:09 +01:00
this.ChatSystemService.getGroupRoom(roomId).send({
2023-08-16 08:01:45 +01:00
file: {
"type": "application/img",
"guid": ''
},
temporaryData: formData,
attachments: [{
"title": file.path,
"text": "description",
"title_link_download": false,
}],
attachmentsModelData: {
fileBase64: imageBase64,
}
})
}
2022-02-03 21:01:53 +01:00
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
2022-09-30 15:13:36 +01:00
this.ChatSystemService.getGroupRoom(this.roomId).send({
2022-03-03 08:21:22 +01:00
file: {
2022-02-03 21:01:53 +01:00
"name": res.data.selected.Assunto,
"type": "application/webtrix",
"ApplicationId": res.data.selected.ApplicationType,
"DocId": res.data.selected.Id,
"Assunto": res.data.selected.Assunto,
},
2022-04-06 16:25:47 +01:00
temporaryData: res,
2022-02-04 00:22:35 +01:00
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,
2022-02-04 08:00:10 +01:00
"type": "webtrix",
2022-02-07 17:55:00 +01:00
//"thumb_url": "assets/images/webtrix-logo.png",
2022-02-04 08:00:10 +01:00
"text": res.data.selected.DocTypeDesc,
"thumb_url": "https://static.ichimura.ed.jp/uploads/2017/10/pdf-icon.png",
2022-02-03 21:01:53 +01:00
}]
})
2022-02-04 00:22:35 +01:00
2022-02-03 21:01:53 +01:00
}
});
}
2023-08-16 08:01:45 +01:00
_getBase64(file) {
2023-10-11 16:20:42 +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-11 16:20:42 +01:00
}
2023-08-16 08:01:45 +01:00
async addFileToChat(types: typeof FileType[]) {
2022-03-03 08:21:22 +01:00
const roomId = this.roomId
2022-02-07 13:41:27 +01:00
const file: any = await this.fileService.getFileFromDevice(types);
2022-02-04 00:22:35 +01:00
2023-09-22 17:25:27 +01:00
2022-02-09 15:09:47 +01:00
2022-04-06 16:25:47 +01:00
if (file.type != "application/img" && file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/gif") {
2023-10-11 16:20:42 +01:00
const encodedData = btoa(JSON.stringify(await this.getBase64(file).catch((error) => {
2022-12-20 17:06:19 +01:00
console.error(error);
})));
2023-06-29 16:04:44 +01:00
const blob = this.fileService.base64toBlob(encodedData, file.type)
2023-08-16 08:01:45 +01:00
const formData = new FormData();
formData.append('blobFile', blob);
2023-09-22 17:25:27 +01:00
2022-09-30 15:13:36 +01:00
this.ChatSystemService.getGroupRoom(roomId).send({
file: {
"type": file.type,
"guid": '',
},
attachments: [{
"title": file.name,
"name": file.name,
// "text": "description",
"title_link_download": false,
}],
2023-06-29 16:04:44 +01:00
temporaryData: formData,
attachmentsModelData: {
2023-08-31 13:07:52 +01:00
fileBase64: encodedData
2023-06-29 16:04:44 +01:00
}
});
} else {
2023-09-22 17:25:27 +01:00
}
}
getFileReader(): FileReader {
const fileReader = new FileReader();
const zoneOriginalInstance = (fileReader as any)["__zone_symbol__originalInstance"];
return zoneOriginalInstance || fileReader;
}
2022-02-04 00:22:35 +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) {
2023-09-22 17:25:27 +01:00
};
});
2022-03-03 08:21:22 +01:00
2022-02-03 21:01:53 +01:00
}
2022-02-09 15:09:47 +01:00
2021-06-03 15:50:12 +01:00
async openChatOptions(ev?: any) {
2023-09-22 17:25:27 +01:00
2022-02-03 21:01:53 +01:00
const roomId = this.roomId;
2021-08-18 09:23:53 +01:00
2020-12-29 12:40:19 +01:00
const popover = await this.popoverController.create({
component: ChatOptionsPopoverPage,
cssClass: 'chat-options-popover',
event: ev,
2021-01-20 10:23:59 +01:00
componentProps: {
2021-01-20 13:17:54 +01:00
room: this.room,
2021-08-18 09:23:53 +01:00
members: this.members,
eventSelectedDate: new Date(),
2021-01-20 10:23:59 +01:00
},
2020-12-29 12:40:19 +01:00
translucent: true
});
2021-09-13 12:37:58 +01:00
await popover.present();
2022-03-03 08:21:22 +01:00
await popover.onDidDismiss().then(async (res) => {
2023-09-22 17:25:27 +01:00
2022-02-04 08:00:10 +01:00
if (res['data'] == 'meeting') {
2021-09-21 14:05:59 +01:00
this.bookMeeting();
2021-09-13 12:37:58 +01:00
}
2022-02-04 08:00:10 +01:00
else if (res['data'] == 'take-picture') {
this.takePicture()
2021-09-21 14:05:59 +01:00
}
2022-02-04 08:00:10 +01:00
else if (res['data'] == 'add-picture') {
2023-08-16 08:01:45 +01:00
this.addImageMobile()
2022-02-03 21:01:53 +01:00
2021-09-21 14:05:59 +01:00
}
2022-02-04 08:00:10 +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
}
2022-02-04 08:00:10 +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-14 10:30:13 +01:00
}
2021-09-13 12:37:58 +01:00
});
2020-12-29 12:40:19 +01:00
}
2021-08-18 18:58:02 +01:00
2022-03-03 08:21:22 +01:00
async addContacts() {
2023-10-11 16:20:42 +01:00
if (this.isAdmin) {
2023-03-09 16:35:54 +01:00
const modal = await this.modalController.create({
component: GroupContactsPage,
componentProps: {
isCreated: this.isGroupCreated,
room: this.room,
2023-08-23 11:19:51 +01:00
members: this.ChatSystemService.getGroupRoom(this.roomId).members,
2023-03-09 16:35:54 +01:00
name: this.room.name,
},
cssClass: 'contacts',
backdropDismiss: false
});
2023-09-22 17:25:27 +01:00
2023-03-09 16:35:54 +01:00
modal.onDidDismiss().then(() => {
this.getRoomInfo();
});
2023-07-15 11:01:09 +01:00
await modal.present();
2023-03-09 16:35:54 +01:00
}
2020-12-29 12:40:19 +01:00
}
2021-03-12 11:56:54 +01:00
2022-03-03 08:21:22 +01:00
async editGroup(roomId) {
2021-08-17 14:17:19 +01:00
const modal = await this.modalController.create({
component: EditGroupPage,
cssClass: 'modal modal-desktop',
componentProps: {
roomId: roomId,
},
});
await modal.present();
2022-03-03 08:21:22 +01:00
modal.onDidDismiss().then((res) => {
2023-09-22 17:25:27 +01:00
2021-09-21 14:28:53 +01:00
this.getRoomInfo();
//this.modalController.dismiss(res.data);
2021-08-17 14:17:19 +01:00
});
}
2022-01-13 17:28:35 +01:00
isJson(str) {
try {
JSON.parse(str);
} catch (e) {
return "";
}
return JSON.parse(str);
}
transformDataMSG(res) {
if (this.platform.is('desktop') || this.platform.is('mobileweb')) {
} else {
res.forEach(element => {
let chatmsg = {
_id: element._id,
attachments: element.attachments,
channels: element.channels,
file: element.file,
mentions: element.mentions,
msg: element.msg,
rid: element.rid,
ts: element.ts,
u: element.u,
_updatedAt: element._updatedAt,
/* image_url: {
name: name,
path: `${IMAGE_DIR}/${name}`,
data: `data:image/jpeg;base64,${readFile.data}`,
}, */
}
2023-06-11 13:36:27 +01:00
// this.sqlservice.addChatMSG(chatmsg)
2022-01-13 17:28:35 +01:00
});
}
}
2022-03-03 08:21:22 +01:00
async serverLongPull() {
2021-07-26 19:31:19 +01:00
this.chatService.getPrivateGroupMessages(this.roomId).subscribe(async res => {
2022-03-03 08:21:22 +01:00
if (res == 502) {
// Connection timeout
// happens when the connection was pending for too long
// let's reconnect
await this.serverLongPull();
} else if (res != 200) {
// Show Error
//showMessage(response.statusText);
//this.loadMessages()
let msgOnly = res['messages'].filter(data => data.t != 'au');
//this.messages = msgOnly.reverse();
2023-09-22 17:25:27 +01:00
//
2022-03-03 08:21:22 +01:00
this.transformDataMSG(msgOnly.reverse());
2023-06-11 13:36:27 +01:00
// this.getRoomMessageDB(this.roomId);
2022-03-03 08:21:22 +01:00
// Reconnect in one second
if (this.route.url != "/home/chat") {
2023-09-22 17:25:27 +01:00
2022-03-03 08:21:22 +01:00
} else {
//Check if modal is opened
if (document.querySelector('.isGroupChatOpened')) {
2023-10-11 16:20:42 +01:00
await new Promise(resolve => setTimeout(resolve, 5000)).catch((error) => {
2022-12-20 17:06:19 +01:00
console.error(error);
});
2022-03-03 08:21:22 +01:00
await this.serverLongPull();
2023-09-22 17:25:27 +01:00
2022-03-03 08:21:22 +01:00
}
2021-08-19 18:54:50 +01:00
}
2021-07-26 20:52:03 +01:00
2022-03-03 08:21:22 +01:00
} else {
// Got message
//let message = await response.text();
//this.loadMessages()
await this.serverLongPull();
}
});
2021-07-26 19:31:19 +01:00
}
2021-11-17 15:34:15 +01:00
sliderOpts = {
zoom: false,
slidesPerView: 1.5,
spaceBetween: 20,
centeredSlides: true
};
zoomActive = false;
2022-03-03 08:21:22 +01:00
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();
}
2021-11-17 15:34:15 +01:00
}
}
2021-12-08 20:27:05 +01:00
2022-03-03 08:21:22 +01:00
touchStart(card) {
// Make card appear above backdrop
card.el.style['z-index'] = 11;
}
b64toBlob(b64Data, contentType) {
contentType = contentType || '';
var sliceSize = 512;
b64Data = b64Data.replace(/^[^,]+,/, '');
b64Data = b64Data.replace(/\s/g, '');
var byteCharacters = window.atob(b64Data);
var byteArrays = [];
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
var slice = byteCharacters.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
var blob = new Blob(byteArrays, { type: contentType });
return blob;
}
2023-08-20 22:05:46 +01:00
async openFile(pdfString, filename, type) {
2023-08-31 13:07:52 +01:00
const blob = this.b64toBlob(pdfString, type)
let pathFile = ''
const fileName = filename
const contentFile = blob
if (this.platform.is('ios')) {
pathFile = this.file.documentsDirectory
} else {
pathFile = this.file.externalRootDirectory
}
2023-09-22 17:25:27 +01:00
2023-08-20 22:05:46 +01:00
await Filesystem.writeFile({
path: fileName,
data: pdfString,
2023-08-31 13:07:52 +01:00
directory: Directory.Data,
2023-08-20 22:05:46 +01:00
}).then((dir) => {
console.log('DIR ', dir)
this.fileOpener
2023-10-11 16:20:42 +01:00
.open(dir.uri, type)
.then(() => console.log())
.catch(e => console.error(e))
2023-08-20 22:05:46 +01:00
});
}
2022-03-22 16:11:30 +01:00
downloadFileMsg(msg: MessageService) {
msg.downloadFileMsg()
2022-01-13 17:28:35 +01:00
}
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-01-21 16:55:05 +01:00
2022-04-06 16:25:47 +01:00
async openPreview(msg: MessageService) {
2022-01-21 16:55:05 +01:00
2023-10-11 16:20: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-03-03 08:21:22 +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 === '') {
this.downloadFileMsg(msg)
2022-04-07 15:22:25 +01:00
} else {
2022-04-07 15:22:25 +01:00
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-12 14:57:02 +01:00
if (msg?.file?.type == "application/img") {
2022-09-23 11:23:24 +01:00
2022-07-06 09:59:22 +01:00
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 {
this.downloadFileFromBrowser("file", str)
2023-03-28 16:11:59 +01:00
this.downloadFileFromBrowser(msg.attachments[0].title, str)
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 {
2023-03-28 16:11:59 +01:00
this.openFile(str, msg.attachments[0].title, msg.file.type);
2022-07-06 17:02:42 +01:00
}
2022-04-06 16:25:47 +01:00
}
}
2022-03-03 08:21:22 +01:00
}
2022-01-21 16:55:05 +01:00
2022-03-03 08:21:22 +01:00
}
2022-03-21 21:09:05 +01:00
async audioPreview(msg) {
2023-09-22 17:25:27 +01:00
2022-03-21 21:09:05 +01:00
if (!msg.attachments[0].title_link || msg.attachments[0].title_link === null || msg.attachments[0].title_link === '') {
this.downloadFileMsg(msg)
} else { }
2022-03-21 21:09:05 +01:00
}
2023-08-16 08:01:45 +01:00
dataURItoBlob(dataURI) {
// 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-22 17:25:27 +01:00
2023-08-16 08:01:45 +01:00
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
2023-09-22 17:25:27 +01:00
2023-08-16 08:01:45 +01:00
// write the bytes of the string to an ArrayBuffer
var ab = new ArrayBuffer(byteString.length);
2023-09-22 17:25:27 +01:00
2023-08-16 08:01:45 +01:00
// create a view into the buffer
var ia = new Uint8Array(ab);
2023-09-22 17:25:27 +01:00
2023-08-16 08:01:45 +01:00
// set the bytes of the buffer to the correct values
for (var i = 0; i < byteString.length; i++) {
2023-10-11 16:20:42 +01:00
ia[i] = byteString.charCodeAt(i);
2023-08-16 08:01:45 +01:00
}
2023-09-22 17:25:27 +01:00
2023-08-16 08:01:45 +01:00
// write the ArrayBuffer to a blob, and you're done
2023-10-11 16:20:42 +01:00
var blob = new Blob([ab], { type: mimeString });
2023-08-16 08:01:45 +01:00
return blob;
2023-09-22 17:25:27 +01:00
2023-08-16 08:01:45 +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-22 17:25:27 +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-22 17:25:27 +01:00
2023-08-28 16:36:31 +01:00
if (newWidth > maxWidth) {
newHeight *= maxWidth / newWidth;
newWidth = maxWidth;
}
2023-09-22 17:25:27 +01:00
2023-08-28 16:36:31 +01:00
if (newHeight > maxHeight) {
newWidth *= maxHeight / newHeight;
newHeight = maxHeight;
}
2023-09-22 17:25:27 +01:00
2023-08-28 16:36:31 +01:00
canvas.width = newWidth;
canvas.height = newHeight;
2023-09-22 17:25:27 +01:00
2023-08-28 16:36:31 +01:00
const context = canvas.getContext('2d');
context?.drawImage(image, 0, 0, newWidth, newHeight);
2023-09-22 17:25:27 +01:00
2023-08-28 16:36:31 +01:00
const compressedBase64 = canvas.toDataURL('image/jpeg', quality);
resolve(compressedBase64);
};
2023-09-22 17:25:27 +01:00
2023-08-28 16:36:31 +01:00
image.onerror = (error) => {
reject(error);
};
});
}
2021-03-12 13:10:20 +01:00
}
2021-11-17 15:34:15 +01:00