summaryrefslogtreecommitdiffstats
path: root/app/src/main/java/fr/gouv/etalab/mastodon/client/PeertubeAPI.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/fr/gouv/etalab/mastodon/client/PeertubeAPI.java')
-rw-r--r--app/src/main/java/fr/gouv/etalab/mastodon/client/PeertubeAPI.java234
1 files changed, 231 insertions, 3 deletions
diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/PeertubeAPI.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/PeertubeAPI.java
index 9e3e9b44a..5d95723b9 100644
--- a/app/src/main/java/fr/gouv/etalab/mastodon/client/PeertubeAPI.java
+++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/PeertubeAPI.java
@@ -48,7 +48,12 @@ import fr.gouv.etalab.mastodon.client.Entities.Filters;
import fr.gouv.etalab.mastodon.client.Entities.HowToVideo;
import fr.gouv.etalab.mastodon.client.Entities.Instance;
import fr.gouv.etalab.mastodon.client.Entities.Peertube;
+import fr.gouv.etalab.mastodon.client.Entities.PeertubeAccountNotification;
+import fr.gouv.etalab.mastodon.client.Entities.PeertubeActorFollow;
+import fr.gouv.etalab.mastodon.client.Entities.PeertubeComment;
import fr.gouv.etalab.mastodon.client.Entities.PeertubeInformation;
+import fr.gouv.etalab.mastodon.client.Entities.PeertubeNotification;
+import fr.gouv.etalab.mastodon.client.Entities.PeertubeVideoNotification;
import fr.gouv.etalab.mastodon.client.Entities.Relationship;
import fr.gouv.etalab.mastodon.client.Entities.Results;
import fr.gouv.etalab.mastodon.client.Entities.Status;
@@ -93,7 +98,13 @@ public class PeertubeAPI {
else {
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
- Account account = new AccountDAO(context, db).getAccountByID(userId);
+ String instance = sharedpreferences.getString(Helper.PREF_INSTANCE, Helper.getLiveInstance(context));
+ Account account = new AccountDAO(context, db).getUniqAccount(userId, instance);
+ if( account == null) {
+ apiResponse = new APIResponse();
+ APIError = new Error();
+ return;
+ }
this.instance = account.getInstance().trim();
}
this.prefKeyOauthTokenT = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
@@ -521,6 +532,40 @@ public class PeertubeAPI {
peertubes = parsePeertube(jsonArray);
} catch (HttpsConnection.HttpsConnectionException e) {
+ if( e.getStatusCode() == 401){ //Avoid the issue with the refresh token
+ SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
+ SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
+ String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
+ String instance = sharedpreferences.getString(Helper.PREF_INSTANCE, Helper.getLiveInstance(context));
+ Account account = new AccountDAO(context, db).getUniqAccount(userId, instance);
+ HashMap<String, String> values = new PeertubeAPI(context).refreshToken(account.getClient_id(), account.getClient_secret(), account.getRefresh_token());
+ if( values != null) {
+ String newtoken = values.get("access_token");
+ String refresh_token = values.get("refresh_token");
+ if (newtoken != null)
+ account.setToken(newtoken);
+ if (refresh_token != null)
+ account.setRefresh_token(refresh_token);
+ new AccountDAO(context, db).updateAccount(account);
+ }
+ HttpsConnection httpsConnection = new HttpsConnection(context);
+ String response;
+ try {
+ response = httpsConnection.get(getAbsoluteUrl("/users/me/videos"), 60, params, prefKeyOauthTokenT);
+ JSONArray jsonArray = new JSONObject(response).getJSONArray("data");
+ peertubes = parsePeertube(jsonArray);
+ } catch (IOException e1) {
+ e1.printStackTrace();
+ } catch (NoSuchAlgorithmException e1) {
+ e1.printStackTrace();
+ } catch (KeyManagementException e1) {
+ e1.printStackTrace();
+ } catch (HttpsConnection.HttpsConnectionException e1) {
+ e1.printStackTrace();
+ } catch (JSONException e1) {
+ e1.printStackTrace();
+ }
+ }
setError(e.getStatusCode(), e);
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
@@ -559,10 +604,8 @@ public class PeertubeAPI {
params.put("count", String.valueOf(limit));
List<Peertube> peertubes = new ArrayList<>();
try {
-
HttpsConnection httpsConnection = new HttpsConnection(context);
String response = httpsConnection.get(getAbsoluteUrl(String.format("/accounts/%s/videos", acct)), 60, params, prefKeyOauthTokenT);
-
JSONArray jsonArray = new JSONObject(response).getJSONArray("data");
peertubes = parsePeertube(jsonArray);
@@ -582,6 +625,67 @@ public class PeertubeAPI {
return apiResponse;
}
+ /**
+ * Retrieves Peertube notifications for the account *synchronously*
+ *
+ * @param max_id String id max
+ * @return APIResponse
+ */
+ public APIResponse getNotifications(String max_id){
+ return getNotifications(max_id, null, 20);
+ }
+
+ /**
+ * Retrieves Peertube notifications since id for the account *synchronously*
+ *
+ * @param since_id String id since
+ * @return APIResponse
+ */
+ public APIResponse getNotificationsSince(String since_id){
+ return getNotifications(null, since_id, 20);
+ }
+
+ /**
+ * Retrieves Peertube notifications for the account *synchronously*
+ *
+ * @param max_id String id max
+ * @param since_id String since the id
+ * @param limit int limit - max value 40
+ * @return APIResponse
+ */
+ @SuppressWarnings("SameParameterValue")
+ private APIResponse getNotifications(String max_id, String since_id, int limit) {
+
+ HashMap<String, String> params = new HashMap<>();
+ if (max_id != null)
+ params.put("start", max_id);
+ if (since_id != null)
+ params.put("since_id", since_id);
+ if (0 < limit || limit > 40)
+ limit = 40;
+ params.put("count", String.valueOf(limit));
+ List<PeertubeNotification> peertubeNotifications = new ArrayList<>();
+ try {
+ HttpsConnection httpsConnection = new HttpsConnection(context);
+ String response = httpsConnection.get(getAbsoluteUrl("/users/me/notifications"), 60, params, prefKeyOauthTokenT);
+ JSONArray jsonArray = new JSONObject(response).getJSONArray("data");
+ peertubeNotifications = parsePeertubeNotifications(jsonArray);
+ } catch (HttpsConnection.HttpsConnectionException e) {
+ setError(e.getStatusCode(), e);
+ e.printStackTrace();
+ } catch (NoSuchAlgorithmException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (KeyManagementException e) {
+ e.printStackTrace();
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ apiResponse.setPeertubeNotifications(peertubeNotifications);
+ return apiResponse;
+ }
+
/**
* Retrieves videos channel for the account *synchronously*
@@ -1387,6 +1491,128 @@ public class PeertubeAPI {
}
/**
+ * Parse json response for peertube notifications
+ * @param jsonArray JSONArray
+ * @return List<PeertubeNotification>
+ */
+ private List<PeertubeNotification> parsePeertubeNotifications(JSONArray jsonArray){
+ List<PeertubeNotification> peertubeNotifications = new ArrayList<>();
+ try {
+ int i = 0;
+ while (i < jsonArray.length() ){
+ JSONObject resobj = jsonArray.getJSONObject(i);
+ PeertubeNotification peertubeNotification = parsePeertubeNotifications(context, resobj);
+ i++;
+ peertubeNotifications.add(peertubeNotification);
+ }
+ } catch (JSONException e) {
+ setDefaultError(e);
+ }
+ return peertubeNotifications;
+ }
+
+ /**
+ * Parse json response for unique how to
+ * @param resobj JSONObject
+ * @return Peertube
+ */
+ private static PeertubeNotification parsePeertubeNotifications(Context context,JSONObject resobj){
+ PeertubeNotification peertubeNotification = new PeertubeNotification();
+ try {
+ peertubeNotification.setId(resobj.get("id").toString());
+ peertubeNotification.setType(resobj.getInt("type"));
+ peertubeNotification.setUpdatedAt(Helper.mstStringToDate(context, resobj.get("updatedAt").toString()));
+ peertubeNotification.setCreatedAt(Helper.mstStringToDate(context, resobj.get("createdAt").toString()));
+ peertubeNotification.setRead(resobj.getBoolean("read"));
+
+ if( resobj.has("comment")){
+ PeertubeComment peertubeComment = new PeertubeComment();
+ JSONObject comment = resobj.getJSONObject("comment");
+ if( comment.has("account")){
+ JSONObject account = comment.getJSONObject("account");
+ PeertubeAccountNotification peertubeAccountNotification = new PeertubeAccountNotification();
+ peertubeAccountNotification.setDisplayName(account.get("displayName").toString());
+ peertubeAccountNotification.setName(account.get("name").toString());
+ peertubeAccountNotification.setId(account.get("id").toString());
+ peertubeAccountNotification.setAvatar(account.getJSONObject("avatar").get("path").toString());
+ peertubeComment.setPeertubeAccountNotification(peertubeAccountNotification);
+ }
+ if( comment.has("video")){
+ JSONObject video = comment.getJSONObject("video");
+ PeertubeVideoNotification peertubeVideoNotification = new PeertubeVideoNotification();
+ peertubeVideoNotification.setUuid(video.get("uuid").toString());
+ peertubeVideoNotification.setName(video.get("name").toString());
+ peertubeVideoNotification.setId(video.get("id").toString());
+ peertubeComment.setPeertubeVideoNotification(peertubeVideoNotification);
+ }
+ peertubeComment.setId(comment.get("id").toString());
+ peertubeComment.setThreadId(comment.get("threadId").toString());
+ peertubeNotification.setPeertubeComment(peertubeComment);
+ }
+
+ if( resobj.has("video")){
+ PeertubeVideoNotification peertubeVideoNotification = new PeertubeVideoNotification();
+ JSONObject video = resobj.getJSONObject("video");
+ peertubeVideoNotification.setUuid(video.get("uuid").toString());
+ peertubeVideoNotification.setName(video.get("name").toString());
+ peertubeVideoNotification.setId(video.get("id").toString());
+ if( video.has("channel")){
+ PeertubeAccountNotification peertubeAccountNotification = new PeertubeAccountNotification();
+ JSONObject channel = video.getJSONObject("channel");
+ peertubeAccountNotification.setDisplayName(channel.get("displayName").toString());
+ peertubeAccountNotification.setName(channel.get("name").toString());
+ peertubeAccountNotification.setId(channel.get("id").toString());
+ if( channel.has("avatar")){
+ peertubeAccountNotification.setAvatar(channel.getJSONObject("avatar").get("path").toString());
+ }
+ peertubeVideoNotification.setPeertubeAccountNotification(peertubeAccountNotification);
+ }
+ peertubeNotification.setPeertubeVideoNotification(peertubeVideoNotification);
+ }
+
+ if( resobj.has("actorFollow")){
+ PeertubeActorFollow peertubeActorFollow = new PeertubeActorFollow();
+ JSONObject actorFollow = resobj.getJSONObject("actorFollow");
+
+ JSONObject follower = actorFollow.getJSONObject("follower");
+ JSONObject following = actorFollow.getJSONObject("following");
+
+ PeertubeAccountNotification peertubeAccountNotification = new PeertubeAccountNotification();
+ peertubeAccountNotification.setDisplayName(follower.get("displayName").toString());
+ peertubeAccountNotification.setName(follower.get("name").toString());
+ peertubeAccountNotification.setId(follower.get("id").toString());
+ if( follower.has("avatar")){
+ peertubeAccountNotification.setAvatar(follower.getJSONObject("avatar").get("path").toString());
+ }
+ peertubeActorFollow.setFollower(peertubeAccountNotification);
+
+ PeertubeAccountNotification peertubeAccounFollowingNotification = new PeertubeAccountNotification();
+ peertubeAccounFollowingNotification.setDisplayName(following.get("displayName").toString());
+ peertubeAccounFollowingNotification.setName(following.get("name").toString());
+ try {
+ peertubeAccounFollowingNotification.setId(following.get("id").toString());
+ }catch (Exception ignored){}
+ if( following.has("avatar")){
+ peertubeAccounFollowingNotification.setAvatar(following.getJSONObject("avatar").get("path").toString());
+ }
+ peertubeActorFollow.setFollowing(peertubeAccounFollowingNotification);
+ peertubeActorFollow.setId(actorFollow.get("id").toString());
+ peertubeNotification.setPeertubeActorFollow(peertubeActorFollow);
+
+ }
+
+ } catch (JSONException e) {
+ e.printStackTrace();
+ } catch (ParseException e) {
+ e.printStackTrace();
+ }
+
+ return peertubeNotification;
+ }
+
+
+
+ /**
* Parse json response for several howto
* @param jsonArray JSONArray
* @return List<Peertube>
@@ -1409,6 +1635,8 @@ public class PeertubeAPI {
return peertubes;
}
+
+
/**
* Parse json response for unique how to
* @param resobj JSONObject