All files / app/store submit.ts

55.63% Statements 74/133
34.48% Branches 30/87
57.14% Functions 32/56
61.11% Lines 66/108

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  167x           167x 167x       167x   388x 388x 388x 388x 388x 388x 388x 388x 388x       388x 388x 388x     388x 388x   388x                     388x 1x 1x 1x             1x               1x       17x       13x       1x 1x         13x 13x       8x       2x 2x         23x       23x             9x       9x               9x 9x                 2x       9x       7x       1x       1x       2x       5x       1x       2x       1x         2x 2x       2x       2x                                               4x 4x 4x 4x 4x         11x 11x 1x 1x   10x                     2x                                                 1x                                                        
import JSZip from 'jszip';
import { flatten, isArray, without } from 'lodash-es';
import { action, autorun, makeAutoObservable, observableShallow } from 'mobx';
import { RouterStore } from 'mobx-angular';
import { Ext } from '../model/ext';
import { Plugin } from '../model/plugin';
import { Ref } from '../model/ref';
import { DEFAULT_WIKI_PREFIX } from '../mods/org/wiki';
import { refCacheIds } from '../util/cache';
import { EventBus } from './bus';
 
export type Saving = { url?: string, name: string, progress?: number };
export class SubmitStore {
 
  wikiPrefix = DEFAULT_WIKI_PREFIX;
  maxPreview = 300;
  submitGenId: Plugin[] = [];
  submitDm: Plugin[] = [];
  files: File[] = [] as any;
  embedFiles: File[] = [] as any;
  caching: Map<File, Saving> = new Map<File, Saving>();
  exts: Ext[] = [];
  refs: Ref[] = [];
  /**
   * Cache files from uploaded zips, keyed by their original cache ID.
   */
  cacheFiles = new Map<string, JSZip.JSZipObject>();
  overwrite = false;
  refLimitOverride = false;
 
  constructor(
    public route: RouterStore,
    private eventBus: EventBus,
  ) {
    makeAutoObservable(this, {
      submitGenId: observableShallow,
      submitDm: observableShallow,
      files: observableShallow,
      embedFiles: observableShallow,
      caching: observableShallow,
      cacheFiles: false,
      setRef: action,
      setExt: action,
    });
 
    this.eventBus.events.subscribe(event => {
      Eif (event.event === 'refresh') {
        Eif (event.ref) {
          this.setRef(event.ref)
        }
      }
    });
  }
 
  get topRefs() {
    return this.refs.slice(0, 5);
  }
 
  get topExts() {
    return this.exts.slice(0, 5);
  }
 
  get subpage() {
    return this.route.routeSnapshot?.firstChild?.firstChild?.routeConfig?.path;
  }
 
  get url() {
    return this.route.routeSnapshot?.queryParams['url'];
  }
 
  get linkTypeOverride() {
    return this.route.routeSnapshot?.queryParams['linkTypeOverride'];
  }
 
  get text() {
    Iif (this.linkTypeOverride) return this.linkTypeOverride === 'text';
    Eif (this.subpage != 'text') return false;
    return this.url?.startsWith('comment:') || !this.url;
  }
 
  get wiki() {
    Iif (this.linkTypeOverride) return this.linkTypeOverride === 'wiki';
    return !!this.url?.startsWith(this.wikiPrefix);
  }
 
  get title(): string {
    return this.route.routeSnapshot?.queryParams['title'] || '';
  }
 
  get to(): string[] {
    const tag = this.route.routeSnapshot?.queryParams['to'];
    Eif (!tag) return [];
    return isArray(tag) ? tag : [tag];
  }
 
  get tag() {
    return this.route.routeSnapshot?.queryParams['tag'] as string;
  }
 
  get tags(): string[] {
    return flatten(this.tag ? [this.tag] : [])
      .flatMap( t => t.split(/[:|!()]/))
      .map(t => t.includes('@') ? t.substring(0, t.indexOf('@')) : t)
      .filter(t => t && !t.includes('*'));
  }
 
  get plugin() {
    return this.route.routeSnapshot?.queryParams['plugin'] || '' as string;
  }
 
  get pluginUpload() {
    Eif (!this.plugin) return '';
    return this.route.routeSnapshot?.queryParams['upload'] || '' as string;
  }
 
