|
|
@ -12,212 +12,211 @@ import { environment } from 'src/environments/environment';
|
|
|
|
})
|
|
|
|
})
|
|
|
|
export class ChatService {
|
|
|
|
export class ChatService {
|
|
|
|
|
|
|
|
|
|
|
|
arr: number[]
|
|
|
|
arr: number[];
|
|
|
|
ownID: number
|
|
|
|
ownID: number;
|
|
|
|
chats: Array<Chat> = []
|
|
|
|
chats: Array<Chat> = [];
|
|
|
|
|
|
|
|
|
|
|
|
constructor(private http: Http, private data: DatasharingService) {
|
|
|
|
constructor(private http: Http, private data: DatasharingService) {
|
|
|
|
this.data.currentUserInfo.subscribe(user => {
|
|
|
|
this.data.currentUserInfo.subscribe(user => {
|
|
|
|
this.ownID = user.userID})
|
|
|
|
this.ownID = user.userID; });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public getAllChats(): Array<Chat> {
|
|
|
|
public getAllChats(): Array<Chat> {
|
|
|
|
console.log("Getting all chats ..")
|
|
|
|
console.log('Getting all chats ..');
|
|
|
|
let url = environment.graphQLUrl
|
|
|
|
const url = environment.graphQLUrl;
|
|
|
|
|
|
|
|
|
|
|
|
let headers = new Headers()
|
|
|
|
const headers = new Headers();
|
|
|
|
headers.set('Content-Type', 'application/json')
|
|
|
|
headers.set('Content-Type', 'application/json');
|
|
|
|
|
|
|
|
|
|
|
|
this.http.post(url, this.getBodyForGetAllChats())
|
|
|
|
this.http.post(url, this.getBodyForGetAllChats())
|
|
|
|
.subscribe(response => {
|
|
|
|
.subscribe(response => {
|
|
|
|
this.chats = this.renderAllChats(response.json())
|
|
|
|
this.chats = this.renderAllChats(response.json());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return this.chats
|
|
|
|
return this.chats;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public getAllChatsRaw(): any {
|
|
|
|
public getAllChatsRaw(): any {
|
|
|
|
console.log("Getting all chats ..")
|
|
|
|
console.log('Getting all chats ..');
|
|
|
|
let url = 'https://greenvironment.net/graphql'
|
|
|
|
const url = 'https://greenvironment.net/graphql';
|
|
|
|
|
|
|
|
|
|
|
|
let headers = new Headers()
|
|
|
|
const headers = new Headers();
|
|
|
|
headers.set('Content-Type', 'application/json')
|
|
|
|
headers.set('Content-Type', 'application/json');
|
|
|
|
|
|
|
|
|
|
|
|
return this.http.post(url, this.getBodyForGetAllChats())
|
|
|
|
return this.http.post(url, this.getBodyForGetAllChats());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public getChatsByID(pChatIDs: number[]): Array<Chat> {
|
|
|
|
public getChatsByID(pChatIDs: number[]): Array<Chat> {
|
|
|
|
this.chats = []
|
|
|
|
this.chats = [];
|
|
|
|
console.log("Getting chats by ID..")
|
|
|
|
console.log('Getting chats by ID..');
|
|
|
|
|
|
|
|
|
|
|
|
for(let chatId of pChatIDs) {
|
|
|
|
for (const chatId of pChatIDs) {
|
|
|
|
let url = environment.graphQLUrl
|
|
|
|
const url = environment.graphQLUrl;
|
|
|
|
|
|
|
|
|
|
|
|
let headers = new Headers()
|
|
|
|
const headers = new Headers();
|
|
|
|
headers.set('Content-Type', 'application/json')
|
|
|
|
headers.set('Content-Type', 'application/json');
|
|
|
|
|
|
|
|
|
|
|
|
this.http.post(url, this.getBodyForGetChatsByID(chatId))
|
|
|
|
this.http.post(url, this.getBodyForGetChatsByID(chatId))
|
|
|
|
.subscribe(response => {
|
|
|
|
.subscribe(response => {
|
|
|
|
this.updateChat(response.json())
|
|
|
|
this.updateChat(response.json());
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.chats
|
|
|
|
return this.chats;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public getChatsByIDRaw(pChatIDs: number[]): any {
|
|
|
|
public getChatsByIDRaw(pChatIDs: number[]): any {
|
|
|
|
console.log("Getting chats by ID..")
|
|
|
|
console.log('Getting chats by ID..');
|
|
|
|
|
|
|
|
|
|
|
|
for(let chatId of pChatIDs) {
|
|
|
|
for (const chatId of pChatIDs) {
|
|
|
|
let url = 'https://greenvironment.net/graphql'
|
|
|
|
const url = 'https://greenvironment.net/graphql';
|
|
|
|
|
|
|
|
|
|
|
|
let headers = new Headers()
|
|
|
|
const headers = new Headers();
|
|
|
|
headers.set('Content-Type', 'application/json')
|
|
|
|
headers.set('Content-Type', 'application/json');
|
|
|
|
|
|
|
|
|
|
|
|
this.http.post(url, this.getBodyForGetChatsByID(chatId))
|
|
|
|
this.http.post(url, this.getBodyForGetChatsByID(chatId))
|
|
|
|
.subscribe(response => {
|
|
|
|
.subscribe(response => {
|
|
|
|
this.updateChat(response.json())
|
|
|
|
this.updateChat(response.json());
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.chats
|
|
|
|
return this.chats;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public createNewChat(pUserID: number) {
|
|
|
|
public createNewChat(pUserID: number) {
|
|
|
|
let url = environment.graphQLUrl
|
|
|
|
const url = environment.graphQLUrl;
|
|
|
|
|
|
|
|
|
|
|
|
let headers = new Headers()
|
|
|
|
const headers = new Headers();
|
|
|
|
headers.set('Content-Type', 'application/json')
|
|
|
|
headers.set('Content-Type', 'application/json');
|
|
|
|
|
|
|
|
|
|
|
|
this.http.post(url, this.getBodyForNewChat(pUserID))
|
|
|
|
this.http.post(url, this.getBodyForNewChat(pUserID));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public requestAllChatPartners(): Array<FriendInfo> {
|
|
|
|
public requestAllChatPartners(): Array<FriendInfo> {
|
|
|
|
let url = environment.graphQLUrl
|
|
|
|
const url = environment.graphQLUrl;
|
|
|
|
let chatPartners: Array<FriendInfo>
|
|
|
|
let chatPartners: Array<FriendInfo>;
|
|
|
|
let temp
|
|
|
|
let temp;
|
|
|
|
|
|
|
|
|
|
|
|
let headers = new Headers()
|
|
|
|
const headers = new Headers();
|
|
|
|
headers.set('Content-Type', 'application/json')
|
|
|
|
headers.set('Content-Type', 'application/json');
|
|
|
|
|
|
|
|
|
|
|
|
this.http.post(url, this.getBodyForRequestOfAllChatPartners())
|
|
|
|
this.http.post(url, this.getBodyForRequestOfAllChatPartners())
|
|
|
|
.subscribe(response => {
|
|
|
|
.subscribe(response => {
|
|
|
|
temp = response.json()
|
|
|
|
temp = response.json();
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
for(let chat of temp.data.getSelf.chats) {
|
|
|
|
for (const chat of temp.data.getSelf.chats) {
|
|
|
|
let memberID: number
|
|
|
|
let memberID: number;
|
|
|
|
let memberName: string
|
|
|
|
let memberName: string;
|
|
|
|
let memberLevel: number
|
|
|
|
let memberLevel: number;
|
|
|
|
for(let member of chat.members) {
|
|
|
|
for (const member of chat.members) {
|
|
|
|
if(member.id != this.ownID) {
|
|
|
|
if (member.id != this.ownID) {
|
|
|
|
memberID = member.id
|
|
|
|
memberID = member.id;
|
|
|
|
memberName = member.name
|
|
|
|
memberName = member.name;
|
|
|
|
memberLevel = member.level
|
|
|
|
memberLevel = member.level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
chatPartners.push(new FriendInfo(memberID, memberName, memberLevel))
|
|
|
|
chatPartners.push(new FriendInfo(memberID, memberName, memberLevel));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return chatPartners
|
|
|
|
return chatPartners;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public sendMessage(pChatID: number, pContent: string): any {
|
|
|
|
public sendMessage(pChatID: number, pContent: string): any {
|
|
|
|
let url = environment.graphQLUrl
|
|
|
|
const url = environment.graphQLUrl;
|
|
|
|
|
|
|
|
|
|
|
|
let headers = new Headers()
|
|
|
|
const headers = new Headers();
|
|
|
|
headers.set('Content-Type', 'application/json')
|
|
|
|
headers.set('Content-Type', 'application/json');
|
|
|
|
|
|
|
|
|
|
|
|
return this.http.post(url, this.getBodyForSendMessage(pChatID, pContent))
|
|
|
|
return this.http.post(url, this.getBodyForSendMessage(pChatID, pContent));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public getMessages(pChatID): Array<Chatmessage> {
|
|
|
|
public getMessages(pChatID): Array<Chatmessage> {
|
|
|
|
let messages: Array<Chatmessage>
|
|
|
|
let messages: Array<Chatmessage>;
|
|
|
|
let url = environment.graphQLUrl
|
|
|
|
const url = environment.graphQLUrl;
|
|
|
|
|
|
|
|
|
|
|
|
let headers = new Headers()
|
|
|
|
const headers = new Headers();
|
|
|
|
headers.set('Content-Type', 'application/json')
|
|
|
|
headers.set('Content-Type', 'application/json');
|
|
|
|
|
|
|
|
|
|
|
|
this.http.post(url, this.getBodyForGetMessagesInChat(pChatID)).subscribe(response =>
|
|
|
|
this.http.post(url, this.getBodyForGetMessagesInChat(pChatID)).subscribe(response => {
|
|
|
|
{
|
|
|
|
console.log('Downloading messages ...');
|
|
|
|
console.log("Downloading messages ...")
|
|
|
|
messages = this.renderMessages(response.json());
|
|
|
|
messages = this.renderMessages(response.json())
|
|
|
|
});
|
|
|
|
})
|
|
|
|
return messages;
|
|
|
|
return messages
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public getMessagesRaw(pChatID): any {
|
|
|
|
public getMessagesRaw(pChatID): any {
|
|
|
|
let url = 'https://greenvironment.net/graphql'
|
|
|
|
const url = 'https://greenvironment.net/graphql';
|
|
|
|
|
|
|
|
|
|
|
|
let headers = new Headers()
|
|
|
|
const headers = new Headers();
|
|
|
|
headers.set('Content-Type', 'application/json')
|
|
|
|
headers.set('Content-Type', 'application/json');
|
|
|
|
|
|
|
|
|
|
|
|
return this.http.post(url, this.getBodyForGetMessagesInChat(pChatID))
|
|
|
|
return this.http.post(url, this.getBodyForGetMessagesInChat(pChatID));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public renderMessages(pResponse: any): Array<Chatmessage> {
|
|
|
|
public renderMessages(pResponse: any): Array<Chatmessage> {
|
|
|
|
let messages = new Array<Chatmessage>()
|
|
|
|
const messages = new Array<Chatmessage>();
|
|
|
|
for(let message of pResponse.data.getChat.messages) {
|
|
|
|
for (const message of pResponse.data.getChat.messages) {
|
|
|
|
if(message.author.id == this.ownID) {
|
|
|
|
if (message.author.id == this.ownID) {
|
|
|
|
messages.push(new Chatmessage(message.content, message.createdAt, true))
|
|
|
|
messages.push(new Chatmessage(message.content, message.createdAt, true));
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
messages.push(new Chatmessage(message.content, message.createdAt, false))
|
|
|
|
messages.push(new Chatmessage(message.content, message.createdAt, false));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return messages
|
|
|
|
return messages;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public renderAllChats(pResponse: any): Array<Chat> {
|
|
|
|
public renderAllChats(pResponse: any): Array<Chat> {
|
|
|
|
let chats = Array<Chat>()
|
|
|
|
const chats = Array<Chat>();
|
|
|
|
for(let chat of pResponse.data.getSelf.chats) {
|
|
|
|
for (const chat of pResponse.data.getSelf.chats) {
|
|
|
|
let memberID: number
|
|
|
|
let memberID: number;
|
|
|
|
let memberName: string
|
|
|
|
let memberName: string;
|
|
|
|
for(let member of chat.members) {
|
|
|
|
for (const member of chat.members) {
|
|
|
|
if(member.id != this.ownID) {
|
|
|
|
if (member.id != this.ownID) {
|
|
|
|
memberID = member.id
|
|
|
|
memberID = member.id;
|
|
|
|
memberName = member.name
|
|
|
|
memberName = member.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let messages = new Array<Chatmessage>()
|
|
|
|
const messages = new Array<Chatmessage>();
|
|
|
|
for(let message of chat.messages) {
|
|
|
|
for (const message of chat.messages) {
|
|
|
|
if(message.author.id == this.ownID) {
|
|
|
|
if (message.author.id == this.ownID) {
|
|
|
|
messages.push(new Chatmessage(message.content, message.createdAt, true))
|
|
|
|
messages.push(new Chatmessage(message.content, message.createdAt, true));
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
messages.push(new Chatmessage(message.content, message.createdAt, false))
|
|
|
|
messages.push(new Chatmessage(message.content, message.createdAt, false));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
chats.push(new Chat(chat.id, memberID, memberName, messages))
|
|
|
|
chats.push(new Chat(chat.id, memberID, memberName, messages));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return chats
|
|
|
|
return chats;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
updateChat(pResponse: any) {
|
|
|
|
updateChat(pResponse: any) {
|
|
|
|
let id = pResponse.data.getChat.id
|
|
|
|
const id = pResponse.data.getChat.id;
|
|
|
|
let memberId : number
|
|
|
|
let memberId: number;
|
|
|
|
let memberName: string
|
|
|
|
let memberName: string;
|
|
|
|
for(let member of pResponse.data.getChat.members) {
|
|
|
|
for (const member of pResponse.data.getChat.members) {
|
|
|
|
if(member.id != this.ownID) {
|
|
|
|
if (member.id != this.ownID) {
|
|
|
|
memberId = member.id
|
|
|
|
memberId = member.id;
|
|
|
|
memberName = member.name
|
|
|
|
memberName = member.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let messages = new Array<Chatmessage>()
|
|
|
|
const messages = new Array<Chatmessage>();
|
|
|
|
for(let message of pResponse.data.getChat.messages) {
|
|
|
|
for (const message of pResponse.data.getChat.messages) {
|
|
|
|
if(message.author.id == this.ownID) {
|
|
|
|
if (message.author.id == this.ownID) {
|
|
|
|
messages.push(new Chatmessage(message.content, message.createdAt, true))
|
|
|
|
messages.push(new Chatmessage(message.content, message.createdAt, true));
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
messages.push(new Chatmessage(message.content, message.createdAt, false))
|
|
|
|
messages.push(new Chatmessage(message.content, message.createdAt, false));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.chats.push(new Chat(id, memberId, memberName, messages))
|
|
|
|
this.chats.push(new Chat(id, memberId, memberName, messages));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
getBodyForNewChat(pUserID: number) {
|
|
|
|
getBodyForNewChat(pUserID: number) {
|
|
|
|
this.arr = [pUserID]
|
|
|
|
this.arr = [pUserID];
|
|
|
|
const body = {query: `mutation($userID: number[]) {
|
|
|
|
const body = {query: `mutation($userID: number[]) {
|
|
|
|
createChat(members: $userID) {id}
|
|
|
|
createChat(members: $userID) {id}
|
|
|
|
}`, variables: {
|
|
|
|
}`, variables: {
|
|
|
|
members: this.arr
|
|
|
|
members: this.arr
|
|
|
|
}};
|
|
|
|
}};
|
|
|
|
|
|
|
|
|
|
|
|
return body
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
getBodyForRequestOfAllChatPartners() {
|
|
|
|
getBodyForRequestOfAllChatPartners() {
|
|
|
@ -225,9 +224,9 @@ export class ChatService {
|
|
|
|
getSelf {
|
|
|
|
getSelf {
|
|
|
|
chats(first: 1000, offset: 0) {members{name, id, level}}
|
|
|
|
chats(first: 1000, offset: 0) {members{name, id, level}}
|
|
|
|
}}`
|
|
|
|
}}`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return body
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
getBodyForSendMessage(pchatID: number, pContent: string) {
|
|
|
|
getBodyForSendMessage(pchatID: number, pContent: string) {
|
|
|
@ -238,7 +237,7 @@ export class ChatService {
|
|
|
|
content: pContent
|
|
|
|
content: pContent
|
|
|
|
}};
|
|
|
|
}};
|
|
|
|
|
|
|
|
|
|
|
|
return body
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
getBodyForGetAllChats() {
|
|
|
|
getBodyForGetAllChats() {
|
|
|
@ -252,8 +251,8 @@ export class ChatService {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
}`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return body
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
getBodyForGetChatsByID(pChatID: number) {
|
|
|
|
getBodyForGetChatsByID(pChatID: number) {
|
|
|
@ -263,8 +262,8 @@ export class ChatService {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`, variables: {
|
|
|
|
}`, variables: {
|
|
|
|
chatId: pChatID
|
|
|
|
chatId: pChatID
|
|
|
|
}}
|
|
|
|
}};
|
|
|
|
return body
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
getBodyForGetMessagesInChat(pChatID: number) {
|
|
|
|
getBodyForGetMessagesInChat(pChatID: number) {
|
|
|
@ -274,7 +273,7 @@ export class ChatService {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`, variables: {
|
|
|
|
}`, variables: {
|
|
|
|
chatId: pChatID
|
|
|
|
chatId: pChatID
|
|
|
|
}}
|
|
|
|
}};
|
|
|
|
return body
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|