~sirn/fanboi2

ref: 857635ed1af899dcfa2f993bd9e5bd4858212b24 fanboi2/assets/app/javascripts/models/post.ts -rw-r--r-- 2.0 KiB
857635edKridsada Thanabulpong services/post: add ident_v6 for ipv6 support 8 months 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
import { Model, IModelData } from "./base";
import { Task } from "./task";
import { CancellableToken } from "../utils/cancellable";
import { request, IRequestBody } from "../utils/request";

export class Post extends Model {
    type: string;
    id: number;
    body: string;
    bodyFormatted: string;
    bumped: boolean;
    createdAt: string;
    ident: string;
    name: string;
    number: number;
    topicId: number;
    path: string;

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

        this.type = data["type"];
        this.id = data["id"];
        this.body = data["body"];
        this.bodyFormatted = data["body_formatted"];
        this.bumped = data["bumped"];
        this.createdAt = data["created_at"];
        this.ident = data["ident"];
        this.name = data["name"];
        this.number = data["number"];
        this.topicId = data["topic_id"];
        this.path = data["path"];
    }

    static queryAll(
        topicId: number,
        query?: string,
        token?: CancellableToken,
    ): Promise<Post[]> {
        let entryPoint = `/api/1.0/topics/${topicId}/posts/`;

        if (query) {
            entryPoint = `${entryPoint}${query}/`;
        }

        return request("GET", entryPoint, {}, token).then(resp => {
            return JSON.parse(resp).map((data: Object): Post => {
                return new Post(data);
            });
        });
    }

    static createOne(
        topicId: number,
        params: IRequestBody,
        token?: CancellableToken,
    ): Promise<Post> {
        return request("POST", `/api/1.0/topics/${topicId}/posts/`, params, token).then(
            (resp: string) => {
                let data: IModelData = JSON.parse(resp);

                if (data["type"] == "task") {
                    return Task.waitFor(data["id"], token).then((task: Task) => {
                        return new Post(task.data);
                    });
                } else {
                    return new Post(data);
                }
            },
        );
    }
}