myadapter.ts
`import { Injectable } from '@angular/core';
import { ChatAdapter, User, Message, UserStatus } from 'ng-chat';
import { Observable, of } from 'rxjs';
import { delay } from "rxjs/operators";
import { map, catchError } from 'rxjs/operators';
import { ChatsService } from '../services/chats.service';
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';
import { TokenService } from '../services/token.service';
@Injectable()
export class MyAdapter extends ChatAdapter
{ private historyMessages: Message[] = [];
public SubmitData: Object = {};
public LoggedInUserDetails: any;
public friends: any;
public roomids: any;
public Onloadchat:boolean = false;
constructor(private _chatService: ChatsService,
public db: AngularFireDatabase,
private token: TokenService,) {
super();
this.LoggedInUserDetails = this.token.getUser();
this.roomids = [];
}
listFriends(): Observable<User[]> {
this._chatService.Friendlist().
subscribe(
data => this.FriendlistResponse(data),
error => this.handleError(error));
return of([]);
}
FriendlistResponse(data){
if(data){
this.friends = data;
this.onFriendsListChanged(data);
data.forEach(element => {
console.log(element);
this.OnLoadMessage(element);
});
}
}
getMessageHistory(userId: any): Observable<Message[]> {
this.historyMessages=[];
this.SubmitData['guid'] = userId;
if(userId != this.LoggedInUserDetails.user_guid){
this._chatService.UpdateChatRoom(this.SubmitData).
subscribe(
data => this.ChatRoomhandleResponse(data),
error => this.handleError(error));
}
return of(this.historyMessages).pipe(delay(5000));
}
ChatRoomhandleResponse(data) {
if(data.room){
// this.roomid =data.room;
if(data.sender != this.LoggedInUserDetails.user_guid){
var receiverid = data.sender;
}else{
var receiverid = data.receiver;
}
this.roomids.push({
roomid: data.room,
receiverid: receiverid
});
this.messagehistory(data);
}
}
OnLoadMessage(data){
// var x = this.onloadchatObj(data.room);
if(data.sender != this.LoggedInUserDetails.user_guid){
var user = this.friends.find(x => x.id == data.sender);
}else{
var user = this.friends.find(x => x.id == data.receiver);
}
console.log(user);
this.db.list('/chats/'+data.room, ref => ref.orderByChild('new').limitToLast(1))
.snapshotChanges().pipe(
map(changes =>
changes.map(c => ({ key: c.payload.key, ...c.payload.val() }))
)
).subscribe(item => {
item.forEach(element => {
var y = element;
if(this.LoggedInUserDetails.user_guid==y["receiverID"]){
if(typeof(user) != "undefined" ){
var firebasedata = new Message();
firebasedata.fromId = y["senderID"];
firebasedata.toId = y["receiverID"];
firebasedata.message = y["messageText"];
this.onMessageReceived(user, firebasedata);
}
}
});
});
}
messagehistory(data){
// var x = this.onloadchatObj(data.room);
this.db.list('/chats/'+data.room, ref => ref.orderByChild('new').limitToLast(99))
.snapshotChanges().pipe(
map(changes =>
changes.map(c => ({ key: c.payload.key, ...c.payload.val() }))
)
).subscribe(item => {
item.forEach(element => {
this.historyMessages.push({
fromId: element["senderID"],
toId: element["receiverID"],
message: element["messageText"]
});
});
});
}
onloadchatObj(chatRoom){
var items = this.db.list('/chats/'+chatRoom);
//console.log(items);
return items;
}
handleError(error) {
console.log(error);
// return false;
}
sendMessage(message: Message): void {
var roomid;
this.roomids.forEach(function(value,key) {
if(message.toId == value.receiverid){
roomid = value.roomid;
}
});
if(roomid){
let time = new Date();
var firebasedb = this.onloadchatObj(roomid);
firebasedb.push({
messageText: message.message,
receiverID: message.toId,
senderID: message.fromId,
timestamp: time.getTime()
});
}
var user = this.friends.find(x => x.id == message.fromId);
setTimeout(() => {
// this.onMessageReceived(user, message);
}, 1000);
}
}`
i am getting two problems
1 when click any two user for chat at time message history coming in wrong window chat
2 when page load chat window is automatic open with notification
myadapter.ts `import { Injectable } from '@angular/core'; import { ChatAdapter, User, Message, UserStatus } from 'ng-chat'; import { Observable, of } from 'rxjs'; import { delay } from "rxjs/operators"; import { map, catchError } from 'rxjs/operators'; import { ChatsService } from '../services/chats.service'; import { AngularFireDatabase, AngularFireList } from 'angularfire2/database'; import { TokenService } from '../services/token.service'; @Injectable() export class MyAdapter extends ChatAdapter { private historyMessages: Message[] = []; public SubmitData: Object = {}; public LoggedInUserDetails: any; public friends: any; public roomids: any; public Onloadchat:boolean = false; constructor(private _chatService: ChatsService, public db: AngularFireDatabase, private token: TokenService,) { super(); this.LoggedInUserDetails = this.token.getUser(); this.roomids = []; } listFriends(): Observable<User[]> { this._chatService.Friendlist(). subscribe( data => this.FriendlistResponse(data), error => this.handleError(error)); return of([]); } FriendlistResponse(data){ if(data){ this.friends = data; this.onFriendsListChanged(data); data.forEach(element => { console.log(element); this.OnLoadMessage(element); }); } } getMessageHistory(userId: any): Observable<Message[]> { this.historyMessages=[]; this.SubmitData['guid'] = userId; if(userId != this.LoggedInUserDetails.user_guid){ this._chatService.UpdateChatRoom(this.SubmitData). subscribe( data => this.ChatRoomhandleResponse(data), error => this.handleError(error)); } return of(this.historyMessages).pipe(delay(5000)); } ChatRoomhandleResponse(data) { if(data.room){ // this.roomid =data.room; if(data.sender != this.LoggedInUserDetails.user_guid){ var receiverid = data.sender; }else{ var receiverid = data.receiver; } this.roomids.push({ roomid: data.room, receiverid: receiverid }); this.messagehistory(data); } }
}`
i am getting two problems 1 when click any two user for chat at time message history coming in wrong window chat 2 when page load chat window is automatic open with notification