All files / src/model SearchResponse.ts

76.47% Statements 26/34
100% Branches 2/2
62.5% Functions 5/8
79.31% Lines 23/29

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 942x   2x   2x 2x 2x 2x 2x                       2x   1x           1x   1x     1x 1x     1x 1x     1x 1x                     1x           1x           1x           1x           1x           1x                    
import { TwitarrError } from '../api/TwitarrError';
 
import { Util } from '../internal/Util';
 
import { Event } from './Event';
import { ForumThread } from './ForumThread';
import { SeamailThread } from './SeamailThread';
import { StreamPost } from './StreamPost';
import { User } from './User';
 
interface ISearchStatus<T> {
  count: number;
  matches: T[];
  more: boolean;
}
 
/**
 * Represents a search response.
 * @module SearchResponse
 */
export class SearchResponse {
  public static fromRest(data: any) {
    Util.assertHasProperties(data, 'query');
 
    if (Util.isEmpty(data.users, data.seamails, data.tweets, data.forums, data.events)) {
      throw new TwitarrError('At least one of users, seamails, tweets, forums, or events is expected on the response!', undefined, undefined, undefined, data);
    }
 
    const ret = new SearchResponse();
    if (data.query && data.query.text) {
      ret.query = data.query.text;
    }
    if (data.users) {
      Util.setProperties(ret.users, data.users, 'count', 'more');
      ret.users.matches = data.users.matches.map((user: any) => User.fromRest(user));
    }
    if (data.seamails) {
      Util.setProperties(ret.seamails, data.seamails, 'count', 'more');
      ret.seamails.matches = data.seamails.matches.map((thread: any) => SeamailThread.fromRest(thread));
    }
    if (data.tweets) {
      Util.setProperties(ret.streamPosts, data.streamPosts, 'count', 'more');
      ret.streamPosts.matches = data.tweets.matches.map((tweet: any) => StreamPost.fromRest(tweet));
    }
    if (data.forums) {
      Util.setProperties(ret.forumThreads, data.forumThreads, 'count', 'more');
      ret.forumThreads.matches = data.forums.matches.map(thread => ForumThread.fromRest(thread));
    }
    if (data.events) {
      Util.setProperties(ret.events, data.events, 'count', 'more');
      ret.events.matches = data.events.matches.map(event => Event.fromRest(event));
    }
 
    return ret;
  }
 
  /** The query that was passed to the database. */
  public query: string;
 
  public users: ISearchStatus<User> = {
    count: 0,
    matches: [],
    more: false,
  };
 
  public seamails: ISearchStatus<SeamailThread> = {
    count: 0,
    matches: [],
    more: false,
  };
 
  public streamPosts: ISearchStatus<StreamPost> = {
    count: 0,
    matches: [],
    more: false,
  };
 
  public forumThreads: ISearchStatus<ForumThread> = {
    count: 0,
    matches: [],
    more: false,
  };
 
  public events: ISearchStatus<Event> = {
    count: 0,
    matches: [],
    more: false,
  };
 
  public toJSON() {
    return this;
  }
}