All files / app/page/ref ref.component.ts

48.41% Statements 61/126
19.44% Branches 28/144
25% Functions 11/44
48.57% Lines 51/105

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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235                1x 1x 1x 1x     1x             1x 1x 1x                                 3x 1x 1x         1x 1x   1x   1x     1x 1x 1x 1x 1x 1x 1x               1x 1x 1x 1x 1x               1x 1x 1x 1x 1x       1x             1x           1x           1x         1x 1x         1x         1x 1x         1x 1x         1x 1x       1x 1x 1x       1x 1x                                                                                                                                                           1x                
import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { Router, RouterLink, RouterLinkActive, RouterOutlet } from '@angular/router';
import { pickBy, uniq } from 'lodash-es';
import { DateTime } from 'luxon';
import { autorun, IReactionDisposer, runInAction } from 'mobx';
import { MobxAngularModule } from 'mobx-angular';
import { catchError, filter, map, of, Subject, Subscription, switchMap, takeUntil, throwError } from 'rxjs';
import { tap } from 'rxjs/operators';
import { LoadingComponent } from '../../component/loading/loading.component';
import { RefComponent } from '../../component/ref/ref.component';
import { SidebarComponent } from '../../component/sidebar/sidebar.component';
import { TabsComponent } from '../../component/tabs/tabs.component';
import { HasChanges } from '../../guard/pending-changes.guard';
import { Ref } from '../../model/ref';
import { isWiki } from '../../mods/org/wiki';
import { AdminService } from '../../service/admin.service';
import { RefService } from '../../service/api/ref.service';
import { StompService } from '../../service/api/stomp.service';
import { TaggingService } from '../../service/api/tagging.service';
import { ConfigService } from '../../service/config.service';
import { Store } from '../../store/store';
import { memo, MemoCache } from '../../util/memo';
import { markRead } from '../../util/response';
import { hasTag, privateTag, top } from '../../util/tag';
 
@Component({
  selector: 'app-ref-page',
  templateUrl: './ref.component.html',
  styleUrls: ['./ref.component.scss'],
  imports: [
    RefComponent,
    MobxAngularModule,
    TabsComponent,
    RouterLink,
    RouterLinkActive,
    SidebarComponent,
    RouterOutlet,
    LoadingComponent,
  ],
})
export class RefPage implements OnInit, OnDestroy, HasChanges {
  private disposers: IReactionDisposer[] = [];
  private destroy$ = new Subject<void>();
 
  @ViewChild('ref')
  ref?: RefComponent;
 
  newResponses = 0;
  private url = '';
  private watchSelf?: Subscription;
  private watchUrl = '';
  private watchResponses?: Subscription;
  private seen = new Set<string>();
 
  constructor(
    public config: ConfigService,
    public admin: AdminService,
    public store: Store,
    private refs: RefService,
    private ts: TaggingService,
    private router: Router,
    private stomp: StompService,
  ) { }
 
  saveChanges() {
    return !this.ref || this.ref.saveChanges();
  }
 
  ngOnInit(): void {
    this.url = this.store.view.url;
    Iif (this.url) this.reload(this.url);
    this.disposers.push(autorun(() => {
      const url = this.store.view.url;
      Eif (!url) return;
      if (url === this.url) return;
      this.url = url;
      this.reload(url);
    }));
  }
 
  ngOnDestroy() {
    this.destroy$.next();
    this.destroy$.complete();
    for (const dispose of this.disposers) dispose();
    this.disposers.length = 0;
    this.store.view.clearRef();
  }
 
  @memo
  get refWarning() {
    const warn = this.sources > 0 && this.store.view.published && +this.store.view.ref!.published! !== +DateTime.fromISO(this.store.view.published);
    if (this.store.view.published) this.router.navigate([], { queryParams: { published: null }, queryParamsHandling: 'merge', replaceUrl: true });
    return warn;
  }
 
  @memo
  get expandedOnLoad() {
    return this.store.view.current === 'ref/thread' ||
      this.store.local.isRefToggled(this.store.view.url, this.store.view.current === 'ref/summary' || this.fullscreen?.onload);
  }
 
  @memo
  get fullscreen() {
    if (!this.admin.getPlugin('plugin/fullscreen')) return undefined;
    return this.store.view.ref?.plugins?.['plugin/fullscreen'];
  }
 
  @memo
  get comment() {
    return this.admin.getPlugin('plugin/comment') && hasTag('plugin/comment', this.store.view.ref);
  }
 
