2021-10-15 19:14:49 +00:00
|
|
|
import {Injectable} from '@angular/core';
|
2021-10-21 15:23:58 +00:00
|
|
|
import {Subject, BehaviorSubject, Observable} from 'rxjs';
|
2021-10-15 19:14:49 +00:00
|
|
|
import {HttpClient, HttpParams} from '@angular/common/http';
|
|
|
|
import {User} from './user.model';
|
2021-10-20 20:51:14 +00:00
|
|
|
import {Owner} from './owner.model';
|
2021-10-15 19:14:49 +00:00
|
|
|
|
|
|
|
@Injectable({providedIn: 'root'})
|
|
|
|
|
|
|
|
export class UserService{
|
2021-10-21 16:22:48 +00:00
|
|
|
private dataStore: { user: User, owner: Owner} = {
|
|
|
|
user: {
|
|
|
|
address: '',
|
|
|
|
session: '',
|
|
|
|
blocktime: 0
|
|
|
|
},
|
|
|
|
owner: {
|
|
|
|
address: '',
|
|
|
|
name: ''
|
|
|
|
}
|
|
|
|
};
|
2021-10-15 19:14:49 +00:00
|
|
|
private uZaddr = '';
|
2021-10-20 20:51:14 +00:00
|
|
|
private oZaddr = '';
|
|
|
|
private uName = '';
|
|
|
|
private session: string | null = '';
|
2021-10-21 15:23:58 +00:00
|
|
|
private _uZaddrUpdated: BehaviorSubject<string> = new BehaviorSubject(this.uZaddr);
|
2021-10-20 20:51:14 +00:00
|
|
|
private uNameUpdated = new Subject<string>();
|
2021-10-21 16:22:48 +00:00
|
|
|
private _ownerUpdated: BehaviorSubject<Owner> = new BehaviorSubject(this.dataStore.owner);
|
2021-10-21 15:23:58 +00:00
|
|
|
public readonly uZaddrUpdate: Observable<string> = this._uZaddrUpdated.asObservable();
|
2021-10-21 16:22:48 +00:00
|
|
|
public readonly ownerUpdate: Observable<Owner> = this._ownerUpdated.asObservable();
|
2021-10-15 19:14:49 +00:00
|
|
|
|
|
|
|
constructor(private http: HttpClient){
|
2021-10-20 20:51:14 +00:00
|
|
|
this.session = localStorage.getItem('s4z_token');
|
2021-10-21 15:23:58 +00:00
|
|
|
if (this.session != null) {
|
|
|
|
this.findUser(this.session);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
findUser(session: string) {
|
|
|
|
const params = new HttpParams().append('session', session);
|
|
|
|
let obs = this.http.get<{message: string, user: any}>('http://localhost:3000/api/getuser', { headers:{}, params: params, observe: 'response'});
|
|
|
|
|
|
|
|
obs.subscribe((UserDataResponse) => {
|
|
|
|
console.log(UserDataResponse.status);
|
|
|
|
if (UserDataResponse.status == 200){
|
|
|
|
this.dataStore.user = UserDataResponse.body!.user[0];
|
|
|
|
console.log(`US: Found user, returning it`);
|
|
|
|
this._uZaddrUpdated.next(Object.assign({},this.dataStore).user.address);
|
2021-10-21 19:29:04 +00:00
|
|
|
this.getOwner(Object.assign({},this.dataStore.user).address);
|
2021-10-21 15:23:58 +00:00
|
|
|
} else {
|
|
|
|
console.log('US: Did not find user');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return obs;
|
2021-10-15 19:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addUser(address: string, session: string, blocktime: number) {
|
|
|
|
const user: User={_id: '', address: address, session: session, blocktime: blocktime};
|
2021-10-21 16:22:48 +00:00
|
|
|
let obs = this.http.post<{message: string}>('http://localhost:3000/api/users', user);
|
|
|
|
|
|
|
|
obs.subscribe((responseData) => {
|
2021-10-20 20:51:14 +00:00
|
|
|
console.log(responseData.message);
|
2021-10-21 16:22:48 +00:00
|
|
|
this.getOwner(address).subscribe((response) => {
|
|
|
|
console.log('addUser, checking owner', response);
|
|
|
|
});
|
2021-10-20 20:51:14 +00:00
|
|
|
});
|
2021-10-21 16:22:48 +00:00
|
|
|
|
|
|
|
return obs;
|
2021-10-20 20:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addOwner(address: string) {
|
|
|
|
const owner: Owner={_id: '', address: address, name: 'Zgo-'.concat(address.substring(0,5))};
|
2021-10-21 16:22:48 +00:00
|
|
|
let obs = this.http.post<{message: string}>('http://localhost:3000/api/addowner', {address: owner.address, name: owner.name});
|
|
|
|
|
|
|
|
obs.subscribe((responseData) => {
|
2021-10-20 20:51:14 +00:00
|
|
|
console.log(responseData.message);
|
|
|
|
});
|
2021-10-21 16:22:48 +00:00
|
|
|
|
|
|
|
return obs;
|
2021-10-20 20:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addOwnerName(address: string, name: string) {
|
|
|
|
this.http.post<{message: string}>('http://localhost:3000/api/addownername', {address, name}).
|
2021-10-15 19:14:49 +00:00
|
|
|
subscribe((responseData) => {
|
|
|
|
console.log(responseData.message);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-20 20:51:14 +00:00
|
|
|
getOwner(address: string) {
|
2021-10-21 19:29:04 +00:00
|
|
|
console.log('getOwner', address);
|
2021-10-20 20:51:14 +00:00
|
|
|
const ownParams = new HttpParams().append('address', address);
|
2021-10-21 16:22:48 +00:00
|
|
|
let obs = this.http.get<{message:string, owner: any}>('http://localhost:3000/api/getowner', {params: ownParams, observe: 'response'});
|
|
|
|
|
|
|
|
obs.subscribe((OwnerDataResponse) => {
|
2021-10-21 19:29:04 +00:00
|
|
|
console.log('api/getowner', OwnerDataResponse.status);
|
2021-10-20 20:51:14 +00:00
|
|
|
if (OwnerDataResponse.status == 200) {
|
2021-10-21 16:22:48 +00:00
|
|
|
this.dataStore.owner = OwnerDataResponse.body!.owner[0];
|
2021-10-21 19:29:04 +00:00
|
|
|
//console.log('getOwner object', this.dataStore.owner);
|
|
|
|
this._ownerUpdated.next(Object.assign({},this.dataStore).owner);
|
2021-10-20 20:51:14 +00:00
|
|
|
} else {
|
2021-10-21 19:29:04 +00:00
|
|
|
console.log("No owner found, adding");
|
2021-10-21 16:22:48 +00:00
|
|
|
this.addOwner(address);
|
2021-10-20 20:51:14 +00:00
|
|
|
}
|
|
|
|
});
|
2021-10-21 16:22:48 +00:00
|
|
|
|
|
|
|
return obs;
|
2021-10-20 20:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hasOwner() {
|
|
|
|
return this.uZaddr === this.oZaddr;
|
|
|
|
}
|
|
|
|
|
2021-10-21 16:22:48 +00:00
|
|
|
//getUser(session: string) {
|
|
|
|
//const params = new HttpParams().append('session', session);
|
|
|
|
//console.log(`US: Searching for session ${session} `);
|
|
|
|
//this.http.get<{message: string, user: any}>('http://localhost:3000/api/getuser', { headers:{}, params: params, observe: 'response'}).
|
|
|
|
//subscribe((UserDataResponse) => {
|
|
|
|
//console.log(UserDataResponse.status);
|
|
|
|
//if (UserDataResponse.status == 200){
|
|
|
|
//this.uZaddr = UserDataResponse.body!.user[0].address;
|
|
|
|
//console.log(`US: Found user, returning zaddr ${this.uZaddr}`);
|
|
|
|
//this._uZaddrUpdated.next(this.uZaddr);
|
|
|
|
//} else {
|
|
|
|
//console.log('US: Did not find user');
|
|
|
|
//this._uZaddrUpdated.next('');
|
|
|
|
//}
|
|
|
|
//});
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
//getName() {
|
|
|
|
//if (this.session != null) {
|
|
|
|
//this.getUser(this.session);
|
|
|
|
//this._uZaddrUpdated.subscribe((addr: string) => {
|
|
|
|
//console.log(` US: ${addr}`);
|
|
|
|
//this.getOwner(addr);
|
|
|
|
//});
|
|
|
|
//}
|
|
|
|
//}
|
2021-10-20 20:51:14 +00:00
|
|
|
|
|
|
|
getNameUpdateListener() {
|
|
|
|
return this.uNameUpdated;
|
|
|
|
}
|
|
|
|
|
2021-10-21 15:23:58 +00:00
|
|
|
//getZaddrUpdateListener() {
|
|
|
|
//return this.uZaddrUpdated;
|
|
|
|
//}
|
2021-10-15 19:14:49 +00:00
|
|
|
|
|
|
|
}
|