All files / app/component/chat/chat-entry chat-entry.component.ts

44.18% Statements 76/172
14.78% Branches 21/142
14.51% Functions 9/62
47.97% Lines 71/148

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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344                                  67x   67x               67x 67x 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       67x 1x 1x 1x 1x 1x                                 67x           67x         67x                 67x         67x 1x       67x 1x       67x 1x       67x         67x         67x 1x       67x 1x       67x         67x         67x         67x           67x           67x           67x         67x         67x           67x         67x             67x           67x                     1x                                             1x                     1x                              
import { AsyncPipe } from '@angular/common';
import { HttpErrorResponse } from '@angular/common/http';
import {
  Component,
  forwardRef,
  HostBinding,
  Input,
  OnChanges,
  OnDestroy,
  QueryList,
  SimpleChanges,
  ViewChildren
} from '@angular/core';
import { RouterLink } from '@angular/router';
import { defer, uniq } from 'lodash-es';
import { catchError, map, of, Subject, switchMap, takeUntil, throwError } from 'rxjs';
import { tap } from 'rxjs/operators';
import { TitleDirective } from '../../../directive/title.directive';
import { Ref } from '../../../model/ref';
import { deleteNotice } from '../../../mods/delete';
import { AdminService } from '../../../service/admin.service';
import { ExtService } from '../../../service/api/ext.service';
import { RefService } from '../../../service/api/ref.service';
import { TaggingService } from '../../../service/api/tagging.service';
import { AuthzService } from '../../../service/authz.service';
import { ConfigService } from '../../../service/config.service';
import { Store } from '../../../store/store';
import { authors, clickableLink, formatAuthor, getNiceTitle } from '../../../util/format';
import { printError } from '../../../util/http';
import { memo, MemoCache } from '../../../util/memo';
import { hasTag, localTag, repost, tagOrigin } from '../../../util/tag';
import { ActionComponent } from '../../action/action.component';
import { ConfirmActionComponent } from '../../action/confirm-action/confirm-action.component';
import { InlineTagComponent } from '../../action/inline-tag/inline-tag.component';
import { LoadingComponent } from '../../loading/loading.component';
import { MdComponent } from '../../md/md.component';
import { NavComponent } from '../../nav/nav.component';
import { ViewerComponent } from '../../viewer/viewer.component';
 
@Component({
  selector: 'app-chat-entry',
  templateUrl: './chat-entry.component.html',
  styleUrls: ['./chat-entry.component.scss'],
  host: { 'class': 'chat-entry' },
  imports: [
    forwardRef(() => ViewerComponent),
    forwardRef(() => MdComponent),
    RouterLink,
    TitleDirective,
    LoadingComponent,
    NavComponent,
    ConfirmActionComponent,
    InlineTagComponent,
    AsyncPipe,
  ],
})
export class ChatEntryComponent implements OnChanges, OnDestroy {
  @HostBinding('attr.tabindex') tabIndex = 0;
  private destroy$ = new Subject<void>();
 
  @ViewChildren('action')
  actionComponents?: QueryList<ActionComponent>;
 
  @Input()
  ref!: Ref;
  @Input()
  focused = false;
  @Input()
  loading = true;
 
  noComment: Ref = {} as any;
  repostRef?: Ref;
  deleted = false;
  writeAccess = false;
  taggingAccess = false;
  deleteAccess = false;
  serverError: string[] = [];
 
  private _allowActions = false;
 
  constructor(
    private config: ConfigService,
    public admin: AdminService,
    public store: Store,
    private auth: AuthzService,
    private exts: ExtService,
    private ts: TaggingService,
    private refs: RefService,
  ) { }
 
