All files / app/store account.ts

53.2% Statements 83/156
36.79% Branches 39/106
66.66% Functions 24/36
57.46% Lines 77/134

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 303167x       167x 167x 167x 167x     167x   392x 392x 392x 392x 392x 392x 392x             392x           392x           392x             392x               392x           392x       392x       392x       392x       392x     392x   392x   392x 392x 392x       90x       16x       37x       1x                                                               73x             4x               6x       3x                       6x       5x 5x       5x       5x         7x 5x 5x     5x     5x     5x       6x 5x 6x       12x 12x 10x             4x       17x 17x 13x       1x 1x       27x 27x                         27x                         27x                           27x   1x       26x                   15x 15x 15x 15x       15x 15x 15x 15x 15x 15x       14x        
import { intersection, uniq } from 'lodash-es';
import { makeAutoObservable } from 'mobx';
import { Ext } from '../model/ext';
import { Roles, User } from '../model/user';
import { getMailbox } from '../mods/mailbox';
import { defaultSubs, UserConfig } from '../mods/user';
import { parseBookmarkParams, parseParams } from '../util/http';
import { braces, defaultOrigin, hasPrefix, localTag, prefix, setPublic, tagOrigin } from '../util/tag';
import { OriginStore } from './origin';
 
export class AccountStore {
 
  debug = false;
  tag = '';
  origin = '';
  access?: User = {} as User;
  ext?: Ext = {} as Ext;
  defaultConfig: UserConfig = {};
  ignoreNotifications: number[] = [];
 
  /**
   * Is admin.
   * Owns everything.
   * Limited to origin and sub origins.
   */
  admin = false;
  /**
   * Is mod.
   * Owns everything except plugins and templates.
   * Limited to origin and sub origins.
   */
  mod = false;
  /**
   * Is editor.
   * Allowed to toggle any public tag (except public and locked) to any Ref in view.
   * Limited to origin and sub origins.
   */
  editor = false;
  /**
   * Is user.
   * Allowed to post Refs.
   * May be given access to other tags.
   * Limited to origin and sub origins.
   */
  user = false;
  /**
   * Is viewer.
   * Allowed to edit user ext.
   * May be given read access to other tags.
   * May not be given write access to other tags.
   * Limited to origin and sub origins.
   */
  viewer = false;
  /**
   * Is banned.
   * No access, ban message shown instead.
   * Limited to origin and sub origins.
   */
  banned = false;
  /**
   * Unread inbox and alarms total count.
   */
  notifications = 0;
  /**
   * Unread alarms count.
   */
  alarmCount = 0;
  /**
   * Flag indicating the interceptor detected an unauthorized request.
   */
  authError = false;
  /**
   * Flag indicating an unrecoverable error loading app from PWA cache.
   */
  unrecoverable = false;
 
  constructor(
    private origins: OriginStore,
  ) {
    makeAutoObservable(this);
    // Initial observables may not be null for MobX
    this.access = undefined;
    this.ext = undefined;
    this.defaultConfig = {};
  }
 
  get signedIn() {
    return !!this.tag;
  }
 
  get root() {
    return !this.origin;
  }
 
  get localTag() {
    return localTag(this.tag);
  }
 
  get tagWithOrigin() {
    return localTag(this.tag) + (this.origin || '@');
  }
 
  get userTag() {
     if (hasPrefix(localTag(this.tag), 'user')) return this.localTag;
     return '';
  }
 
  get role() {
    if (!this.signedIn) return '';
    if (this.admin) return 'admin';
    if (this.mod) return 'mod';
    if (this.editor) return 'editor';
    if (this.user) return 'user';
    if (this.viewer) return 'viewer';
    return 'anon';
  }
 
  get roles(): Roles {
    return {
      debug: this.debug,
      tag: this.tag,
      admin: this.admin,
      mod: this.mod,
      editor: this.editor,
      user: this.user,
      viewer: this.viewer,
      banned: this.banned,
    };
  }
 
  get config(): UserConfig {
    return {
      ...(this.defaultConfig || {}),
      ...(this.ext?.config || {}),
    };
  }
 
  get subs(): string[] {
    return this.config.subscriptions || defaultSubs;
  }
 
  get userSubs(): string[] {
    return this.subs.filter(s => hasPrefix(s, 'user'));
  }
 
  get tagSubs(): string[] {
    return this.subs.filter(s => !hasPrefix(s, 'user'));
  }
 
  get bookmarks() {
    return this.config.bookmarks || [];
  }
 
  get bookmarkQueries() {
    return this.bookmarks.map(b => b.includes('?') ? b.substring(0, b.indexOf('?')) : b);
  }
 
  get bookmarkParams() {
    return this.bookmarks.map(b => parseBookmarkParams(b.includes('?') ? b.substring(b.indexOf('?')) : b));
  }
 
