~sirn/fanboi2

ref: 5b482cfc1bfb0bb1e89715cb57c7bac864391768 fanboi2/assets/app/javascripts/models/board.ts -rw-r--r-- 1.8 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
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);
    }
}