Refactor profile and self service

master
trivernis 5 years ago
parent 0be475e79a
commit f9ef6c2669

@ -15,7 +15,7 @@ export class AppComponent implements OnInit {
ngOnInit() {
this.data.currentUserInfo.subscribe(user => {
if (user.loggedIn !== true) {
this.selfservice.checkIfLoggedIn();
this.selfservice.checkIfLoggedIn().subscribe();
}
});
}

@ -58,9 +58,7 @@ export class ProfileComponent implements OnInit {
this.userProfile = response;
// tslint:disable-next-line:max-line-length
this.userProfile.allowedToSendRequest = this.requestService.isAllowedToSendRequest(this.userProfile.userID, this.self);
if (this.userProfile.userID === this.self.userID) {
this.ownProfile = true;
} else {this.ownProfile = false; }
this.ownProfile = this.userProfile.userID === this.self.userID;
this.rankname = this.levellist.getLevelName(this.userProfile.level);
} else { this.profileNotFound = true; }
this.loading = false;

@ -46,19 +46,29 @@ export class User {
} catch (err) {
console.error(err);
}
this.friends = userDataResponse.friends
.map(friend => new FriendInfo(
friend.id, friend.name,
friend.level,
this.buildProfilePictureUrl(friend.profilePicture)
));
this.groups = userDataResponse.groups
.map(group => new GroupInfo(group.id, group.name));
this.chatIDs = userDataResponse.chats.map(chat => chat.id);
this.sentRequestUserIDs = userDataResponse.sentRequests
.map(request => request.receiver.id);
this.receivedRequests = userDataResponse.receivedRequests
.map(request => new FriendRequest(request.id, request.sender.id, request.sender.handle, request.sender.name));
if (userDataResponse.friends) {
this.friends = userDataResponse.friends
.map(friend => new FriendInfo(
friend.id, friend.name,
friend.level,
this.buildProfilePictureUrl(friend.profilePicture)
));
}
if (userDataResponse.groups) {
this.groups = userDataResponse.groups
.map(group => new GroupInfo(group.id, group.name));
}
if (userDataResponse.chats) {
this.chatIDs = userDataResponse.chats.map(chat => chat.id);
}
if (userDataResponse.sentRequests) {
this.sentRequestUserIDs = userDataResponse.sentRequests
.map(request => request.receiver.id);
}
if (userDataResponse.receivedRequests) {
this.receivedRequests = userDataResponse.receivedRequests
.map(request => new FriendRequest(request.id, request.sender.id, request.sender.handle, request.sender.name));
}
}
buildProfilePictureUrl(path: string): string {

@ -1,92 +1,97 @@
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Post } from 'src/app/models/post';
import { Author } from 'src/app/models/author';
import { environment } from 'src/environments/environment';
import { User } from 'src/app/models/user';
import { Observable, Subject } from 'rxjs';
import { Activity } from 'src/app/models/activity';
import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Post} from 'src/app/models/post';
import {Author} from 'src/app/models/author';
import {environment} from 'src/environments/environment';
import {User} from 'src/app/models/user';
import {Subject} from 'rxjs';
import {Activity} from 'src/app/models/activity';
import {BaseService} from '../base.service';
const graphqlGetProfileQuery = `query($userId: ID) {
getUser(userId:$userId){
id
handle
name
profilePicture
points
level
friendCount
groupCount
joinedAt
friends{
id
}
posts{
id,
content,
htmlContent,
upvotes,
downvotes,
userVote,
deletable,
activity{
id
name
description
points
},
author{
name,
handle,
profilePicture
id},
createdAt
}
}
}`;
@Injectable({
providedIn: 'root'
})
export class ProfileService {
export class ProfileService extends BaseService {
constructor(private http: HttpClient) {
super();
}
public proflile: Subject<any> = new Subject();
constructor(private http: Http) { }
/**
* Builds the request body of a getProfile request
* @param id
*/
private static buildGetProfileBody(id: string): any {
return {
query: graphqlGetProfileQuery,
variables: {
userId: id,
}
};
}
/**
* Returns the data for the specified user.
* @param userId
*/
public getUserData(userId: string) {
const headers = new Headers();
headers.set('Content-Type', 'application/json');
// return this.renderProfile(this.http.post(environment.graphQLUrl, this.buildGetProfileJson(userId)));
this.http.post(environment.graphQLUrl, this.buildGetProfileJson(userId)).subscribe(result => {
// push onto subject
this.proflile.next(this.renderProfile(result.json()));
this.http.post(environment.graphQLUrl, ProfileService.buildGetProfileBody(userId)).subscribe(result => {
this.proflile.next(this.getProfileData(result));
return this.proflile;
});
}
public buildGetProfileJson(id: string): any {
const body = {query: `query($userId: ID) {
getUser(userId:$userId){
id
handle
name
profilePicture
points
level
friendCount
groupCount
joinedAt
friends{
id
}
posts{
id,
content,
htmlContent,
upvotes,
downvotes,
userVote,
deletable,
activity{
id
name
description
points
},
author{
name,
handle,
profilePicture
id},
createdAt
}
}
}`, variables: {
userId: id,
}};
return body;
}
public renderProfile(response: any): User {
/**
* Returns a userinstance filled with profile data
* @param response
*/
public getProfileData(response: any): User {
const posts = new Array<Post>();
const profile = new User();
if (response.data.getUser != null) {
profile.userID = response.data.getUser.id;
profile.username = response.data.getUser.name;
profile.handle = response.data.getUser.handle;
profile.points = response.data.getUser.points;
profile.level = response.data.getUser.level;
profile.friendCount = response.data.getUser.friendCount;
profile.groupCount = response.data.getUser.groupCount;
if (response.data.getUser.profilePicture) {
profile.profilePicture = environment.greenvironmentUrl + response.data.getUser.profilePicture;
} else {
profile.profilePicture = 'assets/images/default-profilepic.svg';
}
if (response.data.getUser) {
profile.assignFromResponse(response.data.getUser);
const temp = new Date(Number(response.data.getUser.joinedAt));
const date = temp.toLocaleString('en-GB');
profile.joinedAt = date;

@ -1,41 +1,87 @@
import { Injectable, EventEmitter, Output } from '@angular/core';
import {Http, URLSearchParams, Headers} from '@angular/http';
import { User } from 'src/app/models/user';
import { DatasharingService } from '../datasharing.service';
import {Injectable} from '@angular/core';
import {User} from 'src/app/models/user';
import {DatasharingService} from '../datasharing.service';
import {Router} from '@angular/router';
import { environment } from 'src/environments/environment';
import { FriendRequest } from 'src/app/models/friendRequest';
import { FriendInfo } from 'src/app/models/friendinfo';
import { GroupInfo } from 'src/app/models/groupinfo';
import {environment} from 'src/environments/environment';
import {FriendRequest} from 'src/app/models/friendRequest';
import {FriendInfo} from 'src/app/models/friendinfo';
import {HttpClient} from '@angular/common/http';
import {tap} from 'rxjs/operators';
import {BaseService} from '../base.service';
const getSelfGraphqlQuery = `{
getSelf{
id,
name,
email,
handle,
points,
level,
profilePicture,
receivedRequests{id, sender{name, handle, id}},
sentRequests{receiver{id}},
friends {
id,
name,
level,
profilePicture,
},
groups {
id,
name
},
chats{
id
},
settings
}
}`;
@Injectable({
providedIn: 'root'
})
export class SelfService {
export class SelfService extends BaseService {
constructor(private http: HttpClient, private data: DatasharingService) {
super();
}
constructor(private http: Http, private data: DatasharingService, private router: Router) { }
/**
* Builds the getself request body
*/
private static buildGetSelfBody(): any {
return {
query: getSelfGraphqlQuery, variables: {}
};
}
/**
* Checks if the user is still logged in
*/
public checkIfLoggedIn() {
const url = environment.graphQLUrl;
const headers = new Headers();
headers.set('Content-Type', 'application/json');
return this.http.post(url, this.buildJson())
.subscribe(response => {
return this.http.post(url, SelfService.buildGetSelfBody(), {headers: this.headers})
.pipe(tap(response => {
this.stillLoggedIn();
this.updateUserInfo(response.json());
this.updateUserInfo(response);
}, error => {
this.notLoggedIn();
// this.fakeLogin();
}
);
}));
}
public stillLoggedIn() {
}
public notLoggedIn() {
}
/**
* Updates the info on a user
* @param response
*/
public updateUserInfo(response: any) {
const user = new User();
user.assignFromResponse(response.data.getSelf);
@ -63,36 +109,4 @@ export class SelfService {
this.data.changeUserInfo(user);
}
public buildJson(): any {
const body = {query: `{
getSelf{
id,
name,
email,
handle,
points,
level,
profilePicture,
receivedRequests{id, sender{name, handle, id}},
sentRequests{receiver{id}},
friends {
id,
name,
level,
profilePicture,
},
groups {
id,
name
},
chats{
id
},
settings
}
}`, variables: {
}};
return body;
}
}

Loading…
Cancel
Save