aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/ui/reader/actions/ReaderTagActions.java
blob: 6732c49fad221088453428150b4d16fb53daa372 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package org.wordpress.android.ui.reader.actions;

import com.android.volley.VolleyError;
import com.wordpress.rest.RestRequest;

import org.json.JSONArray;
import org.json.JSONObject;
import org.wordpress.android.WordPress;
import org.wordpress.android.datasets.ReaderTagTable;
import org.wordpress.android.models.ReaderTag;
import org.wordpress.android.models.ReaderTagList;
import org.wordpress.android.models.ReaderTagType;
import org.wordpress.android.ui.reader.ReaderConstants;
import org.wordpress.android.ui.reader.utils.ReaderUtils;
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.AppLog.T;
import org.wordpress.android.util.JSONUtils;
import org.wordpress.android.util.VolleyUtils;

public class ReaderTagActions {
    private ReaderTagActions() {
        throw new AssertionError();
    }

    public static boolean deleteTag(final ReaderTag tag,
                                    final ReaderActions.ActionListener actionListener) {
        if (tag == null) {
            ReaderActions.callActionListener(actionListener, false);
            return false;
        }

        final String tagNameForApi = ReaderUtils.sanitizeWithDashes(tag.getTagSlug());
        final String path = "read/tags/" + tagNameForApi + "/mine/delete";

        com.wordpress.rest.RestRequest.Listener listener = new RestRequest.Listener() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                AppLog.i(T.READER, "delete tag succeeded");
                ReaderActions.callActionListener(actionListener, true);
            }
        };

        RestRequest.ErrorListener errorListener = new RestRequest.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                // treat it as a success if the error says the user isn't following the deleted tag
                String error = VolleyUtils.errStringFromVolleyError(volleyError);
                if (error.equals("not_subscribed")) {
                    AppLog.w(T.READER, "delete tag succeeded with error " + error);
                    ReaderActions.callActionListener(actionListener, true);
                    return;
                }

                AppLog.w(T.READER, " delete tag failed");
                AppLog.e(T.READER, volleyError);

                // add back original tag
                ReaderTagTable.addOrUpdateTag(tag);

                ReaderActions.callActionListener(actionListener, false);
            }
        };

        ReaderTagTable.deleteTag(tag);
        WordPress.getRestClientUtilsV1_1().post(path, listener, errorListener);

        return true;
    }

    public static boolean addTag(final ReaderTag tag,
                                 final ReaderActions.ActionListener actionListener) {
        if (tag == null) {
            ReaderActions.callActionListener(actionListener, false);
            return false;
        }

        final String tagNameForApi = ReaderUtils.sanitizeWithDashes(tag.getTagSlug());
        final String path = "read/tags/" + tagNameForApi + "/mine/new";
        String endpoint = "/read/tags/" + tagNameForApi + "/posts";

        ReaderTag newTag = new ReaderTag(
                tag.getTagSlug(),
                tag.getTagDisplayName(),
                tag.getTagTitle(),
                endpoint,
                ReaderTagType.FOLLOWED);

        com.wordpress.rest.RestRequest.Listener listener = new RestRequest.Listener() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                AppLog.i(T.READER, "add tag  succeeded");
                // the response will contain the list of the user's followed tags
                ReaderTagList tags = parseFollowedTags(jsonObject);
                ReaderTagTable.replaceFollowedTags(tags);
                ReaderActions.callActionListener(actionListener, true);
            }
        };

        RestRequest.ErrorListener errorListener = new RestRequest.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                // treat is as a success if we're adding a tag and the error says the user is
                // already following it
                String error = VolleyUtils.errStringFromVolleyError(volleyError);
                if (error.equals("already_subscribed")) {
                    AppLog.w(T.READER, "add tag succeeded with error " + error);
                    ReaderActions.callActionListener(actionListener, true);
                    return;
                }

                AppLog.w(T.READER, "add tag failed");
                AppLog.e(T.READER, volleyError);

                // revert on failure
                ReaderTagTable.deleteTag(tag);

                ReaderActions.callActionListener(actionListener, false);
            }
        };

        ReaderTagTable.addOrUpdateTag(newTag);
        WordPress.getRestClientUtilsV1_1().post(path, listener, errorListener);

        return true;
    }

    /*
     * return the user's followed tags from the response to read/tags/{tag}/mine/new
     */
    /*
        {
        "added_tag": "84776",
        "subscribed": true,
        "tags": [
            {
                "display_name": "fitness",
                "ID": "5189",
                "slug": "fitness",
                "title": "Fitness",
                "URL": "https://public-api.wordpress.com/rest/v1.1/read/tags/fitness/posts"
            },
            ...
        }
     */
    private static ReaderTagList parseFollowedTags(JSONObject jsonObject) {
        if (jsonObject == null) {
            return null;
        }

        JSONArray jsonTags = jsonObject.optJSONArray(ReaderConstants.JSON_TAG_TAGS_ARRAY);
        if (jsonTags == null || jsonTags.length() == 0) {
            return null;
        }

        ReaderTagList tags = new ReaderTagList();
        for (int i=0; i < jsonTags.length(); i++) {
            JSONObject jsonThisTag = jsonTags.optJSONObject(i);
            String tagTitle = JSONUtils.getStringDecoded(jsonThisTag, ReaderConstants.JSON_TAG_TITLE);
            String tagDisplayName = JSONUtils.getStringDecoded(jsonThisTag, ReaderConstants.JSON_TAG_DISPLAY_NAME);
            String tagSlug = JSONUtils.getStringDecoded(jsonThisTag, ReaderConstants.JSON_TAG_SLUG);
            String endpoint = JSONUtils.getString(jsonThisTag, ReaderConstants.JSON_TAG_URL);
            tags.add(new ReaderTag(tagSlug, tagDisplayName, tagTitle, endpoint, ReaderTagType.FOLLOWED));
        }

        return tags;
    }

}