Files
doneit-web/src/app/services/chat/room.service.ts
T

418 lines
9.9 KiB
TypeScript
Raw Normal View History

2022-01-26 17:28:55 +01:00
import { Injectable } from '@angular/core';
2022-01-12 12:44:51 +01:00
import { WsChatService } from 'src/app/services/chat/ws-chat.service';
2022-01-26 17:28:55 +01:00
import { MessageService } from 'src/app/services/chat/message.service';
import { showDateDuration } from 'src/plugin/showDateDuration';
2022-01-13 10:19:14 +01:00
import { ToastsService } from '../toast.service';
2022-02-08 17:44:15 +01:00
import { chatHistory } from 'src/app/models/chatMethod';
2022-01-19 09:12:30 +01:00
import { Storage } from '@ionic/storage';
2022-01-21 16:55:05 +01:00
import { Platform } from '@ionic/angular';
import { SqliteService } from 'src/app/services/sqlite.service';
2022-01-26 17:28:55 +01:00
import { NativeNotificationService } from 'src/app/services/native-notification.service';
import { SessionStore } from 'src/app/store/session.service';
2022-01-28 16:15:20 +01:00
import { capitalizeTxt } from 'src/plugin/text'
2022-01-28 15:59:01 +01:00
import { SortService } from '../functions/sort.service';
2022-01-29 19:21:46 +01:00
import { chatUser } from 'src/app/models/chatMethod';
2022-02-02 20:16:12 +01:00
import { environment } from 'src/environments/environment';
2022-02-03 21:01:53 +01:00
import { ChatService } from 'src/app/services/chat.service';
2022-02-07 17:55:00 +01:00
import { NfService } from 'src/app/services/chat/nf.service'
2022-02-07 18:48:33 +01:00
import alasql from 'alasql'
2022-01-10 13:31:15 +01:00
@Injectable({
providedIn: 'root'
})
export class RoomService {
2022-01-28 15:59:01 +01:00
messages: MessageService[] = []
2022-01-20 14:31:49 +01:00
storageMessage: any[] = [];
2022-01-11 13:03:43 +01:00
lastMessage: MessageService;
2022-02-07 15:51:21 +01:00
2022-01-20 14:31:49 +01:00
customFields: any;
2022-01-11 13:03:43 +01:00
id = ''
t = ''
2022-01-13 21:24:43 +01:00
name = ''
2022-01-11 20:56:21 +01:00
_updatedAt = {}
2022-01-11 15:43:09 +01:00
private hasLoadHistory = false
2022-01-20 14:31:49 +01:00
duration = ''
2022-01-28 16:15:20 +01:00
isTyping = false
otherUserType = false
lastTimeType = null
message = ''
lastMessageTxt = ''
userThatIsTyping = ''
2022-01-13 13:45:04 +01:00
2022-01-13 10:26:40 +01:00
private ToastService = ToastsService
2022-01-20 14:31:49 +01:00
mgsArray = [];
2022-01-10 23:52:33 +01:00
2022-01-20 14:31:49 +01:00
scrollDown = () => { }
2022-01-29 20:16:39 +01:00
/**
* @description get user list from ws-chat-methods.service
* @returns chatUser[]
*/
2022-01-29 19:21:46 +01:00
getAllUsers = (): chatUser[] => {
return []
}
2022-01-14 14:50:47 +01:00
2022-02-02 20:16:12 +01:00
sortRoomList = () => {}
2022-02-04 00:22:35 +01:00
uploadAttachment = (formData) => {}
2022-02-02 20:16:12 +01:00
2022-01-10 23:52:33 +01:00
constructor(
2022-01-12 12:44:51 +01:00
public WsChatService: WsChatService,
2022-01-11 20:56:21 +01:00
private MessageService: MessageService,
2022-01-19 09:12:30 +01:00
private storage: Storage,
2022-01-21 16:55:05 +01:00
private platform: Platform,
private sqlservice: SqliteService,
2022-01-28 16:26:21 +01:00
private NativeNotificationService: NativeNotificationService,
2022-01-28 17:34:27 +01:00
private sortService: SortService,
2022-02-03 21:01:53 +01:00
private chatService: ChatService,
2022-02-07 17:55:00 +01:00
private NfService: NfService
2022-01-28 16:37:18 +01:00
) {
2022-01-26 17:28:55 +01:00
this.NativeNotificationService.askForPermission()
}
2022-01-10 23:52:33 +01:00
2022-02-07 20:18:48 +01:00
setData({ customFields, id, name, t, lastMessage = new MessageService(this.storage, this.NfService, this.WsChatService), _updatedAt }) {
2022-01-13 21:24:43 +01:00
this.customFields = customFields
2022-01-11 13:03:43 +01:00
this.id = id
this.name = name
this.t = t
2022-01-11 13:03:43 +01:00
this.lastMessage = lastMessage
2022-01-11 20:56:21 +01:00
this._updatedAt = _updatedAt
this.calDateDuration()
2022-01-10 23:52:33 +01:00
}
2022-02-03 15:52:21 +01:00
isSenderIsNotMe(ChatMessage) {
return SessionStore.user.RochetChatUser != ChatMessage.u.username
}
2022-01-11 15:43:09 +01:00
receiveMessage() {
2022-01-14 11:47:55 +01:00
2022-01-28 17:33:26 +01:00
this.WsChatService.updateRoomEventss(
2022-01-14 11:47:55 +01:00
this.id,
2022-01-28 15:31:52 +01:00
"stream-room-messages",
2022-01-14 11:47:55 +01:00
(ChatMessage) => {
2022-02-03 21:01:53 +01:00
ChatMessage = ChatMessage.fields.args[0]
2022-01-30 09:34:56 +01:00
2022-02-09 14:07:34 +01:00
ChatMessage = this.fix_updatedAt(ChatMessage)
// console.log('recivemessage', ChatMessage)
const messageIsFound = this.messages.find((message) => {
message._id == ChatMessage._id
})
if(!messageIsFound) {
2022-02-02 20:16:12 +01:00
const message = this.prepareMessage(ChatMessage)
2022-01-14 14:50:47 +01:00
2022-02-02 20:16:12 +01:00
this.lastMessage = message
if (message.t == 'r') { this.name = message.msg }
this.calDateDuration(ChatMessage._updatedAt)
setTimeout(() => {
this.scrollDown()
}, 100)
2022-02-03 15:52:21 +01:00
this.NativeNotificationService.sendNotificationChat({
message: message.msg,
title: this.name
});
2022-02-02 20:16:12 +01:00
this.addMessageDB(ChatMessage)
2022-01-26 17:28:55 +01:00
}
2022-02-09 14:07:34 +01:00
2022-01-14 11:47:55 +01:00
}
)
2022-01-28 16:15:20 +01:00
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') {}
})
2022-01-28 15:31:52 +01:00
}
2022-02-02 20:16:12 +01:00
addMessageDB(ChatMessage) {
this.storage.get('chatmsg' + this.id).then((messages: any = []) => {
2022-02-08 17:44:15 +01:00
if(messages==null) messages = []
2022-02-07 20:18:48 +01:00
delete ChatMessage.temporaryData
2022-02-02 20:16:12 +01:00
messages.push(ChatMessage)
this.storage.set('chatmsg' + this.id, messages)
})
}
2022-01-28 15:31:52 +01:00
async receiveMessageDelete() {
2022-01-28 17:33:26 +01:00
this.WsChatService.updateRoomEventss(
2022-01-28 15:31:52 +01:00
this.id,
"stream-notify-room",
async (ChatMessage) => {
2022-01-30 09:34:56 +01:00
const DeletedMessageId = ChatMessage.fields.args[0]._id;
console.log(DeletedMessageId);
this.deleteMessage(DeletedMessageId)
2022-01-28 15:31:52 +01:00
}
)
2022-02-08 19:17:44 +01:00
2022-01-11 15:43:09 +01:00
}
2022-01-30 09:34:56 +01:00
/**
* @description delete message in the view
* @param id message ID
*/
deleteMessage(id) {
this.messages.forEach((message, index) => {
if(message._id == id) {
this.messages.splice(index, 1)
this.deleteMessageFromDb(id)
//Get previous last message from room
const previousLastMessage = this.messages.slice(-1)[0];
this.lastMessage = previousLastMessage;
this.calDateDuration(previousLastMessage._updatedAt)
}
})
}
/**
* @description delete message in the DB. get all messages, delete then corresponding message and update the store
* @param id message ID
*/
private deleteMessageFromDb(id) {
this.storage.get('chatmsg' + this.id).then((messages: any = []) => {
2022-02-08 17:44:15 +01:00
if(messages==null) messages = []
2022-01-30 09:34:56 +01:00
messages.forEach((message, index) => {
if(message._id == id) {
messages.splice(index, 1)
}
2022-01-30 09:34:56 +01:00
})
this.storage.set('chatmsg' + this.id, messages).then((value) => {
console.log('MSG SAVED ON STORAGE', value)
});
})
}
/**
* @description sen text message
*/
2022-02-07 20:18:48 +01:00
async send({file = null, attachments = null, temporaryData = {}}) {
2022-02-02 20:16:12 +01:00
2022-02-03 21:01:53 +01:00
let offlineChatMessage = {
rid: this.id,
msg: this.message,
attachments,
file,
2022-02-07 20:18:48 +01:00
temporaryData
2022-02-03 21:01:53 +01:00
}
2022-02-07 20:18:48 +01:00
console.log('offlineChatMessage', offlineChatMessage)
2022-02-03 21:01:53 +01:00
this.addMessageDB(offlineChatMessage)
const message: MessageService = this.prepareMessage(offlineChatMessage)
2022-02-02 20:16:12 +01:00
2022-02-03 21:01:53 +01:00
setTimeout(() => {
this.scrollDown()
}, 150)
2022-02-02 20:16:12 +01:00
2022-02-03 21:01:53 +01:00
this.lastMessage = message
2022-02-02 20:16:12 +01:00
2022-02-08 17:44:15 +01:00
message.send()
2022-02-04 00:22:35 +01:00
2022-02-03 21:01:53 +01:00
this.calDateDuration(message._updatedAt)
this.sortRoomList()
2022-02-04 00:22:35 +01:00
this.message= ''
2022-02-03 21:01:53 +01:00
}
redefinedMessage (message: MessageService, ChatMessage) {
ChatMessage = this.fix_updatedAt(ChatMessage)
message.setData(ChatMessage)
if( new Date(this.lastMessage._updatedAt).getTime() < new Date(message._updatedAt).getTime()) {
this.lastMessage = message
this.calDateDuration(message._updatedAt)
}
this.sortRoomList()
2022-01-28 16:15:20 +01:00
}
typing(text:string = this.message) {
if(this.lastMessageTxt == text) { return false }
this.lastTimeType = new Date().getTime()
const lastIsTyping = this.isTyping
if(text.length >= 1) {
2022-01-28 17:34:27 +01:00
this.isTyping = true
2022-01-28 16:15:20 +01:00
} else {
this.isTyping = false
}
if(lastIsTyping != this.isTyping) {
this.WsChatService.sendStreamNotifyRoom(this.id, SessionStore.user.RochetChatUser, 'typing', this.isTyping)
}
2022-01-28 17:34:27 +01:00
2022-01-28 16:15:20 +01:00
this.lastMessageTxt = this.message
this.typingWatch()
}
private typingWatch() {
setTimeout(()=>{
const now = new Date().getTime()
if((now - this.lastTimeType) >= 2888) {
2022-01-28 17:34:27 +01:00
2022-01-28 16:15:20 +01:00
if(this.isTyping == true) {
this.isTyping = false
this.WsChatService.sendStreamNotifyRoom(this.id, SessionStore.user.RochetChatUser, 'typing', this.isTyping)
}
} else {
//console.log(now - this.lastTimeType)
2022-01-28 16:15:20 +01:00
}
}, 3000)
2022-01-11 15:43:09 +01:00
}
2022-01-29 19:21:46 +01:00
private setTypingOff() {
this.typing('')
}
roomLeave() {
this.setTypingOff()
}
open() {
// this.typing(this.message)
}
2022-01-29 19:21:46 +01:00
2022-01-26 09:19:54 +01:00
leave(rid?) {
this.WsChatService.leaveRoom(this.id)
}
2022-01-21 16:55:05 +01:00
isJson(str) {
try {
JSON.parse(str);
} catch (e) {
return "";
}
return JSON.parse(str);
}
2022-02-07 20:18:48 +01:00
async restoreMessageFromDB() {
await this.storage.get('chatmsg' + this.id).then( async (messages = []) => {
2022-02-04 00:22:35 +01:00
if(messages==null) messages = []
2022-02-07 20:18:48 +01:00
await messages.forEach( async (ChatMessage, index) => {
2022-02-09 14:07:34 +01:00
const wewMessage = this.prepareMessage(ChatMessage, false)
2022-02-02 20:16:12 +01:00
2022-02-09 14:07:34 +01:00
if(wewMessage.offline == false) {
this.prepareMessage(ChatMessage)
2022-02-02 20:16:12 +01:00
}
2022-01-28 15:28:21 +01:00
});
2022-01-28 16:37:18 +01:00
2022-02-07 20:18:48 +01:00
setTimeout(()=> {
this.scrollDown()
}, 50)
2022-01-28 15:28:21 +01:00
})
2022-02-02 20:16:12 +01:00
}
// runs onces only
2022-02-07 20:18:48 +01:00
async loadHistory(limit = 100) {
2022-02-02 20:16:12 +01:00
if (this.hasLoadHistory) { return false }
2022-02-09 14:07:34 +01:00
this.restoreMessageFromDB()
2022-01-28 15:28:21 +01:00
2022-02-07 20:18:48 +01:00
await this.WsChatService.loadHistory(this.id, limit).then( async (chatHistory:chatHistory) => {
2022-01-21 16:55:05 +01:00
console.log('loadHistory', chatHistory)
2022-02-07 20:18:48 +01:00
this.messages = []
2022-01-21 11:42:02 +01:00
2022-02-07 20:18:48 +01:00
await chatHistory.result.messages.reverse().forEach( async (message) => {
this.prepareMessage(message)
2022-01-21 16:55:05 +01:00
});
2022-01-28 15:28:21 +01:00
})
2022-01-28 15:31:52 +01:00
2022-01-20 14:31:49 +01:00
setTimeout(() => {
2022-01-14 14:58:47 +01:00
this.scrollDown()
}, 50)
2022-01-11 15:43:09 +01:00
this.hasLoadHistory = true
}
2022-01-20 14:31:49 +01:00
2022-01-30 09:34:56 +01:00
2022-02-09 14:07:34 +01:00
prepareMessage(message, save = true): MessageService {
2022-01-30 09:34:56 +01:00
message = this.fix_updatedAt(message)
2022-02-07 20:18:48 +01:00
const wewMessage = new MessageService(this.storage, this.NfService, this.WsChatService)
2022-01-30 09:34:56 +01:00
wewMessage.setData(message)
2022-02-09 14:07:34 +01:00
if (save) {
this.messages.push(wewMessage)
}
2022-01-30 09:34:56 +01:00
return wewMessage
}
2022-01-20 14:31:49 +01:00
async returnData(res) {
return res;
}
2022-01-19 09:12:30 +01:00
2022-01-20 14:31:49 +01:00
ReactToMessage() { }
2022-01-10 23:52:33 +01:00
2022-01-11 20:56:21 +01:00
private calDateDuration(date = null) {
this.duration = showDateDuration(date || this._updatedAt);
2022-01-28 19:01:24 +01:00
this._updatedAt = date || this._updatedAt
2022-01-11 20:56:21 +01:00
}
private fix_updatedAt(message) {
2022-01-20 14:31:49 +01:00
if (message.result) {
//console.log('FIX UPDATE ', message.result)
2022-01-11 20:56:21 +01:00
message.result._updatedAt = message.result._updatedAt['$date']
2022-02-03 21:01:53 +01:00
} else if(message._updatedAt) {
if(message._updatedAt.hasOwnProperty('$date')) {
// console.log('FIX UPDATE 11', message)
message._updatedAt = message._updatedAt['$date']
}
2022-01-11 20:56:21 +01:00
}
return message
}
2022-01-12 16:10:59 +01:00
2022-01-28 16:15:20 +01:00
usernameToDisplayName(username) {
const firstName = capitalizeTxt(username.split('.')[0])
const lastName = capitalizeTxt(username.split('.')[1])
return firstName + ' ' + lastName
}
2022-01-12 16:10:59 +01:00
2022-01-10 13:31:15 +01:00
}