All files / src/dao StreamDAO.ts

77.55% Statements 38/49
100% Branches 0/0
91.67% Functions 11/12
77.55% Lines 38/49

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 1922x 2x 2x 2x   2x                                                       2x         10x   9x   1x     10x 10x               3x   2x     1x   3x 3x               1x                   1x 1x               1x                   1x 1x               1x                 1x 1x               1x           1x 1x               1x 1x               1x 1x               1x 1x                                 1x 1x               1x 1x        
import { AbstractDAO } from './AbstractDAO';
import { TwitarrHTTPOptions } from '../api/TwitarrHTTPOptions';
import { StreamResponse } from '../model/StreamResponse';
import { Util } from '../internal/Util';
import { Moment } from 'moment';
import { ReactionsSummary } from '../model/ReactionsSummary';
 
export interface IStreamOptions {
  /** limit posts to those written by the specified username */
  author?: string;
 
  /** limit posts to those containing the specified hashtag */
  hashtag?: string;
 
  /** whether to include the author in `mentions` queries */
  include_author?: boolean;
 
  /** return at most `limit` posts. */
  limit?: number;
 
  /** limit posts to those mentioning the specified username */
  mentions?: string;
 
  /** whether to return posts newer or older than `start` */
  newer_posts?: boolean;
 
  /** limit posts to those by starred (followed) users */
  starred?: boolean;
 
  /** the starting date to query */
  start?: Moment;
}
 
export class StreamDAO extends AbstractDAO {
  /**
   * Retrieve a collection of twarrts.
   */
  public async posts(streamOptions?: IStreamOptions) {
    const options = new TwitarrHTTPOptions().withParameter('app', 'plain');
    if (streamOptions) {
      Util.setProperties(options.parameters, streamOptions, 'author', 'hashtag', 'include_author', 'limit', 'mentions', 'newer_posts', 'starred');
      if (streamOptions.start) {
        options.parameters.start = '' + streamOptions.start.valueOf();
      }
    }
    return this.http.get('/api/v2/stream', options).then(result => {
      return StreamResponse.fromRest(result.data);
    });
  }
 
  /**
   * Retrieve a particular twarrt (including its children).
   */
  public async thread(id: string, limit?: number, page?: number) {
    const options = new TwitarrHTTPOptions().withParameter('app', 'plain');
    if (!Util.isEmpty(limit)) {
      options.parameters.limit = '' + limit;
    }
    if (!Util.isEmpty(page)) {
      options.parameters.page = '' + page;
    }
    return this.http.get('/api/v2/thread/' + id, options).then(result => {
      return StreamResponse.fromRest(result.data);
    });
  }
 
  /**
   * Retrieve a list of twarrts that mention a particular user.
   */
  public async mentions(username: string, limit?: number, page?: number, after?: Moment) {
    const options = new TwitarrHTTPOptions().withParameter('app', 'plain');
    if (!Util.isEmpty(limit)) {
      options.parameters.limit = '' + limit;
    }
    if (!Util.isEmpty(page)) {
      options.parameters.page = '' + page;
    }
    if (!Util.isEmpty(after)) {
      options.parameters.after = '' + after.valueOf();
    }
    return this.http.get('/api/v2/stream/m/' + username, options).then(result => {
      return StreamResponse.fromRest(result.data);
    });
  }
 
  /**
   * Retrieve a list of twarrts that contain a particular hashtag.
   */
  public async hashtag(hashtag: string, limit?: number, page?: number, after?: Moment) {
    const options = new TwitarrHTTPOptions().withParameter('app', 'plain');
    if (!Util.isEmpty(limit)) {
      options.parameters.limit = '' + limit;
    }
    if (!Util.isEmpty(page)) {
      options.parameters.page = '' + page;
    }
    if (!Util.isEmpty(after)) {
      options.parameters.after = '' + after.valueOf();
    }
    return this.http.get('/api/v2/stream/h/' + hashtag, options).then(result => {
      return StreamResponse.fromRest(result.data);
    });
  }
 
  /**
   * Send a new twarrt.
   */
  public async send(message: string, parent?: string, photo?: string) {
    const options = new TwitarrHTTPOptions().withData({
      text: message,
    });
    if (!Util.isEmpty(parent)) {
      options.data.parent = parent;
    }
    if (!Util.isEmpty(photo)) {
      options.data.photo = photo;
    }
    return this.http.post('/api/v2/stream', options).then(result => {
      return StreamResponse.fromRest(result.data);
    });
  }
 
  /**
   * Edit an existing twarrt.
   */
  public async updatePost(id: string, message: string, photo?: string) {
    const options = new TwitarrHTTPOptions().withData({
      text: message,
    });
    if (!Util.isEmpty(photo)) {
      options.data.photo = photo;
    }
    return this.http.post('/api/v2/tweet/' + id, options).then(result => {
      return StreamResponse.fromRest(result.data);
    });
  }
 
  /**
   * Delete a twarrt.
   */
  public async deletePost(id: string) {
    return this.http.httpDelete('/api/v2/tweet/' + id).then(() => {
      return true;
    });
  }
 
  /**
   * Lock a twarrt.
   */
  public async lockPost(id: string) {
    return this.http.post('/api/v2/tweet/' + id + '/locked/true').then(result => {
      return result.data.locked as boolean;
    });
  }
 
  /**
   * Unlock a twarrt.
   */
  public async unlockPost(id: string) {
    return this.http.post('/api/v2/tweet/' + id + '/locked/false').then(result => {
      return result.data.locked as boolean;
    });
  }
 
  /**
   * Retrieve the reactions to a twarrt.
   */
  public async reactions(id: string) {
    return this.http.get('/api/v2/tweet/' + id + '/react').then(result => {
      return ReactionsSummary.fromRest(result.data.reactions);
    });
  }
 
  /**
   * Add a reaction to a twarrt.
   */
  public async react(id: string, reaction: string) {
    return this.http.post('/api/v2/tweet/' + id + '/react/' + reaction).then(result => {
      return ReactionsSummary.fromRest(result.data.reactions);
    });
  }
 
  /**
   * Remove a reaction from a twarrt.
   */
  public async deleteReact(id: string, reaction: string) {
    return this.http.httpDelete('/api/v2/tweet/' + id + '/react/' + reaction).then(result => {
      return ReactionsSummary.fromRest(result.data.reactions);
    });
  }
}