All files / app/model user.ts

16.66% Statements 3/18
0% Branches 0/8
33.33% Functions 1/3
16.66% Lines 3/18

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 66117x 127x                             127x                                                                                                  
import { Schema } from 'jtd';
import { DateTime } from 'luxon';
import { Tag, TagSort } from './tag';
 
export interface User extends Tag {
  type?: 'user';
  role?: Role;
  readAccess?: string[];
  writeAccess?: string[];
  tagReadAccess?: string[];
  tagWriteAccess?: string[];
  pubKey?: string;
  authorizedKeys?: string;
  external?: any;
}
 
export const userSchema: Schema = {
  optionalProperties: {
    tag: { type: 'string' },
    readAccess: { elements: { type: 'string' } },
    writeAccess: { elements: { type: 'string' } },
    tagReadAccess: { elements: { type: 'string' } },
    tagWriteAccess: { elements: { type: 'string' } },
    pubKey: { type: 'string' },
    authorizedKeys: { type: 'string' },
    external: {},
  }
};
 
export type Role = 'ROLE_ADMIN' | 'ROLE_MOD' | 'ROLE_EDITOR' | 'ROLE_USER' | 'ROLE_VIEWER' | 'ROLE_ANONYMOUS' | 'ROLE_BANNED';
 
export interface Roles {
  debug: boolean;
  tag: string;
  admin: boolean;
  mod: boolean;
  editor: boolean;
  user: boolean;
  viewer: boolean;
  banned: boolean;
}
 
export function mapUser(obj: any): User {
  obj.type = 'user';
  obj.origin ||= '';
  obj.modifiedString = obj.modified;
  obj.modified &&= DateTime.fromISO(obj.modified);
  obj.pubKey &&= atob(obj.pubKey);
  return obj;
}
 
export function writeUser(user: User): User {
  const result = { ...user } as any;
  result.modified = result.modifiedString as any;
  result.pubKey = user.pubKey && btoa(user.pubKey);
  delete result.type;
  delete result.upload;
  delete result.exists;
  delete result.modifiedString;
  delete result.qualifiedTag;
  return result;
}
 
export type UserSort = TagSort |
  `external->${string}` | `external->${string},ASC` | `external->${string},DESC`;