android: update contacts on profile update (#2969)

* android: update contacts on profile update

* returned back

* change
This commit is contained in:
Stanislav Dmitrenko 2023-08-23 20:43:21 +03:00 committed by GitHub
parent 16792de67a
commit 06369e277c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 8 deletions

View File

@ -946,6 +946,14 @@ data class LocalProfile(
} }
} }
@Serializable
data class UserProfileUpdateSummary(
val notChanged: Int,
val updateSuccesses: Int,
val updateFailures: Int,
val changedContacts: List<Contact>
)
@Serializable @Serializable
class Group ( class Group (
val groupInfo: GroupInfo, val groupInfo: GroupInfo,

View File

@ -910,11 +910,11 @@ object ChatController {
return null return null
} }
suspend fun apiUpdateProfile(profile: Profile): Profile? { suspend fun apiUpdateProfile(profile: Profile): Pair<Profile, List<Contact>>? {
val userId = kotlin.runCatching { currentUserId("apiUpdateProfile") }.getOrElse { return null } val userId = kotlin.runCatching { currentUserId("apiUpdateProfile") }.getOrElse { return null }
val r = sendCmd(CC.ApiUpdateProfile(userId, profile)) val r = sendCmd(CC.ApiUpdateProfile(userId, profile))
if (r is CR.UserProfileNoChange) return profile if (r is CR.UserProfileNoChange) return profile to emptyList()
if (r is CR.UserProfileUpdated) return r.toProfile if (r is CR.UserProfileUpdated) return r.toProfile to r.updateSummary.changedContacts
Log.e(TAG, "apiUpdateProfile bad response: ${r.responseType} ${r.details}") Log.e(TAG, "apiUpdateProfile bad response: ${r.responseType} ${r.details}")
return null return null
} }
@ -3254,7 +3254,7 @@ sealed class CR {
@Serializable @SerialName("contactDeleted") class ContactDeleted(val user: UserRef, val contact: Contact): CR() @Serializable @SerialName("contactDeleted") class ContactDeleted(val user: UserRef, val contact: Contact): CR()
@Serializable @SerialName("chatCleared") class ChatCleared(val user: UserRef, val chatInfo: ChatInfo): CR() @Serializable @SerialName("chatCleared") class ChatCleared(val user: UserRef, val chatInfo: ChatInfo): CR()
@Serializable @SerialName("userProfileNoChange") class UserProfileNoChange(val user: User): CR() @Serializable @SerialName("userProfileNoChange") class UserProfileNoChange(val user: User): CR()
@Serializable @SerialName("userProfileUpdated") class UserProfileUpdated(val user: User, val fromProfile: Profile, val toProfile: Profile): CR() @Serializable @SerialName("userProfileUpdated") class UserProfileUpdated(val user: User, val fromProfile: Profile, val toProfile: Profile, val updateSummary: UserProfileUpdateSummary): CR()
@Serializable @SerialName("userPrivacy") class UserPrivacy(val user: User, val updatedUser: User): CR() @Serializable @SerialName("userPrivacy") class UserPrivacy(val user: User, val updatedUser: User): CR()
@Serializable @SerialName("contactAliasUpdated") class ContactAliasUpdated(val user: UserRef, val toContact: Contact): CR() @Serializable @SerialName("contactAliasUpdated") class ContactAliasUpdated(val user: UserRef, val toContact: Contact): CR()
@Serializable @SerialName("connectionAliasUpdated") class ConnectionAliasUpdated(val user: UserRef, val toConnection: PendingContactConnection): CR() @Serializable @SerialName("connectionAliasUpdated") class ConnectionAliasUpdated(val user: UserRef, val toConnection: PendingContactConnection): CR()

View File

@ -25,9 +25,11 @@ fun PreferencesView(m: ChatModel, user: User, close: () -> Unit,) {
fun savePrefs(afterSave: () -> Unit = {}) { fun savePrefs(afterSave: () -> Unit = {}) {
withApi { withApi {
val newProfile = user.profile.toProfile().copy(preferences = preferences.toPreferences()) val newProfile = user.profile.toProfile().copy(preferences = preferences.toPreferences())
val updatedProfile = m.controller.apiUpdateProfile(newProfile) val updated = m.controller.apiUpdateProfile(newProfile)
if (updatedProfile != null) { if (updated != null) {
val (updatedProfile, updatedContacts) = updated
m.updateCurrentUser(updatedProfile, preferences) m.updateCurrentUser(updatedProfile, preferences)
updatedContacts.forEach(m::updateContact)
currentPreferences = preferences currentPreferences = preferences
} }
afterSave() afterSave()

View File

@ -39,8 +39,9 @@ fun UserProfileView(chatModel: ChatModel, close: () -> Unit) {
close, close,
saveProfile = { displayName, fullName, image -> saveProfile = { displayName, fullName, image ->
withApi { withApi {
val newProfile = chatModel.controller.apiUpdateProfile(profile.copy(displayName = displayName, fullName = fullName, image = image)) val updated = chatModel.controller.apiUpdateProfile(profile.copy(displayName = displayName, fullName = fullName, image = image))
if (newProfile != null) { if (updated != null) {
val (newProfile, _) = updated
chatModel.updateCurrentUser(newProfile) chatModel.updateCurrentUser(newProfile)
profile = newProfile profile = newProfile
} }