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

70 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-01-10 23:52:33 +01:00
import { Injectable } from '@angular/core'
import { RocketChatClientService } from 'src/app/services/socket/rocket-chat-client.service';
import { MessageService } from 'src/app/services/chat/message.service'
import { ChatUserService } from 'src/app/services/chat/chat-user.service'
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-11 13:03:43 +01:00
id = ''
name = ''
2022-01-11 15:43:09 +01:00
private hasLoadHistory = false
2022-01-10 23:52:33 +01:00
constructor(
2022-01-11 15:43:09 +01:00
public RocketChatClientService: RocketChatClientService,
2022-01-10 23:52:33 +01:00
private MessageService: MessageService
2022-01-11 15:43:09 +01:00
) {
}
2022-01-10 23:52:33 +01:00
2022-01-11 13:03:43 +01:00
setData({id, name, lastMessage}) {
2022-01-11 15:43:09 +01:00
2022-01-11 13:03:43 +01:00
this.id = id
this.name = name
this.lastMessage = lastMessage
2022-01-10 23:52:33 +01:00
}
2022-01-11 15:43:09 +01:00
receiveMessage() {
this.RocketChatClientService.receiveLiveMessageFromRoom(
this.id,
this.constructor.name+this.id,
(Chatmessage)=>{
2022-01-11 16:07:54 +01:00
this.massages.push(Chatmessage.result)
2022-01-11 15:43:09 +01:00
}
)
}
send(msg) {
this.RocketChatClientService.send(this.id, msg)
}
// runs onces only
loadHistory(limit= 100) {
if(this.hasLoadHistory){ return false }
2022-01-11 16:07:54 +01:00
this.RocketChatClientService.loadHistory(this.id, limit).then((message:any) => {
2022-01-11 15:43:09 +01:00
console.log('loadHistory', message)
2022-01-11 16:07:54 +01:00
message.result.messages.reverse().forEach(element => {
const message = new MessageService()
message.setData(element)
this.massages.push(message)
});
2022-01-11 15:43:09 +01:00
})
this.hasLoadHistory = true
}
2022-01-10 23:52:33 +01:00
create() {}
2022-01-11 15:43:09 +01:00
deleteMessage(msgId) {}
2022-01-10 23:52:33 +01:00
ReactToMessage() {}
2022-01-10 13:31:15 +01:00
}