UserManager.kt 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Nextcloud Talk application
  3. *
  4. * @author Mario Danic
  5. * @author Andy Scherzinger
  6. * Copyright (C) 2022 Andy Scherzinger <info@andy-scherzinger.de>
  7. * Copyright (C) 2017 Mario Danic <mario@lovelyhq.com>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. package com.nextcloud.talk.users
  23. import android.text.TextUtils
  24. import androidx.lifecycle.LiveData
  25. import com.bluelinelabs.logansquare.LoganSquare
  26. import com.nextcloud.talk.data.user.UsersRepository
  27. import com.nextcloud.talk.data.user.model.UserNgEntity
  28. import com.nextcloud.talk.models.ExternalSignalingServer
  29. import com.nextcloud.talk.models.json.capabilities.Capabilities
  30. import com.nextcloud.talk.models.json.push.PushConfigurationState
  31. import com.nextcloud.talk.utils.database.user.CurrentUserProviderNew
  32. class UserManager internal constructor(private val userRepository: UsersRepository) : CurrentUserProviderNew {
  33. fun anyUserExists(): Boolean {
  34. return userRepository.getUsers().isNotEmpty()
  35. }
  36. fun hasMultipleUsers(): Boolean {
  37. return userRepository.getUsers().size > 1
  38. }
  39. val users: List<UserNgEntity>
  40. get() = userRepository.getUsers()
  41. val usersScheduledForDeletion: List<UserNgEntity>
  42. get() = userRepository.getUsersScheduledForDeletion()
  43. suspend fun setAnyUserAndSetAsActive(): UserNgEntity? {
  44. val results = userRepository.getUsersNotScheduledForDeletion()
  45. if (results.isNotEmpty()) {
  46. val UserNgEntity = results[0]
  47. UserNgEntity.current = true
  48. userRepository.updateUser(UserNgEntity)
  49. return UserNgEntity
  50. }
  51. return null
  52. }
  53. override val currentUser: UserNgEntity?
  54. get() {
  55. return userRepository.getActiveUser()
  56. }
  57. suspend fun deleteUser(internalId: Long) {
  58. userRepository.deleteUserWithId(internalId)
  59. }
  60. suspend fun deleteUserWithId(internalId: Long) {
  61. userRepository.deleteUserWithId(internalId)
  62. }
  63. fun getUserById(userId: String): UserNgEntity? {
  64. return userRepository.getUserWithUserId(userId)
  65. }
  66. fun getUserWithId(id: Long): UserNgEntity? {
  67. return userRepository.getUserWithId(id)
  68. }
  69. suspend fun disableAllUsersWithoutId(userId: Long) {
  70. val results = userRepository.getUsersWithoutUserId(userId)
  71. if (results.isNotEmpty()) {
  72. for (entity in results) {
  73. entity.current = false
  74. userRepository.updateUser(entity)
  75. }
  76. }
  77. }
  78. suspend fun checkIfUserIsScheduledForDeletion(username: String, server: String): Boolean {
  79. val results = userRepository.getUserWithUsernameAndServer(username, server)
  80. return results?.scheduledForDeletion ?: false
  81. }
  82. fun getUserWithInternalId(id: Long): UserNgEntity? {
  83. return userRepository.getUserWithIdNotScheduledForDeletion(id)
  84. }
  85. suspend fun getIfUserWithUsernameAndServer(username: String, server: String): Boolean {
  86. return userRepository.getUserWithUsernameAndServer(username, server) != null
  87. }
  88. suspend fun scheduleUserForDeletionWithId(id: Long): Boolean {
  89. val result = userRepository.getUserWithId(id)
  90. if (result != null) {
  91. result.scheduledForDeletion = true
  92. result.current = false
  93. userRepository.updateUser(result)
  94. }
  95. return setAnyUserAndSetAsActive() != null
  96. }
  97. suspend fun createOrUpdateUser(
  98. username: String?,
  99. token: String?,
  100. serverUrl: String?,
  101. displayName: String?,
  102. pushConfigurationState: String?,
  103. currentUser: Boolean?,
  104. userId: String?,
  105. internalId: Long?,
  106. capabilities: String?,
  107. certificateAlias: String?,
  108. externalSignalingServer: String?
  109. ): LiveData<UserNgEntity?> {
  110. var user = if (internalId == null && username != null && serverUrl != null) {
  111. userRepository.getUserWithUsernameAndServer(username, serverUrl)
  112. } else if (internalId != null) {
  113. userRepository.getUserWithId(internalId)
  114. } else {
  115. null
  116. }
  117. if (user == null) {
  118. user = UserNgEntity()
  119. user.baseUrl = serverUrl
  120. user.username = username
  121. user.token = token
  122. if (!TextUtils.isEmpty(displayName)) {
  123. user.displayName = displayName
  124. }
  125. if (pushConfigurationState != null) {
  126. user.pushConfigurationState = LoganSquare
  127. .parse(pushConfigurationState, PushConfigurationState::class.java)
  128. }
  129. if (!TextUtils.isEmpty(userId)) {
  130. user.userId = userId
  131. }
  132. if (!TextUtils.isEmpty(capabilities)) {
  133. user.capabilities = LoganSquare.parse(capabilities, Capabilities::class.java)
  134. }
  135. if (!TextUtils.isEmpty(certificateAlias)) {
  136. user.clientCertificate = certificateAlias
  137. }
  138. if (!TextUtils.isEmpty(externalSignalingServer)) {
  139. user.externalSignalingServer = LoganSquare
  140. .parse(externalSignalingServer, ExternalSignalingServer::class.java)
  141. }
  142. user.current = true
  143. } else {
  144. if (userId != null && (user.userId == null || user.userId != userId)) {
  145. user.userId = userId
  146. }
  147. if (token != null && token != user.token) {
  148. user.token = token
  149. }
  150. if (
  151. displayName != null &&
  152. user.displayName == null ||
  153. displayName != null &&
  154. (user.displayName != null) &&
  155. displayName != user.displayName
  156. ) {
  157. user.displayName = displayName
  158. }
  159. if (pushConfigurationState != null) {
  160. val newPushConfigurationState = LoganSquare
  161. .parse(pushConfigurationState, PushConfigurationState::class.java)
  162. if (newPushConfigurationState != user.pushConfigurationState) {
  163. user.pushConfigurationState = newPushConfigurationState
  164. }
  165. }
  166. if (capabilities != null) {
  167. val newCapabilities = LoganSquare.parse(capabilities, Capabilities::class.java)
  168. if (newCapabilities != user.capabilities) {
  169. user.capabilities = newCapabilities
  170. }
  171. }
  172. if (certificateAlias != null && certificateAlias != user.clientCertificate) {
  173. user.clientCertificate = certificateAlias
  174. }
  175. if (externalSignalingServer != null) {
  176. val newExternalSignalingServer = LoganSquare
  177. .parse(externalSignalingServer, ExternalSignalingServer::class.java)
  178. if (newExternalSignalingServer != user.externalSignalingServer) {
  179. user.externalSignalingServer = newExternalSignalingServer
  180. }
  181. }
  182. if (currentUser != null) {
  183. user.current = currentUser
  184. }
  185. }
  186. userRepository.insertUser(user)
  187. return userRepository.getUserWithIdLiveData(user.id)
  188. }
  189. }