Skip to content
This repository was archived by the owner on Jul 25, 2024. It is now read-only.

Commit b645481

Browse files
Sam1301timabbott
authored andcommitted
Fix: Crash on narrow to pm with notification bot.
Fixes #244
1 parent 370d2b1 commit b645481

File tree

4 files changed

+36
-12
lines changed

4 files changed

+36
-12
lines changed

app/src/androidTest/java/com/zulip/android/test/UnsortedTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public void testAGOMFetch() throws SQLException, InterruptedException,
217217
private Message sampleMessage(ZulipApp app, int id) throws SQLException {
218218
Message rtr = new Message(app);
219219
rtr.setSender(Person.getOrUpdate(app, "Test User",
220-
TESTUSER_EXAMPLE_COM, ""));
220+
TESTUSER_EXAMPLE_COM, "", 0));
221221
rtr.setContent("Test message");
222222
rtr.setType(MessageType.PRIVATE_MESSAGE);
223223
rtr.setRecipient(new String[]{TESTUSER_EXAMPLE_COM});

app/src/main/java/com/zulip/android/ZulipApp.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.lang.reflect.Type;
4545
import java.sql.SQLException;
4646
import java.util.Date;
47+
import java.util.List;
4748
import java.util.Map;
4849
import java.util.Queue;
4950
import java.util.concurrent.Callable;
@@ -242,7 +243,12 @@ public Message deserialize(JsonElement json, Type typeOfT, JsonDeserializationCo
242243
} else {
243244
Message.ZulipDirectMessage msg = naiveGson.fromJson(json, Message.ZulipDirectMessage.class);
244245
if (msg.getDisplayRecipient() != null) {
245-
msg.setRecipients(msg.getDisplayRecipient().toArray(new Person[msg.getDisplayRecipient().size()]));
246+
List<Person> people = msg.getDisplayRecipient();
247+
for (Person person : people) {
248+
person.setId(person.getRecipientId());
249+
}
250+
251+
msg.setRecipients(people.toArray(new Person[people.size()]));
246252
}
247253

248254
msg.setContent(Message.formatContent(msg.getFormattedContent(), ZulipApp.get()).toString());
@@ -390,7 +396,13 @@ public String getEmail() {
390396

391397
public void setEmail(String email) {
392398
databaseHelper = new DatabaseHelper(this, email);
393-
this.you = Person.getOrUpdate(this, email, null, null);
399+
if (this.getYou() != null) {
400+
// on every consequent refresh
401+
this.you = Person.getOrUpdate(this, email, null, null, this.getYou().getId());
402+
} else {
403+
// only on first login
404+
this.you = Person.getOrUpdate(this, email, null, null, 0);
405+
}
394406
}
395407

396408
public DatabaseHelper getDatabaseHelper() {

app/src/main/java/com/zulip/android/models/Message.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ public Message(ZulipApp app, JSONObject message,
148148
this.setSender(Person.getOrUpdate(app,
149149
message.getString("sender_email"),
150150
message.getString("sender_full_name"),
151-
message.getString("avatar_url"), personCache));
151+
message.getString("avatar_url"),
152+
Integer.parseInt(message.getString("sender_id")), personCache));
152153

153154
if (message.getString("type").equals("stream")) {
154155
this.setType(MessageType.STREAM_MESSAGE);
@@ -178,7 +179,7 @@ public Message(ZulipApp app, JSONObject message,
178179
for (int i = 0; i < jsonRecipients.length(); i++) {
179180
JSONObject obj = jsonRecipients.getJSONObject(i);
180181
Person person = Person.getOrUpdate(app, obj.getString("email"),
181-
obj.getString("full_name"), null, personCache);
182+
obj.getString("full_name"), null, Integer.parseInt(obj.getString("sender_id")), personCache);
182183
r[i] = person;
183184
}
184185
setRecipients(recipientList(r));
@@ -234,7 +235,7 @@ public Void call() throws Exception {
234235
RuntimeExceptionDao<Message, Object> messageDao = app.getDao(Message.class);
235236

236237
for (Message m : messages) {
237-
Person person = Person.getOrUpdate(app, m.getSenderEmail(), m.getSenderFullName(), m.getAvatarUrl());
238+
Person person = Person.getOrUpdate(app, m.getSenderEmail(), m.getSenderFullName(), m.getAvatarUrl(), m.getSenderId());
238239
m.setSender(person);
239240
Stream stream = null;
240241
if (m.getType() == MessageType.STREAM_MESSAGE) {
@@ -429,14 +430,14 @@ public void setRecipients(Person[] list) {
429430
this.recipientsCache = list;
430431

431432
try {
432-
Person to = ZulipApp.get().getDao(Person.class, true).queryBuilder().where().eq(Person.EMAIL_FIELD, list[0].getEmail()).queryForFirst();
433-
;
433+
Person to = list[0];
434+
434435
if (list.length == 1) {
435436
setRecipients(to.getId() + "");
436437
return;
437438
}
438-
Person from = ZulipApp.get().getDao(Person.class, true).queryBuilder().where().eq(Person.EMAIL_FIELD, list[1].getEmail()).queryForFirst();
439439

440+
Person from = list[1];
440441
if (to == null && from != null) {
441442
setRecipients("" + from.getId());
442443
}

app/src/main/java/com/zulip/android/models/Person.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public class Person {
6262
@SerializedName("is_mirror_dummy")
6363
private boolean isMirrorDummy;
6464

65+
@SerializedName("id")
66+
private int recipientId;
67+
6568
public Person(String name, String email) {
6669
this.setName(name);
6770
this.setEmail(email);
@@ -113,7 +116,7 @@ public static Person getByEmail(Dao<Person, ?> dao, String email) {
113116

114117
@SuppressWarnings("WeakerAccess")
115118
public static Person getOrUpdate(ZulipApp app, String email, String name,
116-
String avatarURL, Map<String, Person> personCache) {
119+
String avatarURL, int personId, Map<String, Person> personCache) {
117120

118121
Person person = null;
119122

@@ -130,6 +133,7 @@ public static Person getOrUpdate(ZulipApp app, String email, String name,
130133

131134
if (person == null) {
132135
person = new Person(name, email, avatarURL);
136+
person.setId(personId);
133137
app.getDao(Person.class).create(person);
134138
} else {
135139
boolean changed = false;
@@ -149,8 +153,8 @@ public static Person getOrUpdate(ZulipApp app, String email, String name,
149153
}
150154

151155
public static Person getOrUpdate(ZulipApp app, String email, String name,
152-
String avatarURL) {
153-
return getOrUpdate(app, email, name, avatarURL, null);
156+
String avatarURL, int personId) {
157+
return getOrUpdate(app, email, name, avatarURL, personId, null);
154158
}
155159

156160
public static Person getById(ZulipApp app, int id) {
@@ -284,4 +288,11 @@ public void setActive(boolean active) {
284288
isActive = active;
285289
}
286290

291+
public int getRecipientId() {
292+
return this.recipientId;
293+
}
294+
295+
public void setRecipientId(int id) {
296+
this.recipientId = id;
297+
}
287298
}

0 commit comments

Comments
 (0)