diff --git a/src/app/pages/publications/view-publications/view-publications.page.ts b/src/app/pages/publications/view-publications/view-publications.page.ts index 0c1b04246..2e5a38217 100644 --- a/src/app/pages/publications/view-publications/view-publications.page.ts +++ b/src/app/pages/publications/view-publications/view-publications.page.ts @@ -70,9 +70,10 @@ export class ViewPublicationsPage implements OnInit { } //this.testForkJoin() this.getPublicationDetail(); - setTimeout(() => { + this.getPublicationsIds(); + /* setTimeout(() => { this.getPublicationsIds(); - }, 1000); + }, 1000); */ this.backgroundservice.registerBackService('Online', () => { this.getPublicationDetail(); @@ -92,7 +93,7 @@ export class ViewPublicationsPage implements OnInit { // this.id = this.id['ProcessId'] // } //this.testForkJoin() - this.getPublicationDetail(); + //this.getPublicationDetail(); // this.getPublicationsIds(); } @@ -119,6 +120,7 @@ export class ViewPublicationsPage implements OnInit { this.publications.GetPresidentialAction(this.folderId).subscribe(res => { console.log(res); this.item = res; + this.sqliteservice.updatePublicationsDetails(this.folderId, JSON.stringify(res)); }); } @@ -144,16 +146,16 @@ export class ViewPublicationsPage implements OnInit { console.log('publications ids', res) this.publicationList = new Array(); - /* for(let i of res) { - this.publications.GetPublicationById(i).subscribe(ress => { + for(let i = 0; i < res.length; i++) { + this.publications.GetPublicationById(res[i]).subscribe(ress => { console.log('publications by ids', ress) let item: Publication = this.publicationPipe.itemList(ress) console.log('publications by ids 2', item) this.publicationList.push(item); }) - } */ + } - res.forEach(element => { + /* res.forEach(element => { console.log('publications elements', element) this.publications.GetPublicationById(element).subscribe(ress => { console.log('publications by ids', ress) @@ -162,9 +164,10 @@ export class ViewPublicationsPage implements OnInit { this.publicationList.push(ress); }) - }); - + }); */ + console.log('PUBLICATIONS IMAGEs',this.publicationList) this.sqliteservice.updateactions(this.folderId, JSON.stringify(this.publicationList)); + console.log('PUBLICATIONS IMAGEs',this.publicationList) this.publicationListStorage.add(folderId, this.publicationList) this.getpublication = this.publicationList; @@ -237,7 +240,7 @@ export class ViewPublicationsPage implements OnInit { this.sqliteservice.getActionById(this.folderId).then((publications) => { console.log('publications', publications) - let item = { + /* let item = { ActionType: publications[0].ActionType, DateBegin: publications[0].DateBegin, DateEnd: publications[0].DateEnd, @@ -245,7 +248,8 @@ export class ViewPublicationsPage implements OnInit { Detail: publications[0].Detail, ProcessId: publications[0].ProcessId } - this.publicationDitails = item; + this.publicationDitails = item; */ + this.item = JSON.parse(publications[0].publicationsDetails); let publicationArray = []; JSON.parse(publications[0].publications).forEach(element => { @@ -263,7 +267,7 @@ export class ViewPublicationsPage implements OnInit { publicationArray.push(publicationlis); }); - + this.getpublication = publicationArray; }) } diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index 00832020e..b39259603 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -1,13 +1,16 @@ -import { Injectable } from '@angular/core' +import { Injectable } from '@angular/core'; import { WsChatService } from 'src/app/services/chat/ws-chat.service'; -import { MessageService } from 'src/app/services/chat/message.service' -import { ChatUserService } from 'src/app/services/chat/chat-user.service' -import { showDateDuration } from 'src/plugin/showDateDuration' +import { MessageService } from 'src/app/services/chat/message.service'; +import { ChatUserService } from 'src/app/services/chat/chat-user.service'; +import { showDateDuration } from 'src/plugin/showDateDuration'; import { ToastsService } from '../toast.service'; -import { chatHistory, ChatMessage } from 'src/app/models/chatMethod' +import { chatHistory, ChatMessage } from 'src/app/models/chatMethod'; import { Storage } from '@ionic/storage'; import { Platform } from '@ionic/angular'; import { SqliteService } from 'src/app/services/sqlite.service'; +import { NativeNotificationService } from 'src/app/services/native-notification.service'; +import { SessionStore } from 'src/app/store/session.service'; +import { capitalizeTxt } from 'src/plugin/text' import { SortService } from '../functions/sort.service'; @Injectable({ providedIn: 'root' @@ -26,6 +29,12 @@ export class RoomService { _updatedAt = {} private hasLoadHistory = false duration = '' + isTyping = false + otherUserType = false + lastTimeType = null + message = '' + lastMessageTxt = '' + userThatIsTyping = '' private ToastService = ToastsService mgsArray = []; @@ -38,8 +47,11 @@ export class RoomService { private storage: Storage, private platform: Platform, private sqlservice: SqliteService, + private NativeNotificationService: NativeNotificationService, private sortService: SortService, - ) { } + ) { + this.NativeNotificationService.askForPermission() + } setData({ customFields, id, name, t, lastMessage = new MessageService(this.storage), _updatedAt }) { this.customFields = customFields @@ -54,7 +66,7 @@ export class RoomService { receiveMessage() { - this.WsChatService.upateRoomEvents( + this.WsChatService.updateRoomEventss( this.id, "stream-room-messages", (ChatMessage) => { @@ -77,6 +89,13 @@ export class RoomService { //this.sortService.sortDate(this.messages, '') + if(SessionStore.user.RochetChatUser != ChatMessage.u.username) { + this.NativeNotificationService.sendNotificationChat({ + message: message.msg, + title: this.name + }); + } + // save to ionic storage this.storage.get('chatmsg' + this.id).then((messages: any) => { const newListMessages = messages.push(ChatMessage) @@ -89,12 +108,25 @@ export class RoomService { } ) + + + this.WsChatService.receiveStreamNotifyRoom((message) => { + + if(message.fields.eventName == this.id+'/'+'typing') { + + this.userThatIsTyping = this.usernameToDisplayName(message.fields.args[0]) + this.isTyping = message.fields.args[1] + this.otherUserType = message.fields.args[1] + + } else if (message.fields.eventName == this.id+'/'+'deleteMessage') {} + + }) this.WsChatService.registerCallback } async receiveMessageDelete() { - this.WsChatService.upateRoomEvents( + this.WsChatService.updateRoomEventss( this.id, "stream-notify-room", async (ChatMessage) => { @@ -121,8 +153,49 @@ export class RoomService { this.WsChatService.registerCallback } - send(msg) { - this.WsChatService.send(this.id, msg) + send() { + this.WsChatService.send(this.id, this.message) + this.message= '' + } + + + typing(text:string = this.message) { + + if(this.lastMessageTxt == text) { return false } + this.lastTimeType = new Date().getTime() + + const lastIsTyping = this.isTyping + if(text.length >= 1) { + this.isTyping = true + } else { + this.isTyping = false + } + + if(lastIsTyping != this.isTyping) { + this.WsChatService.sendStreamNotifyRoom(this.id, SessionStore.user.RochetChatUser, 'typing', this.isTyping) + } + + + this.lastMessageTxt = this.message + this.typingWatch() + } + + + private typingWatch() { + setTimeout(()=>{ + const now = new Date().getTime() + + if((now - this.lastTimeType) >= 2888) { + + if(this.isTyping == true) { + this.isTyping = false + this.WsChatService.sendStreamNotifyRoom(this.id, SessionStore.user.RochetChatUser, 'typing', this.isTyping) + } + } else { + //console.log(now - this.lastTimeType) + } + + }, 3000) } leave(rid?) { @@ -206,20 +279,25 @@ export class RoomService { if (this.hasLoadHistory) { return false } - this.storage.get('chatmsg' + this.id).then((messages = [])=>{ + this.storage.get('chatmsg' + this.id).then((messages = []) => { + + let localMessages = [] + messages.forEach(message => { message = this.fix_updatedAt(message) const wewMessage = new MessageService(this.storage) wewMessage.setData(message) - this.messages.push(wewMessage) + localMessages.push(wewMessage) }); + + this.messages = localMessages }) this.WsChatService.loadHistory(this.id, limit).then((chatHistory:chatHistory) => { console.log('loadHistory', chatHistory) - let localMessages = [] + //const sortedRoomList = this.sortService.sortDate(chatHistory.result.messages, "_updatedAt.$date") chatHistory.result.messages.reverse().forEach(message => { message = this.fix_updatedAt(message) @@ -230,9 +308,11 @@ export class RoomService { this.messages = localMessages - this.storage.set('chatmsg' + this.id, chatHistory.result.messages.reverse()) + console.log(chatHistory.result.messages); + this.storage.set('chatmsg' + this.id, chatHistory.result.messages.reverse()) + }) /* this.WsChatService.loadHistory(this.id, limit).then(async (chatHistory: chatHistory) => { @@ -348,6 +428,7 @@ export class RoomService { private calDateDuration(date = null) { this.duration = showDateDuration(date || this._updatedAt); + this._updatedAt = date || this._updatedAt } @@ -363,7 +444,11 @@ export class RoomService { } - // to add - countDownDate() { } + usernameToDisplayName(username) { + + const firstName = capitalizeTxt(username.split('.')[0]) + const lastName = capitalizeTxt(username.split('.')[1]) + return firstName + ' ' + lastName + } } diff --git a/src/app/services/chat/ws-chat-methods.service.ts b/src/app/services/chat/ws-chat-methods.service.ts index 6cc607027..2f30489a9 100644 --- a/src/app/services/chat/ws-chat-methods.service.ts +++ b/src/app/services/chat/ws-chat-methods.service.ts @@ -8,6 +8,7 @@ import { Rooms, Update as room } from 'src/app/models/chatMethod'; import { Storage } from '@ionic/storage'; 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'; @Injectable({ @@ -15,11 +16,13 @@ import { SortService } from '../functions/sort.service'; }) export class WsChatMethodsService { - - dm: {[key: string]: RoomService} = {} group: {[key: string]: RoomService} = {} + + _dm = [] + _group = [] + loadingWholeList = false dmCount = 0; @@ -30,6 +33,7 @@ export class WsChatMethodsService { private storage: Storage, private platform: Platform, private sqlservice: SqliteService, + private NativeNotificationService: NativeNotificationService, private sortService: SortService ) { (async()=>{ @@ -56,20 +60,56 @@ export class WsChatMethodsService { this.storage.set('Rooms', rooms); // console.log("ROOMS" + JSON.stringify(rooms)) + this.WsChatService.registerCallback({ + type:'Onmessage', + funx:(message)=>{ - rooms.result.update.forEach((roomData: room) => { - this.prepareRoom(roomData); + if(message.msg =='changed' && message.collection == "stream-room-messages") { + if(message.fields.args[0].rid) { + + setTimeout(()=>{ + console.log('sort this._dm', this._dm) + this._dm = this.sortService.sortDate(this._dm,'_updatedAt').reverse() + this._group = this.sortService.sortDate(this._group,'_updatedAt').reverse() + }, 100) + + } + + } + + if(message.msg =='changed' && message.collection == "stream-notify-room") { + if(message.fields.eventName.includes('deleteMessage')){ + setTimeout(()=>{ + console.log('sort this._dm', this._dm) + this._dm = this.sortService.sortDate(this._dm,'_updatedAt').reverse() + this._group = this.sortService.sortDate(this._group,'_updatedAt').reverse() + }, 100) + } + } + } + }) + + + await rooms.result.update.forEach( async (roomData: room) => { + await this.prepareRoom(roomData); }); + this._dm = this.sortService.sortDate(this._dm,'_updatedAt').reverse() + this._group = this.sortService.sortDate(this._group,'_updatedAt').reverse() + this.loadingWholeList = false } + + subscribeToRoom() { for (const id in this.dm) { this.WsChatService.streamRoomMessages(id).then((subscription)=>{ console.log('streamRoomMessages', subscription) }) + + this.WsChatService.subStreamNotifyRoom(id, 'typing', false) this.WsChatService.streamNotifyRoomDeleteMessage(id).then((subscription)=>{ console.log('streamNotifyRoomDeleteMessage', subscription); }) @@ -79,6 +119,8 @@ export class WsChatMethodsService { this.WsChatService.streamRoomMessages(id).then((subscription)=>{ console.log('streamRoomMessages', subscription) }) + + this.WsChatService.subStreamNotifyRoom(id, 'typing', false) this.WsChatService.streamNotifyRoomDeleteMessage(id).then((subscription)=>{ console.log('streamNotifyRoomDeleteMessage', subscription); }) @@ -90,13 +132,13 @@ export class WsChatMethodsService { subscribeToRoomUpdate(id, roomData) { - this.WsChatService.streamRoomMessages(id).then((subscription)=>{ + this.WsChatService.streamRoomMessages(id).then((subscription)=> { console.log('streamRoomMessages', subscription) }) - this.WsChatService.streamRoomMessages(id).then((subscription)=>{ + this.WsChatService.streamRoomMessages(id).then((subscription) => { console.log('streamRoomMessages', subscription) }) - this.WsChatService.streamNotifyLogged().then((subscription=>{ + this.WsChatService.streamNotifyLogged().then((subscription=> { console.log('streamRoomMessages', subscription) })) this.WsChatService.streamNotifyRoomDeleteMessage(id).then((subscription)=>{ @@ -109,10 +151,11 @@ export class WsChatMethodsService { } - prepareRoom(roomData){ + prepareRoom(roomData) { let room:RoomService; - room = new RoomService(this.WsChatService, new MessageService(this.storage), this.storage, this.platform, this.sqlservice, this.sortService) + room = new RoomService(this.WsChatService, new MessageService(this.storage), this.storage, this.platform, this.sqlservice, this.NativeNotificationService, this.sortService) + room.setData({ customFields: roomData.customFields, id: this.getRoomId(roomData), @@ -128,12 +171,12 @@ export class WsChatMethodsService { let roomId = this.getRoomId(roomData) if(this.isIndividual(roomData)) { - console.log(room); - this.dm[roomId] = room + this._dm.push(room) this.dmCount++ } else { this.group[roomId] = room + this._group.push(room) this.groupCount++ } } diff --git a/src/app/services/chat/ws-chat.service.ts b/src/app/services/chat/ws-chat.service.ts index d0b29a972..724b9cce1 100644 --- a/src/app/services/chat/ws-chat.service.ts +++ b/src/app/services/chat/ws-chat.service.ts @@ -282,7 +282,73 @@ export class WsChatService { }); } + sendStreamNotifyRoom(roomId : string, username, event: 'typing', param: any) { + const requestId = uuidv4() + + let message = { + msg: "method", + method: "stream-notify-room", + id: requestId, + params: [ + `${roomId}/${event}`, + username, + param + ] + }; + + this.ws.send({message, requestId}) + + return new Promise((resolve, reject) => { + this.ws.registerCallback({type:'Onmessage', funx:(message)=>{ + if(message.id == requestId || deepFind(message,'result.id') == requestId) { // same request send + resolve(message) + return true + } + }}) + }); + + } + + + subStreamNotifyRoom(roomId : string , event: 'typing' | 'deleteMessage', param: any) { + + const requestId = uuidv4() + + let message = { + msg: "sub", + id: requestId, + name: "stream-notify-room", + params:[ + `${roomId}/${event}`, + param + ] + } + + + this.ws.send({message, requestId}) + + return new Promise((resolve, reject) => { + this.ws.registerCallback({type:'Onmessage', funx:(message)=>{ + if(message.id == requestId ) { // same request send + resolve(message) + return true + } + }}) + }); + + } + + receiveStreamNotifyRoom(funx: Function) { + + this.ws.registerCallback({ + type:'Onmessage', + funx:(message)=> { + if(message.collection == "stream-notify-room" && message.msg == 'changed') { + funx(message) + } + }}) + } loadHistory(roomId, limit: number = 50) { @@ -359,7 +425,7 @@ export class WsChatService { } -upateRoomEvents(roomId, collection:string, funx: Function, ) { +updateRoomEventss(roomId, collection:string, funx: Function, ) { this.ws.registerCallback({ type:'Onmessage', @@ -451,7 +517,7 @@ upateRoomEvents(roomId, collection:string, funx: Function, ) { return new Promise((resolve, reject) => { this.ws.registerCallback({type:'Onmessage', funx:(message)=>{ if(message.id == requestId || deepFind(message,'result.id') == requestId) { // same request send - resolve('') + resolve(message) return true } }}) diff --git a/src/app/services/functions/object.service.ts b/src/app/services/functions/object.service.ts index 9b52713c8..562d28868 100644 --- a/src/app/services/functions/object.service.ts +++ b/src/app/services/functions/object.service.ts @@ -8,11 +8,11 @@ export class ObjectService { constructor() { } - deepFind(obj, path) { + deepFind(obj, path):any { var paths = path.split('.') , current = obj , i; - + for (i = 0; i < paths.length; ++i) { if (current[paths[i]] == undefined) { return undefined; diff --git a/src/app/services/functions/sort.service.ts b/src/app/services/functions/sort.service.ts index dc4c4b84f..6f65ab277 100644 --- a/src/app/services/functions/sort.service.ts +++ b/src/app/services/functions/sort.service.ts @@ -26,8 +26,18 @@ export class SortService { } sortDate(array = [], path: string) { + return array.sort( (a,b)=> { - return new Date(this.ObjectService.deepFind(b, path)).getTime() - new Date(this.ObjectService.deepFind(a, path)).getTime() + + /* console.log("AAA"+new Date(this.ObjectService.deepFind(a, path))); + + console.log("BB"+new Date(this.ObjectService.deepFind(b, path))); */ + + return (new Date(this.ObjectService.deepFind(a, path)) < new Date(this.ObjectService.deepFind(b, path))) ? -1 : ((new Date(this.ObjectService.deepFind(a, path)) > new Date(this.ObjectService.deepFind(b, path))) ? 1 : 0); + + //return new Date(this.ObjectService.deepFind(b, path)).getTime() - new Date(this.ObjectService.deepFind(a, path)).getTime(); + }) + } } diff --git a/src/app/services/native-notification.service.spec.ts b/src/app/services/native-notification.service.spec.ts new file mode 100644 index 000000000..f3ffd7e3c --- /dev/null +++ b/src/app/services/native-notification.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { NativeNotificationService } from './native-notification.service'; + +describe('NativeNotificationService', () => { + let service: NativeNotificationService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(NativeNotificationService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/native-notification.service.ts b/src/app/services/native-notification.service.ts new file mode 100644 index 000000000..dea860b50 --- /dev/null +++ b/src/app/services/native-notification.service.ts @@ -0,0 +1,51 @@ +import { Injectable } from '@angular/core'; +import { Platform } from '@ionic/angular'; +import { CapacitorConfig } from '@capacitor/cli'; +import { LocalNotifications } from '@capacitor/local-notifications'; + + +@Injectable({ + providedIn: 'root' +}) +export class NativeNotificationService { + + constructor(private platform: Platform,) { + this.askForPermission() + } + + askForPermission() { + + LocalNotifications.requestPermissions() + + LocalNotifications.checkPermissions().then((data)=>{ + console.log('success', data) + }).catch((data)=>{ + console.log('error', data) + }) + + } + + sendNotificationChat({title = 'User', icon = '', message = 'hello'}) { + + LocalNotifications.schedule({ + notifications:[ + { + title : title, + body : message, + id : new Date().getTime() + } + ] + }); + + } + + isDesktop() { + if (this.platform.is('desktop') || this.platform.is('mobileweb')) { + return true + } else { + return false + + } + } + +} diff --git a/src/app/services/sqlite.service.ts b/src/app/services/sqlite.service.ts index 37638c4c0..1cf19d6e6 100644 --- a/src/app/services/sqlite.service.ts +++ b/src/app/services/sqlite.service.ts @@ -127,7 +127,8 @@ export class SqliteService { DateEnd varchar(255), Detail varchar(255), Description varchar(255), - publications Text + publications Text, + publicationsDetails Text )`, []) .then((res) => { console.log("Sucess action Table created: ", res) @@ -195,8 +196,8 @@ export class SqliteService { public addactions(data) { console.log('Action insert', data) this.dbInstance.executeSql(` - INSERT OR IGNORE INTO ${this.actions} (ActionType,DateBegin,DateEnd,Description,Detail,ProcessId,publications) - VALUES ('${data.ActionType}','${data.DateBegin}', '${data.DateEnd}','${data.Description}','${data.Detail}','${data.ProcessId}','${data.publications}')`, []) + INSERT OR IGNORE INTO ${this.actions} (ActionType,DateBegin,DateEnd,Description,Detail,ProcessId,publications,publicationsDetails) + VALUES ('${data.ActionType}','${data.DateBegin}', '${data.DateEnd}','${data.Description}','${data.Detail}','${data.ProcessId}','${data.publications}','${data.publicationsDetails}')`, []) .then(() => { console.log("action add with Success"); @@ -319,6 +320,22 @@ export class SqliteService { } + //updatePublicationsDetails + public updatePublicationsDetails(id, data) { + try { + console.log("update action data", data) + this.dbInstance.executeSql(` + UPDATE ${this.actions} SET publicationsDetails = ? WHERE ProcessId = ${id}`, [data]) + .then(() => { + console.log("action update with Success"); + + }, (e) => { + console.log(JSON.stringify(e.err)); + }); + } catch(error) {} + + } + //updateChatMsg public updateChatMsg(id, data) { let jsonId = JSON.stringify(id) diff --git a/src/app/shared/chat/group-messages/group-messages.page.html b/src/app/shared/chat/group-messages/group-messages.page.html index 234c7876a..2bbb6b651 100644 --- a/src/app/shared/chat/group-messages/group-messages.page.html +++ b/src/app/shared/chat/group-messages/group-messages.page.html @@ -234,18 +234,18 @@
- +
- - 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 70d0fcec0..b14ec33ec 100644 --- a/src/app/shared/chat/group-messages/group-messages.page.ts +++ b/src/app/shared/chat/group-messages/group-messages.page.ts @@ -220,6 +220,11 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe window.removeEventListener('scroll', this.scrollChangeCallback, true); } + + changeInput() { + this.wsChatMethodsService.getDmRoom(this.roomId).typing() + } + async getChatMembers() { //return await this.chatService.getMembers(roomId).toPromise(); this.chatService.getAllUsers().subscribe(res => { @@ -302,8 +307,7 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe } sendMessage() { - this.wsChatMethodsService.getGroupRoom(this.roomId).send(this.message) - this.message = ""; + this.wsChatMethodsService.getGroupRoom(this.roomId).send() } deleteMessage(msgId: string, room:any) { diff --git a/src/app/shared/chat/messages/messages.page.html b/src/app/shared/chat/messages/messages.page.html index bb03a9645..10a3032be 100644 --- a/src/app/shared/chat/messages/messages.page.html +++ b/src/app/shared/chat/messages/messages.page.html @@ -215,20 +215,20 @@
+ class="message-input" rows="1" [(ngModel)]="wsChatMethodsService.getDmRoom(roomId).message" (ionChange)="changeInput()">
- -