All files / app/model tag.ts

36.6% Statements 56/153
0.84% Branches 1/119
16.27% Functions 7/43
48.18% Lines 53/110

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 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637117x 126x             126x 126x 126x 126x 126x 126x 126x 126x                                                                       126x                                                                                                                                                                                                                                                                                                                                                                                                                                         136x 136x       1x 1x       6x                                                                                                                                                                                                                                                                                                                                                                                                                                                 126x 126x   126x 126x 126x 126x                   126x 126x 126x                 126x                                                                 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x     126x       126x                                               2x 4x 2x 3x 2x 2x 2x 2x 2x 2x 2x 2x                                                              
import { FormlyFieldConfig } from '@ngx-formly/core';
import * as d3 from 'd3';
import * as Handlebars from 'handlebars/dist/cjs/handlebars';
import { Schema } from 'jtd';
import { defer, isEqual, omitBy, uniqWith } from 'lodash-es';
import { DateTime, Duration, DurationObjectUnits } from 'luxon';
import { toJS } from 'mobx';
import { v4 as uuid } from 'uuid';
import { interestingTags } from '../util/format';
import { hasAnyResponse, hasResponse, hasTag, prefix } from '../util/tag';
import { filterModels } from '../util/zip';
import { Ext, extSchema } from './ext';
import { Plugin, pluginSchema } from './plugin';
import { Ref, refSchema } from './ref';
import { Template, templateSchema } from './template';
import { Role, User, userSchema } from './user';
 
export interface Cursor {
  origin?: string;
  modified?: DateTime;
  // Saved to pass modified check since moment looses precision
  // TODO: Does luxon loose precision?
  modifiedString?: string;
  // Client-only
  upload?: boolean;
  exists?: boolean;
  outdated?: boolean;
}
 
export interface Tag extends Cursor {
  type?: 'ext' | 'user' | 'plugin' | 'template';
  tag: string;
  name?: string;
}
 
export interface Mod {
  ref?: Ref[];
  ext?: Ext[];
  user?: User[];
  plugin?: Plugin[];
  template?: Template[];
}
 
export function bundleSize(mod: Mod) {
  return (mod.ref?.length || 0) +
    (mod.ext?.length || 0) +
    (mod.user?.length || 0) +
    (mod.plugin?.length || 0) +
    (mod.template?.length || 0);
}
 
export const modSchema: Schema = {
  optionalProperties: {
    ref: { elements: refSchema },
    ext: { elements: extSchema },
    user: { elements: userSchema },
    plugin: { elements: pluginSchema },
    template: { elements: templateSchema },
  }
};
 
export type ModType = 'config' | 'icon' | 'feature' | 'lens' | 'plugin' | 'editor' | 'semantic' | 'theme' | 'tool';
 
