mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-20 21:35:50 +00:00
556 lines
14 KiB
TypeScript
556 lines
14 KiB
TypeScript
import {
|
|
ChangeDetectorRef,
|
|
Component,
|
|
OnInit,
|
|
} from '@angular/core';
|
|
import { ModalController } from '@ionic/angular';
|
|
import { GroupMessagesPage } from './modal/group-messages/group-messages.page';
|
|
import { ContactsPage } from './modal/./messages/contacts/contacts.page';
|
|
import { MessagesPage } from './modal/./messages/messages.page';
|
|
import { NewGroupPage } from './modal/./new-group/new-group.page';
|
|
import { Observable, Subject } from "rxjs/Rx";
|
|
import { Router, NavigationEnd } from '@angular/router';
|
|
import { TimeService } from 'src/app/services/functions/time.service';
|
|
import { ThemeService } from 'src/app/services/theme.service'
|
|
import { DataService } from 'src/app/services/data.service';
|
|
import { RouteService } from 'src/app/services/route.service';
|
|
import { EditGroupPage } from './modal/edit-group/edit-group.page';
|
|
import { ChatServiceService } from 'src/app/module/chat/domain/chat-service.service'
|
|
import { RoomLocalRepository } from 'src/app/module/chat/data/repository/room/room-local-repository.service'
|
|
import { map, tap } from 'rxjs/operators';
|
|
import { BehaviorSubject, interval, Subscription } from 'rxjs';
|
|
import { RoomTable } from 'src/app/infra/database/dexie/instance/chat/schema/room';
|
|
import { RoomType } from 'src/app/core/chat/entity/group';
|
|
import { BoldLocalRepository } from 'src/app/module/chat/data/repository/bold/bold-local-repository'
|
|
import { BoldTable } from 'src/app/infra/database/dexie/instance/chat/schema/bold';
|
|
import { RoomViewModel } from './store/model/room';
|
|
import { MessageLocalDataSourceService } from 'src/app/module/chat/data/repository/message/message-local-data-source.service'
|
|
import { ToastService } from 'src/app/services/toast.service';
|
|
import { Logger } from 'src/app/services/logger/main/service';
|
|
@Component({
|
|
selector: 'app-chat',
|
|
templateUrl: './chat.page.html',
|
|
styleUrls: ['./chat.page.scss'],
|
|
})
|
|
export class ChatPage implements OnInit {
|
|
|
|
showLoader: boolean;
|
|
segment: RoomType = RoomType.Direct
|
|
selectedRoomId: string;
|
|
selectedReceiverId: string;
|
|
|
|
roomId: any;
|
|
groupRoomId: any;
|
|
showEmptyComponent = true;
|
|
showMessages = false;
|
|
showContacts = false;
|
|
showNewGroup = false;
|
|
showEditGroup = false;
|
|
showGroupMessages = false;
|
|
showGroupContacts = false;
|
|
showNewEvent = false;
|
|
showAttendees = false;
|
|
emptyTextDescription = 'Sem conversa selecionada';
|
|
|
|
subject: any;
|
|
public messages: Subject<any>;
|
|
hideRefreshBtn = true;
|
|
|
|
routerSubscription
|
|
|
|
// count$: Observable<RoomRemoteDataSourceState>;
|
|
//items$!: DexieObservable<RoomTable[]>;
|
|
items$!: Observable<RoomTable[]>;
|
|
rooms: RoomViewModel[] = [];
|
|
private subscription: Subscription;
|
|
expirationDate = {}
|
|
private intervalSubscription: Subscription;
|
|
|
|
RoomType = RoomType
|
|
|
|
boldTable: {[key: string]: BoldTable} = {}
|
|
private roomListSubject = new BehaviorSubject<{ id: string, name: string }[]>([]);
|
|
roomsWithLastMessages: { name: string, lastMessage: any }[] = [];
|
|
private subscription1: Subscription;
|
|
|
|
RoomSelected!: RoomViewModel;
|
|
|
|
constructor(
|
|
private modalController: ModalController,
|
|
private timeService: TimeService,
|
|
public ThemeService: ThemeService,
|
|
private dataService: DataService,
|
|
private router: Router,
|
|
public RouteService: RouteService,
|
|
private ChatServiceService: ChatServiceService,
|
|
private roomLocalDataSourceService: RoomLocalRepository,
|
|
private boldLocalRepository: BoldLocalRepository,
|
|
private MessageLocalDataSourceService: MessageLocalDataSourceService,
|
|
private toastService: ToastService
|
|
) {
|
|
|
|
window.onresize = (event) => {
|
|
if (window.innerWidth > 701) {
|
|
this.modalController.dismiss();
|
|
}
|
|
}
|
|
|
|
this.showLoader = true;
|
|
|
|
this.boldLocalRepository.listen().subscribe((data) => {
|
|
const distributionObject: {[key: string]: BoldTable} = {};
|
|
|
|
for (const distribution of data) {
|
|
distributionObject[distribution.roomId] = distribution;
|
|
}
|
|
|
|
this.boldTable = distributionObject
|
|
})
|
|
|
|
}
|
|
|
|
|
|
updatemessage(sortedRooms) {
|
|
this.rooms = sortedRooms
|
|
|
|
this.rooms.sort((a, b) =>
|
|
new Date(b.messages?.[0]?.sentAt as string || b.createdAt ).getTime() -
|
|
new Date(a.messages?.[0]?.sentAt as string || a.createdAt).getTime()
|
|
);
|
|
|
|
// this.RoomSelected = this.rooms.filter(e => e.id == this.selectedRoomId)[0]
|
|
}
|
|
ngOnInit() {
|
|
// this.subscription = this.roomListSubject.pipe(
|
|
// switchMap(roomList =>
|
|
// this.MessageLocalDataSourceService.getLastMessageForRooms(roomList.map(room => room.id))
|
|
// )
|
|
// ).subscribe(lastMessages => {
|
|
// this.roomsWithLastMessages = this.roomListSubject.value.map(room => ({
|
|
// name: room.name,
|
|
// lastMessage: lastMessages.find(msg => msg.roomId === room.id)?.message || null
|
|
// }));
|
|
// this.roomsWithLastMessages.sort((a, b) =>
|
|
// new Date(b.lastMessage.messages?.[0]?.sentAt as unknown as string) as unknown as number -
|
|
// (new Date(a.lastMessage.messages?.[0]?.sentAt as unknown as string) as any) as unknown as number
|
|
// )
|
|
// });
|
|
|
|
// // Initialize room list (can be loaded from a service, API, etc.)
|
|
// this.roomListSubject.next([
|
|
// { id: '1135e73c-af8e-416c-a51f-1602b4770cf7', name: 'Room 1' }
|
|
// // Add more rooms as needed
|
|
// ]);
|
|
|
|
this.roomLocalDataSourceService.getItemsLive().pipe(
|
|
map((roomList) => roomList.map((room)=> new RoomViewModel(room))),
|
|
tap((roomList) => {
|
|
console.log('update')
|
|
this.updatemessage(roomList)
|
|
})
|
|
).subscribe()
|
|
|
|
// Create the interval observable
|
|
const interval$ = interval(1000).pipe(
|
|
tap(() => {
|
|
for (const room of this.rooms) {
|
|
if(room.expirationDate) {
|
|
this.expirationDate[room.$id] = this.getSecondsLeft(room.expirationDate);
|
|
}
|
|
|
|
}
|
|
})
|
|
);
|
|
|
|
// Subscribe to the interval observable
|
|
this.intervalSubscription = interval$.subscribe();
|
|
|
|
// this.ChatServiceService.getRoomList()
|
|
|
|
this.hideRefreshButton();
|
|
|
|
this.router.events.forEach((event) => {
|
|
if (event instanceof NavigationEnd && event.url == '/home/chat' ||
|
|
event instanceof NavigationEnd && event.url == "/home/chat?gbCreateGroup=true") {
|
|
this.checkCreateGroup();
|
|
}
|
|
});
|
|
|
|
this.routerSubscription = this.router.events.subscribe((event) => {
|
|
if (event instanceof NavigationEnd && event.url.startsWith('/home/chat')) {
|
|
this.routeCheck()
|
|
// this.ChatServiceService.getRoomList()
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
onSegmentChange() {}
|
|
|
|
getSecondsLeft(expirationDate: any): number | null {
|
|
if (!expirationDate) return null;
|
|
const now = new Date().getTime();
|
|
const expTime = new Date(expirationDate).getTime();
|
|
return Math.max(Math.floor((expTime - now) / 1000), 0);
|
|
}
|
|
|
|
ngOnDestroy() {
|
|
// this.setStatus('offline');
|
|
this.routerSubscription?.unsubscribe();
|
|
// Unsubscribe from the interval to prevent memory leaks
|
|
this.intervalSubscription?.unsubscribe();
|
|
}
|
|
|
|
|
|
firstEnter = true
|
|
|
|
routeCheck() {
|
|
const urlParams = new URLSearchParams(window.location.search);
|
|
const roomId = urlParams.get('roomId');
|
|
|
|
if (roomId) {
|
|
|
|
if (this.firstEnter) {
|
|
this.firstEnter = false
|
|
|
|
let delay = this.RouteService.liveHistory.find((item) => {
|
|
return ['/home/publications', '/home/agenda', '/home/gabinete', '/home/events'].filter(x => {
|
|
return item.includes(x)
|
|
}).length >= 1
|
|
})
|
|
if (!delay) {
|
|
console.log("delay")
|
|
setTimeout(() => {
|
|
this.openChat(roomId)
|
|
}, 2000)
|
|
} else {
|
|
console.log("no dalay")
|
|
this.openChat(roomId)
|
|
}
|
|
} else {
|
|
this.openChat(roomId)
|
|
}
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
openFailed = 0
|
|
openChat(roomId) {
|
|
// const room = this.ChatSystemService.getRoomById(roomId);
|
|
if (roomId) {
|
|
//this.openFailed = 0
|
|
//if (room.isGroup) {
|
|
// this.segment = 'Grupos'
|
|
// this.openGroupMessagesPage(roomId)
|
|
//} else {
|
|
// this.segment = 'Contactos'
|
|
this.openMessagesPage(roomId)
|
|
//}
|
|
} else {
|
|
if (this.openFailed <= 3) {
|
|
this.openFailed++
|
|
|
|
setTimeout(() => {
|
|
this.openChat(roomId)
|
|
}, 1000)
|
|
} else {
|
|
this.openFailed = 0
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
checkCreateGroup() {
|
|
if (this.dataService.get("newGroup")) {
|
|
this.openNewGroupPage();
|
|
}
|
|
else {
|
|
this.closeAllDesktopComponents();
|
|
this.showEmptyComponent = true;
|
|
}
|
|
|
|
}
|
|
|
|
numSequence(n: number): Array<number> {
|
|
return Array(n);
|
|
}
|
|
|
|
setStatus(status: string) {
|
|
let body = {
|
|
message: '',
|
|
status: status,
|
|
}
|
|
|
|
}
|
|
|
|
hideRefreshButton() {
|
|
window.onresize = (event) => {
|
|
if (window.innerWidth < 701) {
|
|
this.selectedRoomId = '';
|
|
this.hideRefreshBtn = false;
|
|
this.closeAllDesktopComponents()
|
|
|
|
// this.ChatSystemService.getRoomById(this.roomId)?.roomLeave()
|
|
}
|
|
else {
|
|
this.hideRefreshBtn = true;
|
|
if (this.selectedRoomId == '') {
|
|
this.showEmptyComponent = true;
|
|
}
|
|
}
|
|
}
|
|
if (window.innerWidth < 701) {
|
|
this.selectedRoomId = '';
|
|
this.hideRefreshBtn = false;
|
|
}
|
|
}
|
|
|
|
closeAllDesktopComponents() {
|
|
this.showMessages = false;
|
|
this.showContacts = false;
|
|
this.showNewGroup = false;
|
|
this.showEditGroup = false;
|
|
this.showGroupMessages = false;
|
|
this.showEmptyComponent = false;
|
|
this.showGroupContacts = false;
|
|
this.showNewEvent = false;
|
|
this.showAttendees = false;
|
|
|
|
}
|
|
|
|
showEmptyContainer() {
|
|
this.selectedRoomId = '';
|
|
this.showEmptyComponent = true;
|
|
}
|
|
openGroupContactsPage(roomId) {
|
|
this.selectedRoomId = '';
|
|
this.groupRoomId = roomId;
|
|
this.RoomSelected = this.rooms.filter(e => e.$id == roomId)[0]
|
|
this.closeAllDesktopComponents();
|
|
if (window.innerWidth < 701) {
|
|
|
|
}
|
|
else {
|
|
this.showGroupContacts = true;
|
|
}
|
|
}
|
|
|
|
|
|
openMessagesPage($roomId: string) {
|
|
// this.chatService.refreshtoken();
|
|
this.roomId = $roomId;
|
|
const exist = this.rooms.filter(e => e.$id == $roomId)[0]
|
|
|
|
if(exist) {
|
|
this.RoomSelected = exist
|
|
this.selectedRoomId = exist.$id;
|
|
if (window.innerWidth < 701) {
|
|
this.openMessagesModal($roomId);
|
|
}
|
|
else {
|
|
this.selectedRoomId = $roomId;
|
|
this.closeAllDesktopComponents();
|
|
this.showEmptyComponent = false;
|
|
this.showMessages = true;
|
|
}
|
|
} else {
|
|
console.log('conversa não existe');
|
|
// this.toastService._badRequest("Pedimos desculpa mas não foi possível executar a acção. Por favor, contacte o apoio técnico.")
|
|
}
|
|
|
|
// Trigger change detection manually
|
|
}
|
|
|
|
openMessagesWithOutValidation(roomId: string) {
|
|
if (window.innerWidth < 701) {
|
|
// this.openMessagesModal(roomId);
|
|
}
|
|
else {
|
|
this.selectedRoomId = roomId;
|
|
this.selectedReceiverId = undefined
|
|
this.closeAllDesktopComponents();
|
|
this.showEmptyComponent = false;
|
|
this.showMessages = true;
|
|
}
|
|
}
|
|
|
|
openMessagesToStartDirectConversation(room: RoomViewModel) {
|
|
if (window.innerWidth < 701) {
|
|
// this.openMessagesModal(roomId);
|
|
}
|
|
else {
|
|
this.roomId = null;
|
|
this.selectedRoomId = room.$id;
|
|
console.log('RoomSelected', room)
|
|
this.RoomSelected = room
|
|
this.closeAllDesktopComponents();
|
|
this.showEmptyComponent = false;
|
|
this.showMessages = true;
|
|
}
|
|
}
|
|
|
|
openContactsPage() {
|
|
this.segment = RoomType.Direct;
|
|
this.selectedRoomId = '';
|
|
this.closeAllDesktopComponents();
|
|
|
|
if (window.innerWidth < 701) {
|
|
this.selectContact();
|
|
}
|
|
else {
|
|
this.showEmptyComponent = false;
|
|
this.showContacts = true;
|
|
}
|
|
}
|
|
openNewGroupPage() {
|
|
this.segment = RoomType.Group;
|
|
|
|
this.selectedRoomId = '';
|
|
if (window.innerWidth < 701) {
|
|
this.newGroup();
|
|
}
|
|
else {
|
|
this.closeAllDesktopComponents();
|
|
this.showEmptyComponent = false;
|
|
this.showNewGroup = true;
|
|
}
|
|
}
|
|
|
|
openEditGroupPage(rid) {
|
|
if (window.innerWidth < 701) {
|
|
this.editGroup(rid);
|
|
}
|
|
else {
|
|
this.closeAllDesktopComponents();
|
|
this.showEditGroup = true;
|
|
}
|
|
}
|
|
|
|
doRefresh(event) {
|
|
setTimeout(() => {
|
|
try {
|
|
event?.target?.complete();
|
|
} catch (error) { }
|
|
}, 1000);
|
|
}
|
|
|
|
|
|
showDateDuration(start: any) {
|
|
return this.timeService.showDateDuration(start);
|
|
}
|
|
|
|
countDownDate(date: any, roomId: string) {
|
|
return this.timeService.countDownDate(date, roomId);
|
|
}
|
|
|
|
|
|
|
|
async emptyTextDescriptionOpen() {
|
|
this.closeAllDesktopComponents()
|
|
this.showEmptyComponent = true
|
|
}
|
|
|
|
|
|
async selectContact() {
|
|
const modal = await this.modalController.create({
|
|
component: ContactsPage,
|
|
cssClass: 'modal modal-desktop',
|
|
});
|
|
|
|
modal.onDidDismiss().then((Data) => {
|
|
|
|
// let data = Data.data
|
|
// let roomId = data.roomId
|
|
// this.openMessagesPage(roomId);
|
|
|
|
});
|
|
|
|
await modal.present();
|
|
|
|
}
|
|
|
|
async newGroup() {
|
|
const modal = await this.modalController.create({
|
|
component: NewGroupPage,
|
|
cssClass: 'modal modal-desktop',
|
|
});
|
|
await modal.present();
|
|
modal.onDidDismiss();
|
|
}
|
|
|
|
async editGroup(roomId) {
|
|
const modal = await this.modalController.create({
|
|
component: EditGroupPage,
|
|
cssClass: 'modal modal-desktop',
|
|
componentProps: {
|
|
roomId: roomId,
|
|
},
|
|
});
|
|
|
|
modal.onDidDismiss().then((res) => {
|
|
|
|
this.modalController.dismiss(res.data);
|
|
});
|
|
|
|
await modal.present();
|
|
}
|
|
|
|
async openMessagesModal(roomId: any) {
|
|
this.closeAllDesktopComponents();
|
|
|
|
//
|
|
|
|
const modal = await this.modalController.create({
|
|
component: MessagesPage,
|
|
cssClass: 'modal modal-desktop isMessagesChatOpened',
|
|
componentProps: {
|
|
roomId: roomId,
|
|
},
|
|
});
|
|
await modal.present();
|
|
modal.onDidDismiss();
|
|
}
|
|
|
|
|
|
backToChat({ roomId }) {
|
|
|
|
//const room = this.ChatSystemService.getRoomById(roomId);
|
|
|
|
//if (room.isGroup) {
|
|
// this.segment = "Grupos"
|
|
// this.openGroupMessagesPage(room.id);
|
|
//} else {
|
|
// this.segment = RoomType.Direct
|
|
this.openMessagesPage(roomId);
|
|
|
|
if(typeof roomId != 'string') {
|
|
Logger.error('roomId must be string', {
|
|
roomId
|
|
})
|
|
}
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
async openGroupMessagesModal(roomId: any) {
|
|
|
|
const modal = await this.modalController.create({
|
|
component: GroupMessagesPage,
|
|
cssClass: 'modal modal-desktop isGroupChatOpened',
|
|
componentProps: {
|
|
roomId: roomId,
|
|
},
|
|
});
|
|
|
|
modal.onDidDismiss().then(() => {
|
|
// this.ChatSystemService.currentRoom.roomLeave()
|
|
});
|
|
await modal.present();
|
|
}
|
|
|
|
|
|
}
|