All files / src/model SeamailResponse.ts

80.95% Statements 17/21
100% Branches 3/3
80% Functions 4/5
78.95% Lines 15/19

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    2x   2x   2x   2x   10x           10x 10x     3x 3x     3x 3x   9x 4x     10x                   10x                            
import { Moment } from 'moment';
 
import { SeamailThread } from './SeamailThread';
 
import { TwitarrError } from '../api/TwitarrError';
 
import { Util } from '../internal/Util';
 
export class SeamailResponse {
  public static fromRest(data: any) {
    Util.assertHasProperties(data, 'status');
 
    if (Util.isEmpty(data.seamail) && Util.isEmpty(data.seamail_meta) && Util.isEmpty(data.seamail_threads)) {
      throw new TwitarrError('At least one of seamail, seamail_meta, or seamail_threads is expected in the response!', undefined, undefined, data);
    }
 
    const ret = new SeamailResponse();
    Util.setDateProperties(ret, data, 'last_checked');
 
    if (!Util.isEmpty(data.seamail)) {
      ret.threads = [SeamailThread.fromRest(data.seamail)];
      ret.is_meta = false;
    }
    if (!Util.isEmpty(data.seamail_meta)) {
      ret.threads = data.seamail_meta.map(thread => SeamailThread.fromRest(thread));
      ret.is_meta = true;
    } else if (!Util.isEmpty(data.seamail_threads)) {
      ret.threads = data.seamail_threads.map(thread => SeamailThread.fromRest(thread));
      ret.is_meta = false;
    }
 
    return ret;
  }
 
  /** When the metadata was last checked. */
  public last_checked: Moment;
 
  /** The list of threads. */
  public threads: SeamailThread[];
 
  /** Whether this is a metadata response or a full response. */
  public is_meta: boolean = false;
 
  public toJSON() {
    const ret = {
      last_checked: this.last_checked,
    } as any;
    if (this.is_meta) {
      ret.seamail_meta = this.threads;
    } else {
      ret.seamail_threads = this.threads;
    }
    return ret;
  }
}