diff --git a/src/app/models/chatMethod.ts b/src/app/models/chatMethod.ts index 3a090d813..99d41f39d 100644 --- a/src/app/models/chatMethod.ts +++ b/src/app/models/chatMethod.ts @@ -167,3 +167,209 @@ export interface chatHistory { unreadNotLoaded: number; }; } + + + + +interface Ts { + $date: any; +} + +interface U { + _id: string; + username: string; + name: string; +} + +interface UpdatedAt { + $date: any; +} + +interface Attachment { + ts: Date; + title_link_download: boolean; +} + +export interface File { + type: string; + guid: string; + image_url: string; + subject: string; + start_date?: Date; + end_date?: Date; + venue: string; + id: string; +} + +interface EditedAt { + $date: number; +} + +interface EditedBy { + _id: string; + username: string; +} + + + +interface Ts2 { + $date: number; +} + +interface U2 { + _id: string; + username: string; + name: string; +} + +interface UpdatedAt2 { + $date: number; +} + +interface FirstUnread { + _id: string; + rid: string; + msg: string; + ts: Ts2; + u: U2; + _updatedAt: UpdatedAt2; + mentions: any[]; + channels: any[]; +} + +export interface Message { + customFields:any; + _id: string; + rid: string; + msg: string; + ts: Ts; + u: U; + t: string; + _updatedAt: UpdatedAt; + mentions: any[]; + channels: any[]; + attachments: Attachment[]; + file: File; + editedAt: EditedAt; + editedBy: EditedBy; + urls: any[]; +} + + + + +export interface Lm { + $date: any; +} + + + + +export interface LastMessage { + _id: string; + rid: string; + msg: string; + ts: Ts; + u: U; + _updatedAt: UpdatedAt2; + mentions: any[]; + channels: any[]; + file: File; + attachments: Attachment[]; +} + +export interface CustomFields { +} + +export interface Update { + _id: string; + t: string; + usernames: string[]; + usersCount: number; + uids: string[]; + default: boolean; + ro: boolean; + sysMes: boolean; + _updatedAt: UpdatedAt; + lm: Lm; + lastMessage: LastMessage; + name: string; + fname: string; + u: U2; + customFields: CustomFields; +} + +export interface DeletedAt { + $date: any; +} + +export interface Remove { + _id: string; + _deletedAt: DeletedAt; +} + +export interface Result { + update: Update[]; + remove: Remove[]; +} + +export interface Rooms { + msg: string; + id: string; + result: Result; +} + + + +export interface ChatMessage { + msg: string; + id: string; + result: Message +} + + + + + +export interface chatHistory { + msg: string; + id: string; + result: { + messages: Message[]; + firstUnread: FirstUnread; + unreadNotLoaded: number; + }; +} + +export interface chatUser { + _id: string; + createdAt: Date; + emails: { + address: string; + verified: boolean; + } + type: string; + status: string; + active: boolean; + _updatedAt: Date; + roles: string[]; + name: string; + lastLogin: Date; + statusConnection: string; + utcOffset: number; + username: string; + __rooms: string[]; + requirePasswordChange?: boolean; + settings: { + preferences: { + language: string; + }; + }; + nickname: string; + statusText: string; + banners: any; + statusDefault: string; + language: string; + avatarOrigin: string; + avatarETag?: any; +} \ No newline at end of file diff --git a/src/app/models/rochet-chat-cliente-service.ts b/src/app/models/rochet-chat-cliente-service.ts index 36b10321b..46698ca58 100644 --- a/src/app/models/rochet-chat-cliente-service.ts +++ b/src/app/models/rochet-chat-cliente-service.ts @@ -4,6 +4,7 @@ export interface wsCallbacksParams { runOnces?: boolean requestId?: string key?: string + order?: number } diff --git a/src/app/pages/chat/group-messages/group-messages.page.ts b/src/app/pages/chat/group-messages/group-messages.page.ts index 0a9b8d3a7..dcbbc9423 100644 --- a/src/app/pages/chat/group-messages/group-messages.page.ts +++ b/src/app/pages/chat/group-messages/group-messages.page.ts @@ -101,7 +101,8 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { this.wsChatMethodsService.getGroupRoom(this.roomId).loadHistory(); this.wsChatMethodsService.getGroupRoom(this.roomId).scrollDown = this.scrollToBottomClicked; - + this.wsChatMethodsService.openRoom(this.roomId) + setTimeout(()=>{ this.scrollToBottomClicked() }, 50) diff --git a/src/app/pages/chat/messages/messages.page.ts b/src/app/pages/chat/messages/messages.page.ts index d177f5bc2..78e4f6f51 100644 --- a/src/app/pages/chat/messages/messages.page.ts +++ b/src/app/pages/chat/messages/messages.page.ts @@ -114,7 +114,8 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { this.wsChatMethodsService.getDmRoom(this.roomId).loadHistory() this.wsChatMethodsService.getDmRoom(this.roomId).scrollDown = this.scrollToBottomClicked - + this.wsChatMethodsService.openRoom(this.roomId) + setTimeout(()=>{ this.scrollToBottomClicked() }, 150) diff --git a/src/app/services/app-process-status.service.spec.ts b/src/app/services/app-process-status.service.spec.ts new file mode 100644 index 000000000..7c629ccb0 --- /dev/null +++ b/src/app/services/app-process-status.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { AppProcessStatusService } from './app-process-status.service'; + +describe('AppProcessStatusService', () => { + let service: AppProcessStatusService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(AppProcessStatusService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/app-process-status.service.ts b/src/app/services/app-process-status.service.ts new file mode 100644 index 000000000..8f66b538b --- /dev/null +++ b/src/app/services/app-process-status.service.ts @@ -0,0 +1,25 @@ +import { Injectable } from '@angular/core'; + +@Injectable({ + providedIn: 'root' +}) +export class AppProcessStatusService { + + status: 'resume' | 'pause' = 'resume' + + constructor() { + this.events() + } + + private events() { + document.addEventListener('pause', () => { + // console.log('App going to background'); + this.status = 'pause' + }); + + document.addEventListener('resume', () => { + // console.log('App going to background'); + this.status = 'resume' + }); + } +} diff --git a/src/app/services/chat/message.service.ts b/src/app/services/chat/message.service.ts index 02ea997be..bde3ef601 100644 --- a/src/app/services/chat/message.service.ts +++ b/src/app/services/chat/message.service.ts @@ -42,7 +42,7 @@ export class MessageService { if (this.file) { if (this.file.guid) { this.storage.get(this.file.guid).then((image) => { - console.log('IMAGE FROM STORAGE', image) + // console.log('IMAGE FROM STORAGE', image) this.file.image_url = image }); } diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index b39259603..4e85074ae 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -12,6 +12,8 @@ import { NativeNotificationService } from 'src/app/services/native-notification. import { SessionStore } from 'src/app/store/session.service'; import { capitalizeTxt } from 'src/plugin/text' import { SortService } from '../functions/sort.service'; +import { chatUser } from 'src/app/models/chatMethod'; + @Injectable({ providedIn: 'root' }) @@ -40,6 +42,9 @@ export class RoomService { mgsArray = []; scrollDown = () => { } + getAllUsers = (): chatUser[] => { + return [] + } constructor( public WsChatService: WsChatService, @@ -198,6 +203,19 @@ export class RoomService { }, 3000) } + private setTypingOff() { + this.typing('') + } + + roomLeave() { + this.setTypingOff() + } + + open() { + // this.typing(this.message) + } + + leave(rid?) { this.WsChatService.leaveRoom(this.id) } @@ -437,7 +455,7 @@ export class RoomService { console.log('FIX UPDATE ', message.result) message.result._updatedAt = message.result._updatedAt['$date'] } else { - console.log('FIX UPDATE 11', message) + // console.log('FIX UPDATE 11', message) message._updatedAt = message._updatedAt['$date'] } return message diff --git a/src/app/services/chat/ws-chat-methods.service.ts b/src/app/services/chat/ws-chat-methods.service.ts index 1950bb692..d2e806d88 100644 --- a/src/app/services/chat/ws-chat-methods.service.ts +++ b/src/app/services/chat/ws-chat-methods.service.ts @@ -10,6 +10,7 @@ import { Platform } from '@ionic/angular'; import { SqliteService } from 'src/app/services/sqlite.service'; import { NativeNotificationService } from 'src/app/services/native-notification.service'; import { SortService } from '../functions/sort.service'; +import { chatUser } from 'src/app/models/chatMethod'; @Injectable({ providedIn: 'root' @@ -28,6 +29,10 @@ export class WsChatMethodsService { dmCount = 0; groupCount = 0; + + currentRoom = null + users: chatUser[] = [] + constructor( private WsChatService: WsChatService, private storage: Storage, @@ -40,10 +45,37 @@ export class WsChatMethodsService { await this.getAllRooms(); this.subscribeToRoom() + + // + await this.getUser() + this.getUserStatus() + })() } + openRoom(roomId) { + + if(this.currentRoom) { + if(this.getDmRoom(this.currentRoom)) { + this.getDmRoom(this.currentRoom).roomLeave() + } else if(this.getGroupRoom(this.currentRoom)) { + this.getGroupRoom(this.currentRoom).roomLeave() + } + } + + this.currentRoom = roomId + + if(this.currentRoom) { + if(this.getDmRoom(this.currentRoom)) { + this.getDmRoom(this.currentRoom).open() + } else if(this.getGroupRoom(this.currentRoom)) { + this.getGroupRoom(this.currentRoom).open() + } + } + + } + async getAllRooms () { this.loadingWholeList = true @@ -140,6 +172,7 @@ export class WsChatMethodsService { } + prepareRoom(roomData) { let room:RoomService; @@ -155,6 +188,7 @@ export class WsChatMethodsService { }) room.receiveMessage() + room.getAllUsers = this.getUsers room.receiveMessageDelete(); let roomId = this.getRoomId(roomData) @@ -170,6 +204,51 @@ export class WsChatMethodsService { } } + + + getReceptorName(roomData) { + try { + return roomData.usernames.find((e)=> e != SessionStore.user.RochetChatUser) + } catch(e) { + return '*' + } + } + + getUserStatus(id?:string) { + + this.WsChatService.getUserStatus((d) => { + + const username = d.fields.args[0][1] + const statusNum = d.fields.args[0][2] + + console.log('d', d) + + const statusText = this.statusNumberToText(statusNum) + this.getUserByName(username).status = statusText + }) + + } + + getUserByName(username) { + return this.users.find((user)=> user.username == username) + } + + statusNumberToText(text) { + if(text == '0') { + return "offline" + } + else if(text == '1') { + return "online" + } + else if(text == '2') { + return "away" + } + else if(text == '3') { + return "busy" + } + } + + deleteMessage(id?) { return this.WsChatService.deleteMessage(id); } @@ -229,4 +308,28 @@ export class WsChatMethodsService { return !roomData.fname } + getUsers = () =>{ + return this.users + } + + async getUser() { + + // let _res = await this.chatService.getAllUsers().toPromise() + + // let user = _res['users'].filter(data => data.username != SessionStore.user.RochetChatUser); + + // user = user.sort((a,b) => { + // if(a.name < b.name) { + // return -1; + // } + // if(a.name > b.name) { + // return 1; + // } + // return 0; + // }); + + // this.users = user + } + + } diff --git a/src/app/services/chat/ws-chat.service.ts b/src/app/services/chat/ws-chat.service.ts index 724b9cce1..586035907 100644 --- a/src/app/services/chat/ws-chat.service.ts +++ b/src/app/services/chat/ws-chat.service.ts @@ -204,7 +204,7 @@ export class WsChatService { }); } - hidingRoom(roomId){ + hidingRoom(roomId) { const requestId = uuidv4() @@ -350,6 +350,19 @@ export class WsChatService { }}) } + + getUserStatus(funx:Function){ + this.ws.registerCallback({ + type:'Onmessage', + funx:(message)=>{ + if(message.msg == 'changed' && message.collection == "stream-notify-logged") { + funx(message) + } + } + }) + } + + loadHistory(roomId, limit: number = 50) { const requestId = uuidv4() diff --git a/src/app/services/native-notification.service.ts b/src/app/services/native-notification.service.ts index dea860b50..4cf54ba31 100644 --- a/src/app/services/native-notification.service.ts +++ b/src/app/services/native-notification.service.ts @@ -18,9 +18,9 @@ export class NativeNotificationService { LocalNotifications.requestPermissions() LocalNotifications.checkPermissions().then((data)=>{ - console.log('success', data) + // console.log('success', data) }).catch((data)=>{ - console.log('error', data) + // console.log('error', data) }) } diff --git a/src/app/shared/chat/group-messages/group-messages.page.ts b/src/app/shared/chat/group-messages/group-messages.page.ts index b14ec33ec..52558f953 100644 --- a/src/app/shared/chat/group-messages/group-messages.page.ts +++ b/src/app/shared/chat/group-messages/group-messages.page.ts @@ -112,7 +112,8 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe //this.scrollToBottom(); this.wsChatMethodsService.getGroupRoom(this.roomId).loadHistory(); this.wsChatMethodsService.getGroupRoom(this.roomId).getMsgFromDB(); - console.log('MESSAGES'+ this.wsChatMethodsService.getGroupRoom(this.roomId).messages); + // console.log('MESSAGES'+ this.wsChatMethodsService.getGroupRoom(this.roomId).messages); + this.wsChatMethodsService.openRoom(this.roomId) this.wsChatMethodsService.getGroupRoom(this.roomId).scrollDown = this.scrollToBottomClicked setTimeout(()=>{ diff --git a/src/app/shared/chat/messages/messages.page.ts b/src/app/shared/chat/messages/messages.page.ts index dd5037409..f799c6d64 100644 --- a/src/app/shared/chat/messages/messages.page.ts +++ b/src/app/shared/chat/messages/messages.page.ts @@ -100,21 +100,13 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy } ngOnChanges(changes: SimpleChanges): void { this.wsChatMethodsService.getDmRoom(this.roomId).loadHistory() - //this.wsChatMethodsService.getDmRoom(this.roomId).getMsgFromDB() + this.wsChatMethodsService.getDmRoom(this.roomId).scrollDown = this.scrollToBottomClicked - /* console.log('DATATATA', this.wsChatMethodsService.getDmRoom(this.roomId).massages) - this.wsChatMethodsService.getDmRoom(this.roomId).massages.forEach((element) => { - console.log('DATATATA 11', element) - }) */ - - - //this.transformData(this.wsChatMethodsService.getDmRoom(this.roomId).massages) - //this.getMessageDB() - + this.wsChatMethodsService.openRoom(this.roomId) setTimeout(() => { this.scrollToBottomClicked() - }, 50) + }, 150) } diff --git a/src/plugin/array.js b/src/plugin/array.js new file mode 100644 index 000000000..a972f8dda --- /dev/null +++ b/src/plugin/array.js @@ -0,0 +1,6 @@ +// https://stackoverflow.com/a/68628927/14115342 + +const ArrayToObject = (array = [], getKey) => array.reduce((obj, cur) => { + const key = getKey(cur); + return (Object.assign(Object.assign({}, obj), { [key]: cur })); +}, {}); \ No newline at end of file