mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-20 21:35:50 +00:00
Merge branch 'feature/websocket' of https://bitbucket.org/equilibriumito/gabinete-digital into feature/websocket
This commit is contained in:
@@ -2,32 +2,111 @@ import { Injectable } from '@angular/core';
|
||||
import { RoomService } from './room.service';
|
||||
import { RocketChatClientService } from 'src/app/services/socket/rocket-chat-client.service';
|
||||
import { MessageService } from 'src/app/services/chat/message.service'
|
||||
import { SessionStore } from 'src/app/store/session.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ChatService {
|
||||
|
||||
rooms: RoomService[] = []
|
||||
group = []
|
||||
|
||||
individual: {[key: string]: RoomService} = {}
|
||||
group: {[key: string]: RoomService} = {}
|
||||
|
||||
loadingWholeList = false
|
||||
|
||||
individualCount = 0;
|
||||
groupCount = 0;
|
||||
|
||||
constructor(
|
||||
private RocketChatClientService: RocketChatClientService
|
||||
) {
|
||||
this.getAllRoomAndSubscribe()
|
||||
|
||||
(async()=>{
|
||||
await this.getAllRoom();
|
||||
this.subscribeToRoom()
|
||||
})()
|
||||
|
||||
}
|
||||
|
||||
getAllRoomAndSubscribe() {
|
||||
this.RocketChatClientService.getRooms().then((rooms: any) => {
|
||||
rooms.result.update.forEach((roomData:any) => {
|
||||
const room = new RoomService(new RocketChatClientService(), new MessageService())
|
||||
room.setData({id: roomData.lastMessage.rid})
|
||||
});
|
||||
async getAllRoom () {
|
||||
this.loadingWholeList = true
|
||||
|
||||
const rooms: any = await this.RocketChatClientService.getRooms();
|
||||
|
||||
rooms.result.update.forEach((roomData:any) => {
|
||||
let room:RoomService;
|
||||
|
||||
room = new RoomService(this.RocketChatClientService, new MessageService())
|
||||
room.setData({
|
||||
id: this.getRoomId(roomData),
|
||||
name: this.getChatName(roomData),
|
||||
lastMessage: this.getRoomLastMessage(roomData),
|
||||
_updatedAt: roomData._updatedAt['$date']
|
||||
})
|
||||
|
||||
room.receiveMessage()
|
||||
|
||||
let roomId = roomData.lastMessage.rid
|
||||
|
||||
if(this.isIndividual(roomData)) {
|
||||
this.individual[roomId] = room
|
||||
this.individualCount++
|
||||
} else {
|
||||
this.group[roomId] = room
|
||||
this.groupCount++
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
this.loadingWholeList = false
|
||||
}
|
||||
|
||||
onJoinRoom() {
|
||||
// live
|
||||
subscribeToRoom() {
|
||||
for (const id in this.individual) {
|
||||
this.RocketChatClientService.subscribe(id).then((subscription)=>{
|
||||
console.log('subscription', subscription)
|
||||
})
|
||||
}
|
||||
|
||||
for (const id in this.group) {
|
||||
this.RocketChatClientService.subscribe(id).then((subscription)=>{
|
||||
console.log('subscription', subscription)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
getRoom(id): RoomService {
|
||||
try {
|
||||
return this.individual[id]
|
||||
} catch(e) {
|
||||
return this.group[id]
|
||||
}
|
||||
}
|
||||
|
||||
getChatName(roomData) {
|
||||
if(this.isIndividual(roomData)) {
|
||||
const names: String[] = roomData.usernames
|
||||
const roomName = names.filter((name)=>{
|
||||
return name != SessionStore.user.RochetChatUser
|
||||
})[0]
|
||||
|
||||
return roomName
|
||||
} else {
|
||||
return roomData.fName
|
||||
}
|
||||
}
|
||||
|
||||
getRoomId(roomData) {
|
||||
return roomData.lastMessage.rid
|
||||
}
|
||||
|
||||
getRoomLastMessage(roomData) {
|
||||
return roomData.lastMessage
|
||||
}
|
||||
|
||||
private isIndividual(roomData) {
|
||||
return !roomData.fname
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,5 +5,30 @@ import { Injectable } from '@angular/core';
|
||||
})
|
||||
export class MessageService {
|
||||
|
||||
channels = []
|
||||
mentions = []
|
||||
msg = ''
|
||||
rid = ''
|
||||
ts = {}
|
||||
u = {}
|
||||
_id =''
|
||||
_updatedAt = {}
|
||||
|
||||
constructor() { }
|
||||
|
||||
setData({channels, mentions, msg ,rid ,ts, u, _id, _updatedAt}) {
|
||||
this.channels = channels
|
||||
this.mentions = mentions
|
||||
this.msg = msg
|
||||
this.rid = rid
|
||||
this.ts = ts
|
||||
this.u = u
|
||||
this._id = _id
|
||||
this._updatedAt = _updatedAt
|
||||
}
|
||||
|
||||
delete() {}
|
||||
|
||||
showDateDuration() {}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,27 +2,96 @@ 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'
|
||||
import { TimeService } from 'src/app/services/functions/time.service';
|
||||
import { ChatService } from '../chat.service';
|
||||
import { showDateDuration } from 'src/plugin/showDateDuration'
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class RoomService {
|
||||
|
||||
massages: MessageService[] = []
|
||||
lastMessage: MessageService;
|
||||
|
||||
chatUser: ChatUserService[] = []
|
||||
id = []
|
||||
id = ''
|
||||
name = ''
|
||||
_updatedAt = {}
|
||||
private hasLoadHistory = false
|
||||
duration = ''
|
||||
|
||||
|
||||
constructor(
|
||||
private RocketChatClientService: RocketChatClientService,
|
||||
private MessageService: MessageService
|
||||
public RocketChatClientService: RocketChatClientService,
|
||||
private MessageService: MessageService,
|
||||
) {}
|
||||
|
||||
setData({id}){
|
||||
this.id= id
|
||||
setData({id, name, lastMessage, _updatedAt}) {
|
||||
|
||||
this.id = id
|
||||
this.name = name
|
||||
this.lastMessage = lastMessage
|
||||
this._updatedAt = _updatedAt
|
||||
|
||||
this.calDateDuration()
|
||||
}
|
||||
|
||||
receiveMessage() {
|
||||
this.RocketChatClientService.receiveLiveMessageFromRoom(
|
||||
this.id,
|
||||
this.constructor.name+this.id,
|
||||
(Chatmessage) => {
|
||||
|
||||
Chatmessage = this.fix_updatedAt(Chatmessage)
|
||||
const message = new MessageService()
|
||||
message.setData(Chatmessage.result)
|
||||
this.massages.push(message)
|
||||
this.calDateDuration(Chatmessage.result._updatedAt)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
send(msg) {
|
||||
this.RocketChatClientService.send(this.id, msg)
|
||||
}
|
||||
|
||||
// runs onces only
|
||||
loadHistory(limit= 100) {
|
||||
|
||||
if(this.hasLoadHistory){ return false }
|
||||
|
||||
this.RocketChatClientService.loadHistory(this.id, limit).then((message:any) => {
|
||||
console.log('loadHistory', message)
|
||||
|
||||
message.result.messages.reverse().forEach(element => {
|
||||
console.log('element', element)
|
||||
element = this.fix_updatedAt(element)
|
||||
const message = new MessageService()
|
||||
message.setData(element)
|
||||
this.massages.push(message)
|
||||
});
|
||||
|
||||
})
|
||||
|
||||
this.hasLoadHistory = true
|
||||
}
|
||||
|
||||
create() {}
|
||||
sendMessage() {}
|
||||
deleteMessage() {}
|
||||
deleteMessage(msgId) {}
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -207,7 +207,7 @@ import { deepFind } from 'src/plugin/deep'
|
||||
* @param key
|
||||
* @param funx
|
||||
*/
|
||||
receiveLiveMessageFromRoom(roomId, key, funx: Function) {
|
||||
receiveLiveMessageFromRoom(roomId =')(', key, funx: Function) {
|
||||
|
||||
this.ws.registerCallback({
|
||||
type:'Onmessage',
|
||||
@@ -300,7 +300,6 @@ import { deepFind } from 'src/plugin/deep'
|
||||
|
||||
},
|
||||
connect:(url)=> {
|
||||
this.ws.connected = false
|
||||
this.wsUrl = url
|
||||
this.socket = new WebSocket(this.wsUrl);
|
||||
// bind function
|
||||
@@ -331,7 +330,7 @@ import { deepFind } from 'src/plugin/deep'
|
||||
send: (message: object, requestId = uuidv4(), loginRequired) => {
|
||||
|
||||
if (this.ws.connected == false || loginRequired == true && this.isLogin == false) { // save data to send when back online
|
||||
console.log('save msgQueue')
|
||||
console.log('save msgQueue this.ws.connected == false || loginRequired == true && this.isLogin == false',this.ws.connected, loginRequired, this.isLogin)
|
||||
this.wsMsgQueue.push({message, requestId, loginRequired})
|
||||
} else {
|
||||
let messageStr = JSON.stringify(message)
|
||||
|
||||
Reference in New Issue
Block a user