This commit is contained in:
Eudes Inácio
2022-01-30 10:45:27 +01:00
22 changed files with 440 additions and 115 deletions
+101 -16
View File
@@ -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
}
}
@@ -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++
}
}
+68 -2
View File
@@ -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
}
}})