~sirn/fanboi2

ref: a68873a108d20a6a49a1b6caf374bfb2cf6a94fe fanboi2/assets/app/javascripts/models/board.ts -rw-r--r-- 1.8 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
import { Model, IModelData } from "./base";
import { Topic } from "./topic";
import { CancellableToken } from "../utils/cancellable";
import { request } from "../utils/request";

export class Board extends Model {
    type: string;
    id: number;
    agreements: string;
    description: string;
    settings: {
        postDelay: number;
        useIdent: boolean;
        name: string;
        maxPosts: number;
    };
    slug: string;
    title: string;
    path: string;

    protected serialize(data: IModelData) {
        Model.assertType(data, "board");

        this.type = data["type"];
        this.id = data["id"];
        this.agreements = data["agreements"];
        this.description = data["description"];
        this.slug = data["slug"];
        this.title = data["title"];
        this.path = data["path"];

        this.settings = {
            postDelay: data["settings"]["post_delay"],
            useIdent: data["settings"]["use_ident"],
            name: data["settings"]["name"],
            maxPosts: data["settings"]["max_posts"],
        };
    }

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

    static querySlug(slug: string, token?: CancellableToken): Promise<Board> {
        return request("GET", `/api/1.0/boards/${slug}/`, {}, token).then(
            (resp: string): Board => {
                return new Board(JSON.parse(resp));
            },
        );
    }

    getTopics(token?: CancellableToken): Promise<Topic[]> {
        return Topic.queryAll(this.slug, token);
    }
}