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

478 lines
13 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 { ChatUserService } from 'src/app/services/chat/chat-user.service';
import { showDateDuration } from 'src/plugin/showDateDuration';
2022-01-13 10:19:14 +01:00
import { ToastsService } from '../toast.service';
2022-01-26 17:28:55 +01:00
import { chatHistory, ChatMessage } 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-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-01-10 23:52:33 +01:00
chatUser: ChatUserService[] = []
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-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-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-01-28 15:28:21 +01:00
setData({ customFields, id, name, t, lastMessage = new MessageService(this.storage), _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-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) => {
ChatMessage = ChatMessage.fields.args[0]
ChatMessage = this.fix_updatedAt(ChatMessage)
2022-01-19 09:12:30 +01:00
console.log('recivemessage', ChatMessage)
2022-01-17 13:48:08 +01:00
/* this.ToastService._chatMessage({message:'Nova mensagem', sender:'Gilson'}) */
2022-01-28 15:28:21 +01:00
const message = new MessageService(this.storage)
2022-01-14 11:47:55 +01:00
message.setData(ChatMessage)
2022-01-26 16:37:59 +01:00
2022-01-28 15:31:52 +01:00
this.lastMessage = message
2022-01-20 14:31:49 +01:00
if (message.t == 'r') { this.name = message.msg }
2022-01-14 12:13:55 +01:00
this.calDateDuration(ChatMessage._updatedAt)
2022-01-28 15:59:01 +01:00
this.messages.push(message)
2022-01-14 14:50:47 +01:00
2022-01-20 14:31:49 +01:00
setTimeout(() => {
2022-01-14 14:50:47 +01:00
this.scrollDown()
}, 100)
2022-01-28 15:59:01 +01:00
//this.sortService.sortDate(this.messages, '')
2022-01-20 15:33:55 +01:00
2022-01-26 17:28:55 +01:00
if(SessionStore.user.RochetChatUser != ChatMessage.u.username) {
this.NativeNotificationService.sendNotificationChat({
2022-01-28 17:34:27 +01:00
message: message.msg,
2022-01-26 17:28:55 +01:00
title: this.name
});
}
2022-01-28 17:34:27 +01:00
2022-01-20 15:33:55 +01:00
// save to ionic storage
2022-01-21 11:42:02 +01:00
this.storage.get('chatmsg' + this.id).then((messages: any) => {
2022-01-20 15:33:55 +01:00
const newListMessages = messages.push(ChatMessage)
2022-01-21 11:42:02 +01:00
this.storage.set('chatmsg' + this.id, newListMessages).then((value) => {
2022-01-20 15:33:55 +01:00
console.log('MSG SAVED ON STORAGE', value)
});
})
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
this.WsChatService.registerCallback
}
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) => {
console.log(ChatMessage.fields.args[0]._id);
const messageId = ChatMessage.fields.args[0]._id;
2022-01-28 15:59:01 +01:00
this.messages.forEach((message, index)=>{
2022-01-28 15:31:52 +01:00
if(message._id == messageId){
2022-01-28 15:59:01 +01:00
this.messages.splice(index, 1)
this.storage.set('chatmsg' + this.id, this.messages).then((value) => {
2022-01-28 15:31:52 +01:00
//console.log('MSG DELETE ON STORAGE', value)
});
//Get previous last message from room
2022-01-28 15:59:01 +01:00
const previousLastMessage = this.messages.slice(-1)[0];
2022-01-28 15:31:52 +01:00
this.lastMessage = previousLastMessage;
this.calDateDuration(previousLastMessage._updatedAt)
}
})
}
)
this.WsChatService.registerCallback
2022-01-11 15:43:09 +01:00
}
2022-01-28 16:15:20 +01:00
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) {
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-26 09:19:54 +01:00
leave(rid?) {
this.WsChatService.leaveRoom(this.id)
}
2022-01-21 16:55:05 +01:00
getMsgFromDBMobile() {
console.log('ALL MSG DBBB', this.id)
this.sqlservice.getAllChatMSG(this.id).then((msg: any = []) => {
let ad = [];
ad = msg
console.log('ALL MSG DBBB', ad.length)
msg.map(element => {
console.log('CHANNEL ELEMENT', element)
let msgChat = {
_id: element.Id,
attachments: this.isJson(element.Attachments),
channels: this.isJson(element.Channels),
file: {
guid: this.isJson(element.File).guid,
image_url: this.isJson(element.image_url),
type: this.isJson(element.File).type
},
mentions: this.isJson(element.Mentions),
msg: element.Msg,
rid: element.Rid,
ts: element.Ts,
u: this.isJson(element.U),
_updatedAt: this.isJson(element.UpdatedAt),
}
let mmessage = this.fix_updatedAt(msgChat)
console.log('FROM DB WEB', mmessage)
2022-01-28 15:28:21 +01:00
const wewMessage = new MessageService(this.storage)
2022-01-21 16:55:05 +01:00
wewMessage.setData(mmessage)
2022-01-28 15:59:01 +01:00
this.messages.push(wewMessage)
console.log('loadHistory 222', this.messages)
2022-01-21 16:55:05 +01:00
});
});
}
isJson(str) {
try {
JSON.parse(str);
} catch (e) {
return "";
}
return JSON.parse(str);
}
2022-01-21 11:42:02 +01:00
getMsgFromDB() {
2022-01-21 16:55:05 +01:00
2022-01-28 15:28:21 +01:00
/* this.storage.get('chatmsg' + this.id).then((message) => {
console.log('ALL MESSAGE WEB', message)
2022-01-21 11:42:02 +01:00
message.forEach(message => {
2022-01-20 14:31:49 +01:00
2022-01-21 11:42:02 +01:00
if (message.file) {
if (message.file.guid) {
this.storage.get(message.file.guid).then((image) => {
2022-01-28 15:31:52 +01:00
//console.log('IMAGE FROM STORAGE', image)
2022-01-21 11:42:02 +01:00
message.file.image_url = image
});
}
}
2022-01-20 14:31:49 +01:00
2022-01-21 11:42:02 +01:00
let mmessage = this.fix_updatedAt(message)
console.log('FROM DB WEB', mmessage)
2022-01-28 15:28:21 +01:00
const wewMessage = new MessageService(this.storage)
2022-01-21 11:42:02 +01:00
wewMessage.setData(mmessage)
2022-01-28 15:59:01 +01:00
this.messages.push(wewMessage)
console.log('loadHistory 222', this.messages)
2022-01-20 15:33:55 +01:00
});
2022-01-28 15:28:21 +01:00
}) */
2022-01-21 11:42:02 +01:00
}
2022-01-26 09:19:54 +01:00
2022-01-21 16:55:05 +01:00
2022-01-21 11:42:02 +01:00
// runs onces only
loadHistory(limit = 100) {
if (this.hasLoadHistory) { return false }
2022-01-29 17:06:25 +01:00
this.storage.get('chatmsg' + this.id).then((messages = []) => {
2022-01-28 16:37:18 +01:00
let localMessages = []
2022-01-28 15:28:21 +01:00
messages.forEach(message => {
message = this.fix_updatedAt(message)
const wewMessage = new MessageService(this.storage)
wewMessage.setData(message)
2022-01-28 16:37:18 +01:00
localMessages.push(wewMessage)
2022-01-28 15:28:21 +01:00
});
2022-01-28 16:37:18 +01:00
this.messages = localMessages
2022-01-28 15:28:21 +01:00
})
this.WsChatService.loadHistory(this.id, limit).then((chatHistory:chatHistory) => {
2022-01-21 16:55:05 +01:00
console.log('loadHistory', chatHistory)
2022-01-21 11:42:02 +01:00
2022-01-28 15:28:21 +01:00
let localMessages = []
2022-01-28 17:34:27 +01:00
//const sortedRoomList = this.sortService.sortDate(chatHistory.result.messages, "_updatedAt.$date")
2022-01-21 16:55:05 +01:00
chatHistory.result.messages.reverse().forEach(message => {
2022-01-13 10:52:03 +01:00
message = this.fix_updatedAt(message)
2022-01-28 15:28:21 +01:00
const wewMessage = new MessageService(this.storage)
2022-01-13 10:52:03 +01:00
wewMessage.setData(message)
2022-01-28 15:28:21 +01:00
localMessages.push(wewMessage)
2022-01-21 16:55:05 +01:00
});
2022-01-28 16:16:23 +01:00
this.messages = localMessages
2022-01-21 16:55:05 +01:00
2022-01-28 17:34:27 +01:00
console.log(chatHistory.result.messages);
2022-01-28 15:28:21 +01:00
this.storage.set('chatmsg' + this.id, chatHistory.result.messages.reverse())
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-28 15:28:21 +01:00
/* this.WsChatService.loadHistory(this.id, limit).then(async (chatHistory: chatHistory) => {
2022-01-11 16:07:54 +01:00
2022-01-28 15:28:21 +01:00
const mgsArray = chatHistory.result.messages.reverse();
if (this.platform.is('desktop') || this.platform.is('mobileweb')) {
await this.storage.remove('chatmsg' + this.id).then(() => {
console.log('MSG REMOVE ON STORAGE')
})
await this.storage.set('chatmsg' + this.id, mgsArray).then((value) => {
console.log('MSG SAVED ON STORAGE', value)
this.getMsgFromDB()
});
} else {
mgsArray.forEach((element) => {
console.log('SQLITE WEBSOCKET', element)
this.sqlservice.addChatMSG(element)
})
this.getMsgFromDBMobile()
}
2022-01-11 15:43:09 +01:00
2022-01-28 15:28:21 +01:00
}) */
2022-01-14 14:58:47 +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
updateMeessage(messageID, imgbase64) {
}
async returnData(res) {
return res;
}
2022-01-19 09:12:30 +01:00
async transformData(res) {
2022-01-20 14:31:49 +01:00
this.mgsArray = [];
2022-01-19 09:12:30 +01:00
res.forEach(async element => {
if (element.file) {
if (element.file.guid) {
await this.storage.get(element.file.guid).then((image) => {
let chatmsg = {
_id: element._id,
attachments: element.attachments,
channels: element.channels,
file: {
guid: element.file.guid,
image_url: image,
type: element.file.type
},
mentions: element.mentions,
msg: element.msg,
rid: element.rid,
ts: element.ts,
u: element.u,
2022-01-21 11:42:02 +01:00
_updatedAt: element._updatedAt,
2022-01-19 09:12:30 +01:00
}
2022-01-20 14:31:49 +01:00
this.mgsArray.push(chatmsg);
2022-01-19 09:12:30 +01:00
})
} else {
let chatmsg = {
_id: element._id,
attachments: element.attachments,
channels: element.channels,
file: element.file,
mentions: element.mentions,
msg: element.msg,
rid: element.rid,
ts: element.ts,
u: element.u,
_updatedAt: element._updatedAt,
}
2022-01-20 14:31:49 +01:00
this.mgsArray.push(chatmsg)
2022-01-19 09:12:30 +01:00
}
} else {
let chatmsg = {
_id: element._id,
attachments: element.attachments,
channels: element.channels,
mentions: element.mentions,
msg: element.msg,
rid: element.rid,
ts: element.ts,
u: element.u,
_updatedAt: element._updatedAt,
}
2022-01-20 14:31:49 +01:00
this.mgsArray.push(chatmsg)
2022-01-19 09:12:30 +01:00
}
});
await this.storage.remove('chatmsg').then(() => {
console.log('MSG REMOVE FROM STORAGE')
});
2022-01-20 14:31:49 +01:00
await this.storage.set('chatmsg', this.mgsArray).then((value) => {
2022-01-19 09:12:30 +01:00
console.log('MSG SAVED ON STORAGE', value)
});
}
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-01-20 14:31:49 +01:00
} else {
2022-01-29 19:21:46 +01:00
// console.log('FIX UPDATE 11', message)
2022-01-11 20:56:21 +01:00
message._updatedAt = message._updatedAt['$date']
}
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
}