All files / app/component/folder/file file.component.ts

65.04% Statements 67/103
32.05% Branches 25/78
46.42% Functions 13/28
65.16% Lines 58/89

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          67x                     67x 67x         67x 67x 67x 67x 67x                           67x 1x 1x 1x         1x   1x   1x   1x   1x     1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x                                                                       1x 1x       67x   1x           67x 1x 1x       67x         67x 1x       67x 1x       67x         67x 1x 1x     67x 1x         67x 1x         67x 1x         67x 1x 1x       67x         67x   1x       67x                                        
import { AsyncPipe } from '@angular/common';
import { Component, forwardRef, HostBinding, Input, OnChanges, OnDestroy, SimpleChanges } from '@angular/core';
import { RouterLink } from '@angular/router';
import { catchError, of, Subject, takeUntil, throwError } from 'rxjs';
import { Ref } from '../../../model/ref';
import {
  Action,
  active,
  Icon,
  ResponseAction,
  sortOrder,
  TagAction,
  uniqueConfigs,
  Visibility,
  visible
} from '../../../model/tag';
import { CssUrlPipe } from '../../../pipe/css-url.pipe';
import { ThumbnailPipe } from '../../../pipe/thumbnail.pipe';
import { AdminService } from '../../../service/admin.service';
import { RefService } from '../../../service/api/ref.service';
import { AuthzService } from '../../../service/authz.service';
import { Store } from '../../../store/store';
import { getTitle, templates } from '../../../util/format';
import { getScheme } from '../../../util/http';
import { memo, MemoCache } from '../../../util/memo';
import { hasTag, isAuthorTag, repost } from '../../../util/tag';
import { ViewerComponent } from '../../viewer/viewer.component';
 
@Component({
  selector: 'app-file',
  templateUrl: './file.component.html',
  styleUrls: ['./file.component.scss'],
  imports: [
    forwardRef(() => ViewerComponent),
    RouterLink,
    AsyncPipe,
    ThumbnailPipe,
    CssUrlPipe,
  ],
})
export class FileComponent implements OnChanges, OnDestroy {
  css = 'file ';
  @HostBinding('attr.tabindex') tabIndex = 0;
  private destroy$ = new Subject<void>();
 
  @Input()
  ref!: Ref;
  @Input()
  expanded = false;
  @Input()
  expandInline = false;
  @Input()
  showToggle = false;
  @Input()
  dragging = false;
  @Input()
  fetchRepost = true;
 
  repostRef?: Ref;
  expandPlugins: string[] = [];
  icons: Icon[] = [];
  actions: Action[] = [];
  editing = false;
  viewSource = false;
  writeAccess = false;
  taggingAccess = false;
  serverError: string[] = [];
 
  constructor(
    public admin: AdminService,
    private refs: RefService,
    public store: Store,
    private auth: AuthzService,
  ) { }
 
  ngOnChanges(changes: SimpleChanges) {
    if (changes.ref) {
      MemoCache.clear(this);
      this.editing = false;
      this.viewSource = false;
      this.writeAccess = this.auth.writeAccess(this.ref);
      this.taggingAccess = this.auth.taggingAccess(this.ref);
      this.icons = uniqueConfigs(sortOrder(this.admin.getIcons(this.ref.tags, this.ref.plugins, getScheme(this.ref.url))));
      this.actions = uniqueConfigs(sortOrder(this.admin.getActions(this.ref.tags, this.ref.plugins)));
 
      this.expandPlugins = this.admin.getEmbeds(this.ref);
      if (this.repost && this.ref && this.fetchRepost && this.repostRef?.url != repost(this.ref)) {
        (this.store.view.top?.url === this.ref.sources![0]
            ? of(this.store.view.top)
            : this.refs.getCurrent(this.url)
        ).pipe(
          catchError(err => err.status === 404 ? of(undefined) : throwError(() => err)),
          takeUntil(this.destroy$),
        ).subscribe(ref => {
          this.repostRef = ref;
          if (!ref) return;
          MemoCache.clear(this);
          if (this.bareRepost) {
            this.expandPlugins = this.admin.getEmbeds(ref);
          } else {
            this.expandPlugins.push('plugin/repost');
          }
        });
      }
    }
  }
 
  ngOnDestroy() {
    this.destroy$.next();
    this.destroy$.complete();
  }
 
  @memo
  @HostBinding('class')
  get pluginClasses() {
    return this.css + templates(this.ref.tags, 'plugin')
      .map(t => t.replace(/\//g, '_').replace(/\./g, '-'))
      .join(' ');
  }
 
  @memo
  get nonLocalOrigin() {
    Iif (this.ref.origin === this.store.account.origin) return undefined;
    return this.ref.origin || '';
  }
 
  @memo
  get local() {
    return this.ref.origin === this.store.account.origin;
  }
 
  @memo
  get repost() {
    return this.ref?.sources?.[0] && hasTag('plugin/repost', this.ref);
  }
 
  @memo
  get bareRepost() {
    return this.repost && !this.ref.title && !this.ref.comment;
  }
 
  @memo
  get url() {
    return this.repost ? this.ref.sources![0] : this.ref.url;
  }
 
  @memo
  get title() {
    Iif (this.bareRepost) return getTitle(this.repostRef) || $localize`Repost`;
    return getTitle(this.ref);
  }
  @memo
  get thumbnail() {
    Eif (!this.admin.getPlugin('plugin/thumbnail')) return false;
    return hasTag('plugin/thumbnail', this.ref) || hasTag('plugin/thumbnail', this.repostRef);
  }
 
  @memo
  get iconColor() {
    Eif (!this.thumbnail) return '';
    return this.ref?.plugins?.['plugin/thumbnail']?.color || this.repostRef?.plugins?.['plugin/thumbnail']?.color || '';
  }
 
  @memo
  get iconEmoji() {
    Eif (!this.thumbnail) return '';
    return this.ref?.plugins?.['plugin/thumbnail']?.emoji || this.repostRef?.plugins?.['plugin/thumbnail']?.emoji || '';
  }
 
  @memo
  get iconEmojiDefaults() {
    const icon = this.icons.filter(i => i.thumbnail || (i.label && (i.order || 0) >= 0) && this.showIcon(i))[0];
    return icon?.label || icon?.thumbnail;
  }
 
  @memo
  get iconRadius() {
    return this.ref?.plugins?.['plugin/thumbnail']?.radius || this.repostRef?.plugins?.['plugin/thumbnail']?.radius || undefined;
  }
 
  @memo
  @HostBinding('class.sent')
  get isAuthor() {
    return isAuthorTag(this.store.account.tag, this.ref);
  }
 
  @memo
  get isRecipient() {
    return hasTag(this.store.account.mailbox, this.ref);
  }
 
  saveRef() {
    this.store.view.preloadRef(this.ref, this.repostRef);
  }
 
  showIcon(i: Icon) {
    return this.visible(i) && this.active(i);
  }
 
  visible(v: Visibility) {
    return visible(this.ref, v, this.isAuthor, this.isRecipient);
  }
 
  active(a: TagAction | ResponseAction | Icon) {
    return active(this.ref, a);
  }
}