export interface Config extends Tag {
  config?: {
    /**
     * Configs may only be created and edited by admin, so we allow anything.
     * Schemas are only used on non-admin config.
     */
    [record: string]: any,
    /**
     * Optional label which can be used to group plugins and templates.
     */
    mod?: string;
    /**
     * Optional category for setup screen.
     */
    type?: ModType,
    /**
     * Increment-only number to indicate version.
     */
    version?: number;
    /**
     * Flag for disabling a config without deleting.
     */
    disabled?: boolean;
    /**
     * Disable update checking.
     */
    noUpdate?: boolean;
    /**
     * Install by default on a fresh instance.
     */
    default?: boolean;
    /**
     * Mark this as an experiment. Only show on setup page if
     * plugin/experiments is installed.
     */
    experimental?: boolean;
    /**
     * Description of what this is used for.
     */
    description?: string,
    /**
     * Snippet added to the AI system prompt.
     */
    aiInstructions?: string,
    /**
     * Add tags when replying to this tag.
     */
    reply?: string[],
    /**
     * Optional handlebars template to use as a UI.
     */
    ui?: string,
    /**
     * Optional CSS to be added to <head> on load.
     */
    css?: string,
    /**
     * Optional script to be added to <head> on load.
     */
    snippet?: string,
    /**
     * Optional html to be added to <body> on load.
     */
    banner?: string,
    /**
     * Optional html to be added to <body> on load.
     */
    consent?: { [key: string]: string },
    /**
     * Optional buttons to add to the editor.
     */
    editorButtons?: EditorButton[],
    /**
     * Optional formly config for editing a form defined by the schema.
     */
    form?: FormlyFieldConfig[],
    /**
     * Extra formly config to hide in advanced tab.
     */
    advancedForm?: FormlyFieldConfig[],
    /**
     * Optional icons to add to refs based on tag triggers.
     */
    icons?: Icon[],
    /**
     * Add query or response filters to the filter dropdown.
     */
    filters?: FilterConfig[],
    /**
     * Add an action to the Ref actions bar.
     */
    actions?: Action[],
    /**
     * Add an action to the Ref actions overflow bar.
     */
    advancedActions?: Action[],
    /**
     * Add themes.
     */
    themes?: Record<string, string>,
    /**
     * Optional default read access tags to give users. Tags will be prefixed
     * with the plugin tag.
     */
    readAccess?: string[],
    /**
     * Optional default write access tags to give users. Tags will be prefixed
     * with the plugin tag.
     */
    writeAccess?: string[],
    /**
     * Show help text for search box.
     */
    searchHelp?: string,
    /**
     * Show help text for filter box.
     */
    filterHelp?: string,
    /**
     * Show help text for sort box.
     */
    sortHelp?: string,
    /**
     * Help links to show in editor dropdown.
     */
    editorHelpLinks?: Array<{ label: string; url: string }>,
  };
  /**
   * Default config values when validating or reading. Should pass validation.
   */
  defaults?: any;
  /**
   * JTD schema for validating config.
   */
  schema?: Schema;
  // Client-only
  type?: 'plugin' | 'template';
  /**
   * Cache for compiled templates.
   */
  _cache?: any;
}
 
 
export function condition(value: string | undefined, config: any | undefined) {
  if (!value) return false;
  if (value.startsWith('!')) {
    return !config?.[value.substring(1)]
  } else {
    return config?.[value];
  }
}
 
export interface Visibility {
  /**
   * Optional handlebars template tooltip.
   */
  title?: string;
  /**
   * Tag to show / hide.
   */
  if?: string;
  /**
   * Minimum role required to be visible.
   */
  role?: Role;
  /**
   * Field name of a config flag to show / hide.
   */
  condition?: string;
  /**
   * If set, limits visibility to the indicated parties.
   */
  visible?: 'author' | 'recipient' | 'participant';
  /**
   * Add this to every Ref, not just Refs with this tag.
   */
  global?: boolean;
  /**
   * Optional number to influence order relative to other items.
   * Unset or 0 has no impact on ordering.
   * Lower positive numbers will be towards the left or start, higher positive
   * numbers will be towards the right or end.
   * Negative numbers will reverse alignment. i.e. 1 will be first and -1 will
   * be last.
   */
  order?: number;
 
  //cache
  _parent?: Config;
}
 
export function visible(ref: Ref, v: Visibility, isAuthor: boolean, isRecipient: boolean) {
  if (('if' in v) && !hasTag(v.if, ref)) return false;
  if (!v.visible) return true;
  if (isAuthor) return v.visible === 'author' || v.visible === 'participant';
  if (isRecipient) return v.visible === 'recipient' || v.visible === 'participant';
  return false;
}
 
export function sortOrder<T extends { order?: number, toggle?: string, _parent?: Config; }>(vs: T[]) {
  const getTag = (o: T) => o.toggle || o._parent?.tag || '';
  return vs.sort((a, b) => a.order === b.order ? getTag(b).localeCompare(getTag(a)) : (b.order || 0) - (a.order || 0));
}
 
export function uniqueConfigs<T extends Visibility>(vs: T[]) {
  const hiddenField = (v: string, k: string) => k.startsWith('_');
  return uniqWith(vs, (a, b) => isEqual(omitBy(a as any, hiddenField), omitBy(b as any, hiddenField)));
}
 
export function latest<T extends Cursor>(cs: T[]) {
  return cs.sort((a, b) => {
    return (b.modified?.valueOf() || 0) - (a.modified?.valueOf() || 0);
  });
}
 
