Improve update process
This commit is contained in:
parent
29a21f6fc5
commit
48312017ef
|
@ -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"
|
||||
|
|
|
@ -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<Account> accountList = new ArrayList<>();
|
||||
accountList.add(account);
|
||||
apiResponse.setAccounts(accountList);
|
||||
|
|
|
@ -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<String, String> 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());
|
||||
|
|
Loading…
Reference in New Issue