diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/PullNotificationWorker.java b/app/src/main/java/eu/toldi/infinityforlemmy/PullNotificationWorker.java index c6fa9cf6..31da6e5f 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/PullNotificationWorker.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/PullNotificationWorker.java @@ -4,10 +4,12 @@ import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.PackageManager; import android.net.Uri; import android.os.Build; import androidx.annotation.NonNull; +import androidx.core.app.ActivityCompat; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; import androidx.work.Worker; @@ -28,13 +30,11 @@ import javax.inject.Named; import eu.toldi.infinityforlemmy.account.Account; import eu.toldi.infinityforlemmy.activities.InboxActivity; import eu.toldi.infinityforlemmy.activities.LinkResolverActivity; -import eu.toldi.infinityforlemmy.apis.RedditAPI; +import eu.toldi.infinityforlemmy.apis.LemmyAPI; +import eu.toldi.infinityforlemmy.comment.Comment; +import eu.toldi.infinityforlemmy.comment.ParseComment; import eu.toldi.infinityforlemmy.customtheme.CustomThemeWrapper; -import eu.toldi.infinityforlemmy.message.FetchMessage; -import eu.toldi.infinityforlemmy.message.Message; -import eu.toldi.infinityforlemmy.message.ParseMessage; -import eu.toldi.infinityforlemmy.utils.APIUtils; -import eu.toldi.infinityforlemmy.utils.JSONUtils; +import eu.toldi.infinityforlemmy.message.CommentInteraction; import eu.toldi.infinityforlemmy.utils.NotificationUtils; import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils; import retrofit2.Call; @@ -88,9 +88,16 @@ public class PullNotificationWorker extends Worker { if (response != null && response.isSuccessful() && response.body() != null) { String responseBody = response.body(); - JSONArray messageArray = new JSONObject(responseBody).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); - ArrayList messages = ParseMessage.parseMessages(messageArray, - context.getResources().getConfiguration().locale, FetchMessage.MESSAGE_TYPE_NOTIFICATION); + JSONArray messageArray = new JSONObject(responseBody).getJSONArray("replies"); + List messages = new ArrayList<>(); + for (int i = 0; i < messageArray.length(); i++) { + JSONObject commentInteractionObject = messageArray.getJSONObject(i); + Comment comment = ParseComment.parseSingleComment(commentInteractionObject); + boolean isRead = !commentInteractionObject.getJSONObject("comment_reply").getBoolean("read"); + int id = commentInteractionObject.getJSONObject("comment_reply").getInt("id"); + messages.add(new CommentInteraction(id, comment, isRead)); + } + if (!messages.isEmpty()) { NotificationCompat.Builder summaryBuilder = NotificationUtils.buildSummaryNotification(context, @@ -110,77 +117,36 @@ public class PullNotificationWorker extends Worker { int pendingIntentFlags = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE : PendingIntent.FLAG_UPDATE_CURRENT; for (int messageIndex = messageSize - 1; messageIndex >= 0; messageIndex--) { - Message message = messages.get(messageIndex); - if (message.getTimeUTC() <= lastNotificationTime) { + CommentInteraction message = messages.get(messageIndex); + if (message.getComment().getCommentTimeMillis() <= lastNotificationTime) { continue; } hasValidMessage = true; - inboxStyle.addLine(message.getAuthor() + " " + message.getBody()); + inboxStyle.addLine(message.getComment().getAuthorQualifiedName() + " " + message.getComment().getCommentMarkdown()); + + + String title = message.getComment().getAuthorQualifiedName(); + String summary = message.getComment().getCommunityQualifiedName(); - String kind = message.getKind(); - String title; - String summary; - if (kind.equals(Message.TYPE_COMMENT) || kind.equals(Message.TYPE_LINK)) { - title = message.getAuthor(); - summary = message.getSubject().substring(0, 1).toUpperCase() + message.getSubject().substring(1); - } else { - title = message.getTitle() == null || message.getTitle().equals("") ? message.getSubject() : message.getTitle(); - if (kind.equals(Message.TYPE_ACCOUNT)) { - summary = context.getString(R.string.notification_summary_account); - } else if (kind.equals(Message.TYPE_MESSAGE)) { - summary = context.getString(R.string.notification_summary_message); - } else if (kind.equals(Message.TYPE_SUBREDDIT)) { - summary = context.getString(R.string.notification_summary_community); - } else { - summary = context.getString(R.string.notification_summary_award); - } - } NotificationCompat.Builder builder = NotificationUtils.buildNotification(notificationManager, - context, title, message.getBody(), summary, + context, title, message.getComment().getCommentMarkdown(), summary, NotificationUtils.CHANNEL_ID_NEW_MESSAGES, NotificationUtils.CHANNEL_NEW_MESSAGES, NotificationUtils.getAccountGroupName(accountName), color); - if (kind.equals(Message.TYPE_COMMENT)) { - Intent intent = new Intent(context, LinkResolverActivity.class); - Uri uri = Uri.parse(message.getContext()); - intent.setData(uri); - intent.putExtra(LinkResolverActivity.EXTRA_NEW_ACCOUNT_NAME, accountName); - intent.putExtra(LinkResolverActivity.EXTRA_MESSAGE_FULLNAME, message.getFullname()); - PendingIntent pendingIntent = PendingIntent.getActivity(context, accountIndex * 6, intent, pendingIntentFlags); - builder.setContentIntent(pendingIntent); - } else if (kind.equals(Message.TYPE_ACCOUNT)) { - Intent intent = new Intent(context, InboxActivity.class); - intent.putExtra(InboxActivity.EXTRA_NEW_ACCOUNT_NAME, accountName); - PendingIntent summaryPendingIntent = PendingIntent.getActivity(context, accountIndex * 6 + 1, intent, pendingIntentFlags); - builder.setContentIntent(summaryPendingIntent); - } else if (kind.equals(Message.TYPE_LINK)) { - Intent intent = new Intent(context, LinkResolverActivity.class); - Uri uri = Uri.parse(message.getContext()); - intent.setData(uri); - intent.putExtra(LinkResolverActivity.EXTRA_NEW_ACCOUNT_NAME, accountName); - intent.putExtra(LinkResolverActivity.EXTRA_MESSAGE_FULLNAME, message.getFullname()); - PendingIntent pendingIntent = PendingIntent.getActivity(context, accountIndex * 6 + 2, intent, pendingIntentFlags); - builder.setContentIntent(pendingIntent); - } else if (kind.equals(Message.TYPE_MESSAGE)) { - Intent intent = new Intent(context, InboxActivity.class); - intent.putExtra(InboxActivity.EXTRA_NEW_ACCOUNT_NAME, accountName); - intent.putExtra(InboxActivity.EXTRA_VIEW_MESSAGE, true); - PendingIntent summaryPendingIntent = PendingIntent.getActivity(context, accountIndex * 6 + 3, intent, pendingIntentFlags); - builder.setContentIntent(summaryPendingIntent); - } else if (kind.equals(Message.TYPE_SUBREDDIT)) { - Intent intent = new Intent(context, InboxActivity.class); - intent.putExtra(InboxActivity.EXTRA_NEW_ACCOUNT_NAME, accountName); - PendingIntent summaryPendingIntent = PendingIntent.getActivity(context, accountIndex * 6 + 4, intent, pendingIntentFlags); - builder.setContentIntent(summaryPendingIntent); - } else { - Intent intent = new Intent(context, InboxActivity.class); - intent.putExtra(InboxActivity.EXTRA_NEW_ACCOUNT_NAME, accountName); - PendingIntent summaryPendingIntent = PendingIntent.getActivity(context, accountIndex * 6 + 5, intent, pendingIntentFlags); - builder.setContentIntent(summaryPendingIntent); + Intent intent = new Intent(context, LinkResolverActivity.class); + Uri uri = Uri.parse(message.getComment().getPermalink()); + intent.setData(uri); + intent.putExtra(LinkResolverActivity.EXTRA_NEW_ACCOUNT_NAME, accountName); + intent.putExtra(LinkResolverActivity.EXTRA_MESSAGE_FULLNAME, message.getId()); + PendingIntent pendingIntent = PendingIntent.getActivity(context, accountIndex * 6, intent, pendingIntentFlags); + builder.setContentIntent(pendingIntent); + + if (ActivityCompat.checkSelfPermission(this.getApplicationContext(), android.Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { + return Result.failure(); } notificationManager.notify(NotificationUtils.getNotificationIdUnreadMessage(accountIndex, messageIndex), builder.build()); } @@ -216,10 +182,7 @@ public class PullNotificationWorker extends Worker { if (retryCount < 0) { return null; } - - Call call = mOauthWithoutAuthenticatorRetrofit.create(RedditAPI.class) - .getMessages(APIUtils.getOAuthHeader(account.getAccessToken()), - FetchMessage.WHERE_UNREAD, null); + Call call = mRetrofit.getRetrofit().create(LemmyAPI.class).userReplies("New", 1, 25, true, account.getAccessToken()); Response response = call.execute(); if (response.isSuccessful()) { diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/message/FetchCommentInteractions.java b/app/src/main/java/eu/toldi/infinityforlemmy/message/FetchCommentInteractions.java index 29f2c918..002c3384 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/message/FetchCommentInteractions.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/message/FetchCommentInteractions.java @@ -16,7 +16,7 @@ import retrofit2.Response; import retrofit2.Retrofit; public class FetchCommentInteractions { - static void fetchReplies(Retrofit retrofit, Integer page, boolean unreadOnly, String auth, FetchCommentInteractionsListener fetchMessagesListener) { + public static void fetchReplies(Retrofit retrofit, Integer page, boolean unreadOnly, String auth, FetchCommentInteractionsListener fetchMessagesListener) { LemmyAPI api = retrofit.create(LemmyAPI.class); api.userReplies("New", page, 25, unreadOnly, auth).enqueue(