aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/models/ReaderComment.java
blob: f0d92cf08f559d18b0d7a25a56292945df660adc (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package org.wordpress.android.models;

import android.text.TextUtils;

import org.json.JSONObject;
import org.wordpress.android.util.DateTimeUtils;
import org.wordpress.android.util.HtmlUtils;
import org.wordpress.android.util.JSONUtils;
import org.wordpress.android.util.StringUtils;

public class ReaderComment {
    public long commentId;
    public long blogId;
    public long postId;
    public long parentId;

    private String authorName;
    private String authorAvatar;
    private String authorUrl;
    private String status;
    private String text;

    private String published;
    public long timestamp;

    public long authorId;
    public long authorBlogId;

    public int numLikes;
    public boolean isLikedByCurrentUser;

    public int pageNumber;

    // not stored in db - denotes the indentation level when displaying this comment
    public transient int level = 0;

    public static ReaderComment fromJson(JSONObject json, long blogId) {
        if (json == null) {
            throw new IllegalArgumentException("null json comment");
        }

        ReaderComment comment = new ReaderComment();

        comment.blogId = blogId;
        comment.commentId = json.optLong("ID");
        comment.status = JSONUtils.getString(json, "status");

        // note that content may contain html, adapter needs to handle it
        comment.text = HtmlUtils.stripScript(JSONUtils.getString(json, "content"));

        comment.published = JSONUtils.getString(json, "date");
        comment.timestamp = DateTimeUtils.timestampFromIso8601(comment.published);

        JSONObject jsonPost = json.optJSONObject("post");
        if (jsonPost != null) {
            comment.postId = jsonPost.optLong("ID");
        }

        JSONObject jsonAuthor = json.optJSONObject("author");
        if (jsonAuthor!=null) {
            // author names may contain html entities (esp. pingbacks)
            comment.authorName = JSONUtils.getStringDecoded(jsonAuthor, "name");
            comment.authorAvatar = JSONUtils.getString(jsonAuthor, "avatar_URL");
            comment.authorUrl = JSONUtils.getString(jsonAuthor, "URL");
            comment.authorId = jsonAuthor.optLong("ID");
            comment.authorBlogId = jsonAuthor.optLong("site_ID");
        }

        JSONObject jsonParent = json.optJSONObject("parent");
        if (jsonParent != null) {
            comment.parentId = jsonParent.optLong("ID");
        }

        // like info is found under meta/data/likes when meta=likes query param is used
        JSONObject jsonLikes = JSONUtils.getJSONChild(json, "meta/data/likes");
        if (jsonLikes != null) {
            comment.numLikes = jsonLikes.optInt("found");
            comment.isLikedByCurrentUser = JSONUtils.getBool(jsonLikes, "i_like");
        }

        return comment;
    }

    public String getAuthorName() {
        return StringUtils.notNullStr(authorName);
    }

    public void setAuthorName(String authorName) {
        this.authorName = StringUtils.notNullStr(authorName);
    }

    public String getAuthorAvatar() {
        return StringUtils.notNullStr(authorAvatar);
    }
    public void setAuthorAvatar(String authorAvatar) {
        this.authorAvatar = StringUtils.notNullStr(authorAvatar);
    }

    public String getAuthorUrl() {
        return StringUtils.notNullStr(authorUrl);
    }
    public void setAuthorUrl(String authorUrl) {
        this.authorUrl = StringUtils.notNullStr(authorUrl);
    }

    public String getText() {
        return StringUtils.notNullStr(text);
    }
    public void setText(String text) {
        this.text = StringUtils.notNullStr(text);
    }

    public String getStatus() {
        return StringUtils.notNullStr(status);
    }
    public void setStatus(String status) {
        this.status = StringUtils.notNullStr(status);
    }

    public String getPublished() {
        return StringUtils.notNullStr(published);
    }
    public void setPublished(String published) {
        this.published = StringUtils.notNullStr(published);
    }

    public boolean hasAuthorUrl() {
        return !TextUtils.isEmpty(authorUrl);
    }

    public boolean hasAuthorBlogId() {
        return (authorBlogId != 0);
    }

    public boolean hasAuthorAvatar() {
        return !TextUtils.isEmpty(authorAvatar);
    }
}