From 6243bf142bc729572a6f657f3d8e38e4acfe957a Mon Sep 17 00:00:00 2001 From: Qiutong Zeng Date: Thu, 17 Oct 2024 10:00:50 +1100 Subject: [PATCH] notification: classify email messages and add mail check prompt This commit adds logic to classify notifications as "email" type when the notification text contains "sent you an email". This change ensures that email notifications are properly categorized, which was previously missing, leading to all email notifications being marked as UNKNOWN type. ### Problem: Previously, email-related notifications from the backend were missing a URL. As a result, when users clicked on these notifications, there was no link to open, and the notifications were categorized as UNKNOWN. This led to a poor user experience since there was no feedback provided when the user clicked on an email notification. ### Changes: - **NotificationClient**: - Modified `WikimediaNotification.toCommonsNotification()` to check if the notification text contains "sent you an email". If it does, the notification is classified as `EMAIL_MESSAGE` instead of the default `UNKNOWN`. - **NotificationActivity**: - In the `NotificatinAdapter` click handler, added a check for `EMAIL_MESSAGE` type. When an email-type notification is clicked, a "Check your mail box" prompt is shown using `Snackbar`, instead of attempting to open a URL (which is typically missing for such notifications). - **NotificationType**: - Added a new `EMAIL` type to categorize email-related notifications. This change improves the user experience by ensuring email notifications are properly categorized and provides useful feedback when they are clicked. --- .../notification/NotificationActivity.java | 8 +- .../notification/NotificationClient.kt | 85 +++++++++++-------- .../notification/models/NotificationType.java | 3 + 3 files changed, 58 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/notification/NotificationActivity.java b/app/src/main/java/fr/free/nrw/commons/notification/NotificationActivity.java index 75a010ea7..d0880e730 100644 --- a/app/src/main/java/fr/free/nrw/commons/notification/NotificationActivity.java +++ b/app/src/main/java/fr/free/nrw/commons/notification/NotificationActivity.java @@ -19,6 +19,7 @@ import fr.free.nrw.commons.databinding.ActivityNotificationBinding; import fr.free.nrw.commons.auth.SessionManager; import fr.free.nrw.commons.auth.csrf.InvalidLoginTokenException; import fr.free.nrw.commons.notification.models.Notification; +import fr.free.nrw.commons.notification.models.NotificationType; import fr.free.nrw.commons.theme.BaseActivity; import fr.free.nrw.commons.utils.NetworkUtils; import fr.free.nrw.commons.utils.ViewUtil; @@ -147,8 +148,11 @@ public class NotificationActivity extends BaseActivity { refresh(false); } adapter = new NotificatinAdapter(item -> { - Timber.d("Notification clicked %s", item.getLink()); - handleUrl(item.getLink()); + if (item.getNotificationType() == NotificationType.EMAIL){ + ViewUtil.showLongSnackbar(binding.container, "Check your mail box"); + } else { + handleUrl(item.getLink()); + } removeNotification(item); return Unit.INSTANCE; }); diff --git a/app/src/main/java/fr/free/nrw/commons/notification/NotificationClient.kt b/app/src/main/java/fr/free/nrw/commons/notification/NotificationClient.kt index aa998ffb5..2737cf000 100644 --- a/app/src/main/java/fr/free/nrw/commons/notification/NotificationClient.kt +++ b/app/src/main/java/fr/free/nrw/commons/notification/NotificationClient.kt @@ -9,6 +9,7 @@ import fr.free.nrw.commons.utils.DateUtil import fr.free.nrw.commons.wikidata.mwapi.MwQueryResponse import io.reactivex.Observable import io.reactivex.Single +import timber.log.Timber import javax.inject.Inject import javax.inject.Named import javax.inject.Singleton @@ -21,43 +22,55 @@ class NotificationClient @param:Named(NetworkingModule.NAMED_COMMONS_CSRF) private val csrfTokenClient: CsrfTokenClient, private val service: NotificationInterface, ) { - fun getNotifications(archived: Boolean): Single> = + fun getNotifications(archived: Boolean): Single> = + service + .getAllNotifications( + wikiList = "wikidatawiki|commonswiki|enwiki", + filter = if (archived) "read" else "!read", + continueStr = null, + ).map { + it.query()?.notifications()?.list() ?: emptyList() + }.flatMap { + Observable.fromIterable(it) + }.map { + it.toCommonsNotification() + }.toList() + + fun markNotificationAsRead(notificationId: String?): Observable = + try { service - .getAllNotifications( - wikiList = "wikidatawiki|commonswiki|enwiki", - filter = if (archived) "read" else "!read", - continueStr = null, - ).map { - it.query()?.notifications()?.list() ?: emptyList() - }.flatMap { - Observable.fromIterable(it) - }.map { - it.toCommonsNotification() - }.toList() - - fun markNotificationAsRead(notificationId: String?): Observable = - try { - service - .markRead( - token = csrfTokenClient.getTokenBlocking(), - readList = notificationId, - unreadList = "", - ).map(MwQueryResponse::success) - } catch (throwable: Throwable) { - if (throwable is InvalidLoginTokenException) { - Observable.error(throwable) - } else { - Observable.just(false) - } + .markRead( + token = csrfTokenClient.getTokenBlocking(), + readList = notificationId, + unreadList = "", + ).map(MwQueryResponse::success) + } catch (throwable: Throwable) { + if (throwable is InvalidLoginTokenException) { + Observable.error(throwable) + } else { + Observable.just(false) } + } + + private fun WikimediaNotification.toCommonsNotification(): Notification { + + val notificationText = contents?.compactHeader ?: "" + + val notificationType = if (notificationText.contains("sent you an email", ignoreCase = true)){ + NotificationType.EMAIL + } else { + NotificationType.UNKNOWN + } + + return Notification( + + notificationType = notificationType, + notificationText = notificationText, + date = DateUtil.getMonthOnlyDateString(timestamp), + link = contents?.links?.primary?.url ?: "", + iconUrl = "", + notificationId = id().toString(), + ) - private fun WikimediaNotification.toCommonsNotification() = - Notification( - notificationType = NotificationType.UNKNOWN, - notificationText = contents?.compactHeader ?: "", - date = DateUtil.getMonthOnlyDateString(timestamp), - link = contents?.links?.primary?.url ?: "", - iconUrl = "", - notificationId = id().toString(), - ) } +} diff --git a/app/src/main/java/fr/free/nrw/commons/notification/models/NotificationType.java b/app/src/main/java/fr/free/nrw/commons/notification/models/NotificationType.java index 1b825f071..100f68abf 100644 --- a/app/src/main/java/fr/free/nrw/commons/notification/models/NotificationType.java +++ b/app/src/main/java/fr/free/nrw/commons/notification/models/NotificationType.java @@ -1,9 +1,12 @@ package fr.free.nrw.commons.notification.models; +import timber.log.Timber; + public enum NotificationType { THANK_YOU_EDIT("thank-you-edit"), EDIT_USER_TALK("edit-user-talk"), MENTION("mention"), + EMAIL("email"), WELCOME("welcome"), UNKNOWN("unknown"); private String type;