From 66098ae4e836d22fefded234c653a02ffe50c73d Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Wed, 26 Jan 2022 17:28:55 +0100 Subject: [PATCH 1/9] notification --- src/app/home/home.page.ts | 6 ++- src/app/services/chat/room.service.ts | 24 +++++++--- .../services/chat/ws-chat-methods.service.ts | 11 ++--- .../native-notification.service.spec.ts | 16 +++++++ .../services/native-notification.service.ts | 45 +++++++++++++++++++ 5 files changed, 87 insertions(+), 15 deletions(-) create mode 100644 src/app/services/native-notification.service.spec.ts create mode 100644 src/app/services/native-notification.service.ts diff --git a/src/app/home/home.page.ts b/src/app/home/home.page.ts index 2c554902f..436971e6b 100644 --- a/src/app/home/home.page.ts +++ b/src/app/home/home.page.ts @@ -29,6 +29,7 @@ import { RouteService } from 'src/app/services/route.service'; import { WsChatService } from 'src/app/services/chat/ws-chat.service'; import { environment } from 'src/environments/environment'; import { v4 as uuidv4 } from 'uuid' +import { NativeNotificationService } from 'src/app/services/native-notification.service'; @Component({ selector: 'app-home', @@ -100,10 +101,13 @@ export class HomePage implements OnInit { private screenOrientation: ScreenOrientation, private sqliteservice: SqliteService, private RouteService: RouteService, - private WsChatService: WsChatService) { + private WsChatService: WsChatService, + private NativeNotificationService: NativeNotificationService) { /* this.webNotificationPopupService.askNotificationPermission() */ + this.NativeNotificationService.askForPermission() + this.router.events.subscribe((val) => { document.querySelectorAll('ion-modal').forEach((e: any) => e.remove()) document.querySelectorAll('popover-viewport').forEach((e: any) => e.remove()) diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index 5ef5762f0..9d46dd229 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.service.ts @@ -1,13 +1,15 @@ -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'; @Injectable({ providedIn: 'root' }) @@ -37,7 +39,10 @@ export class RoomService { private storage: Storage, private platform: Platform, private sqlservice: SqliteService, - ) { } + private NativeNotificationService: NativeNotificationService + ) { + this.NativeNotificationService.askForPermission() + } setData({ customFields, id, name, t, lastMessage, _updatedAt }) { this.customFields = customFields @@ -72,6 +77,13 @@ export class RoomService { }, 100) + 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) diff --git a/src/app/services/chat/ws-chat-methods.service.ts b/src/app/services/chat/ws-chat-methods.service.ts index b43c3b155..8d7609575 100644 --- a/src/app/services/chat/ws-chat-methods.service.ts +++ b/src/app/services/chat/ws-chat-methods.service.ts @@ -8,14 +8,13 @@ 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'; @Injectable({ providedIn: 'root' }) export class WsChatMethodsService { - - dm: {[key: string]: RoomService} = {} group: {[key: string]: RoomService} = {} @@ -29,6 +28,7 @@ export class WsChatMethodsService { private storage: Storage, private platform: Platform, private sqlservice: SqliteService, + private NativeNotificationService: NativeNotificationService ) { (async()=>{ await this.getAllRooms(); @@ -48,10 +48,7 @@ export class WsChatMethodsService { rooms.result.update.forEach((roomData: room) => { let room:RoomService; - //console.log(roomData); - - - room = new RoomService(this.WsChatService, new MessageService(), this.storage, this.platform, this.sqlservice) + room = new RoomService(this.WsChatService, new MessageService(), this.storage, this.platform, this.sqlservice, this.NativeNotificationService) room.setData({ customFields: roomData.customFields, id: this.getRoomId(roomData), @@ -73,8 +70,6 @@ export class WsChatMethodsService { this.groupCount++ } - - }); this.loadingWholeList = false 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..ec45d3fa4 --- /dev/null +++ b/src/app/services/native-notification.service.ts @@ -0,0 +1,45 @@ +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() + + } + + sendNotificationChat({title = 'User', icon = '', message = 'hello'}) { + + LocalNotifications.schedule({ + notifications:[ + { + title : 'tile', + body : 'df', + id : 55 + } + ] + }); + + } + + isDesktop() { + if (this.platform.is('desktop') || this.platform.is('mobileweb')) { + return true + } else { + return false + + } + } + +} From 005efbd20be628ac612c1b839adfc755cc0dce50 Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Fri, 28 Jan 2022 16:15:20 +0100 Subject: [PATCH 2/9] add typing --- src/app/pages/chat/chat.page.html | 9 ++- .../group-messages/group-messages.page.html | 6 +- .../group-messages/group-messages.page.ts | 9 ++- .../pages/chat/messages/messages.page.html | 6 +- src/app/pages/chat/messages/messages.page.ts | 4 + src/app/services/chat/room.service.ts | 76 ++++++++++++++++++- .../services/chat/ws-chat-methods.service.ts | 4 + src/app/services/chat/ws-chat.service.ts | 66 ++++++++++++++++ .../group-messages/group-messages.page.html | 6 +- .../group-messages/group-messages.page.ts | 8 +- .../shared/chat/messages/messages.page.html | 6 +- src/app/shared/chat/messages/messages.page.ts | 8 +- 12 files changed, 182 insertions(+), 26 deletions(-) diff --git a/src/app/pages/chat/chat.page.html b/src/app/pages/chat/chat.page.html index 770139c39..99fa196ef 100644 --- a/src/app/pages/chat/chat.page.html +++ b/src/app/pages/chat/chat.page.html @@ -67,7 +67,8 @@
{{room.value.duration}}
- {{room.value.lastMessage.msg}} + {{room.value.lastMessage.msg}} + A escrever ... @@ -121,7 +122,11 @@
{{countDownDate(group.value.customFields.countDownDate, group.value.id)}}
-
{{group.value.lastMessage.u.name}}: {{group.value.lastMessage.msg}}
+
{{group.value.lastMessage.u.name}}: {{group.value.lastMessage.msg}}
+ +
{{group.value.lastMessage.u.name}}: {{group.value.lastMessage.msg}}
+
{{group.value.userThatIsTyping}} A escrever ...
+
diff --git a/src/app/pages/chat/group-messages/group-messages.page.html b/src/app/pages/chat/group-messages/group-messages.page.html index 8f9430043..fba00d888 100644 --- a/src/app/pages/chat/group-messages/group-messages.page.html +++ b/src/app/pages/chat/group-messages/group-messages.page.html @@ -220,18 +220,18 @@
- +
- - 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 406c41827..9ad1f4e7e 100644 --- a/src/app/pages/chat/group-messages/group-messages.page.ts +++ b/src/app/pages/chat/group-messages/group-messages.page.ts @@ -39,7 +39,6 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { showLoader: boolean; isGroupCreated:boolean; loggedUser: any; - message:any; messages:any; room:any; @@ -184,6 +183,11 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { this.currentPosition = scroll; } + + changeInput() { + this.wsChatMethodsService.getDmRoom(this.roomId).typing() + } + async goToEvent(eventId: any) { let classs; if (window.innerWidth < 701) { @@ -323,8 +327,7 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy { sendMessage() { - this.wsChatMethodsService.getGroupRoom(this.roomId).send(this.message) - this.message = ""; + this.wsChatMethodsService.getGroupRoom(this.roomId).send() } async openOptions() { diff --git a/src/app/pages/chat/messages/messages.page.html b/src/app/pages/chat/messages/messages.page.html index d9027acc9..43947b077 100644 --- a/src/app/pages/chat/messages/messages.page.html +++ b/src/app/pages/chat/messages/messages.page.html @@ -212,7 +212,7 @@
- +
- - diff --git a/src/app/pages/chat/messages/messages.page.ts b/src/app/pages/chat/messages/messages.page.ts index b5742811f..8627b05f1 100644 --- a/src/app/pages/chat/messages/messages.page.ts +++ b/src/app/pages/chat/messages/messages.page.ts @@ -343,6 +343,10 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy { window.removeEventListener('scroll', this.scrollChangeCallback, true); } + changeInput() { + this.wsChatMethodsService.getDmRoom(this.roomId).typing() + } + sendMessage() { let body = { "message": diff --git a/src/app/services/chat/room.service.ts b/src/app/services/chat/room.service.ts index 9d46dd229..e9d02ea06 100644 --- a/src/app/services/chat/room.service.ts +++ b/src/app/services/chat/room.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 { SessionStore } from 'src/app/store/session.service'; +import { capitalizeTxt } from 'src/plugin/text' @Injectable({ providedIn: 'root' }) @@ -27,6 +28,12 @@ export class RoomService { _updatedAt = {} private hasLoadHistory = false duration = '' + isTyping = false + otherUserType = false + lastTimeType = null + message = '' + lastMessageTxt = '' + userThatIsTyping = '' private ToastService = ToastsService mgsArray = []; @@ -95,10 +102,67 @@ export class RoomService { } ) + + + + this.WsChatService.receiveStreamNotifyRoom((message) => { + + console.log(message.fields) + + 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') {} + + }) } - 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?) { @@ -324,7 +388,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 8d7609575..b07a7737d 100644 --- a/src/app/services/chat/ws-chat-methods.service.ts +++ b/src/app/services/chat/ws-chat-methods.service.ts @@ -81,12 +81,16 @@ export class WsChatMethodsService { this.WsChatService.streamRoomMessages(id).then((subscription)=>{ console.log('streamRoomMessages', subscription) }) + + this.WsChatService.subStreamNotifyRoom(id, 'typing', false) } for (const id in this.group) { this.WsChatService.streamRoomMessages(id).then((subscription)=>{ console.log('streamRoomMessages', subscription) }) + + this.WsChatService.subStreamNotifyRoom(id, 'typing', false) } this.WsChatService.streamNotifyLogged().then((subscription=>{ diff --git a/src/app/services/chat/ws-chat.service.ts b/src/app/services/chat/ws-chat.service.ts index 872970ece..39f6327a2 100644 --- a/src/app/services/chat/ws-chat.service.ts +++ b/src/app/services/chat/ws-chat.service.ts @@ -231,8 +231,74 @@ export class WsChatService { deleteMessage() {} createRoom() {} + 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('') + 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('') + 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) { const requestId = uuidv4() 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 7b1ee94b9..75dd39a56 100644 --- a/src/app/shared/chat/group-messages/group-messages.page.html +++ b/src/app/shared/chat/group-messages/group-messages.page.html @@ -260,18 +260,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 71a7b751c..227265f8f 100644 --- a/src/app/shared/chat/group-messages/group-messages.page.ts +++ b/src/app/shared/chat/group-messages/group-messages.page.ts @@ -216,6 +216,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 => { @@ -314,8 +319,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) { diff --git a/src/app/shared/chat/messages/messages.page.html b/src/app/shared/chat/messages/messages.page.html index 5ceb5633e..e8aff450e 100644 --- a/src/app/shared/chat/messages/messages.page.html +++ b/src/app/shared/chat/messages/messages.page.html @@ -206,20 +206,20 @@
+ class="message-input" rows="1" [(ngModel)]="wsChatMethodsService.getDmRoom(roomId).message" (ionChange)="changeInput()">
- -