improve chat remove loop

This commit is contained in:
Peter Maquiran
2022-01-11 15:43:09 +01:00
parent a8a1307bec
commit dcee5aa2f4
9 changed files with 96 additions and 84 deletions
-22
View File
@@ -99,28 +99,6 @@ export class HomePage implements OnInit {
console.log('loadHistory', message)
})
this.RocketChatClientService.getRooms().then((rooms: any)=>{
console.log('rooms', rooms)
rooms.result.update.forEach((room:any) => {
console.log('room', room)
this.RocketChatClientService.subscribe(room.lastMessage.rid).then((subscription)=>{
console.log('subscription', subscription)
})
});
});
this.RocketChatClientService.receiveLiveMessageFromRoom(
'fsMwcNdufWvdnChj7ya9nF9cX2HizxxWAM',
this.constructor.name,
(Chatmessage)=>{
console.log('chat', Chatmessage)
}
)
window['jj'] = ()=>{
//send message // roomId // Message
this.RocketChatClientService.send('fsMwcNdufWvdnChj7ya9nF9cX2HizxxWAM', 'Mensagem enviada programaticamente.'+ new Date().toISOString())
@@ -49,7 +49,7 @@
</ion-refresher-content>
</ion-refresher> -->
<div (click)="handleClick()" class="messages" #scrollMe>
<div class="messages-list-item-wrapper container-width-100" *ngFor="let msg of messages; let last = last"
<div class="messages-list-item-wrapper container-width-100" *ngFor="let msg of ChatServiceGPR.getRoom(this.roomId).mmm; let last = last"
[class.messages-list-item-wrapper-active]="msg._id == selectedMsgId" >
<div (press)="handlePress(msg._id)" class='message-container incoming-{{msg.u.username!=loggedUser.me.username}}' (click)="openPreview(msg)" *ngIf="msg.msg !=''">
<div class="title">
+7 -2
View File
@@ -28,6 +28,7 @@ import { SqliteService } from 'src/app/services/sqlite.service';
import { elementAt } from 'rxjs-compat/operator/elementAt';
import { ViewMediaPage } from 'src/app/modals/view-media/view-media.page';
import { ViewEventPage } from 'src/app/modals/view-event/view-event.page';
import { ChatService as ChatServiceGPR} from 'src/app/services/chat/chat.service'
const IMAGE_DIR = 'stored-images';
@@ -94,7 +95,8 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
public ThemeService: ThemeService,
private changeDetectorRef: ChangeDetectorRef,
private platform: Platform,
private sqlservice: SqliteService
private sqlservice: SqliteService,
public ChatServiceGPR: ChatServiceGPR
) {
this.loggedUser = authService.ValidatedUserChat['data'];
this.roomId = this.navParams.get('roomId');
@@ -105,10 +107,13 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
this.modalController.dismiss();
}
};
this.ChatServiceGPR.getRoom(this.roomId).loadHistory()
}
ngOnInit() {
this.load();
//this.load();
this.setStatus('online');
//this.loadFiles();
+35 -6
View File
@@ -21,33 +21,41 @@ export class ChatService {
constructor(
private RocketChatClientService: RocketChatClientService
) {
this.getAllRoomAndSubscribe()
(async()=>{
await this.getAllRoom();
this.subscribeToRoom()
})()
}
async getAllRoomAndSubscribe () {
async getAllRoom () {
this.loadingWholeList = true
const rooms: any = await this.RocketChatClientService.getRooms();
rooms.result.update.forEach((roomData:any) => {
let room
let room:RoomService;
let roomId = roomData.lastMessage.rid
if(this.isIndividual(roomData)) {
room = new RoomService(new RocketChatClientService(), new MessageService())
room = new RoomService(this.RocketChatClientService, new MessageService())
room.setData({
id: this.getRoomId(roomData),
name: this.getChatName(roomData),
lastMessage: this.getRoomLastMessage(roomData)
})
room.receiveMessage()
this.individual[roomId] = room
this.individualCount++
} else {
room = new RoomService(new RocketChatClientService(), new MessageService())
room = new RoomService(this.RocketChatClientService, new MessageService())
room.setData({
id: this.getRoomId(roomData),
name: this.getChatName(roomData),
lastMessage: this.getRoomLastMessage(roomData)
})
room.receiveMessage()
this.group[roomId] = room
this.groupCount++
}
@@ -55,7 +63,28 @@ export class ChatService {
});
this.loadingWholeList = false
}
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) {
+4
View File
@@ -5,5 +5,9 @@ import { Injectable } from '@angular/core';
})
export class MessageService {
channels = []
mentions = []
msg = ''
rid = ''
constructor() { }
}
+36 -4
View File
@@ -13,21 +13,53 @@ export class RoomService {
chatUser: ChatUserService[] = []
id = ''
name = ''
mmm = []
private hasLoadHistory = false
constructor(
private RocketChatClientService: RocketChatClientService,
public RocketChatClientService: RocketChatClientService,
private MessageService: MessageService
) {}
) {
}
setData({id, name, lastMessage}) {
this.id = id
this.name = name
this.lastMessage = lastMessage
}
receiveMessage() {
this.RocketChatClientService.receiveLiveMessageFromRoom(
this.id,
this.constructor.name+this.id,
(Chatmessage)=>{
this.hasLoadHistory = false
this.loadHistory()
}
)
}
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)
this.mmm = message.result.messages.reverse()
})
this.hasLoadHistory = true
}
create() {}
sendMessage() {}
deleteMessage() {}
deleteMessage(msgId) {}
ReactToMessage() {}
}
@@ -209,7 +209,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',
@@ -302,7 +302,6 @@ import { deepFind } from 'src/plugin/deep'
},
connect:(url)=> {
this.ws.connected = false
this.wsUrl = url
this.socket = new WebSocket(this.wsUrl);
// bind function
@@ -333,7 +332,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)
@@ -37,7 +37,7 @@
</ion-refresher-content>
</ion-refresher>
<div class="messages" #scrollMe>
<div class="messages-list-item-wrapper container-width-100" *ngFor="let msg of chatMessageStore.message[roomId]; let last = last">
<div class="messages-list-item-wrapper container-width-100" *ngFor="let msg of ChatServiceGPR.getRoom(roomId).mmm; let last = last">
<div class='message-item incoming-{{msg.u.username!=loggedUser.me.username}} max-width-45' *ngIf="msg.msg !=''">
<div class="message-item-options d-flex justify-content-end">
<fa-icon [matMenuTriggerFor]="beforeMenu" icon="chevron-down" class="message-options-icon cursor-pointer"></fa-icon>
+10 -45
View File
@@ -19,6 +19,7 @@ import { ThemeService } from 'src/app/services/theme.service'
import { PreviewCameraPage } from 'src/app/modals/preview-camera/preview-camera.page';
import { ViewMediaPage } from 'src/app/modals/view-media/view-media.page';
import { ViewEventPage } from 'src/app/modals/view-event/view-event.page';
import { ChatService as ChatServiceGPR} from 'src/app/services/chat/chat.service'
@Component({
selector: 'app-messages',
@@ -77,27 +78,17 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
public ThemeService: ThemeService,
private changeDetectorRef: ChangeDetectorRef,
private router: Router,
public ChatServiceGPR: ChatServiceGPR
) {
this.loggedUser = authService.ValidatedUserChat['data'];
/* this.dm = this.navParams.get('dm'); */
}
ngOnChanges(changes: SimpleChanges): void {
this.load();
//throw new Error('Method not implemented.');
this.ChatServiceGPR.getRoom(this.roomId).loadHistory()
}
ngOnInit() {
this.scrollToBottom();
/* setInterval(()=>{ */
this.load();
/* }, 9000); */
console.log(this.roomId);
console.log("Chat route", this.route.url)
this.setStatus('online');
}
@@ -223,19 +214,11 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
}
sendMessage() {
let body = {
"message":
{
"rid": this.roomId, "msg": this.message,
}
}
this.chatService.sendMessage(body).subscribe(res=> {
this.scrollingOnce = true;
});
this.ChatServiceGPR.getRoom(this.roomId).send(this.message)
this.message = "";
}
deleteMessage(msgId:string){
deleteMessage(msgId:string) {
let body = {
"roomId": this.roomId,
"msgId": msgId,
@@ -247,23 +230,6 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
}); */
}
loadMessages(){
//this.showLoader = true;
const roomId = this.roomId
this.chatService.getRoomMessages(this.roomId).subscribe(res => {
console.log(res);
this.messages = res['messages'].reverse();
this.chatMessageStore.add(roomId, this.messages)
console.log(this.messages);
//this.serverLongPull(res)
/* this.chatService.subscribe(this.roomId).then(res => {
console.log("Real fake msg", res)
}); */
//this.showLoader = false;
})
}
async viewDocument(msg:any, url?:string){
if(msg.file.type == "application/img"){
let response:any = await this.fileService.getFile(msg.file.guid).toPromise();
@@ -539,7 +505,6 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
this.showLoader = false;
//this.addDocGestaoDocumental();
}
this.loadMessages();
});
}
@@ -555,7 +520,6 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
if (res['success'] == true) {
// Show Error
//showMessage(response.statusText);
this.loadMessages()
this.messages = res['messages'].reverse();
this.chatMessageStore.add(roomId, this.messages)
@@ -567,7 +531,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
else{
if(document.querySelector('app-messages')){
await new Promise(resolve => setTimeout(resolve, 5000));
await this.serverLongPull();
// await this.serverLongPull();
this.getDirectMessages.emit();
console.log('Timer message running')
}
@@ -580,10 +544,11 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
} */
}, (error)=>{
console.log(error);
this.serverLongPull();
// this.serverLongPull();
});
}sliderOpts = {
}
sliderOpts = {
zoom: false,
slidesPerView: 1.5,
spaceBetween: 20,