Fetch own username from the whoami endpoint.

main
Greyson Parrelli 2022-11-01 10:26:50 -04:00 zatwierdzone przez Cody Henthorne
rodzic 53883ee3d3
commit 8c915572fb
4 zmienionych plików z 23 dodań i 12 usunięć

Wyświetl plik

@ -2068,20 +2068,19 @@ open class RecipientDatabase(context: Context, databaseHelper: SignalDatabase) :
} }
fun setUsername(id: RecipientId, username: String?) { fun setUsername(id: RecipientId, username: String?) {
if (username != null) { writableDatabase.withinTransaction {
val existingUsername = getByUsername(username) if (username != null) {
if (existingUsername.isPresent && id != existingUsername.get()) { val existingUsername = getByUsername(username)
Log.i(TAG, "Username was previously thought to be owned by " + existingUsername.get() + ". Clearing their username.") if (existingUsername.isPresent && id != existingUsername.get()) {
setUsername(existingUsername.get(), null) Log.i(TAG, "Username was previously thought to be owned by " + existingUsername.get() + ". Clearing their username.")
setUsername(existingUsername.get(), null)
}
} }
}
val contentValues = ContentValues(1).apply { if (update(id, contentValuesOf(USERNAME to username))) {
put(USERNAME, username) ApplicationDependencies.getDatabaseObserver().notifyRecipientChanged(id)
} StorageSyncHelper.scheduleSyncForDataChange()
if (update(id, contentValues)) { }
ApplicationDependencies.getDatabaseObserver().notifyRecipientChanged(id)
StorageSyncHelper.scheduleSyncForDataChange()
} }
} }

Wyświetl plik

@ -141,6 +141,9 @@ public class RefreshOwnProfileJob extends BaseJob {
profileAndCredential.getExpiringProfileKeyCredential() profileAndCredential.getExpiringProfileKeyCredential()
.ifPresent(expiringProfileKeyCredential -> setExpiringProfileKeyCredential(self, ProfileKeyUtil.getSelfProfileKey(), expiringProfileKeyCredential)); .ifPresent(expiringProfileKeyCredential -> setExpiringProfileKeyCredential(self, ProfileKeyUtil.getSelfProfileKey(), expiringProfileKeyCredential));
String username = ApplicationDependencies.getSignalServiceAccountManager().getWhoAmI().getUsername();
SignalDatabase.recipients().setUsername(Recipient.self().getId(), username);
StoryOnboardingDownloadJob.Companion.enqueueIfNeeded(); StoryOnboardingDownloadJob.Companion.enqueueIfNeeded();
} }

Wyświetl plik

@ -8,6 +8,7 @@ import org.signal.core.util.concurrent.SignalExecutors;
import org.signal.core.util.logging.Log; import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.database.SignalDatabase; import org.thoughtcrime.securesms.database.SignalDatabase;
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies; import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
import org.thoughtcrime.securesms.jobs.MultiDeviceProfileContentUpdateJob;
import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.recipients.Recipient;
import org.whispersystems.signalservice.api.SignalServiceAccountManager; import org.whispersystems.signalservice.api.SignalServiceAccountManager;
import org.whispersystems.signalservice.api.push.exceptions.UsernameIsNotReservedException; import org.whispersystems.signalservice.api.push.exceptions.UsernameIsNotReservedException;
@ -66,6 +67,7 @@ class UsernameEditRepository {
try { try {
accountManager.confirmUsername(reserveUsernameResponse); accountManager.confirmUsername(reserveUsernameResponse);
SignalDatabase.recipients().setUsername(Recipient.self().getId(), reserveUsernameResponse.getUsername()); SignalDatabase.recipients().setUsername(Recipient.self().getId(), reserveUsernameResponse.getUsername());
ApplicationDependencies.getJobManager().add(new MultiDeviceProfileContentUpdateJob());
Log.i(TAG, "[confirmUsername] Successfully reserved username."); Log.i(TAG, "[confirmUsername] Successfully reserved username.");
return UsernameSetResult.SUCCESS; return UsernameSetResult.SUCCESS;
} catch (UsernameTakenException e) { } catch (UsernameTakenException e) {

Wyświetl plik

@ -12,6 +12,9 @@ public class WhoAmIResponse {
@JsonProperty @JsonProperty
public String number; public String number;
@JsonProperty
public String username;
public String getAci() { public String getAci() {
return uuid; return uuid;
} }
@ -23,4 +26,8 @@ public class WhoAmIResponse {
public String getNumber() { public String getNumber() {
return number; return number;
} }
public String getUsername() {
return username;
}
} }