mirror of
https://codeberg.org/tom79/Fedilab.git
synced 2024-12-22 16:50:04 +02:00
Merge pull request 'Fix missing push notifications' (#946) from s1m/Fedilab:fix-push into main
Reviewed-on: https://codeberg.org/tom79/Fedilab/pulls/946
This commit is contained in:
commit
e735947362
1 changed files with 33 additions and 52 deletions
|
@ -42,6 +42,7 @@ import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
import app.fedilab.android.BaseMainActivity;
|
import app.fedilab.android.BaseMainActivity;
|
||||||
import app.fedilab.android.R;
|
import app.fedilab.android.R;
|
||||||
|
@ -62,9 +63,10 @@ import retrofit2.converter.gson.GsonConverterFactory;
|
||||||
|
|
||||||
public class NotificationsHelper {
|
public class NotificationsHelper {
|
||||||
|
|
||||||
public static HashMap<String, String> since_ids = new HashMap<>();
|
|
||||||
public static HashMap<String, List<String>> pushed_notifications = new HashMap<>();
|
public static HashMap<String, List<String>> pushed_notifications = new HashMap<>();
|
||||||
|
|
||||||
|
public static ReentrantLock lock = new ReentrantLock();
|
||||||
|
|
||||||
public static synchronized void task(Context context, String slug) throws DBException {
|
public static synchronized void task(Context context, String slug) throws DBException {
|
||||||
|
|
||||||
SharedPreferences prefs = PreferenceManager
|
SharedPreferences prefs = PreferenceManager
|
||||||
|
@ -74,16 +76,6 @@ public class NotificationsHelper {
|
||||||
if (accountDb == null) {
|
if (accountDb == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String last_notifid;
|
|
||||||
last_notifid = prefs.getString(context.getString(R.string.LAST_NOTIFICATION_ID) + slug, null);
|
|
||||||
if (since_ids.containsKey(slug)) {
|
|
||||||
String last_notifid_reccorded = since_ids.get(slug);
|
|
||||||
if (last_notifid_reccorded != null && last_notifid_reccorded.compareToIgnoreCase(last_notifid) == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
since_ids.put(slug, last_notifid);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Check which notifications the user wants to see
|
//Check which notifications the user wants to see
|
||||||
boolean notif_follow = prefs.getBoolean(context.getString(R.string.SET_NOTIF_FOLLOW), true);
|
boolean notif_follow = prefs.getBoolean(context.getString(R.string.SET_NOTIF_FOLLOW), true);
|
||||||
|
@ -101,37 +93,42 @@ public class NotificationsHelper {
|
||||||
return; //Nothing is done
|
return; //Nothing is done
|
||||||
}
|
}
|
||||||
|
|
||||||
MastodonNotificationsService mastodonNotificationsService = init(context, slugArray[1]);
|
|
||||||
String finalLast_notifid = last_notifid;
|
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
Notifications notifications = new Notifications();
|
try {
|
||||||
Call<List<Notification>> notificationsCall;
|
// fetch if we get the lock, or ignore, another thread is doing the job
|
||||||
if (finalLast_notifid != null) {
|
if (lock.tryLock()) {
|
||||||
notificationsCall = mastodonNotificationsService.getNotifications(accountDb.token, null, null, null, finalLast_notifid, null, 30);
|
MastodonNotificationsService mastodonNotificationsService = init(context, slugArray[1]);
|
||||||
} else {
|
Notifications notifications = new Notifications();
|
||||||
notificationsCall = mastodonNotificationsService.getNotifications(accountDb.token, null, null, null, null, null, 5);
|
Call<List<Notification>> notificationsCall;
|
||||||
}
|
String last_notif_id = prefs.getString(context.getString(R.string.LAST_NOTIFICATION_ID) + slug, null);
|
||||||
if (notificationsCall != null) {
|
notificationsCall = mastodonNotificationsService.getNotifications(accountDb.token, null, null, null, last_notif_id, null, 30);
|
||||||
try {
|
if (notificationsCall != null) {
|
||||||
Response<List<Notification>> notificationsResponse = notificationsCall.execute();
|
try {
|
||||||
if (notificationsResponse.isSuccessful()) {
|
Response<List<Notification>> notificationsResponse = notificationsCall.execute();
|
||||||
notifications.notifications = notificationsResponse.body();
|
if (notificationsResponse.isSuccessful()) {
|
||||||
if (notifications.notifications != null) {
|
notifications.notifications = notificationsResponse.body();
|
||||||
if (notifications.notifications.size() > 0) {
|
if (notifications.notifications != null) {
|
||||||
since_ids.put(slug, notifications.notifications.get(0).id);
|
if (notifications.notifications.size() > 0) {
|
||||||
|
prefs.edit().putString(
|
||||||
|
context.getString(R.string.LAST_NOTIFICATION_ID) + slug,
|
||||||
|
notifications.notifications.get(0).id
|
||||||
|
).apply();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
notifications.pagination = MastodonHelper.getPagination(notificationsResponse.headers());
|
||||||
}
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
notifications.pagination = MastodonHelper.getPagination(notificationsResponse.headers());
|
Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||||
|
Runnable myRunnable = () -> onRetrieveNotifications(context, notifications, accountDb, last_notif_id);
|
||||||
|
mainHandler.post(myRunnable);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
Handler mainHandler = new Handler(Looper.getMainLooper());
|
} finally {
|
||||||
Runnable myRunnable = () -> onRetrieveNotifications(context, notifications, accountDb);
|
lock.unlock();
|
||||||
mainHandler.post(myRunnable);
|
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -150,7 +147,7 @@ public class NotificationsHelper {
|
||||||
return retrofit.create(MastodonNotificationsService.class);
|
return retrofit.create(MastodonNotificationsService.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void onRetrieveNotifications(Context context, Notifications newNotifications, final BaseAccount account) {
|
public static void onRetrieveNotifications(Context context, Notifications newNotifications, final BaseAccount account, String max_id) {
|
||||||
if (newNotifications == null || newNotifications.notifications == null || newNotifications.notifications.size() == 0 || account == null) {
|
if (newNotifications == null || newNotifications.notifications == null || newNotifications.notifications.size() == 0 || account == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -169,8 +166,6 @@ public class NotificationsHelper {
|
||||||
boolean notif_signup = prefs.getBoolean(context.getString(R.string.SET_NOTIF_ADMIN_SIGNUP), true);
|
boolean notif_signup = prefs.getBoolean(context.getString(R.string.SET_NOTIF_ADMIN_SIGNUP), true);
|
||||||
boolean notif_report = prefs.getBoolean(context.getString(R.string.SET_NOTIF_ADMIN_REPORT), true);
|
boolean notif_report = prefs.getBoolean(context.getString(R.string.SET_NOTIF_ADMIN_REPORT), true);
|
||||||
|
|
||||||
final String max_id = prefs.getString(context.getString(R.string.LAST_NOTIFICATION_ID) + key, null);
|
|
||||||
|
|
||||||
final List<Notification> notifications = new ArrayList<>();
|
final List<Notification> notifications = new ArrayList<>();
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
for (Notification notif : notificationsReceived) {
|
for (Notification notif : notificationsReceived) {
|
||||||
|
@ -401,28 +396,14 @@ public class NotificationsHelper {
|
||||||
.into(new CustomTarget<Bitmap>() {
|
.into(new CustomTarget<Bitmap>() {
|
||||||
@Override
|
@Override
|
||||||
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
|
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
|
||||||
String lastNotif = prefs.getString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, null);
|
|
||||||
// if (lastNotif == null || Helper.compareTo(notification.id, lastNotif) > 0) {
|
|
||||||
SharedPreferences.Editor editor = prefs.edit();
|
|
||||||
editor.putString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, notifications.get(0).id);
|
|
||||||
editor.commit();
|
|
||||||
since_ids.put(account.user_id + "@" + account.instance, lastNotif);
|
|
||||||
Helper.notify_user(context, account, intent, resource, finalNotifType, finalTitle, finalMessage);
|
Helper.notify_user(context, account, intent, resource, finalNotifType, finalTitle, finalMessage);
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLoadFailed(@Nullable Drawable errorDrawable) {
|
public void onLoadFailed(@Nullable Drawable errorDrawable) {
|
||||||
super.onLoadFailed(errorDrawable);
|
super.onLoadFailed(errorDrawable);
|
||||||
String lastNotif = prefs.getString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, null);
|
|
||||||
// if (lastNotif == null || Helper.compareTo(notification.id, lastNotif) > 0) {
|
|
||||||
SharedPreferences.Editor editor = prefs.edit();
|
|
||||||
since_ids.put(account.user_id + "@" + account.instance, lastNotif);
|
|
||||||
editor.putString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, notifications.get(0).id);
|
|
||||||
editor.commit();
|
|
||||||
Helper.notify_user(context, account, intent, BitmapFactory.decodeResource(context.getResources(),
|
Helper.notify_user(context, account, intent, BitmapFactory.decodeResource(context.getResources(),
|
||||||
getMainLogo(context)), finalNotifType, finalTitle, finalMessage);
|
getMainLogo(context)), finalNotifType, finalTitle, finalMessage);
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in a new issue