2022-01-10 23:52:33 +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-10 23:52:33 +01:00
|
|
|
import { MessageService } from 'src/app/services/chat/message.service'
|
|
|
|
|
import { ChatUserService } from 'src/app/services/chat/chat-user.service'
|
2022-01-11 20:56:21 +01:00
|
|
|
import { showDateDuration } from 'src/plugin/showDateDuration'
|
2022-01-13 10:19:14 +01:00
|
|
|
import { ToastsService } from '../toast.service';
|
2022-01-13 10:52:03 +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-10 13:31:15 +01:00
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
|
|
|
|
})
|
|
|
|
|
export class RoomService {
|
|
|
|
|
|
2022-01-11 16:07:54 +01:00
|
|
|
massages: MessageService[] = []
|
2022-01-11 13:03:43 +01:00
|
|
|
lastMessage: MessageService;
|
|
|
|
|
|
2022-01-10 23:52:33 +01:00
|
|
|
chatUser: ChatUserService[] = []
|
2022-01-13 21:24:43 +01:00
|
|
|
customFields:any;
|
2022-01-11 13:03:43 +01:00
|
|
|
id = ''
|
2022-01-17 11:27:25 +01:00
|
|
|
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-11 20:56:21 +01:00
|
|
|
duration = ''
|
2022-01-13 13:45:04 +01:00
|
|
|
|
2022-01-13 10:26:40 +01:00
|
|
|
private ToastService = ToastsService
|
2022-01-10 23:52:33 +01:00
|
|
|
|
2022-01-14 14:50:47 +01:00
|
|
|
scrollDown = () => {}
|
|
|
|
|
|
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-11 20:56:21 +01:00
|
|
|
) {}
|
2022-01-10 23:52:33 +01:00
|
|
|
|
2022-01-17 11:27:25 +01:00
|
|
|
setData({customFields, id, name, t, lastMessage, _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
|
2022-01-17 11:27:25 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
this.WsChatService.receiveLiveMessageFromRoom(
|
|
|
|
|
this.id,
|
|
|
|
|
(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-14 12:18:50 +01:00
|
|
|
|
2022-01-17 13:48:08 +01:00
|
|
|
/* this.ToastService._chatMessage({message:'Nova mensagem', sender:'Gilson'}) */
|
2022-01-14 12:13:55 +01:00
|
|
|
const message = new MessageService()
|
2022-01-14 11:47:55 +01:00
|
|
|
message.setData(ChatMessage)
|
2022-01-14 12:13:55 +01:00
|
|
|
this.lastMessage.msg = message.msg
|
2022-01-17 11:27:25 +01:00
|
|
|
if(message.t == 'r'){this.name = message.msg}
|
2022-01-14 12:13:55 +01:00
|
|
|
this.calDateDuration(ChatMessage._updatedAt)
|
2022-01-14 11:47:55 +01:00
|
|
|
this.massages.push(message)
|
2022-01-14 14:50:47 +01:00
|
|
|
|
|
|
|
|
setTimeout(()=>{
|
|
|
|
|
this.scrollDown()
|
|
|
|
|
}, 100)
|
2022-01-17 11:27:25 +01:00
|
|
|
|
2022-01-14 11:47:55 +01:00
|
|
|
}
|
|
|
|
|
)
|
2022-01-11 15:43:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
send(msg) {
|
2022-01-12 12:44:51 +01:00
|
|
|
this.WsChatService.send(this.id, msg)
|
2022-01-11 15:43:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// runs onces only
|
|
|
|
|
loadHistory(limit= 100) {
|
|
|
|
|
|
2022-01-12 11:52:05 +01:00
|
|
|
if(this.hasLoadHistory){ return false}
|
2022-01-11 15:43:09 +01:00
|
|
|
|
2022-01-19 09:12:30 +01:00
|
|
|
this.WsChatService.loadHistory(this.id, limit).then(async (chatHistory:chatHistory) => {
|
|
|
|
|
|
|
|
|
|
await this.transformData(chatHistory.result.messages.reverse());
|
2022-01-13 13:45:04 +01:00
|
|
|
|
2022-01-13 10:52:03 +01:00
|
|
|
chatHistory.result.messages.reverse().forEach(message => {
|
2022-01-13 13:46:56 +01:00
|
|
|
|
2022-01-13 10:52:03 +01:00
|
|
|
message = this.fix_updatedAt(message)
|
2022-01-19 09:12:30 +01:00
|
|
|
console.log('loadHistory', message)
|
|
|
|
|
|
2022-01-13 10:52:03 +01:00
|
|
|
const wewMessage = new MessageService()
|
|
|
|
|
wewMessage.setData(message)
|
|
|
|
|
this.massages.push(wewMessage)
|
2022-01-11 16:07:54 +01:00
|
|
|
});
|
|
|
|
|
|
2022-01-11 15:43:09 +01:00
|
|
|
|
2022-01-14 10:35:54 +01:00
|
|
|
})
|
2022-01-14 14:58:47 +01:00
|
|
|
|
|
|
|
|
setTimeout(()=>{
|
|
|
|
|
this.scrollDown()
|
|
|
|
|
}, 50)
|
|
|
|
|
|
2022-01-11 15:43:09 +01:00
|
|
|
this.hasLoadHistory = true
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-19 09:12:30 +01:00
|
|
|
async transformData(res) {
|
|
|
|
|
|
|
|
|
|
let mgsArray = [];
|
|
|
|
|
res.forEach(async element => {
|
|
|
|
|
console.log('TRANSFORM DATA ELEMENT' ,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,
|
|
|
|
|
_updatedAt: element._updatedAt,
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mgsArray.push(this.fix_updatedAt(chatmsg));
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
} 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,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mgsArray.push(this.fix_updatedAt(chatmsg))
|
|
|
|
|
}
|
|
|
|
|
} 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,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mgsArray.push(this.fix_updatedAt(chatmsg))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
await this.storage.remove('chatmsg').then(() => {
|
|
|
|
|
console.log('MSG REMOVE FROM STORAGE')
|
|
|
|
|
});
|
|
|
|
|
await this.storage.set('chatmsg', mgsArray).then((value) => {
|
|
|
|
|
console.log('MSG SAVED ON STORAGE', value)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-10 23:52:33 +01:00
|
|
|
ReactToMessage() {}
|
|
|
|
|
|
2022-01-11 20:56:21 +01:00
|
|
|
private calDateDuration(date = null) {
|
|
|
|
|
this.duration = showDateDuration(date || this._updatedAt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fix_updatedAt(message) {
|
|
|
|
|
if(message.result) {
|
|
|
|
|
message.result._updatedAt = message.result._updatedAt['$date']
|
|
|
|
|
} else{
|
|
|
|
|
message._updatedAt = message._updatedAt['$date']
|
|
|
|
|
}
|
|
|
|
|
return message
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 16:10:59 +01:00
|
|
|
|
|
|
|
|
// to add
|
|
|
|
|
countDownDate(){}
|
|
|
|
|
|
2022-01-10 13:31:15 +01:00
|
|
|
}
|