export interface Icon extends Visibility {
  /**
   * Label to show in info row.
   * Will also use as default thumbnail if order is not negative.
   */
  label?: string;
  /**
   * Emoji to use as default thumbnail.
   */
  thumbnail?: string;
  /**
   * If set, makes this icon conditional on a tag.
   */
  tag?: string;
  /**
   * If set, makes this icon conditional on a plugin response from any user.
   */
  anyResponse?: `plugin/${string}` | `+plugin/${string}` | `_plugin/${string}`;
  /**
   * If set, makes this icon conditional on no plugin response from any user.
   */
  noResponse?: `plugin/${string}` | `+plugin/${string}` | `_plugin/${string}`;
  /**
   * If set, makes this icon conditional on the current user's plugin response.
   */
  response?: `plugin/user/${string}` | `+plugin/user/${string}` | `_plugin/user/${string}`;
  /**
   * If set, makes this icon conditional on a ref scheme.
   */
  scheme?: `${string}:`;
}
 
export interface FilterConfig {
  /**
   * Filter based on a tag query.
   * If set, no other filter types must be set.
   */
  query?: string;
  /**
   * Filter based on URL scheme.
   * If set, no other filter types must be set.
   */
  scheme?: string;
  /**
   * Filter based on sources to a Ref.
   * If set, no other filter types must be set.
   */
  sources?: string;
  /**
   * Filter based on responses to a Ref.
   * If set, no other filter types must be set.
   */
  responses?: string;
  /**
   * Filter based on plugin responses in metadata.
   * If set, query and scheme must not be set.
   */
  response?: `plugin/${string}` | `+plugin/${string}` | `_plugin/${string}` | `!plugin/${string}` | `!+plugin/${string}` | `!_plugin/${string}`;
  /**
   * Filter based on user plugin responses in metadata.
   * If set, query and scheme must not be set.
   */
  user?: `plugin/user/${string}` | `+plugin/user/${string}` | `_plugin/user/${string}` | `!plugin/user/${string}` | `!+plugin/user/${string}` | `!_plugin/user/${string}`;
  label?: string;
  title?: string;
  group?: string;
}
 
export interface EditorButton {
  /**
   * Query required to show this button (default to the parent tag).
   */
  query?: string;
  /**
   * Label for editor button.
   */
  label?: string;
  /**
   * Optional tooltip.
   */
  title?: string;
  /**
   * Label for editor button when toggled.
   */
  labelOn?: string;
  /**
   * Label for editor button when un-toggled.
   */
  labelOff?: string;
  /**
   * Tag to toggle on/off.
   */
  toggle?: string;
  /**
   * Show toggle as ribbon.
   */
  ribbon?: boolean;
  /**
   * Save toggle choice as default.
   */
  remember?: boolean;
  /**
   * Event to emit when clicked.
   */
  event?: string;
  /**
   * Event to listen for to stop showing loading indicator.
   * If set, button will show loading indicator after click until this event fires.
   */
  eventDone?: string;
  /**
   * Only show button if URL is of scheme.
   */
  scheme?: `${string}:`;
  /**
   * Show button on all Refs, not just refs with this tag.
   */
  global?: boolean;
  /**
   * Optional number to influence order relative to other items.
   * Unset or 0 has no impact on ordering.
   * Lower positive numbers will be towards the left or start, higher positive
   * numbers will be towards the right or end.
   * Negative numbers will reverse alignment. i.e. 1 will be first and -1 will
   * be last.
   */
  order?: number;
 
  //cache
  _parent?: Config;
  _on?: boolean;
}
 
export type Action = (TagAction | ResponseAction | EmitAction | EventAction) & {
  /**
   * Display confirm message.
   */
  confirm?: string;
};
 
export interface TagAction extends Visibility {
  /**
   * Add a tag directly to the Ref.
   */
  tag: string;
  /**
   * Handlebars template label to show when this action has been applied.
   */
  labelOn?: string;
  /**
   * Handlebars template label to show when this action has not been applied.
   */
  labelOff?: string;
}
 
