Ctrl K
ComunidadGuías…

app/models/Share.ts

Rama main · réplica de lectura del repositorio original.

Archivo original ↗Preguntar a KiBot
import { isPast } from "date-fns";
import { computed, observable } from "mobx";
import type { NavigationNode, PublicTeam } from "@shared/types";
import type SharesStore from "~/stores/SharesStore";
import Collection from "./Collection";
import Document from "./Document";
import User from "./User";
import Model from "./base/Model";
import Field from "./decorators/Field";
import Relation from "./decorators/Relation";
import type { Searchable } from "./interfaces/Searchable";

class Share extends Model implements Searchable {
  static modelName = "Share";

  constructor(fields: Record<string, unknown>, store: SharesStore) {
    super(fields, store);
    this.initialize(fields);
  }

  store: SharesStore;

  /** Whether the share is published. This ignores expiry, prefer `isActive` to check if the link works. */
  @Field
  @observable
  published: boolean;

  @Field
  @observable
  includeChildDocuments: boolean;

  /** The document ID that is shared. */
  @Field
  @observable
  documentId: string;

  /** The document that is shared. */
  @Relation(() => Document, { onDelete: "cascade" })
  document: Document;

  /** The collection ID that is shared. */
  @Field
  @observable
  collectionId: string;

  /** The collection that is shared. */
  @Relation(() => Collection, { onDelete: "cascade" })
  collection: Collection;

  @Field
  @observable
  urlId: string;

  @Field
  @observable
  domain: string;

  @observable
  sourceTitle: string;

  @observable
  sourcePath: string;

  @observable
  documentTitle: string;

  @observable
  documentUrl: string;

  @observable
  lastAccessedAt: string | null | undefined = undefined;

  @observable
  url: string;

  @Field
  @observable
  allowIndexing: boolean;

  @Field
  @observable
  allowSubscriptions: boolean;

  @Field
  @observable
  showLastUpdated: boolean;

  @Field
  @observable
  showTOC: boolean;

  /** Custom branding title to display on the shared page, supersedes team name. */
  @Field
  @observable
  title: string | null;

  /** Custom branding icon URL to display on the shared page, supersedes team avatar. */
  @Field
  @observable
  iconUrl: string | null;

  /** The time after which the public link stops working, if any. */
  @Field
  @observable
  expiresAt: string | null;

  @observable
  views: number;

  /** The user that shared the document. */
  @Relation(() => User, { onDelete: "null" })
  createdBy: User;

  /** Whether the expiry date has passed, not computed as time is not observable. */
  get isExpired(): boolean {
    return this.expiresAt ? isPast(new Date(this.expiresAt)) : false;
  }

  /** Whether the share is published and its public link still works. */
  get isActive(): boolean {
    return this.published && !this.isExpired;
  }

  @computed
  get sourcePathWithFallback(): string {
    return this.sourcePath ?? this.documentUrl;
  }

  @computed
  get searchContent(): string[] {
    return [this.sourceTitle ?? this.documentTitle];
  }

  @computed
  get searchSuppressed(): boolean {
    return false;
  }

  @computed
  get sharedCache() {
    return (
      this.store.sharedCache.get(this.id) ??
      this.store.sharedCache.get(this.urlId)
    );
  }

  @computed
  get team(): PublicTeam | undefined {
    return this.sharedCache?.team;
  }

  @computed
  get tree(): NavigationNode | undefined {
    return this.sharedCache?.sharedTree ?? undefined;
  }
}

export default Share;