All files / src/model ForumPost.ts

19.05% Statements 4/21
100% Branches 0/0
0% Functions 0/5
21.05% Lines 4/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 57 58 59 60 61 62 63 64    2x   2x 2x           2x                                                                                                        
import { Moment } from 'moment';
 
import { Util } from '../internal/Util';
 
import { PhotoDetails } from './PhotoDetails';
import { User } from './User';
 
/**
 * Represents a forum post.
 * @module ForumPost
 */
export class ForumPost {
  public static fromRest(data: any) {
    Util.assertHasProperties(data, 'id', 'forum_id', 'author', 'timestamp');
 
    const ret = new ForumPost();
    Util.setProperties(ret, data, 'id', 'forum_id', 'thread_locked', 'text');
    Util.setDateProperties(ret, data, 'timestamp');
    if (!Util.isEmpty(data.new)) {
      ret.is_new = data.new;
    }
    if (!Util.isEmpty(data.author)) {
      ret.author = User.fromRest(data.author);
    }
    if (!Util.isEmpty(data.photos)) {
      ret.photos = data.photos.map(photo => PhotoDetails.fromRest(photo));
    }
    return ret;
  }
 
  /** The unique post ID. */
  public id: string;
 
  /** The containing forum's ID */
  public forum_id: string;
 
  /** The author of the post */
  public author: User;
 
  /** Whether the thread is locked */
  public thread_locked: boolean;
 
  /** The contents of the post */
  public text: string;
 
  /** The time the post was made */
  public timestamp: Moment;
 
  /** Photos in the post */
  public photos: PhotoDetails[] = [];
 
  /** Whether the post is unread/new */
  public is_new: boolean;
 
  public toJSON() {
    const ret = {} as any;
    Util.setProperties(ret, this, 'id', 'forum_id', 'thread_locked', 'text');
    ret.author = this.author.toJSON();
    ret.timestamp = this.timestamp.valueOf();
    ret.photos = this.photos.map(photo => photo.toJSON());
    return ret;
  }
}