export interface ResponseAction extends Visibility {
  /**
   * Add a tag response to the Ref.
   */
  response: `plugin/user/${string}` | `+plugin/user/${string}` | `_plugin/user/${string}` | `!plugin/user/${string}` | `!+plugin/user/${string}` | `!_plugin/user/${string}`;
  /**
   * Clear other tag responses when adding tag response.
   */
  clear?: (`plugin/user/${string}` | `+plugin/user/${string}` | `_plugin/user/${string}`)[];
  /**
   * Handlebars template label to show when this action has been applied.
   * The response plugin must have metadata generation turned on.
   */
  labelOn?: string;
  /**
   * Handlebars template label to show when this action has not been applied.
   * The response plugin must have metadata generation turned on.
   */
  labelOff?: string;
}
 
export interface EventAction extends Visibility {
  /**
   * Fire an event when action is triggered.
   */
  event: string;
  /**
   * If set, makes this icon conditional on a ref scheme.
   */
  scheme?: `${string}:`;
  /**
   * Handlebars template label.
   */
  label?: string;
}
 
export interface EmitAction extends Visibility {
  /**
   * Emit the templated json models.
   */
  emit: string;
  /**
   * Handlebars template label.
   */
  label?: string;
}
 
export function active(ref: Ref, o: TagAction | ResponseAction | EventAction | Icon) {
  if (('tag' in o) && !hasTag(o.tag, ref)) return false;
  if (('response' in o) && !hasResponse(o.response, ref)) return false;
  if (('anyResponse' in o) && !hasAnyResponse(o.anyResponse, ref)) return false;
  if (('noResponse' in o) && hasAnyResponse(o.noResponse, ref)) return false;
  return true;
}
 
// https://github.com/handlebars-lang/handlebars.js/issues/1593
// @ts-ignore
window.global = {};
window.Handlebars = Handlebars as any;
 
Handlebars.registerHelper('prefix', (p: string, r: string) => prefix(p, r));
Handlebars.registerHelper('uuid', () => uuid());
Handlebars.registerHelper('d3', () => d3);
Handlebars.registerHelper('defer', (el: Element, fn: () => {}) => {
  // @ts-ignore
  if (el.defered) {
    fn();
  } else {
    // @ts-ignore
    el.deferred = true;
    defer(fn);
  }
});
Handlebars.registerHelper('fromNow', (value: string) => DateTime.fromISO(value).toRelative());
Handlebars.registerHelper('formatInterval', (value: string) => Duration.fromISO(value).toHuman());
Handlebars.registerHelper('duration', (ref: Ref, tag: string) => {
  const p = tag + '/';
  const t = ref?.tags?.find(t => t.startsWith(p));
  if (!t) return undefined;
  const result = t.substring(p.length);
  const value = result.split('/')[0];
  const d = Duration.fromISO(value.toUpperCase());
  return d.isValid ? d : undefined;
});
Handlebars.registerHelper('human', (value: any) => {
  if (!value) return '';
  const formatDuration = (d: Duration) => {
    // Standard duration units ordered by scale
    const units: (keyof DurationObjectUnits)[] = [
      'years', 'months', 'weeks', 'days', 'hours', 'minutes', 'seconds'
    ];
    // Find the largest unit that has at least "1" of that unit
    const largestUnitIndex = units.findIndex(unit => d.as(unit) >= 1);
    // Fallback for durations < 1 second (e.g., "0.5 seconds" becomes "1 second")
    if (largestUnitIndex === -1) {
      return d.shiftTo('seconds').mapUnits(Math.round).toHuman({ listStyle: 'narrow', unitDisplay: 'short', showZeros: false });
    }
    const primaryUnit = units[largestUnitIndex];
    const secondaryUnit = units[largestUnitIndex + 1];
    // Build target units (e.g., ['years', 'months'] or ['seconds'])
    const targetUnits = secondaryUnit ? [primaryUnit, secondaryUnit] : [primaryUnit];
    return d
      .shiftTo(...targetUnits)
      .mapUnits(Math.round) // Rounds the smallest visible unit
      .normalize()          // Handles carry-over (e.g., 60m -> 1h)
      .toHuman({ listStyle: 'narrow', unitDisplay: 'short', showZeros: false });
  };
  if (Duration.isDuration(value)) return formatDuration(value);
  if (DateTime.isDateTime(value)) return value.toRelative() ?? '';
  if (typeof value === 'string') {
    const d = Duration.fromISO(value.toUpperCase());
    if (d.isValid) return formatDuration(d);
    const dt = DateTime.fromISO(value);
    if (dt.isValid) return dt.toRelative() ?? '';
  }
  return String(value);
});
Handlebars.registerHelper('plugins', (ref: Ref, plugin: string) => ref.metadata?.plugins?.[plugin]);
Handlebars.registerHelper('response', (ref: Ref, value: string) => ref.metadata?.userUrls?.includes(value));
Handlebars.registerHelper('includes', (array: string[], value: string) => array?.includes(value));
Handlebars.registerHelper('interestingTags', (tags: string[]) => interestingTags(tags));
Handlebars.registerHelper('hasTag', (tag: string | undefined, ref: Ref | string[] | undefined) => hasTag(tag, ref));
Handlebars.registerHelper('tail', (text: string) => text.split('\n').pop()!.trim());
Handlebars.registerHelper('eq', (v1, v2) => v1 === v2);
Handlebars.registerHelper('ne', (v1, v2) => v1 !== v2);
Handlebars.registerHelper('lt', (v1, v2) => v1 < v2);
Handlebars.registerHelper('gt', (v1, v2) => v1 > v2);
Handlebars.registerHelper('lte', (v1, v2) => v1 <= v2);
Handlebars.registerHelper('gte', (v1, v2) => v1 >= v2);
Handlebars.registerHelper('and', function() {
  return Array.prototype.slice.call(arguments, 0, arguments.length - 1).every(Boolean);
});
Handlebars.registerHelper('or', function() {
  return Array.prototype.slice.call(arguments, 0, arguments.length - 1).some(Boolean);
});
 
