mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-18 20:47:54 +00:00
Merge with websocket branch
This commit is contained in:
@@ -0,0 +1,197 @@
|
||||
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 { ToastsService } from '../toast.service';
|
||||
import { chatHistory, ChatMessage } from 'src/app/models/chatMethod'
|
||||
import { Storage } from '@ionic/storage';
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class RoomService {
|
||||
|
||||
massages: MessageService[] = []
|
||||
lastMessage: MessageService;
|
||||
|
||||
chatUser: ChatUserService[] = []
|
||||
customFields:any;
|
||||
id = ''
|
||||
t = ''
|
||||
name = ''
|
||||
_updatedAt = {}
|
||||
private hasLoadHistory = false
|
||||
duration = ''
|
||||
|
||||
private ToastService = ToastsService
|
||||
|
||||
scrollDown = () => {}
|
||||
|
||||
constructor(
|
||||
public WsChatService: WsChatService,
|
||||
private MessageService: MessageService,
|
||||
private storage: Storage,
|
||||
) {}
|
||||
|
||||
setData({customFields, id, name, t, lastMessage, _updatedAt}) {
|
||||
this.customFields = customFields
|
||||
this.id = id
|
||||
this.name = name
|
||||
this.t = t
|
||||
this.lastMessage = lastMessage
|
||||
this._updatedAt = _updatedAt
|
||||
|
||||
this.calDateDuration()
|
||||
}
|
||||
|
||||
receiveMessage() {
|
||||
|
||||
this.WsChatService.receiveLiveMessageFromRoom(
|
||||
this.id,
|
||||
(ChatMessage) => {
|
||||
ChatMessage = ChatMessage.fields.args[0]
|
||||
ChatMessage = this.fix_updatedAt(ChatMessage)
|
||||
console.log('recivemessage', ChatMessage)
|
||||
|
||||
/* this.ToastService._chatMessage({message:'Nova mensagem', sender:'Gilson'}) */
|
||||
const message = new MessageService()
|
||||
message.setData(ChatMessage)
|
||||
this.lastMessage.msg = message.msg
|
||||
if(message.t == 'r'){this.name = message.msg}
|
||||
this.calDateDuration(ChatMessage._updatedAt)
|
||||
this.massages.push(message)
|
||||
|
||||
setTimeout(()=>{
|
||||
this.scrollDown()
|
||||
}, 100)
|
||||
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
send(msg) {
|
||||
this.WsChatService.send(this.id, msg)
|
||||
}
|
||||
|
||||
// runs onces only
|
||||
loadHistory(limit= 100) {
|
||||
|
||||
if(this.hasLoadHistory){ return false}
|
||||
|
||||
this.WsChatService.loadHistory(this.id, limit).then(async (chatHistory:chatHistory) => {
|
||||
|
||||
await this.transformData(chatHistory.result.messages.reverse());
|
||||
|
||||
chatHistory.result.messages.reverse().forEach(message => {
|
||||
|
||||
message = this.fix_updatedAt(message)
|
||||
console.log('loadHistory', message)
|
||||
|
||||
const wewMessage = new MessageService()
|
||||
wewMessage.setData(message)
|
||||
this.massages.push(wewMessage)
|
||||
});
|
||||
|
||||
|
||||
})
|
||||
|
||||
setTimeout(()=>{
|
||||
this.scrollDown()
|
||||
}, 50)
|
||||
|
||||
this.hasLoadHistory = true
|
||||
}
|
||||
|
||||
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)
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
ReactToMessage() {}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
// to add
|
||||
countDownDate(){}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user