~sirn/fanboi2

ref: 5b482cfc1bfb0bb1e89715cb57c7bac864391768 fanboi2/assets/app/javascripts/models/topic.ts -rw-r--r-- 1.9 KiB
5b482cfcKridsada Thanabulpong 0.30.0 3 years ago
                                                                                
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
import {Model, IModelData} from './base';
import {Post} from './post';
import {CancellableToken} from '../utils/cancellable';
import {request} from '../utils/request';


enum Statuses {
    Open,
    Locked,
    Archived,
}


export class Topic extends Model {
    type: string;
    id: number;
    boardId: number;
    bumpedAt: string;
    createdAt: string;
    postCount: number;
    postedAt: string;
    status: Statuses;
    title: string;
    path: string;

    serialize(data: IModelData) {
        Model.assertType(data, 'topic');

        this.type = data['type'];
        this.id = data['id'];
        this.boardId = data['board_id'];
        this.bumpedAt = data['bumped_at'];
        this.createdAt = data['created_at'];
        this.postCount = data['post_count'];
        this.postedAt = data['posted_at'];
        this.title = data['title'];
        this.path = data['path'];

        switch (data['status']) {
            case "open" : this.status = Statuses.Open; break;
            case "locked" : this.status = Statuses.Locked; break;
            case "archived" : this.status = Statuses.Archived; break;
        }
    }

    static queryAll(
        slug: string,
        token?: CancellableToken
    ): Promise<Topic[]> {
        return request('GET', `/api/1.0/boards/${slug}/topics/`, {}, token).
            then((resp: string): Topic[] => {
                return JSON.parse(resp).map((data: Object) => {
                    return new Topic(data);
                });
            });
    }

    static queryId(
        id: number,
        token?: CancellableToken
    ): Promise<Topic> {
        return request('GET', `/api/1.0/topics/${id}/`, {}, token).
            then((resp: string) => {
                return new Topic(JSON.parse(resp));
            });
    }

    getPosts(
        query?: string,
        token?: CancellableToken
    ): Promise<Post[]> {
        return Post.queryAll(this.id, query, token);
    }
}