let hydrateError = false;
export function hydrate(config: any, field: string, model: any): string {
  if (!config[field]) return '';
  config._cache ||= {};
  config._cache[field] ||= Handlebars.compile(config[field]);
  try {
    return config._cache[field](model);
  } catch (e) {
    if (!hydrateError) console.error('hydrate error', config, field, model, e);
    hydrateError = true;
    return '';
  }
}
 
export function emitModels(action: EmitAction, ref?: Ref, user?: string) {
  const hydrated = hydrate(action, 'emit', {
    action: toJS(action),
    ref: toJS(ref),
    user: user,
  });
  return filterModels(JSON.parse(hydrated));
}
 
export function clear<T extends Config>(c: T) {
  const { tag } = c;
  c = omitBy(c, i => !i) as any;
  Eif (tag !== undefined) c.tag = tag;
  c.config = omitBy(c.config, i => !i);
  delete c.config!.generated;
  delete c.config!._parent;
  delete c.type;
  delete c.origin;
  delete c.modified;
  delete c.modifiedString;
  delete c._cache;
  return c;
}
 
export type TagQueryArgs = {
  query?: string,
  nesting?: number,
  level?: number,
  deleted?: boolean,
  search?: string,
  modifiedBefore?: string,
  modifiedAfter?: string,
};
 
export type TagPageArgs = TagQueryArgs & {
  page?: number,
  size?: number,
  sort?: TagSort[],
};
 
export type TagSort = '' |
  'modified' | 'modified,ASC' | 'modified,DESC' |
  'tag' | 'tag,ASC' | 'tag,DESC' |
  'tag:len' | 'tag:len,ASC' | 'tag:len,DESC' |
  'name' | 'name,ASC' | 'name,DESC' |
  'origin' | 'origin,ASC' | 'origin,DESC' |
  'origin:len' | 'origin:len,ASC' | 'origin:len,DESC' |
  `config->${string}` | `config->${string},ASC` | `config->${string},DESC`;
 
export type ConfigSort = TagSort |
  `defaults->${string}` | `defaults->${string},ASC` | `defaults->${string},DESC` |
  `schema->${string}` | `schema->${string},ASC` | `schema->${string},DESC`;