ConversationUtils.kt 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package com.nextcloud.talk.utils
  2. import com.nextcloud.talk.models.domain.ConversationModel
  3. import com.nextcloud.talk.models.domain.ConversationType
  4. import com.nextcloud.talk.models.domain.ParticipantType
  5. import com.nextcloud.talk.models.json.capabilities.SpreedCapability
  6. /*
  7. * Nextcloud Talk application
  8. * @author Marcel Hibbe
  9. * Copyright (C) 2023 Marcel Hibbe <dev@mhibbe.de>
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. object ConversationUtils {
  25. private val TAG = ConversationUtils::class.java.simpleName
  26. private const val NOTE_TO_SELF = "Note to self"
  27. fun isPublic(conversation: ConversationModel): Boolean {
  28. return ConversationType.ROOM_PUBLIC_CALL == conversation.type
  29. }
  30. fun isGuest(conversation: ConversationModel): Boolean {
  31. return ParticipantType.GUEST == conversation.participantType ||
  32. ParticipantType.GUEST_MODERATOR == conversation.participantType ||
  33. ParticipantType.USER_FOLLOWING_LINK == conversation.participantType
  34. }
  35. fun isParticipantOwnerOrModerator(conversation: ConversationModel): Boolean {
  36. return ParticipantType.OWNER == conversation.participantType ||
  37. ParticipantType.GUEST_MODERATOR == conversation.participantType ||
  38. ParticipantType.MODERATOR == conversation.participantType
  39. }
  40. fun isLockedOneToOne(conversation: ConversationModel, spreedCapabilities: SpreedCapability): Boolean {
  41. return conversation.type == ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL &&
  42. CapabilitiesUtil.hasSpreedFeatureCapability(spreedCapabilities, "locked-one-to-one-rooms")
  43. }
  44. fun canModerate(conversation: ConversationModel, spreedCapabilities: SpreedCapability): Boolean {
  45. return isParticipantOwnerOrModerator(conversation) &&
  46. !isLockedOneToOne(conversation, spreedCapabilities) &&
  47. conversation.type != ConversationType.FORMER_ONE_TO_ONE &&
  48. !isNoteToSelfConversation(conversation)
  49. }
  50. fun isLobbyViewApplicable(conversation: ConversationModel, spreedCapabilities: SpreedCapability): Boolean {
  51. return !canModerate(conversation, spreedCapabilities) &&
  52. (
  53. conversation.type == ConversationType.ROOM_GROUP_CALL ||
  54. conversation.type == ConversationType.ROOM_PUBLIC_CALL
  55. )
  56. }
  57. fun isNameEditable(conversation: ConversationModel, spreedCapabilities: SpreedCapability): Boolean {
  58. return canModerate(conversation, spreedCapabilities) &&
  59. ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL != conversation.type
  60. }
  61. fun canLeave(conversation: ConversationModel): Boolean {
  62. return if (conversation.canLeaveConversation != null) {
  63. // Available since APIv2
  64. conversation.canLeaveConversation!!
  65. } else {
  66. true
  67. }
  68. }
  69. fun canDelete(conversation: ConversationModel, spreedCapability: SpreedCapability): Boolean {
  70. return if (conversation.canDeleteConversation != null) {
  71. // Available since APIv2
  72. conversation.canDeleteConversation!!
  73. } else {
  74. canModerate(conversation, spreedCapability)
  75. // Fallback for APIv1
  76. }
  77. }
  78. fun isNoteToSelfConversation(currentConversation: ConversationModel?): Boolean {
  79. return currentConversation != null && currentConversation.name == NOTE_TO_SELF
  80. }
  81. }