diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 35e899d..9ac92e0 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -30,6 +30,7 @@ export class AppComponent implements OnInit { this.data.currentUserInfo.subscribe(user => { this.userInfo = user; console.log(this.userInfo); + this.data.changeChatIDs(this.chatIDs) }) } } diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 70177f6..2029fdc 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -25,6 +25,7 @@ import { HttpClientModule } from '@angular/common/http'; import { ProfileComponent } from './components/profile/profile.component'; import { ImprintComponent } from './components/imprint/imprint.component'; import { AboutComponent } from './components/about/about.component'; +import { ChatcontactsComponent } from './components/chatmanager/chatcontacts/chatcontacts.component'; const config: SocketIoConfig = { url: 'http://localhost:4444', options: {} }; @@ -53,6 +54,7 @@ const appRoutes: Routes = [ GroupsComponent, ChatmanagerComponent, ChatlistComponent, + ChatcontactsComponent, PostlistComponent, ImprintComponent, AboutComponent, diff --git a/src/app/components/chat/chat.component.ts b/src/app/components/chat/chat.component.ts index 45b8749..50dedca 100644 --- a/src/app/components/chat/chat.component.ts +++ b/src/app/components/chat/chat.component.ts @@ -9,9 +9,9 @@ import { Chatinfo } from 'src/app/models/chatinfo'; }) export class ChatComponent implements OnInit { - messages:Array = [new Chatmessage("Hallo",true), new Chatmessage("Hallo",false), - new Chatmessage("Hallo",true), new Chatmessage("Hallo",false), new Chatmessage("Hallo",true), - new Chatmessage("Hallo",false)] + messages:Array = [new Chatmessage("Hallo", "01.01.",true), new Chatmessage("Hallo", "01.01.",true), + new Chatmessage("Hallo", "01.01.",true), new Chatmessage("Hallo", "01.01.",true), new Chatmessage("Hallo", "01.01.",true), + new Chatmessage("Hallo", "01.01.",true)] @Output() goBackEvent = new EventEmitter(); @Input() childChat: Chatinfo; diff --git a/src/app/components/chatlist/chatlist.component.ts b/src/app/components/chatlist/chatlist.component.ts index 83c2402..4c12acc 100644 --- a/src/app/components/chatlist/chatlist.component.ts +++ b/src/app/components/chatlist/chatlist.component.ts @@ -13,6 +13,7 @@ export class ChatlistComponent implements OnInit { new Chatinfo("David", "23.06.19 12:50"), new Chatinfo("Bruno", "23.06.19 12:50")] @Output() showChatEvent = new EventEmitter(); + @Output() showCreateChatEvent = new EventEmitter(); selectedChat: Chatinfo; constructor(private chatService: ChatService) { } @@ -20,8 +21,8 @@ export class ChatlistComponent implements OnInit { ngOnInit() { } - newChat() { - this.chatService.getSelfName() + showNewChat() { + this.showCreateChatEvent.emit(true) } showChat(pChat: Chatinfo) { diff --git a/src/app/components/chatmanager/chatcontacts/chatcontacts.component.html b/src/app/components/chatmanager/chatcontacts/chatcontacts.component.html new file mode 100644 index 0000000..a55f166 --- /dev/null +++ b/src/app/components/chatmanager/chatcontacts/chatcontacts.component.html @@ -0,0 +1,15 @@ +
+ +
+
+
+
+
Pic
+
{{contact.name}}
+
+
+
diff --git a/src/app/components/chatmanager/chatcontacts/chatcontacts.component.sass b/src/app/components/chatmanager/chatcontacts/chatcontacts.component.sass new file mode 100644 index 0000000..e69de29 diff --git a/src/app/components/chatmanager/chatcontacts/chatcontacts.component.spec.ts b/src/app/components/chatmanager/chatcontacts/chatcontacts.component.spec.ts new file mode 100644 index 0000000..f94d884 --- /dev/null +++ b/src/app/components/chatmanager/chatcontacts/chatcontacts.component.spec.ts @@ -0,0 +1,25 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; + +import { ChatcontactsComponent } from './chatcontacts.component'; + +describe('ChatcontactsComponent', () => { + let component: ChatcontactsComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ ChatcontactsComponent ] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(ChatcontactsComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/components/chatmanager/chatcontacts/chatcontacts.component.ts b/src/app/components/chatmanager/chatcontacts/chatcontacts.component.ts new file mode 100644 index 0000000..6f2d3c3 --- /dev/null +++ b/src/app/components/chatmanager/chatcontacts/chatcontacts.component.ts @@ -0,0 +1,30 @@ +import { Component, OnInit, Output, EventEmitter } from '@angular/core'; +import { FriendInfo } from 'src/app/models/friendinfo'; +import { ChatService } from 'src/app/services/chat/chat.service'; + +@Component({ + selector: 'chatmanager-chatcontacts', + templateUrl: './chatcontacts.component.html', + styleUrls: ['./chatcontacts.component.sass'] +}) +export class ChatcontactsComponent implements OnInit { + + @Output() goBackEvent = new EventEmitter(); + selectedContact: FriendInfo + + constructor(private chatService: ChatService) { } + + ngOnInit() { + } + + goBack() { + this.goBackEvent.emit(true) + } + + createChat(pFriendInfo: FriendInfo) { + this.selectedContact = pFriendInfo + this.chatService.createNewChat(pFriendInfo.id) + this.goBack() + } + +} diff --git a/src/app/components/chatmanager/chatmanager.component.html b/src/app/components/chatmanager/chatmanager.component.html index e3964c7..039d4a1 100644 --- a/src/app/components/chatmanager/chatmanager.component.html +++ b/src/app/components/chatmanager/chatmanager.component.html @@ -1,6 +1,9 @@
- +
+
+ +
diff --git a/src/app/components/chatmanager/chatmanager.component.ts b/src/app/components/chatmanager/chatmanager.component.ts index b2db03a..578f96d 100644 --- a/src/app/components/chatmanager/chatmanager.component.ts +++ b/src/app/components/chatmanager/chatmanager.component.ts @@ -2,6 +2,7 @@ import { Component, OnInit, ViewChild } from '@angular/core'; import { ChatService } from '../../services/chat/chat.service' import { ChatComponent } from '../chat/chat.component'; import { Chatinfo } from 'src/app/models/chatinfo'; +import { DatasharingService } from 'src/app/services/datasharing.service'; @Component({ selector: 'home-chatmanager', @@ -12,22 +13,36 @@ export class ChatmanagerComponent implements OnInit { showChatlist: boolean = true showChat: boolean = false + showCreateNewChat: boolean = false parentSelectedChat: Chatinfo - constructor() { } + chatIDs: number[] + + constructor(private data: DatasharingService) { } ngOnInit() { + this.data.currentChatIDs.subscribe(chatIDs => { + this.chatIDs = chatIDs + }) } goBackToChatlist($event) { this.showChatlist = $event this.showChat = false + this.showCreateNewChat = false } showSpecialChat($event) { this.parentSelectedChat = $event this.showChatlist = false this.showChat = true + this.showCreateNewChat = false + } + + showNewChat($event) { + this.showChatlist = false + this.showChat = false + this.showCreateNewChat = $event } } diff --git a/src/app/components/feed/feed.component.ts b/src/app/components/feed/feed.component.ts index d7932a0..681817a 100644 --- a/src/app/components/feed/feed.component.ts +++ b/src/app/components/feed/feed.component.ts @@ -1,5 +1,6 @@ import { Component, OnInit } from '@angular/core'; import { Post } from 'src/app/models/post'; +import { FeedService } from 'src/app/services/feed/feed.service'; @Component({ selector: 'home-feed', @@ -11,7 +12,7 @@ export class FeedComponent implements OnInit { viewNew: boolean = true viewMostLiked: boolean = false - feedNew: Array = [ + feedNew: Array/* = [ new Post("1", "Niklas", "@nick123", "This is a test message", "01.10.2019 10:00 Uhr", 10), new Post("2", "Niklas", "@nick123", "This is a test message", "01.10.2019 10:00 Uhr", 10), new Post("3", "Niklas", "@nick123", "This is a test message", "01.10.2019 10:00 Uhr", 10), @@ -19,29 +20,33 @@ export class FeedComponent implements OnInit { new Post("5", "Niklas", "@nick123", "This is a test message", "01.10.2019 10:00 Uhr", 10), new Post("6", "Niklas", "@nick123", "This is a test message", "01.10.2019 10:00 Uhr", 10), new Post("7", "Niklas", "@nick123", "This is a test message", "01.10.2019 10:00 Uhr", 10) - ] - feedMostLiked: Array = [ + ]*/ + feedMostLiked: Array/* = [ new Post("1", "Max", "@max123", "This is a test message", "01.10.2019 10:00 Uhr", 50), new Post("2", "Max", "@max123", "This is a test message", "01.10.2019 10:00 Uhr", 50), new Post("3", "Max", "@max123", "This is a test message", "01.10.2019 10:00 Uhr", 50), new Post("4", "Max", "@max123", "This is a test message", "01.10.2019 10:00 Uhr", 50), new Post("5", "Max", "@max123", "This is a test message", "01.10.2019 10:00 Uhr", 50) - ] + ]*/ parentSelectedPostList: Array = this.feedNew - constructor() { } + constructor(private feedService: FeedService) { } ngOnInit() { + this.feedNew = this.feedService.getAllPosts() + this.feedMostLiked = this.feedService.getAllPosts() } showNew() { + this.feedNew = this.feedService.getAllPosts() this.viewNew = true this.viewMostLiked = false this.parentSelectedPostList = this.feedNew } showMostLiked() { + this.feedMostLiked = this.feedService.getAllPosts() this.viewNew = false this.viewMostLiked = true this.parentSelectedPostList = this.feedMostLiked diff --git a/src/app/components/feed/postlist/postlist.component.html b/src/app/components/feed/postlist/postlist.component.html index cf57aa2..f233e61 100644 --- a/src/app/components/feed/postlist/postlist.component.html +++ b/src/app/components/feed/postlist/postlist.component.html @@ -1,19 +1,20 @@ -
+
- {{post.username}} - {{post.handle}} + {{post.author.name}} + @{{post.author.handle}}
{{post.date}}
-

{{post.message}}

+

{{post.content}}

- - - Votes: {{post.votes}} + + + {{post.downvotes}} + {{post.upvotes}}
\ No newline at end of file diff --git a/src/app/components/feed/postlist/postlist.component.sass b/src/app/components/feed/postlist/postlist.component.sass index 7686625..9638469 100644 --- a/src/app/components/feed/postlist/postlist.component.sass +++ b/src/app/components/feed/postlist/postlist.component.sass @@ -53,6 +53,9 @@ color: $cFeedDownVote #down:hover color: darken($cFeedDownVote, 10%) - #votecounter - @include gridPosition(2, 3, 1, 3) + #downvotes + @include gridPosition(2, 3, 1, 2) + text-align: center + #upvotes + @include gridPosition(2, 3, 2, 3) text-align: center \ No newline at end of file diff --git a/src/app/components/feed/postlist/postlist.component.ts b/src/app/components/feed/postlist/postlist.component.ts index 5e64f59..d95d5dd 100644 --- a/src/app/components/feed/postlist/postlist.component.ts +++ b/src/app/components/feed/postlist/postlist.component.ts @@ -1,5 +1,6 @@ import { Component, OnInit, Input } from '@angular/core'; import { Post } from 'src/app/models/post'; +import { FeedService } from 'src/app/services/feed/feed.service'; @Component({ selector: 'feed-postlist', @@ -9,10 +10,20 @@ import { Post } from 'src/app/models/post'; export class PostlistComponent implements OnInit { @Input() childPostList: Array + selectedPost: Post - constructor() { } + constructor(private feedService: FeedService) { } ngOnInit() { } + voteUp(pPost: Post){ + this.feedService.voteUp(pPost.id) + console.log("UPVOTE", pPost.id) + } + + voteDown(pPost: Post){ + this.feedService.voteDown(pPost.id) + } + } diff --git a/src/app/models/author.ts b/src/app/models/author.ts new file mode 100644 index 0000000..f5f9965 --- /dev/null +++ b/src/app/models/author.ts @@ -0,0 +1,11 @@ +export class Author { + id: number + name: string + handle: string + + constructor(pId: number, pName: string, pHandle: string) { + this.id = pId + this.name = pName + this.handle = pHandle + } +} \ No newline at end of file diff --git a/src/app/models/chat.ts b/src/app/models/chat.ts new file mode 100644 index 0000000..5af6bdc --- /dev/null +++ b/src/app/models/chat.ts @@ -0,0 +1,16 @@ +import { Chatmessage } from "./chatmessage" + +export class Chat { + id: number + memberID: number + memberName: string + messages: Array + + + constructor(pId: number, pMemberID: number, pMemberName: string, pMessages: Array) { + this.id = pId + this.memberID = pMemberID + this.memberName = pMemberName + this.messages = pMessages + } +} \ No newline at end of file diff --git a/src/app/models/chatmessage.ts b/src/app/models/chatmessage.ts index 286a2e6..a2a4910 100644 --- a/src/app/models/chatmessage.ts +++ b/src/app/models/chatmessage.ts @@ -1,9 +1,11 @@ export class Chatmessage { - content: string; - myself: boolean; + content: string + date: string + myself: boolean - constructor(pContent: string, pMyself: boolean) { + constructor(pContent: string, pDate: string, pMyself: boolean) { this.content = pContent + this.date = pDate this.myself = pMyself } } \ No newline at end of file diff --git a/src/app/models/friendinfo.ts b/src/app/models/friendinfo.ts new file mode 100644 index 0000000..970d7c6 --- /dev/null +++ b/src/app/models/friendinfo.ts @@ -0,0 +1,9 @@ +export class FriendInfo { + id: number + name: string + + constructor(pId: number, pName: string) { + this.id = pId + this.name = pName + } +} \ No newline at end of file diff --git a/src/app/models/post.ts b/src/app/models/post.ts index 6628a63..eb7f2b3 100644 --- a/src/app/models/post.ts +++ b/src/app/models/post.ts @@ -1,17 +1,19 @@ +import { Author } from "./author" + export class Post { - id: string - username: string - handle: string - message: string + id: number + content: string date: string - votes: number + upvotes: number + downvotes: number + author: Author - constructor(pId: string, pUsername: string, pHandle: string, pMessage: string, pDate: string, pVotes: number) { + constructor(pId: number, pContent: string, pUpvotes: number, pDownvotes: number, pDate: string, pAuthor: Author) { this.id = pId - this.username = pUsername - this.handle = pHandle - this.message = pMessage + this.content = pContent + this.upvotes = pUpvotes + this.downvotes = pDownvotes this.date = pDate - this.votes = pVotes + this.author = pAuthor } } \ No newline at end of file diff --git a/src/app/services/chat/chat.service.ts b/src/app/services/chat/chat.service.ts index 6856582..55cde5d 100644 --- a/src/app/services/chat/chat.service.ts +++ b/src/app/services/chat/chat.service.ts @@ -1,59 +1,143 @@ import { Injectable } from '@angular/core'; import {Http, URLSearchParams, Headers} from '@angular/http'; -import {Apollo} from 'apollo-angular'; -import gql from 'graphql-tag'; +import { Chat } from 'src/app/models/chat'; +import { responsePathAsArray } from 'graphql'; +import { Chatmessage } from 'src/app/models/chatmessage'; +import { FriendInfo } from 'src/app/models/friendinfo'; @Injectable({ providedIn: 'root' }) export class ChatService { - name: any + arr: number[] + ownID: number + chats: Array - rates: any[]; - loading = true; - error: any; + constructor(private http: Http) { } - constructor(private apollo: Apollo) { } + public getAllChats(): Array { + let url = 'https://greenvironment.net/graphql' + + let headers = new Headers() + headers.set('Content-Type', 'application/json') + + this.http.post(url, this.getBodyForGetAllChats()) + .subscribe(response => { + this.chats = this.updateAllChats(response.json()) + }); + return this.chats + } - public getSelfName2() { - this.apollo - .watchQuery({ - query: gql` - { - getSelf { - name - } - } - `, + public createNewChat(pUserID: number) { + let url = 'https://greenvironment.net/graphql' + + let headers = new Headers() + headers.set('Content-Type', 'application/json') + + this.http.post(url, this.getBodyForNewChat(pUserID)) + } + + public requestAllChatPartners(): Array { + let url = 'https://greenvironment.net/graphql' + let chatPartners: Array + let temp + + let headers = new Headers() + headers.set('Content-Type', 'application/json') + + this.http.post(url, this.getBodyForRequestOfAllChatPartners()) + .subscribe(response => { + temp = response.json() }) - .valueChanges.subscribe(result => { - this.name = result.data && result.data.name; - this.loading = result.loading; - this.error = result.errors; - }); - - console.log(this.name) - } - - public getSelfName() { - this.apollo - .query({ - query: gql` - { - rates(currency: "USD") { - currency - rate - } + + for(let chat of temp.data.getSelf.chats) { + let memberID: number + let memberName: string + for(let member of chat.members) { + if(member.id != this.ownID) { + memberID = member.id + memberName = member.name } - `, - }) - .subscribe(result => { - this.rates = result.data && result.data.rates; - this.loading = result.loading; - this.error = result.errors; - }); + } + chatPartners.push(new FriendInfo(memberID, memberName)) + } + + return chatPartners + } + + public sendMessage(pChatID: number, pContent: string) { + let url = 'https://greenvironment.net/graphql' + + let headers = new Headers() + headers.set('Content-Type', 'application/json') + + this.http.post(url, this.getBodyForSendMessage(pChatID, pContent)) + } + + updateAllChats(pResponse: any): Array { + let chats: Array + for(let chat of pResponse.data.getSelf.chats) { + let memberID: number + let memberName: string + for(let member of chat.members) { + if(member.id != this.ownID) { + memberID = member.id + memberName = member.name + } + } + let messages: Array + for(let message of chat.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)) + } + } + chats.push(new Chat(chat.id, memberID, memberName, messages)) + } + return chats + } + + getBodyForNewChat(pUserID: number) { + this.arr = [pUserID] + const body = {query: `mutation($userID: number[]) { + createChat(members: $userID) {id} + }`, variables: { + members: this.arr + }}; + + return body + } + + getBodyForRequestOfAllChatPartners() { + const body = {query: `query { + getSelf { + chats(first: 1000, offset: 0) {members{name, id}} + }}` + } - console.log(this.rates) + return body + } + + getBodyForSendMessage(pchatID: number, pContent: string) { + const body = {query: `mutation($chatID: number, $content: string) { + sendMessage(chatId: $chatID, content: $content) {id} + }`, variables: { + chatId: pchatID, + content: pContent + }}; + + return body + } + + getBodyForGetAllChats() { + const body = {query: `query { + getSelf { + chats(first: 1000, offset: 0) {id, members{name, id}, + messages(first: 1000, offset: 0) {author {id}, createdAt, content}} + } + }` + } } } diff --git a/src/app/services/datasharing.service.ts b/src/app/services/datasharing.service.ts index f15a310..d9f3916 100644 --- a/src/app/services/datasharing.service.ts +++ b/src/app/services/datasharing.service.ts @@ -8,11 +8,17 @@ import { User } from '../models/user'; export class DatasharingService { private userInfoSource = new BehaviorSubject(new User()) - currentUserInfo = this.userInfoSource.asObservable(); + private chatIDsSource = new BehaviorSubject(new Array()) + currentUserInfo = this.userInfoSource.asObservable() + currentChatIDs = this.chatIDsSource.asObservable() constructor() { } changeUserInfo(pUserInfo: User) { this.userInfoSource.next(pUserInfo) } + + changeChatIDs(pChatIDs: number[]) { + this.chatIDsSource.next(pChatIDs) + } } diff --git a/src/app/services/feed/feed.service.spec.ts b/src/app/services/feed/feed.service.spec.ts new file mode 100644 index 0000000..a8a5cfb --- /dev/null +++ b/src/app/services/feed/feed.service.spec.ts @@ -0,0 +1,12 @@ +import { TestBed } from '@angular/core/testing'; + +import { FeedService } from './feed.service'; + +describe('FeedService', () => { + beforeEach(() => TestBed.configureTestingModule({})); + + it('should be created', () => { + const service: FeedService = TestBed.get(FeedService); + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/feed/feed.service.ts b/src/app/services/feed/feed.service.ts new file mode 100644 index 0000000..6c6181f --- /dev/null +++ b/src/app/services/feed/feed.service.ts @@ -0,0 +1,81 @@ +import { Injectable } from '@angular/core'; +import { Http } from '@angular/http'; +import { Post } from 'src/app/models/post'; +import { Author } from 'src/app/models/author'; + +@Injectable({ + providedIn: 'root' +}) +export class FeedService { + + posts: Array + + constructor(private http: Http) { } + + public voteUp(pPostID: number): void { + let url = 'https://greenvironment.net/graphql' + + let headers = new Headers() + headers.set('Content-Type', 'application/json') + + const body = {query: `mutation($postId: number) { + vote(postId: $postId, type: UPVOTE) + }`, variables: { + postId: pPostID + }} + + this.http.post(url, body) + } + + public voteDown(pPostID: number): void { + let url = 'https://greenvironment.net/graphql' + + let headers = new Headers() + headers.set('Content-Type', 'application/json') + + const body = {query: `mutation($postId: number) { + vote(postId: $postId, type: DOWNVOTE) + }`, variables: { + postId: pPostID + }} + + this.http.post(url, body) + } + + public getAllPosts(): Array { + let url = 'https://greenvironment.net/graphql' + + let headers = new Headers() + headers.set('Content-Type', 'application/json') + + this.http.post(url, this.getBodyForGetAllChats()) + .subscribe(response => { + this.posts = this.renderAllPosts(response.json()) + }); + return this.posts + } + + getBodyForGetAllChats() { + const body = {query: `query { + getPosts (first: 1000, offset: 0) {id, content, upvotes, downvotes, author{name, handle, id}, createdAt} + }` + } + + return body + } + + renderAllPosts(pResponse: any): Array { + let posts = new Array() + for(let post of pResponse.data.getPosts) { + let id: number = post.id + let content: string = post.content + let upvotes: number = post.upvotes + let downvotes: number = post.downvotes + let author = new Author(post.author.id, post.author.name, post.author.handle) + let date = post.createdAt + + posts.push(new Post(id, content, upvotes, downvotes, date, author)) + } + return posts + } +}