  init() {
    MemoCache.clear(this);
    this.actionComponents?.forEach(c => c.reset());
    this.writeAccess = this.auth.writeAccess(this.ref);
    this.taggingAccess = this.auth.taggingAccess(this.ref);
    this.deleteAccess = this.auth.deleteAccess(this.ref);
    if (this.bareRepost && this.ref && 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;
        this.noComment = {
          ...ref,
          comment: '',
        };
      });
    } else {
      this.noComment = {
        ...this.ref,
        comment: '',
      };
    }
  }
 
  ngOnChanges(changes: SimpleChanges) {
    if (changes.ref) {
      this.init();
    } else if (changes.focused) {
      MemoCache.clear(this);
      if (!this.focused && !this._allowActions) this.actionComponents?.forEach(c => c.reset());
    }
  }
 
  ngOnDestroy() {
    this.destroy$.next();
    this.destroy$.complete();
  }
 
  @memo
  get title() {
    const title = (this.ref?.title || '').trim();
    Iif (title) return title;
    Iif (this.focused) return '';
    Iif (this.bareRepost) return getNiceTitle(this.repostRef) || '';
    return getNiceTitle(this.ref);
  }
 
  get allowActions(): boolean {
    return this._allowActions || this.focused || !!this.actionComponents?.find(c => c.active());
  }
 
  set allowActions(value: boolean) {
    if (value === this._allowActions) return;
    if (value) {
      defer(() => this._allowActions = value);
    } else {
      this._allowActions = false;
    }
  }
 
  @memo
  get nonLocalOrigin() {
    if (this.ref.origin === this.store.account.origin) return undefined;
    return this.ref.origin || '';
  }
 
  @memo
  get localhost() {
    return this.ref.url.startsWith(this.config.base);
  }
 
  @memo
  get authors() {
    const lookup = this.store.origins.originMap.get(this.ref.origin || '');
    return uniq([
      ...this.ref.tags?.filter(t => this.admin.getPlugin(t)?.config?.signature === t) || [],
      ...authors(this.ref).map(a => !tagOrigin(a) ? a : localTag(a) + (lookup?.get(tagOrigin(a)) ?? tagOrigin(a))),
    ]);
  }
 
  @memo
  get authorExts$() {
    return this.exts.getCachedExts(this.authors, this.ref.origin || '').pipe(this.admin.authorFallback);
  }
 
  @memo
  get tagLink() {
    return this.url.toLowerCase().startsWith('tag:/');
  }
 
  @memo
  get clickableLink() {
    return clickableLink(this.url);
  }
 
  @memo
  get url() {
    return this.repost ? this.ref.sources![0] : this.ref.url;
  }
 
  @memo
  get currentRef() {
    return this.repost ? this.repostRef : this.ref;
  }
 
  @memo
  get bareRef() {
    return this.bareRepost ? this.repostRef : this.ref;
  }
 
  @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 approved() {
    return hasTag('_moderated', this.currentRef);
  }
 
  @memo
  get locked() {
    return hasTag('locked', this.currentRef);
  }
 
  @memo
  get qr() {
    return hasTag('plugin/qr', this.currentRef);
  }
 
  @memo
  get audio() {
    return hasTag('plugin/audio', this.currentRef) ||
      this.admin.getPluginsForUrl(this.url).find(p => p.tag === 'plugin/audio');
  }
 
  @memo
  get video() {
    return hasTag('plugin/video', this.currentRef) ||
      this.admin.getPluginsForUrl(this.url).find(p => p.tag === 'plugin/image');
  }
 
  @memo
  get image() {
    return hasTag('plugin/image', this.currentRef) ||
      this.admin.getPluginsForUrl(this.url).find(p => p.tag === 'plugin/image');
  }
 
  @memo
  get media() {
    return this.qr || this.audio || this.video || this.image;
  }
 
  @memo
  get expand() {
    return this.currentRef?.comment || this.media;
  }
 
  @memo
  get comments() {
    if (!this.admin.getPlugin('plugin/comment')) return 0;
    return this.ref.metadata?.plugins?.['plugin/comment'] || 0;
  }
 
  @memo
  get chatroom() {
    return this.admin.getPlugin('plugin/chat') && hasTag('plugin/chat', this.ref);
  }
 
  @memo
  get thread() {
    if (!this.admin.getPlugin('plugin/thread')) return '';
    if (!hasTag('plugin/thread', this.ref) && !this.threads) return '';
    return this.ref.sources?.[1] || this.ref.sources?.[0] || this.ref.url;
  }
 
  @memo
  get threads() {
    if (!this.admin.getPlugin('plugin/thread')) return 0;
    return this.ref.metadata?.plugins?.['plugin/thread'] || 0;
  }
 
  @memo
  formatAuthor(user: string) {
    if (this.store.account.origin && tagOrigin(user) === this.store.account.origin) {
      user = user.replace(this.store.account.origin, '');
    }
    return formatAuthor(user);
  }
 
  saveRef() {
    this.store.view.preloadRef(this.ref, this.repostRef);
  }
 
  tag$ = (tag: string) => {
    this.serverError = [];
    return this.store.eventBus.runAndReload$(this.ts.create(tag, this.ref.url, this.ref.origin!), this.ref);
  }
 
  approve() {
    this.refs.patch(this.ref.url, this.ref.origin!, this.ref.modifiedString!, [{
      op: 'add',
      path: '/tags/-',
      value: '_moderated',
    }]).pipe(
      switchMap(() => this.refs.get(this.ref.url, this.ref.origin!).pipe(takeUntil(this.destroy$))),
      catchError((err: HttpErrorResponse) => {
        this.serverError = printError(err);
        return throwError(() => err);
      }),
    ).subscribe(ref => {
      this.serverError = [];
      this.ref = ref;
      this.init();
    });
  }
 
  forceDelete$ = () => {
    this.serverError = [];
    return this.refs.delete(this.ref.url, this.ref.origin).pipe(
      tap(() => this.deleted = true),
      catchError((err: HttpErrorResponse) => {
        this.serverError = printError(err);
        return throwError(() => err);
      }),
    );
  }
 
  delete$ = () => {
    this.serverError = [];
    return (this.admin.getPlugin('plugin/delete')
        ? this.refs.update(deleteNotice(this.ref))
        : this.refs.delete(this.ref.url, this.ref.origin).pipe(map(() => ''))
    ).pipe(
      tap(() => this.deleted = true),
      catchError((err: HttpErrorResponse) => {
        this.serverError = printError(err);
        return throwError(() => err);
      }),
    );
  }
 
}