~sirn/fanboi2

ref: a68873a108d20a6a49a1b6caf374bfb2cf6a94fe fanboi2/assets/app/javascripts/models/topic.ts -rw-r--r-- 2.0 KiB
a68873a1Kridsada Thanabulpong Coding style cleanups and setup pre-commit hooks (#42) 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
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);
    }
}