All files / app/component/debug debug.component.ts

41.66% Statements 25/60
34.48% Branches 10/29
10% Functions 2/20
40% Lines 20/50

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129  8x                           8x 8x                 8x   1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                                                                        
import { HttpErrorResponse } from '@angular/common/http';
import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { catchError, concat, concatMap, generate, last, Observable, of } from 'rxjs';
import { tap } from 'rxjs/operators';
import { v4 as uuid } from 'uuid';
import { AdminService } from '../../service/admin.service';
import { RefService } from '../../service/api/ref.service';
import { TaggingService } from '../../service/api/tagging.service';
import { ExtStore } from '../../store/ext';
import { PluginStore } from '../../store/plugin';
import { QueryStore } from '../../store/query';
import { Store } from '../../store/store';
import { TemplateStore } from '../../store/template';
import { UserStore } from '../../store/user';
import { printError } from '../../util/http';
import { LoadingComponent } from '../loading/loading.component';
 
@Component({
  selector: 'app-debug',
  templateUrl: './debug.component.html',
  styleUrls: ['./debug.component.scss'],
  host: { 'class': 'debug actions' },
  imports: [LoadingComponent]
})
export class DebugComponentI {
 
  generating = false;
  settingUser = false;
  sourcing = false;
  batchRunning = false;
  serverError: string[] = [];
  debug = this.admin.getPlugin('plugin/debug') || this.admin.getTemplate('debug');
 
  constructor(
    public admin: AdminService,
    private store: Store,
    public query: QueryStore,
    public ext: ExtStore,
    public user: UserStore,
    public plugin: PluginStore,
    public template: TemplateStore,
    private refs: RefService,
    private ts: TaggingService,
    private router: Router,
  ) { }
 
  get empty() {
    return !this.query.page?.content?.length;
  }
 
  batch(fn: (e: any) => Observable<any>) {
    if (this.batchRunning) return;
    this.batchRunning = true;
    concat(...this.query.page!.content.map(e => fn(e).pipe(
      catchError((err: HttpErrorResponse) => {
        this.serverError.push(...printError(err));
        return of(null);
      }),
    ))).pipe(last()).subscribe(() => {
      this.query.refresh();
      this.batchRunning = false;
    });
  }
 
  repeat(fn: (i: number) => Observable<any>, n = 100) {
    if (this.batchRunning) return;
    this.batchRunning = true;
    generate(0, x => x < n, x => x + 1).pipe(
      concatMap(i => fn(i)),
      catchError((err: HttpErrorResponse) => {
        this.serverError.push(...printError(err));
        return of(null);
      }),
    ).subscribe(() => {
      this.batchRunning = false;
    });
  }
 
  setUser(tag: string) {
    this.router.navigate([], { queryParamsHandling: 'merge', queryParams: { debug: 'USER', tag }})
      .then(() => location.reload());
  }
 
  gen(n: any = 100) {
    this.generating = false;
    this.repeat(i => {
      const url = 'comment:' + uuid();
      return this.refs.create({
        url,
        origin: this.store.account.origin,
        title: 'Generated: ' + i,
        comment: uuid(),
        tags: ['public', 'gen'],
      }).pipe(
        tap(() => {
          if (this.admin.getPlugin('plugin/user/vote/up')) {
            this.ts.createResponse('plugin/user/vote/up', url).subscribe();
          }
        }),
      );
    }, n);
  }
 
  source(url: string) {
    this.sourcing = false;
    this.batch(ref => {
      if (!ref.sources?.includes(url)) {
        if (ref.sources) {
          return this.refs.patch(ref.url, ref.origin!, ref.modifiedString, [{
            op: 'add',
            path: '/sources/-',
            value: url,
          }]);
        } else {
          return this.refs.patch(ref.url, ref.origin!, ref.modifiedString, [{
            op: 'add',
            path: '/sources',
            value: [url],
          }]);
        }
      } else {
        return of(null);
      }
    });
  }
 
}