diff --git a/app/build.gradle b/app/build.gradle index 59d563152..997a85f7a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -38,7 +38,7 @@ android { } productFlavors { fdroid { - applicationId "fr.gouv.etalab.mastodon.updev" + applicationId "fr.gouv.etalab.mastodon" buildConfigField "boolean", "DONATIONS", "true" buildConfigField "boolean", "lite", "false" resValue "string", "app_name", "Fedilab" diff --git a/app/src/main/java/app/fedilab/android/asynctasks/PostSubscriptionAsyncTask.java b/app/src/main/java/app/fedilab/android/asynctasks/PostSubscriptionAsyncTask.java index 9d6461842..1e183cc25 100644 --- a/app/src/main/java/app/fedilab/android/asynctasks/PostSubscriptionAsyncTask.java +++ b/app/src/main/java/app/fedilab/android/asynctasks/PostSubscriptionAsyncTask.java @@ -47,7 +47,12 @@ public class PostSubscriptionAsyncTask { protected void doInBackground() { new Thread(() -> { - apiResponse = new API(contextReference.get(), account.getInstance(), account.getToken()).pushSubscription(endpoint, account); + apiResponse = new API(contextReference.get(), account.getInstance(), account.getToken()).getPushSubscription(); + if (apiResponse == null || apiResponse.getPushSubscription() == null) { + apiResponse = new API(contextReference.get(), account.getInstance(), account.getToken()).pushSubscription(endpoint, account); + } else if (apiResponse != null && endpoint.compareTo(apiResponse.getPushSubscription().getEndpoint()) != 0) { + apiResponse = new API(contextReference.get(), account.getInstance(), account.getToken()).updatePushSubscription(endpoint); + } List accountList = new ArrayList<>(); accountList.add(account); apiResponse.setAccounts(accountList); diff --git a/app/src/main/java/app/fedilab/android/client/API.java b/app/src/main/java/app/fedilab/android/client/API.java index 0b872ffc7..d99111eb3 100644 --- a/app/src/main/java/app/fedilab/android/client/API.java +++ b/app/src/main/java/app/fedilab/android/client/API.java @@ -5781,6 +5781,64 @@ public class API { return apiResponse; } + /** + * Get subscribed push notifications + * + * @return APIResponse + */ + public APIResponse getPushSubscription() { + PushSubscription pushSubscription = null; + try { + String response = new HttpsConnection(context, this.instance).get(getAbsoluteUrl("/push/subscription"), 10, null, prefKeyOauthTokenT); + pushSubscription = parsePushNotifications(new JSONObject(response)); + Log.v(Helper.TAG, "response2: " + response); + } catch (HttpsConnection.HttpsConnectionException e) { + setError(e.getStatusCode(), e); + e.printStackTrace(); + } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { + e.printStackTrace(); + } + apiResponse.setPushSubscription(pushSubscription); + return apiResponse; + } + + + /** + * Update subscribed push notifications + * + * @return APIResponse + */ + public APIResponse updatePushSubscription(String endpoint) { + PushSubscription pushSubscription = new PushSubscription(); + final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + boolean notif_follow = sharedpreferences.getBoolean(Helper.SET_NOTIF_FOLLOW, true); + boolean notif_add = sharedpreferences.getBoolean(Helper.SET_NOTIF_ADD, true); + boolean notif_mention = sharedpreferences.getBoolean(Helper.SET_NOTIF_MENTION, true); + boolean notif_share = sharedpreferences.getBoolean(Helper.SET_NOTIF_SHARE, true); + boolean notif_status = sharedpreferences.getBoolean(Helper.SET_NOTIF_STATUS, true); + boolean notif_poll = sharedpreferences.getBoolean(Helper.SET_NOTIF_POLL, true); + + HashMap params = new HashMap<>(); + params.put("data[alerts][follow]", String.valueOf(notif_follow)); + params.put("data[alerts][mention]", String.valueOf(notif_mention)); + params.put("data[alerts][favourite]", String.valueOf(notif_add)); + params.put("data[alerts][reblog]", String.valueOf(notif_share)); + params.put("data[alerts][poll]", String.valueOf(notif_poll)); + params.put("subscription[endpoint]", endpoint); + try { + String response = new HttpsConnection(context, this.instance).put(getAbsoluteUrl("/push/subscription"), 10, params, prefKeyOauthTokenT); + pushSubscription = parsePushNotifications(new JSONObject(response)); + Log.v(Helper.TAG, "response3: " + response); + } catch (HttpsConnection.HttpsConnectionException e) { + setError(e.getStatusCode(), e); + e.printStackTrace(); + } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { + e.printStackTrace(); + } + apiResponse.setPushSubscription(pushSubscription); + return apiResponse; + } + /** * Subscribe to push notifications * @@ -5813,7 +5871,7 @@ public class API { params.put("subscription[keys][p256dh]", pubKey); params.put("subscription[keys][auth]", auth); try { - String response = new HttpsConnection(context, this.instance).post(getAbsoluteUrl("/push/subscription"), 10, params, account.getToken()); + String response = new HttpsConnection(context, this.instance).post(getAbsoluteUrl("/push/subscription"), 10, params, prefKeyOauthTokenT); pushSubscription = parsePushNotifications(new JSONObject(response)); Log.v(Helper.TAG, "response: " + response); ecdh.saveServerKey(context, account, pushSubscription.getServer_key());