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

Commit 706aba4

Browse files
Sam1301timabbott
authored andcommitted
Avoid use of random id in ZulipApp.setEmail().
1 parent 81c76a7 commit 706aba4

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -400,13 +400,7 @@ public String getEmail() {
400400

401401
public void setEmail(String email) {
402402
databaseHelper = new DatabaseHelper(this, email);
403-
if (this.getYou() != null) {
404-
// on every consequent refresh
405-
this.you = Person.getOrUpdate(this, email, null, null, this.getYou().getId());
406-
} else {
407-
// only on first login
408-
this.you = Person.getOrUpdate(this, email, null, null, 0);
409-
}
403+
this.you = Person.get(this, email);
410404
}
411405

412406
public DatabaseHelper getDatabaseHelper() {
@@ -514,6 +508,10 @@ public Person getYou() {
514508
return you;
515509
}
516510

511+
public void setYou(Person person) {
512+
this.you = person;
513+
}
514+
517515
public void syncPointer(final int mID) {
518516
getZulipServices().updatePointer(Integer.toString(mID))
519517
.enqueue(new Callback<ResponseBody>() {

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,24 @@ public static Person getOrUpdate(ZulipApp app, String email, String name,
171171
return person;
172172
}
173173

174+
/**
175+
* This function is called from setEmail {@link ZulipApp#setEmail(String)}.
176+
* It is used to get current user person {@link Person} object if it exists in the database
177+
* or create a new object using {@param email}.
178+
*
179+
* @param app {@link ZulipApp}
180+
* @param email Person email id {@link Person#email}
181+
* @return {@link Person} object
182+
*/
183+
public static Person get(ZulipApp app, String email) {
184+
Person foundPerson = getByEmail(app, email);
185+
if (foundPerson == null) {
186+
// fake it till you make it
187+
return new Person(null, email);
188+
}
189+
return foundPerson;
190+
}
191+
174192
/**
175193
* This function is used to call {@link Person#getOrUpdate(ZulipApp, String, String, String, int, Map)}
176194
* with no cache.

app/src/main/java/com/zulip/android/networking/AsyncGetEvents.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,8 @@ public Void call() throws Exception {
190190
person.setActive(true);
191191
try {
192192
if (person.getEmail().equalsIgnoreCase(app.getYou().getEmail())) {
193-
// change the id of "you" to correct server id after login
194-
// this statement also updates app.getYou with updated id
195-
personDao.updateId(app.getYou(), person.getId());
193+
// set app.you to point to current user
194+
app.setYou(person);
196195
}
197196
personDao.createOrUpdate(person);
198197
} catch (Exception e) {

0 commit comments

Comments
 (0)