Skip to content

Commit 907410d

Browse files
committed
android notif: Group "recipient" fields in an ADT, to crunchify more.
This makes our "crunchy shell" crunchy in an additional area where up to this point we'd been soft (and prone to crashing, or other buggy behavior, on surprises): now the "stream" and "topic" fields are required for a stream message. Perhaps of more importance in this case: it makes the interrelationships of all these fields explicit, making the application code that refers to them easier to reason about.
1 parent a862246 commit 907410d

File tree

3 files changed

+40
-29
lines changed

3 files changed

+40
-29
lines changed

android/app/src/main/java/com/zulipmobile/notifications/FCMPushNotifications.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,11 @@ private static Notification.Builder getNotificationBuilder(
143143

144144
builder.setAutoCancel(true);
145145

146-
String type = fcmMessage.getRecipientType();
146+
Recipient recipient = fcmMessage.getRecipient();
147147
String content = fcmMessage.getContent();
148148
String senderFullName = fcmMessage.getSenderFullName();
149149
String avatarURL = fcmMessage.getAvatarURL();
150150
String time = fcmMessage.getTime();
151-
String stream = fcmMessage.getStream();
152-
String topic = fcmMessage.getTopic();
153151
int totalMessagesCount = extractTotalMessagesCount(conversations);
154152

155153
if (BuildConfig.DEBUG) {
@@ -166,8 +164,9 @@ private static Notification.Builder getNotificationBuilder(
166164
builder.setContentTitle(senderFullName);
167165
}
168166
builder.setContentText(content);
169-
if (type.equals("stream")) {
170-
String displayTopic = stream + " > " + topic;
167+
if (recipient instanceof Recipient.Stream) {
168+
Recipient.Stream r = (Recipient.Stream) recipient;
169+
String displayTopic = r.getStream() + " > " + r.getTopic();
171170
builder.setSubText("Message on " + displayTopic);
172171
}
173172
if (avatarURL.startsWith("http")) {

android/app/src/main/java/com/zulipmobile/notifications/MessageFcmMessage.kt

+29-20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@ import android.os.Bundle
44
import java.net.MalformedURLException
55
import java.net.URL
66

7+
/**
8+
* Data identifying where a Zulip message was sent.
9+
*/
10+
internal sealed class Recipient {
11+
/** A 1:1 private message. Must have been sent to this user, so nothing more to say. */
12+
object Pm : Recipient()
13+
14+
/**
15+
* A group PM.
16+
*
17+
* pmUsers: the user IDs of all users in the conversation, sorted,
18+
* in ASCII decimal, comma-separated.
19+
*/
20+
data class GroupPm(val pmUsers: String) : Recipient()
21+
22+
/** A stream message. */
23+
data class Stream(val stream: String, val topic: String) : Recipient()
24+
}
725

826
/**
927
* Parsed version of an FCM message of type `message`.
@@ -28,13 +46,8 @@ internal data class MessageFcmMessage(
2846
val senderFullName: String,
2947
val avatarURL: String,
3048

31-
val recipientType: String,
32-
val isGroupMessage: Boolean,
33-
val stream: String?,
34-
val topic: String?,
35-
val pmUsers: String?,
36-
3749
val zulipMessageId: Int,
50+
val recipient: Recipient,
3851
val content: String,
3952
val time: String,
4053

@@ -43,14 +56,15 @@ internal data class MessageFcmMessage(
4356
companion object {
4457
fun fromBundle(bundle: Bundle): MessageFcmMessage {
4558
val recipientType = bundle.requireString("recipient_type")
46-
when (recipientType) {
47-
"stream" -> {
48-
bundle.requireString("stream")
49-
bundle.requireString("topic")
50-
}
51-
"private" -> {
52-
// "pm_users" optional -- present just for group PMs
53-
}
59+
val recipient = when (recipientType) {
60+
"stream" ->
61+
Recipient.Stream(
62+
bundle.requireString("stream"),
63+
bundle.requireString("topic"))
64+
"private" ->
65+
bundle.getString("pm_users")?.let {
66+
Recipient.GroupPm(it)
67+
} ?: Recipient.Pm
5468
else -> throw FcmMessageParseException("unexpected recipient_type: $recipientType")
5569
}
5670

@@ -66,13 +80,8 @@ internal data class MessageFcmMessage(
6680
senderFullName = bundle.requireString("sender_full_name"),
6781
avatarURL = avatarURL,
6882

69-
recipientType = recipientType,
70-
isGroupMessage = recipientType == "private" && bundle.getString("pm_users") != null,
71-
stream = bundle.getString("stream"),
72-
topic = bundle.getString("topic"),
73-
pmUsers = bundle.getString("pm_users"),
74-
7583
zulipMessageId = bundle.requireIntString("zulip_message_id"),
84+
recipient = recipient,
7685
content = bundle.requireString("content"),
7786
time = bundle.requireString("time"),
7887

android/app/src/main/java/com/zulipmobile/notifications/NotificationHelper.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,13 @@ static int extractTotalMessagesCount(ConversationMap conversations) {
9090
* private message - fullName:Email:'private'
9191
*/
9292
private static String buildKeyString(MessageFcmMessage fcmMessage) {
93-
if (fcmMessage.getRecipientType().equals("stream"))
94-
return String.format("%s:%s:stream", fcmMessage.getSenderFullName(), fcmMessage.getStream());
95-
else if (fcmMessage.isGroupMessage()) {
96-
return String.format("%s:%s:group", fcmMessage.getSenderFullName(), fcmMessage.getPmUsers());
93+
final Recipient recipient = fcmMessage.getRecipient();
94+
if (recipient instanceof Recipient.Stream)
95+
return String.format("%s:%s:stream", fcmMessage.getSenderFullName(),
96+
((Recipient.Stream) recipient).getStream());
97+
else if (recipient instanceof Recipient.GroupPm) {
98+
return String.format("%s:%s:group", fcmMessage.getSenderFullName(),
99+
((Recipient.GroupPm) recipient).getPmUsers());
97100
} else {
98101
return String.format("%s:%s:private", fcmMessage.getSenderFullName(), fcmMessage.getEmail());
99102
}

0 commit comments

Comments
 (0)