Following Misskey - step 2

This commit is contained in:
stom79 2018-12-29 19:21:39 +01:00
parent 4c00ba823b
commit bcf0772ba3
2 changed files with 104 additions and 9 deletions

View File

@ -17,7 +17,6 @@ package fr.gouv.etalab.mastodon.client;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
@ -1048,17 +1047,28 @@ public class API {
*/
public APIResponse getMisskey(String instance, String max_id) {
JSONObject params = new JSONObject();
try {
params.put("file", false);
if( max_id != null)
params.put("untilId",max_id);
params.put("local",true);
params.put("poll",false);
params.put("renote",false);
params.put("reply",false);
} catch (JSONException e) {
e.printStackTrace();
}
HashMap<String, String> params = new HashMap<>();
if( max_id != null)
params.put("untilId",max_id);
try {
statuses = new ArrayList<>();
HttpsConnection httpsConnection = new HttpsConnection(context);
String response = httpsConnection.post(String.format("https://"+instance+"/api/notes/local-timeline", max_id), 60, params, null);
apiResponse.setSince_id(httpsConnection.getSince_id());
apiResponse.setMax_id(httpsConnection.getMax_id());
String response = httpsConnection.postMisskey("https://"+instance+"/api/notes", 60, params, null);
statuses = parseNotes(context, instance, new JSONArray(response));
if( statuses != null && statuses.size() > 0){
apiResponse.setSince_id(statuses.get(0).getId());
apiResponse.setMax_id(statuses.get(statuses.size() -1).getId());
}
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
} catch (NoSuchAlgorithmException e) {
@ -3285,7 +3295,7 @@ public class API {
if( emojisTag != null){
for(int j = 0 ; j < emojisTag.length() ; j++){
JSONObject emojisObj = emojisTag.getJSONObject(j);
Emojis emojis = parseEmojis(emojisObj);
Emojis emojis = parseMisskeyEmojis(emojisObj);
emojiList.add(emojis);
}
}
@ -3372,6 +3382,43 @@ public class API {
}
/**
* Parse emojis
* @param jsonArray JSONArray
* @return List<Emojis> of emojis
*/
private List<Emojis> parseMisskeyEmojis(JSONArray jsonArray){
List<Emojis> emojis = new ArrayList<>();
try {
int i = 0;
while (i < jsonArray.length() ) {
JSONObject resobj = jsonArray.getJSONObject(i);
Emojis emojis1 = parseMisskeyEmojis(resobj);
emojis.add(emojis1);
i++;
}
} catch (JSONException e) {
setDefaultError(e);
}
return emojis;
}
/**
* Parse json response for emoji
* @param resobj JSONObject
* @return Emojis
*/
private static Emojis parseMisskeyEmojis(JSONObject resobj){
Emojis emojis = new Emojis();
try {
emojis.setShortcode(resobj.get("name").toString());
emojis.setStatic_url(resobj.get("url").toString());
emojis.setUrl(resobj.get("url").toString());
}catch (Exception ignored){}
return emojis;
}
/**
* Parse Filters
@ -3608,7 +3655,6 @@ public class API {
@SuppressWarnings("InfiniteRecursion")
private static Account parseMisskeyAccountResponse(Context context, String instance, JSONObject resobj){
Log.v(Helper.TAG,"resobj= " + resobj);
Account account = new Account();
try {
account.setId(resobj.get("id").toString());

View File

@ -13,6 +13,7 @@ package fr.gouv.etalab.mastodon.client;
*
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
@ -21,7 +22,9 @@ import android.text.Html;
import android.text.SpannableString;
import com.google.common.io.ByteStreams;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
@ -49,7 +52,9 @@ import java.util.Map;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.net.ssl.HttpsURLConnection;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.activities.MediaActivity;
import fr.gouv.etalab.mastodon.activities.TootActivity;
@ -411,6 +416,50 @@ public class HttpsConnection {
}
public String postMisskey(String urlConnection, int timeout, JSONObject paramaters, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
URL url = new URL(urlConnection);
byte[] postDataBytes = paramaters.toString().getBytes("UTF-8");
if (proxy != null)
httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy);
else
httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT);
httpsURLConnection.setConnectTimeout(timeout * 1000);
httpsURLConnection.setDoOutput(true);
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory());
httpsURLConnection.setRequestMethod("POST");
if (token != null)
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
httpsURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
httpsURLConnection.getOutputStream().write(postDataBytes);
String response;
if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) {
getSinceMaxId();
response = converToString(httpsURLConnection.getInputStream());
} else {
String error = null;
if( httpsURLConnection.getErrorStream() != null) {
InputStream stream = httpsURLConnection.getErrorStream();
if (stream == null) {
stream = httpsURLConnection.getInputStream();
}
try (Scanner scanner = new Scanner(stream)) {
scanner.useDelimiter("\\Z");
error = scanner.next();
}catch (Exception e){e.printStackTrace();}
}
int responseCode = httpsURLConnection.getResponseCode();
throw new HttpsConnectionException(responseCode, error);
}
getSinceMaxId();
httpsURLConnection.getInputStream().close();
return response;
}
/***
* Download method which works for http and https connections
* @param downloadUrl String download url