  /**
   * Location from the "lng,lat" location query param.
   */
  get location(): [number, number] | undefined {
    const value = this.route.routeSnapshot?.queryParams['location'];
    Eif (typeof value !== 'string') return undefined;
    const location = value.split(',').map(n => n.trim() ? Number(n) : NaN);
    if (location.length !== 2 || !location.every(n => isFinite(n))) return undefined;
    const [lng, lat] = location;
    if (lat < -90 || lat > 90 || lng < -180 || lng > 180) return undefined;
    return [lng, lat];
  }
 
  get repost() {
    return this.tags.includes('plugin/repost');
  }
 
  get source() {
    return this.route.routeSnapshot?.queryParams['source'];
  }
 
  get sources(): string[] {
    return flatten(this.source ? [this.source] : []);
  }
 
  get web() {
    return !this.wiki && (!this.subpage || this.subpage === 'web');
  }
 
  get upload() {
    return this.subpage === 'upload';
  }
 
  get filesEmpty() {
    return !this.files.length;
  }
 
  get empty() {
    return !this.exts.length && !this.refs.length;
  }
 
  get genId() {
    return this.tags.find(t => this.submitGenId.find(p => p.tag === t));
  }
 
  get dmPlugin() {
    return [...this.tags, ...this.to].find(t => this.submitDm.find(p => p.tag === t));
  }
 
  get withoutGenId() {
    Eif (!this.submitGenId.length) return this.tags;
    return without(this.tags, ...this.submitGenId.map(p => p.tag));
  }
 
  get huge() {
    Iif (this.refLimitOverride) return false;
    return this.refs.length > 100 || this.exts.length > 100;
  }
 
  get uploads() {
    return [...this.caching.values()];
  }
 
  clearOverride() {
    this.refLimitOverride = false;
  }
 
  overrideHuge() {
    this.refLimitOverride = true;
  }
 
  addRefs(...refs: Ref[]) {
    this.refs = [...this.refs, ...refs];
  }
 
  addExts(...exts: Ext[]) {
    this.exts = [...this.exts, ...exts];
  }
 
  removeRef(ref: Ref) {
    this.refs = this.refs.filter(r => r.url !== ref.url || r.modifiedString !== ref.modifiedString);
  }
 
  removeExt(ext: Ext) {
    this.exts = this.exts.filter(x => x.tag !== ext.tag || x.modifiedString !== ext.modifiedString);
  }
 
  clearUpload(refs: Ref[] = [], exts: Ext[] = []) {
    this.exts = exts;
    this.refs = refs;
    const keep = new Set(refs.flatMap(refCacheIds));
    for (const id of [...this.cacheFiles.keys()]) {
      if (!keep.has(id)) this.cacheFiles.delete(id);
    }
  }
 
  addCacheFiles(files: Map<string, JSZip.JSZipObject>) {
    for (const [id, file] of files) {
      if (this.cacheFiles.has(id)) {
        console.warn(`Skipping duplicate cache file in upload: ${id}`);
        continue;
      }
      this.cacheFiles.set(id, file);
    }
  }
 
  addFiles(files?: File[]) {
    if (!files) return;
    this.files ||= [];
    this.files.push(...files);
  }
 
  clearFiles() {
    Eif (this.filesEmpty) return;
    this.files = [] as any;
  }
 
  setEmbedFiles(files: File[] = []) {
    this.embedFiles = files;
  }
 
  foundRef(url: string) {
    for (const r of this.refs) {
      if (r.url === url) {
        r.exists = true;
      }
    }
  }
 
  foundExt(tag: string) {
    for (const e of this.exts) {
      if (e.tag === tag) {
        e.exists = true;
      }
    }
  }
 
  setRef(ref: Ref, url = ref.url) {
    for (let i = 0; i < this.refs.length; i++) {
      if (this.refs[i].url === url) {
        this.refs[i] = ref;
      }
    }
  }
 
  setExt(ext: Ext) {
    for (let i = 0; i < this.exts.length; i++) {
      if (this.exts[i].tag === ext.tag) {
        this.exts[i] = ext;
      }
    }
  }
 
  tagRefs(tags: string[]) {
    for (const ref of this.refs)
    for (const t of tags) {
      if (t.startsWith('-')) {
        const r = t.substring(1);
        if (ref.tags?.includes(r)) ref.tags.splice(ref.tags.indexOf(r), 1);
      } else if (!ref.tags?.includes(t)) {
        ref.tags ||= [];
        ref.tags.push(t)
      }
    }
  }
}