@ -30,11 +30,21 @@ export class ChatService {
this . http . post ( url , this . getBodyForGetAllChats ( ) )
this . http . post ( url , this . getBodyForGetAllChats ( ) )
. subscribe ( response = > {
. subscribe ( response = > {
this . chats = this . update AllChats( response . json ( ) )
this . chats = this . render AllChats( response . json ( ) )
} ) ;
} ) ;
return this . chats
return this . chats
}
}
public getAllChatsRaw ( ) : any {
console . log ( "Getting all chats .." )
let url = 'https://greenvironment.net/graphql'
let headers = new Headers ( )
headers . set ( 'Content-Type' , 'application/json' )
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.." )
@ -53,6 +63,23 @@ export class ChatService {
return this . chats
return this . chats
}
}
public getChatsByIDRaw ( pChatIDs : number [ ] ) : any {
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 = environment . graphQLUrl
let url = environment . graphQLUrl
@ -90,13 +117,13 @@ export class ChatService {
return chatPartners
return chatPartners
}
}
public sendMessage ( pChatID : number , pContent : string ) {
public sendMessage ( pChatID : number , pContent : string ) : any {
let url = environment . graphQLUrl
let url = environment . graphQLUrl
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 ) ) . subscribe ( response = > console . log ( "Message sent" ) )
return this. http . post ( url , this . getBodyForSendMessage ( pChatID , pContent ) )
}
}
public getMessages ( pChatID ) : Array < Chatmessage > {
public getMessages ( pChatID ) : Array < Chatmessage > {
@ -109,12 +136,21 @@ export class ChatService {
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 . update Messages( response . json ( ) )
messages = this . render Messages( response . json ( ) )
} )
} )
return messages
return messages
}
}
updateMessages ( pResponse : any ) : Array < Chatmessage > {
public getMessagesRaw ( pChatID ) : any {
let url = 'https://greenvironment.net/graphql'
let headers = new Headers ( )
headers . set ( 'Content-Type' , 'application/json' )
return this . http . post ( url , this . getBodyForGetMessagesInChat ( pChatID ) )
}
public renderMessages ( pResponse : any ) : Array < Chatmessage > {
let messages = new Array < Chatmessage > ( )
let messages = new Array < Chatmessage > ( )
for ( let message of pResponse . data . getChat . messages ) {
for ( let message of pResponse . data . getChat . messages ) {
if ( message . author . id == this . ownID ) {
if ( message . author . id == this . ownID ) {
@ -126,7 +162,7 @@ export class ChatService {
return messages
return messages
}
}
update AllChats( pResponse : any ) : Array < Chat > {
public render AllChats( 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
@ -193,8 +229,8 @@ export class ChatService {
}
}
getBodyForSendMessage ( pchatID : number , pContent : string ) {
getBodyForSendMessage ( pchatID : number , pContent : string ) {
const body = { query : ` mutation( $ chatI D: number, $ content: string ) {
const body = { query : ` mutation( $ chatI d: ID!, $ content: String! ) {
sendMessage ( chatId : $chatI D , content : $content ) { id }
sendMessage ( chatId : $chatI d , content : $content ) { id }
} ` , variables: {
} ` , variables: {
chatId : pchatID ,
chatId : pchatID ,
content : pContent
content : pContent
@ -205,9 +241,13 @@ export class ChatService {
getBodyForGetAllChats() {
getBodyForGetAllChats() {
const body = { query : ` query {
const body = { query : ` query {
getUser {
getSelf {
chats ( first : 1000 , offset : 0 ) { id , members { name , id } ,
chats ( first : 1000 , offset : 0 ) {
messages ( first : 1000 , offset : 0 ) { author { id } , createdAt , content } }
id , members { name , id } ,
messages ( first : 1000 , offset : 0 ) {
author { id } , createdAt , content
}
}
}
}
} `
} `
}
}
@ -226,8 +266,8 @@ export class ChatService {
}
}
getBodyForGetMessagesInChat ( pChatID : number ) {
getBodyForGetMessagesInChat ( pChatID : number ) {
const body = { query : ` query( $ chatI D : ID!) {
const body = { query : ` query( $ chatI d : ID!) {
getChat ( chatId : $chatI D ) {
getChat ( chatId : $chatI d ) {
messages ( first : 1000 , offset : 0 ) { author { id } , createdAt , content }
messages ( first : 1000 , offset : 0 ) { author { id } , createdAt , content }
}
}
} ` , variables: {
} ` , variables: {