IMClientManager+Silent.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // IMClientManager+Silent.swift
  3. // bugu
  4. //
  5. // Created by Bugu on 2023/11/3.
  6. // Copyright © 2023 Bugu. All rights reserved.
  7. //
  8. import Foundation
  9. private let notificationProvider = APIProvider<NotificationAPI>()
  10. // MARK: - 消息免打扰
  11. extension IMClientManager {
  12. /// 设置消息免打扰
  13. /// - Parameters:
  14. /// - id: 会话 id, 系统传 0
  15. /// - convType: 会话类型
  16. /// - success: 成功回调
  17. /// - failed: 失败回调
  18. func openNotificationSilent(convId: String, convType: ConversationType, success: @escaping () -> Void, failed: @escaping (_ code: Int, _ des: String) -> Void) {
  19. let targetId: Int?
  20. switch convType {
  21. case .single:
  22. targetId = convId.convId(without: .single).int
  23. case .system:
  24. targetId = convId.convId(without: .system).int
  25. case .group:
  26. targetId = convId.convId(without: .group).int
  27. case .microServer:
  28. targetId = convId.convId(without: .microServer).int
  29. }
  30. guard let targetId = targetId else {
  31. failed(-1, "id error")
  32. return
  33. }
  34. notificationProvider.request(.openSilent(targetId: targetId, type: convType)) { result in
  35. switch result {
  36. case .success:
  37. if imdatabase.setConversationNoDisturbing(convId: convId, convType: convType) {
  38. mainQueueTask {
  39. self.conversationListeners.forEach{ $0.conversationChanged() }
  40. }
  41. }
  42. success()
  43. case .failure(let error):
  44. failed(error.httpErrorCode, error.httpErrorMessage ?? "")
  45. }
  46. }
  47. }
  48. /// 取消消息免打扰
  49. /// - Parameters:
  50. /// - id: 会话 id, 系统传 0
  51. /// - convType: 会话类型
  52. /// - success: 成功回调
  53. /// - failed: 失败回调
  54. func closeNotificationSilent(convId: String, convType: ConversationType, success: @escaping () -> Void, failed: @escaping (_ code: Int, _ des: String) -> Void) {
  55. let targetId: Int?
  56. switch convType {
  57. case .single:
  58. targetId = convId.convId(without: .single).int
  59. case .system:
  60. targetId = convId.convId(without: .system).int
  61. case .group:
  62. targetId = convId.convId(without: .group).int
  63. case .microServer:
  64. targetId = convId.convId(without: .microServer).int
  65. }
  66. guard let targetId = targetId else {
  67. failed(-1, "id error")
  68. return
  69. }
  70. notificationProvider.request(.closeSilent(targetId: targetId, type: convType)) { result in
  71. switch result {
  72. case .success:
  73. if imdatabase.cancelConversationNoDisturbing(convId: convId, convType: convType) {
  74. mainQueueTask {
  75. self.conversationListeners.forEach{ $0.conversationChanged() }
  76. }
  77. }
  78. success()
  79. case .failure(let error):
  80. failed(error.httpErrorCode, error.httpErrorMessage ?? "")
  81. }
  82. }
  83. }
  84. /// 获取消息免打扰列表
  85. func getNotificationSilentList(success: @escaping () -> Void, failed: @escaping (_ code: Int, _ des: String) -> Void) {
  86. notificationProvider.request(.getSilentList) { result in
  87. switch result {
  88. case .success(let response):
  89. let list = response.mapHandyModelArray(type: MessageNotification.self)?.compactMap({ $0 }) ?? []
  90. if self.setNotificationSilentList(silentList: list) {
  91. success()
  92. } else {
  93. failed(-1,"")
  94. }
  95. case .failure(let error):
  96. failed(error.httpErrorCode, error.httpErrorMessage ?? "")
  97. }
  98. }
  99. }
  100. func setNotificationSilentList(silentList: [MessageNotification]) -> Bool {
  101. var result: Bool = true
  102. if silentList.count == 0 {
  103. result = imdatabase.cancelAllConversationNoDisturbing()
  104. } else {
  105. for notification in silentList {
  106. guard let convId = notification.convId,
  107. let convType = notification.convType else {
  108. continue
  109. }
  110. result = imdatabase.setConversationNoDisturbing(convId: convId, convType: convType)
  111. if result == false {
  112. break
  113. }
  114. }
  115. }
  116. if result {
  117. mainQueueTask {
  118. self.conversationListeners.forEach{ $0.conversationChanged() }
  119. }
  120. }
  121. return result
  122. }
  123. func getNoDisturbing(targetId: Int, convType: ConversationType) -> Bool {
  124. let convId: String
  125. switch convType {
  126. case .single:
  127. convId = targetId.string.convId(add: .single)
  128. case .system:
  129. convId = targetId.string.convId(add: .system)
  130. case .group:
  131. convId = targetId.string.convId(add: .group)
  132. case .microServer:
  133. convId = targetId.string.convId(add: .microServer)
  134. }
  135. return imdatabase.findNoDisturbingExist(convId: convId, convType: convType)
  136. }
  137. }