  get alarms(): string[] {
    return this.config.alarms || [];
  }
 
  get mailbox() {
    Iif (!this.signedIn) return undefined;
    return getMailbox(this.tag, this.origin) + (this.origin || '@');
  }
 
  get modmail() {
    return this.access?.readAccess?.filter(t => hasPrefix(t, 'plugin/inbox')).map(t => defaultOrigin(t, this.origin || '@'));
  }
 
  get outboxes() {
    return Array.from(this.origins.reverseLookup)
      .map(([remote, localAlias]) => setPublic(prefix('plugin/outbox', localAlias, this.localTag)) + remote);
  }
 
  get inboxQuery() {
    if (!this.signedIn) return '';
    let tags = [this.mailbox];
    Iif (this.origin) {
      tags.push(setPublic(prefix('plugin/outbox', this.origin, this.tagWithOrigin)) + this.origin);
    }
    Iif (this.modmail?.length) {
      tags.push(...this.modmail);
    }
    Iif (this.outboxes?.length) {
      tags.push(...this.outboxes);
    }
    return uniq(tags).join('|');
  }
 
  get notificationsQuery() {
    if (!this.signedIn) return undefined;
    const alarms = this.alarmsQuery ? '|' + this.alarmsQuery : '';
    return `!${this.tag}:!plugin/delete:` + braces(this.inboxQuery + alarms);
  }
 
  get alarmNotificationsQuery() {
    Iif (!this.signedIn) return undefined;
    if (!this.alarmsQuery) return '';
    return `!${this.tag}:!plugin/delete:` + braces(this.alarmsQuery);
  }
 
  /**
   * Unread inbox count, not including alarms.
   */
  get unreadCount() {
    return Math.max(0, this.notifications - this.alarmCount);
  }
 
  get alarmsQuery() {
    Iif (!this.signedIn) return undefined;
    if (!this.config.alarms?.length) return '';
    return this.config.alarms.join('|');
  }
 
  get subscriptionQuery() {
    Iif (!this.tagSubs.length) return 'none';
    return '!internal:(' + this.tagSubs.join('|') + ')';
  }
 
  querySymbol(...ops: ('/' | '{' | '}' | ',' | ':' | '|' | '(' | ')' | `!`)[]): string {
    return ops.map(op => {
      Iif (this.config.queryStyle === 'set') {
        switch (op) {
          case '/': return $localize`\u00A0/ `;
          case ':': return $localize` ∩ `;
          case '|': return $localize` ∪ `;
          case '(': return $localize` (\u00A0`;
          case ')': return $localize`\u00A0) `;
          case `!`: return $localize`' `;
          case `{`: return $localize` {\u00A0`;
          case `}`: return $localize`\u00A0} `;
          case `,`: return $localize`, `;
        }
      }
      Iif (this.config.queryStyle === 'logic') {
        switch (op) {
          case '/': return $localize`\u00A0/ `;
          case ':': return $localize` & `;
          case '|': return $localize` | `;
          case '(': return $localize` (\u00A0`;
          case ')': return $localize`\u00A0) `;
          case `!`: return $localize` ¬`;
          case `{`: return $localize` {\u00A0`;
          case `}`: return $localize`\u00A0} `;
          case `,`: return $localize` | `;
        }
      }
      Iif (this.config.queryStyle === 'code') {
        switch (op) {
          case '/': return $localize`\u00A0/ `;
          case ':': return $localize` & `;
          case '|': return $localize`, `;
          case '(': return $localize` (\u00A0`;
          case ')': return $localize`\u00A0) `;
          case `!`: return $localize` !`;
          case `{`: return $localize` {\u00A0`;
          case `}`: return $localize`\u00A0} `;
          case `,`: return $localize`, `;
        }
      }
 
      switch (op) {
        case '/': return $localize`\u00A0/ `;
        case ':': return $localize` : `;
        case '|': return $localize` | `;
        case '(': return $localize` (\u00A0`;
        case ')': return $localize`\u00A0) `;
        case `!`: return $localize` ❗`;
        case `{`: return $localize` {\u00A0`;
        case `}`: return $localize`\u00A0} `;
        case `,`: return $localize`, `;
      }
      return op;
    }).join(' ');
  }
 
  setRoles(roles: Roles) {
    this.debug = roles.debug;
    this.origin = tagOrigin(roles.tag);
    this.tag = roles.tag || '';
    Iif (this.tag.startsWith('@')) {
      // Not logged in, only local origin is set
      this.tag = '';
    }
    this.admin = roles.admin;
    this.mod = roles.mod;
    this.editor = roles.editor;
    this.user = roles.user;
    this.viewer = roles.viewer;
    this.banned = roles.banned;
  }
 
  defaultEditors(plugins: string[]) {
    Eif (!this.config?.editors) return [];
    return intersection(this.config.editors, plugins);
  }
}