  @memo
  get comments() {
    Eif (!this.admin.getPlugin('plugin/comment')) return 0;
    return this.store.view.ref?.metadata?.plugins?.['plugin/comment'] || 0;
  }
 
  @memo
  get thread() {
    return this.admin.getPlugin('plugin/thread') && (hasTag('plugin/thread', this.store.view.ref) || this.store.view.current === 'ref/thread');
  }
 
  @memo
  get threads() {
    Eif (!this.admin.getPlugin('plugin/thread')) return 0;
    return hasTag('plugin/thread', this.store.view.ref) || this.store.view.ref?.metadata?.plugins?.['plugin/thread'];
  }
 
  @memo
  get logs() {
    Eif (!this.admin.getPlugin('+plugin/log')) return 0;
    return this.store.view.ref?.metadata?.plugins?.['+plugin/log'];
  }
 
  @memo
  get responses() {
    return this.store.view.ref?.metadata?.responses || 0;
  }
 
  @memo
  get sources() {
    const sources = (this.store.view.ref?.sources || []).filter( s => s != this.store.view.url);
    return sources.length || 0;
  }
 
  @memo
  get alts() {
    return this.store.view.ref?.alternateUrls?.length || 0;
  }
 
  reload(url?: string) {
    url ||= this.url || '';
    if (!url) {
      this.store.view.clear();
      return;
    }
    this.newResponses = 0;
    this.refs.count({ url, obsolete: true }).subscribe(count => runInAction(() =>
      this.store.view.versions = count));
    const fetchTop = (ref: Ref) => hasTag('plugin/thread', ref) || hasTag('plugin/comment', ref);
    (url === this.store.view.ref?.url
        ? of(this.store.view.ref)
        : this.refs.getCurrent(url)
    ).pipe(
      catchError(err => err.status === 404 ? of(undefined) : throwError(() => err)),
      map(ref => ref || { url }),
      tap(ref => this.markRead(ref)),
      switchMap(ref => !fetchTop(ref) ? of([ref, undefined])
        : top(ref) === url ? of([ref, ref])
        : top(ref) === this.store.view.top?.url ? of([ref, this.store.view.top])
        : this.refs.getCurrent(top(ref)).pipe(
          map(top => [ref, top]),
          catchError(err => err.status === 404 ? of([ref, undefined]) : throwError(() => err)),
        )),
      tap(([ref, top]) => runInAction(() => this.store.view.setRef(ref, top))),
      takeUntil(this.destroy$),
    ).subscribe(() => MemoCache.clear(this));
    if (this.config.websockets && this.watchUrl !== url) {
      this.watchUrl = url;
      this.watchSelf?.unsubscribe();
      this.watchSelf = this.stomp.watchRef(url).pipe(
        takeUntil(this.destroy$),
      ).subscribe(ud => {
        MemoCache.clear(this);
        // Merge updates with existing Ref because updates do not contain any private tags
        const tags = uniq([...this.store.view.ref!.tags || [], ...ud.tags || []])
          .filter(t => privateTag(t) || ud.tags?.includes(t));
        const merged: Ref = {
          ...ud,
          tags,
          metadata: {
            ...ud.metadata,
            plugins: {
              ...pickBy(this.store.view.ref?.metadata?.plugins, (v, k) => tags.includes(k)),
              ...ud.metadata?.plugins || {},
            }
          },
          plugins: {
            ...pickBy(this.store.view.ref!.plugins, (v, k) => tags.includes(k)),
            ...ud.plugins || {},
          },
          // Don't allow editing an update Ref, as we cannot tell when a private
          // tag was deleted
          // TODO: mark Ref as modified remotely to warn user before editing
          modified: this.store.view.ref?.modified,
          modifiedString: this.store.view.ref?.modifiedString,
        };
        runInAction(() => Object.assign(this.store.view.ref!, merged));
        this.store.eventBus.refresh(merged);
        this.store.eventBus.reset();
      });
      this.watchResponses?.unsubscribe();
      this.watchResponses = this.stomp.watchResponse(url).pipe(
        filter(url => url != this.store.view.url),
        filter(url => !url.startsWith('tag:')),
        filter(url => !this.seen.has(url)),
        takeUntil(this.destroy$),
      ).subscribe(url => {
        this.seen.add(url);
        this.newResponses++;
      });
    }
  }
 
  @memo
  isWiki(url: string) {
    return !this.admin.isWikiExternal() && isWiki(url, this.admin.getWikiPrefix());
  }
 
  markRead(ref: Ref) {
    markRead(this.admin, this.ts, ref);
  }
}