parent
dcf3808d49
commit
7e12f42ce0
@ -0,0 +1,12 @@
|
|||||||
|
import {IUser} from './IUser';
|
||||||
|
|
||||||
|
export interface IChat {
|
||||||
|
|
||||||
|
id: number;
|
||||||
|
|
||||||
|
namespace: string;
|
||||||
|
|
||||||
|
members: IUser[];
|
||||||
|
|
||||||
|
messages: any[];
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
import {IGraphqlError} from './IGraphqlError';
|
||||||
|
|
||||||
|
export interface IErrorResponse {
|
||||||
|
error: {
|
||||||
|
errors: IGraphqlError[];
|
||||||
|
data: any;
|
||||||
|
};
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
export interface IGraphqlError {
|
||||||
|
|
||||||
|
message: string;
|
||||||
|
|
||||||
|
path: string[];
|
||||||
|
|
||||||
|
location: {line: number; column: number}[];
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
import {IUser} from './IUser';
|
||||||
|
|
||||||
|
export enum RequestType {
|
||||||
|
FRIENDREQUEST = 'FRIENDREQUEST',
|
||||||
|
GROUPINVITE = 'GROUPINVITE',
|
||||||
|
EVENTINVITE = 'EVENTINVITE',
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IRequest {
|
||||||
|
|
||||||
|
id: number;
|
||||||
|
|
||||||
|
sender: IUser;
|
||||||
|
|
||||||
|
receiver: IUser;
|
||||||
|
|
||||||
|
type: RequestType;
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
export interface ISettings {
|
||||||
|
darkmode?: boolean;
|
||||||
|
}
|
@ -1,105 +1,83 @@
|
|||||||
import {Injectable} from '@angular/core';
|
import {Injectable} from '@angular/core';
|
||||||
import {Headers, Http, Request} from '@angular/http';
|
import {HttpClient} from '@angular/common/http';
|
||||||
import {Login} from '../../models/login';
|
import {Login} from '../../models/login';
|
||||||
import {User} from 'src/app/models/user';
|
import {User} from 'src/app/models/user';
|
||||||
import {DatasharingService} from '../datasharing.service';
|
import {DatasharingService} from '../datasharing.service';
|
||||||
import {Router} from '@angular/router';
|
import {Router} from '@angular/router';
|
||||||
import {environment} from 'src/environments/environment';
|
import {environment} from 'src/environments/environment';
|
||||||
import { FriendRequest } from 'src/app/models/friendRequest';
|
import {FriendRequest} from 'src/app/models/friendRequest';
|
||||||
import { FriendInfo } from 'src/app/models/friendinfo';
|
import {FriendInfo} from 'src/app/models/friendinfo';
|
||||||
import { GroupInfo } from 'src/app/models/groupinfo';
|
import {GroupInfo} from 'src/app/models/groupinfo';
|
||||||
|
import {IUser} from '../../models/interfaces/IUser';
|
||||||
|
import {BaseService} from '../base.service';
|
||||||
|
import {tap} from 'rxjs/operators';
|
||||||
|
|
||||||
@Injectable({
|
interface ILoginRequestResult {
|
||||||
providedIn: 'root'
|
data: {
|
||||||
})
|
login: IUser;
|
||||||
export class LoginService {
|
};
|
||||||
|
}
|
||||||
constructor(private http: Http, private data: DatasharingService, private router: Router) {
|
|
||||||
}
|
|
||||||
|
|
||||||
public login(login: Login, errorCb: any) {
|
|
||||||
|
|
||||||
const headers = new Headers();
|
|
||||||
headers.set('Content-Type', 'application/json');
|
|
||||||
|
|
||||||
return this.http.post(environment.graphQLUrl, this.buildJson(login))
|
const graphqlQuery = `mutation($email: String!, $pwHash: String!) {
|
||||||
.subscribe(response => {
|
login(email: $email, passwordHash: $pwHash) {
|
||||||
this.loginSuccess();
|
id,
|
||||||
this.updateUserInfo(response.json());
|
name,
|
||||||
}, errorCb
|
email,
|
||||||
);
|
handle,
|
||||||
|
points,
|
||||||
|
level,
|
||||||
|
receivedRequests{id, sender{name, handle, id}},
|
||||||
|
sentRequests{receiver{id}},
|
||||||
|
friends {
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
level
|
||||||
|
},
|
||||||
|
groups {
|
||||||
|
id,
|
||||||
|
name
|
||||||
|
},
|
||||||
|
chats{
|
||||||
|
id
|
||||||
|
},
|
||||||
|
settings
|
||||||
}
|
}
|
||||||
|
}`;
|
||||||
|
|
||||||
public loginSuccess() {
|
@Injectable({
|
||||||
this.router.navigateByUrl('');
|
providedIn: 'root'
|
||||||
}
|
})
|
||||||
|
export class LoginService extends BaseService {
|
||||||
|
|
||||||
public updateUserInfo(response: any) {
|
constructor(private http: HttpClient, private datasharingService: DatasharingService, private router: Router) {
|
||||||
const user: User = new User();
|
super();
|
||||||
let friendRequest: FriendRequest = new FriendRequest();
|
|
||||||
user.loggedIn = true;
|
|
||||||
user.userID = response.data.login.id;
|
|
||||||
user.username = response.data.login.name;
|
|
||||||
user.handle = response.data.login.handle;
|
|
||||||
user.email = response.data.login.email;
|
|
||||||
user.points = response.data.login.points;
|
|
||||||
user.level = response.data.login.level;
|
|
||||||
for (const friend of response.data.login.friends) {
|
|
||||||
user.friends.push(new FriendInfo(friend.id, friend.name, friend.level));
|
|
||||||
}
|
|
||||||
for (const group of response.data.login.groups) {
|
|
||||||
user.groups.push(new GroupInfo(group.id, group.name));
|
|
||||||
}
|
|
||||||
user.chatIDs = response.data.login.chats;
|
|
||||||
for (const request of response.data.login.sentRequests) {
|
|
||||||
user.sentRequestUserIDs.push(request.receiver.id);
|
|
||||||
}
|
|
||||||
for (const request of response.data.login.receivedRequests) {
|
|
||||||
friendRequest = new FriendRequest();
|
|
||||||
friendRequest.id = request.id;
|
|
||||||
friendRequest.senderUserID = request.sender.id;
|
|
||||||
friendRequest.senderUsername = request.sender.name;
|
|
||||||
friendRequest.senderHandle = request.sender.handle;
|
|
||||||
user.receivedRequests.push(friendRequest);
|
|
||||||
}
|
|
||||||
if (JSON.parse(response.data.login.settings).darkmode === 'true') {
|
|
||||||
user.darkmode = true;
|
|
||||||
}
|
|
||||||
this.data.changeUserInfo(user);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public buildJson(login: Login): any {
|
/**
|
||||||
const body = {
|
* Builds the body for the login request
|
||||||
query: `mutation($email: String!, $pwHash: String!) {
|
* @param login
|
||||||
login(email: $email, passwordHash: $pwHash) {
|
*/
|
||||||
id,
|
private static buildRequestBody(login: Login): any {
|
||||||
name,
|
return {
|
||||||
email,
|
query: graphqlQuery,
|
||||||
handle,
|
variables: {
|
||||||
points,
|
|
||||||
level,
|
|
||||||
receivedRequests{id, sender{name, handle, id}},
|
|
||||||
sentRequests{receiver{id}},
|
|
||||||
friends {
|
|
||||||
id,
|
|
||||||
name,
|
|
||||||
level
|
|
||||||
},
|
|
||||||
groups {
|
|
||||||
id,
|
|
||||||
name
|
|
||||||
},
|
|
||||||
chats{
|
|
||||||
id
|
|
||||||
},
|
|
||||||
settings
|
|
||||||
}
|
|
||||||
}`
|
|
||||||
, variables: {
|
|
||||||
email: login.email,
|
email: login.email,
|
||||||
pwHash: login.passwordHash,
|
pwHash: login.passwordHash,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
return body;
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs a login request and returns the data of the logged in user.
|
||||||
|
* @param login
|
||||||
|
*/
|
||||||
|
public login(login: Login) {
|
||||||
|
const body = LoginService.buildRequestBody(login);
|
||||||
|
return this.http.post<ILoginRequestResult>(environment.graphQLUrl, body)
|
||||||
|
.pipe(tap(response => {
|
||||||
|
const user = new User();
|
||||||
|
user.assignFromResponse(response.data.login);
|
||||||
|
this.datasharingService.changeUserInfo(user);
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue