|
|
@ -4,6 +4,7 @@ import { Chat } from 'src/app/models/chat';
|
|
|
|
import { responsePathAsArray } from 'graphql';
|
|
|
|
import { responsePathAsArray } from 'graphql';
|
|
|
|
import { Chatmessage } from 'src/app/models/chatmessage';
|
|
|
|
import { Chatmessage } from 'src/app/models/chatmessage';
|
|
|
|
import { FriendInfo } from 'src/app/models/friendinfo';
|
|
|
|
import { FriendInfo } from 'src/app/models/friendinfo';
|
|
|
|
|
|
|
|
import { DatasharingService } from '../datasharing.service';
|
|
|
|
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
providedIn: 'root'
|
|
|
@ -12,11 +13,15 @@ export class ChatService {
|
|
|
|
|
|
|
|
|
|
|
|
arr: number[]
|
|
|
|
arr: number[]
|
|
|
|
ownID: number
|
|
|
|
ownID: number
|
|
|
|
chats: Array<Chat>
|
|
|
|
chats: Array<Chat> = []
|
|
|
|
|
|
|
|
|
|
|
|
constructor(private http: Http) { }
|
|
|
|
constructor(private http: Http, private data: DatasharingService) {
|
|
|
|
|
|
|
|
this.data.currentUserInfo.subscribe(user => {
|
|
|
|
|
|
|
|
this.ownID = user.userID})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public getAllChats(): Array<Chat> {
|
|
|
|
public getAllChats(): Array<Chat> {
|
|
|
|
|
|
|
|
console.log("Getting all chats ..")
|
|
|
|
let url = 'https://greenvironment.net/graphql'
|
|
|
|
let url = 'https://greenvironment.net/graphql'
|
|
|
|
|
|
|
|
|
|
|
|
let headers = new Headers()
|
|
|
|
let headers = new Headers()
|
|
|
@ -29,6 +34,24 @@ export class ChatService {
|
|
|
|
return this.chats
|
|
|
|
return this.chats
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public getChatsByID(pChatIDs: number[]): Array<Chat> {
|
|
|
|
|
|
|
|
this.chats = []
|
|
|
|
|
|
|
|
console.log("Getting chats by ID..")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(let chatId of pChatIDs) {
|
|
|
|
|
|
|
|
let url = 'https://greenvironment.net/graphql'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let headers = new Headers()
|
|
|
|
|
|
|
|
headers.set('Content-Type', 'application/json')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.http.post(url, this.getBodyForGetChatsByID(chatId))
|
|
|
|
|
|
|
|
.subscribe(response => {
|
|
|
|
|
|
|
|
this.updateChat(response.json())
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.chats
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public createNewChat(pUserID: number) {
|
|
|
|
public createNewChat(pUserID: number) {
|
|
|
|
let url = 'https://greenvironment.net/graphql'
|
|
|
|
let url = 'https://greenvironment.net/graphql'
|
|
|
|
|
|
|
|
|
|
|
@ -72,11 +95,38 @@ export class ChatService {
|
|
|
|
let headers = new Headers()
|
|
|
|
let headers = new Headers()
|
|
|
|
headers.set('Content-Type', 'application/json')
|
|
|
|
headers.set('Content-Type', 'application/json')
|
|
|
|
|
|
|
|
|
|
|
|
this.http.post(url, this.getBodyForSendMessage(pChatID, pContent))
|
|
|
|
this.http.post(url, this.getBodyForSendMessage(pChatID, pContent)).subscribe(response => console.log("Message sent"))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public getMessages(pChatID): Array<Chatmessage> {
|
|
|
|
|
|
|
|
let messages: Array<Chatmessage>
|
|
|
|
|
|
|
|
let url = 'https://greenvironment.net/graphql'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let headers = new Headers()
|
|
|
|
|
|
|
|
headers.set('Content-Type', 'application/json')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.http.post(url, this.getBodyForGetMessagesInChat(pChatID)).subscribe(response =>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
console.log("Downloading messages ...")
|
|
|
|
|
|
|
|
messages = this.updateMessages(response.json())
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
return messages
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
updateMessages(pResponse: any): Array<Chatmessage> {
|
|
|
|
|
|
|
|
let messages = new Array<Chatmessage>()
|
|
|
|
|
|
|
|
for(let message of pResponse.data.getChat.messages) {
|
|
|
|
|
|
|
|
if(message.author.id == this.ownID) {
|
|
|
|
|
|
|
|
messages.push(new Chatmessage(message.content, message.createdAt, true))
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
messages.push(new Chatmessage(message.content, message.createdAt, false))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return messages
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
updateAllChats(pResponse: any): Array<Chat> {
|
|
|
|
updateAllChats(pResponse: any): Array<Chat> {
|
|
|
|
let chats: Array<Chat>
|
|
|
|
let chats = Array<Chat>()
|
|
|
|
for(let chat of pResponse.data.getSelf.chats) {
|
|
|
|
for(let chat of pResponse.data.getSelf.chats) {
|
|
|
|
let memberID: number
|
|
|
|
let memberID: number
|
|
|
|
let memberName: string
|
|
|
|
let memberName: string
|
|
|
@ -86,7 +136,7 @@ export class ChatService {
|
|
|
|
memberName = member.name
|
|
|
|
memberName = member.name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let messages: Array<Chatmessage>
|
|
|
|
let messages = new Array<Chatmessage>()
|
|
|
|
for(let message of chat.messages) {
|
|
|
|
for(let 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))
|
|
|
@ -99,6 +149,27 @@ export class ChatService {
|
|
|
|
return chats
|
|
|
|
return chats
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
updateChat(pResponse: any) {
|
|
|
|
|
|
|
|
let id = pResponse.data.getChat.id
|
|
|
|
|
|
|
|
let memberId : number
|
|
|
|
|
|
|
|
let memberName: string
|
|
|
|
|
|
|
|
for(let member of pResponse.data.getChat.members) {
|
|
|
|
|
|
|
|
if(member.id != this.ownID) {
|
|
|
|
|
|
|
|
memberId = member.id
|
|
|
|
|
|
|
|
memberName = member.name
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
let messages = new Array<Chatmessage>()
|
|
|
|
|
|
|
|
for(let message of pResponse.data.getChat.messages) {
|
|
|
|
|
|
|
|
if(message.author.id == this.ownID) {
|
|
|
|
|
|
|
|
messages.push(new Chatmessage(message.content, message.createdAt, true))
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
messages.push(new Chatmessage(message.content, message.createdAt, false))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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[]) {
|
|
|
@ -133,11 +204,34 @@ export class ChatService {
|
|
|
|
|
|
|
|
|
|
|
|
getBodyForGetAllChats() {
|
|
|
|
getBodyForGetAllChats() {
|
|
|
|
const body = {query: `query {
|
|
|
|
const body = {query: `query {
|
|
|
|
getSelf {
|
|
|
|
getUser {
|
|
|
|
chats(first: 1000, offset: 0) {id, members{name, id},
|
|
|
|
chats(first: 1000, offset: 0) {id, members{name, id},
|
|
|
|
messages(first: 1000, offset: 0) {author {id}, createdAt, content}}
|
|
|
|
messages(first: 1000, offset: 0) {author {id}, createdAt, content}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return body
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getBodyForGetChatsByID(pChatID: number) {
|
|
|
|
|
|
|
|
const body = {query: `query($chatID: ID!) {
|
|
|
|
|
|
|
|
getChat(chatId: $chatID) {id, members{name, id},
|
|
|
|
|
|
|
|
messages(first: 1000, offset: 0) {author {id}, createdAt, content}}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}`, variables: {
|
|
|
|
|
|
|
|
chatId: pChatID
|
|
|
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
return body
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getBodyForGetMessagesInChat(pChatID: number) {
|
|
|
|
|
|
|
|
const body = {query: `query($chatID: ID!) {
|
|
|
|
|
|
|
|
getChat(chatId: $chatID) {
|
|
|
|
|
|
|
|
messages(first: 1000, offset: 0) {author {id}, createdAt, content}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}`, variables: {
|
|
|
|
|
|
|
|
chatId: pChatID
|
|
|
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
return body
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|