All files / src/dao SeamailDAO.ts

100% Statements 34/34
100% Branches 0/0
100% Functions 8/8
100% Lines 34/34

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    2x 2x 2x 2x 2x   2x     1x   2x   1x   2x   2x   2x         3x   1x     1x   3x   3x   3x         4x   1x     1x     1x   4x 4x         1x 1x         1x 1x         1x 1x 1x 1x         1x 1x        
import { Moment } from 'moment';
 
import { AbstractDAO } from './AbstractDAO';
import { TwitarrHTTPOptions } from '../api/TwitarrHTTPOptions';
import { SeamailResponse } from '../model/SeamailResponse';
import { TwitarrError } from '../api/TwitarrError';
import { SeamailMessage } from '../model/SeamailMessage';
 
export class SeamailDAO extends AbstractDAO {
  public async get(id: string, skip_mark_read?: boolean) {
    if (!id) {
      throw new TwitarrError('id is required!');
    }
    const options = new TwitarrHTTPOptions().withParameter('app', 'plain');
    if (skip_mark_read) {
      options.parameters.skip_mark_read = 'true';
    }
    return this.http
      .get('/api/v2/seamail/' + id, options)
      .then(result => this.handleErrors(result))
      .then(data => {
        return SeamailResponse.fromRest(data);
      });
  }
 
  public async getMetadata(unread?: boolean, after?: Moment) {
    const options = new TwitarrHTTPOptions().withParameter('app', 'plain');
    if (unread) {
      options.parameters.unread = 'true';
    }
    if (after) {
      options.parameters.after = '' + after.valueOf();
    }
    return this.http
      .get('/api/v2/seamail', options)
      .then(result => this.handleErrors(result))
      .then(data => {
        return SeamailResponse.fromRest(data);
      });
  }
 
  public async getThreads(unread?: boolean, exclude_read_messages?: boolean, after?: Moment) {
    const options = new TwitarrHTTPOptions().withParameter('app', 'plain');
    if (unread) {
      options.parameters.unread = 'true';
    }
    if (exclude_read_messages) {
      options.parameters.exclude_read_messages = 'true';
    }
    if (after) {
      options.parameters.after = '' + after.valueOf();
    }
    return this.http.get('/api/v2/seamail_threads', options).then(result => {
      return SeamailResponse.fromRest(result.data);
    });
  }
 
  public async create(subject: string, message: string, ...users: string[]) {
    const options = new TwitarrHTTPOptions();
    options.data = {
      subject: subject,
      text: message,
      users: users,
    };
    return this.http.post('/api/v2/seamail', options).then(result => {
      return SeamailResponse.fromRest(result.data);
    });
  }
 
  public async post(id: string, message: string) {
    const options = new TwitarrHTTPOptions();
    options.data = { text: message };
    return this.http.post('/api/v2/seamail/' + id, options).then(result => {
      return SeamailMessage.fromRest(result.data.seamail_message);
    });
  }
 
  public async unreadCount() {
    return this.http.get('/api/v2/user/new_seamail').then(result => {
      return result.data.email_count;
    });
  }
}