Prepares filters

This commit is contained in:
stom79 2018-09-05 17:52:13 +02:00
parent 1d9e27b4b4
commit 0ae9bee19d
3 changed files with 269 additions and 0 deletions

View File

@ -1409,6 +1409,134 @@ public class API {
}
/**
* Get filters for the user
* @return APIResponse
*/
public APIResponse getFilters(){
List<fr.gouv.etalab.mastodon.client.Entities.Filters> filters = new ArrayList<>();
try {
String response = new HttpsConnection(context).get(getAbsoluteUrl("/filters"), 60, null, prefKeyOauthTokenT);
filters = parseFilters(new JSONArray(response));
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
apiResponse.setFilters(filters);
return apiResponse;
}
/**
* Get a Filter by its id
* @return APIResponse
*/
@SuppressWarnings("unused")
public APIResponse getFilters(String filterId){
List<fr.gouv.etalab.mastodon.client.Entities.Filters> filters = new ArrayList<>();
fr.gouv.etalab.mastodon.client.Entities.Filters filter;
try {
String response = new HttpsConnection(context).get(getAbsoluteUrl(String.format("/filters/%s", filterId)), 60, null, prefKeyOauthTokenT);
filter = parseFilter(new JSONObject(response));
filters.add(filter);
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
apiResponse.setFilters(filters);
return apiResponse;
}
/**
* Create a filter
* @param filter Filter
* @return APIResponse
*/
public APIResponse addFilters(Filters filter){
HashMap<String, String> params = new HashMap<>();
params.put("custom_filter[phrase]", filter.getPhrase());
for(String context: filter.getContext())
params.put("custom_filter[context]", context);
params.put("custom_filter[irreversible]", String.valueOf(filter.isIrreversible()));
params.put("custom_filter[whole_word]", String.valueOf(filter.isWhole_word()));
params.put("custom_filter[expires_in]", String.valueOf(filter.getExpires_in()));
try {
new HttpsConnection(context).post(getAbsoluteUrl("/filters"), 60, params, prefKeyOauthTokenT);
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
return apiResponse;
}
/**
* Delete a filter
* @param filter Filter
* @return APIResponse
*/
public APIResponse deleteFilters(Filters filter){
try {
new HttpsConnection(context).delete(getAbsoluteUrl(String.format("/filters/%s", filter.getId())), 60, null, prefKeyOauthTokenT);
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
return apiResponse;
}
/**
* Delete a filter
* @param filter Filter
* @return APIResponse
*/
public APIResponse updateFilters(Filters filter){
HashMap<String, String> params = new HashMap<>();
params.put("custom_filter[phrase]", filter.getPhrase());
for(String context: filter.getContext())
params.put("custom_filter[context]", context);
params.put("custom_filter[irreversible]", String.valueOf(filter.isIrreversible()));
params.put("custom_filter[whole_word]", String.valueOf(filter.isWhole_word()));
params.put("custom_filter[expires_in]", String.valueOf(filter.getExpires_in()));
try {
new HttpsConnection(context).put(getAbsoluteUrl(String.format("/filters/%s", filter.getId())), 60, params, prefKeyOauthTokenT);
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
return apiResponse;
}
/**
* Get lists for the user
@ -2054,6 +2182,57 @@ public class API {
}
/**
* Parse Filters
* @param jsonArray JSONArray
* @return List<Filters> of filters
*/
private List<fr.gouv.etalab.mastodon.client.Entities.Filters> parseFilters(JSONArray jsonArray){
List<fr.gouv.etalab.mastodon.client.Entities.Filters> filters = new ArrayList<>();
try {
int i = 0;
while (i < jsonArray.length() ) {
JSONObject resobj = jsonArray.getJSONObject(i);
fr.gouv.etalab.mastodon.client.Entities.Filters filter = parseFilter(resobj);
filters.add(filter);
i++;
}
} catch (JSONException e) {
setDefaultError(e);
}
return filters;
}
/**
* Parse json response for filter
* @param resobj JSONObject
* @return Filter
*/
private static fr.gouv.etalab.mastodon.client.Entities.Filters parseFilter(JSONObject resobj){
fr.gouv.etalab.mastodon.client.Entities.Filters filter = new fr.gouv.etalab.mastodon.client.Entities.Filters();
try {
filter.setPhrase(resobj.get("phrase").toString());
filter.setExpires_in(Integer.parseInt(resobj.get("expires_in").toString()));
filter.setWhole_word(Boolean.parseBoolean(resobj.get("whole_word").toString()));
filter.setIrreversible(Boolean.parseBoolean(resobj.get("irreversible").toString()));
String contextString = resobj.get("context").toString();
contextString = contextString.replace("[","");
contextString = contextString.replace("]","");
if( contextString != null) {
String[] context = contextString.split(",");
if( contextString.length() > 0 ){
ArrayList<String> finalContext = new ArrayList<>();
for(String c: context)
finalContext.add(c.trim());
filter.setContext(finalContext);
}
}
}catch (Exception ignored){}
return filter;
}
/**
* Parse Lists
* @param jsonArray JSONArray

View File

@ -30,6 +30,7 @@ public class APIResponse {
private List<Context> contexts = null;
private List<Notification> notifications = null;
private List<Relationship> relationships = null;
private List<Filters> filters = null;
private List<fr.gouv.etalab.mastodon.client.Entities.List> lists = null;
private List<Emojis> emojis = null;
private fr.gouv.etalab.mastodon.client.Entities.Error error = null;
@ -123,4 +124,12 @@ public class APIResponse {
public void setLists(List<fr.gouv.etalab.mastodon.client.Entities.List> lists) {
this.lists = lists;
}
public List<Filters> getFilters() {
return filters;
}
public void setFilters(List<Filters> filters) {
this.filters = filters;
}
}

View File

@ -0,0 +1,81 @@
package fr.gouv.etalab.mastodon.client.Entities;
/* Copyright 2018 Thomas Schneider
*
* This file is a part of Mastalab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
* see <http://www.gnu.org/licenses>. */
import java.util.ArrayList;
/**
* Created by Thomas on 05/09/2018.
* Manage filters
*/
public class Filters {
private String id;
private String phrase;
private ArrayList<String> context;
private boolean irreversible;
private boolean whole_word;
private int expires_in;
public String getPhrase() {
return phrase;
}
public void setPhrase(String phrase) {
this.phrase = phrase;
}
public ArrayList<String> getContext() {
return context;
}
public void setContext(ArrayList<String> context) {
this.context = context;
}
public boolean isIrreversible() {
return irreversible;
}
public void setIrreversible(boolean irreversible) {
this.irreversible = irreversible;
}
public boolean isWhole_word() {
return whole_word;
}
public void setWhole_word(boolean whole_word) {
this.whole_word = whole_word;
}
public int getExpires_in() {
return expires_in;
}
public void setExpires_in(int expires_in) {
this.expires_in = expires_in;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}