EiEich

Untitled

Jun 16th, 2021
680
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { HttpClient } from '@angular/common/http';
  2. import { Injectable } from '@angular/core';
  3. import { MongoService } from 'wacom';
  4. import { Router } from '@angular/router'
  5. export interface User {
  6.     email: string;
  7.     name: string;
  8.     _id: string;
  9.     data: object;
  10. }
  11.  
  12. @Injectable({
  13.     providedIn: 'root'
  14. })
  15.  
  16. export class UserService implements User {
  17.     public users: User[];
  18.     public name;
  19.     public email;
  20.     public avatarUrl;
  21.     public _users: any = {};
  22.     public _id;
  23.     public is: any = {};
  24.     public data: any = {
  25.         resetPin: {},
  26.         resetCreate: Date,
  27.         resetCounter: Number,
  28.         phone: Number,
  29.         balance: {},
  30.     };
  31.     set_user(user){
  32.         if(typeof user.data != 'object') user.data = {};
  33.         if(typeof user.is != 'object') user.is = {};
  34.         if(typeof user.data.balance != 'object') user.data.balance = {};
  35.         if(typeof user.data.balance != 'object') user.data.balance = {};
  36.         if(!Array.isArray(user.data.invites)) user.data.invites = []
  37.         if(!Array.isArray(user.data.declinedInvites)) user.data.declinedInvites = []
  38.         if(!Array.isArray(user.data.incomingInvites)) user.data.incomingInvites = []
  39.         if(!Array.isArray(user.data.deleted)) user.data.deleted = []
  40.         if(!Array.isArray(user.data.fr)) user.data.fr = []
  41.         if(!Array.isArray(user.data.friends)) user.data.friends = []
  42.         if(!Array.isArray(user.data.offlineFriends)) user.data.offlineFriends = []
  43.         for (var key in user) {
  44.             this[key] = user[key];
  45.         }
  46.     }
  47.     constructor(private mongo: MongoService, private http: HttpClient, private router: Router) {
  48.         if(localStorage.getItem('memo_user')){
  49.             this.set_user(JSON.parse(localStorage.getItem('memo_user')));
  50.         }
  51.         this.http.get('/api/user/me').subscribe((resp:any) => {
  52.             this.set_user(resp);
  53.         });
  54.         this.users = mongo.get('user', {
  55.             replace: {
  56.                 avatarUrl: (val, cb)=>{
  57.                     cb(val||'/assets/default.png');
  58.                 },
  59.                 data: (field, cb, doc) => {
  60.                     if(typeof field != 'object') field = {};
  61.                     if(typeof field.request!='object')  field.request = {};
  62.                 },
  63.                 is: (field, cb) => {
  64.                     cb(field || {})
  65.                 }
  66.             }
  67.         }, (arr, obj) => {
  68.             this._users = obj;
  69.         });
  70.     }
  71.     update(){
  72.         this.mongo.afterWhile(this, ()=>{
  73.             this.mongo.update('user', this, {
  74.                 fields: 'name data avatarUrl'
  75.             });
  76.         });
  77.     }
  78.     change_password(oldPass, newPass){
  79.         this.http.post('/api/user/changePassword', {
  80.             newPass: newPass,
  81.             oldPass: oldPass
  82.         }).subscribe(resp => {
  83.             if(resp) alert('successfully changed password');
  84.             else alert('failed to change password');
  85.         });
  86.     }
  87.     todataUrl(fl, cb) {
  88.         var a = new FileReader();
  89.         a.onload = (e)=>{
  90.             var target: any = e.target;
  91.             cb(target.result);
  92.         }
  93.         a.readAsDataURL(fl);
  94.     }
  95.     changeAvatar(e){
  96.         this.todataUrl(e.target.files[0], (dataUrl)=>{
  97.             this.http.post('/api/user/avatar', {
  98.                 dataUrl: dataUrl
  99.             }).subscribe((resp:any)=> {
  100.                 this.avatarUrl = resp;
  101.             });
  102.         });
  103.     }
  104.     create(user) {
  105.         this.mongo.create('user', user, created=>{});
  106.     }
  107.     logout(){
  108.         localStorage.removeItem('memo_user')
  109.         this.http.get('/api/user/logout').subscribe((resp:any)=> {});
  110.         this.router.navigate(['/']);
  111.     }
  112. }
RAW Paste Data