| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- //
- // IMClientManager+Silent.swift
- // bugu
- //
- // Created by Bugu on 2023/11/3.
- // Copyright © 2023 Bugu. All rights reserved.
- //
- import Foundation
- private let notificationProvider = APIProvider<NotificationAPI>()
- // MARK: - 消息免打扰
- extension IMClientManager {
-
-
- /// 设置消息免打扰
- /// - Parameters:
- /// - id: 会话 id, 系统传 0
- /// - convType: 会话类型
- /// - success: 成功回调
- /// - failed: 失败回调
- func openNotificationSilent(convId: String, convType: ConversationType, success: @escaping () -> Void, failed: @escaping (_ code: Int, _ des: String) -> Void) {
- let targetId: Int?
- switch convType {
- case .single:
- targetId = convId.convId(without: .single).int
- case .system:
- targetId = convId.convId(without: .system).int
- case .group:
- targetId = convId.convId(without: .group).int
- case .microServer:
- targetId = convId.convId(without: .microServer).int
- }
-
- guard let targetId = targetId else {
- failed(-1, "id error")
- return
- }
-
- notificationProvider.request(.openSilent(targetId: targetId, type: convType)) { result in
- switch result {
- case .success:
- if imdatabase.setConversationNoDisturbing(convId: convId, convType: convType) {
- mainQueueTask {
- self.conversationListeners.forEach{ $0.conversationChanged() }
- }
- }
- success()
- case .failure(let error):
- failed(error.httpErrorCode, error.httpErrorMessage ?? "")
- }
- }
- }
-
- /// 取消消息免打扰
- /// - Parameters:
- /// - id: 会话 id, 系统传 0
- /// - convType: 会话类型
- /// - success: 成功回调
- /// - failed: 失败回调
- func closeNotificationSilent(convId: String, convType: ConversationType, success: @escaping () -> Void, failed: @escaping (_ code: Int, _ des: String) -> Void) {
- let targetId: Int?
- switch convType {
- case .single:
- targetId = convId.convId(without: .single).int
- case .system:
- targetId = convId.convId(without: .system).int
- case .group:
- targetId = convId.convId(without: .group).int
- case .microServer:
- targetId = convId.convId(without: .microServer).int
- }
-
- guard let targetId = targetId else {
- failed(-1, "id error")
- return
- }
-
- notificationProvider.request(.closeSilent(targetId: targetId, type: convType)) { result in
- switch result {
- case .success:
- if imdatabase.cancelConversationNoDisturbing(convId: convId, convType: convType) {
- mainQueueTask {
- self.conversationListeners.forEach{ $0.conversationChanged() }
- }
- }
- success()
- case .failure(let error):
- failed(error.httpErrorCode, error.httpErrorMessage ?? "")
- }
- }
- }
-
- /// 获取消息免打扰列表
- func getNotificationSilentList(success: @escaping () -> Void, failed: @escaping (_ code: Int, _ des: String) -> Void) {
-
- notificationProvider.request(.getSilentList) { result in
- switch result {
- case .success(let response):
- let list = response.mapHandyModelArray(type: MessageNotification.self)?.compactMap({ $0 }) ?? []
- if self.setNotificationSilentList(silentList: list) {
- success()
- } else {
- failed(-1,"")
- }
- case .failure(let error):
- failed(error.httpErrorCode, error.httpErrorMessage ?? "")
- }
- }
- }
-
- func setNotificationSilentList(silentList: [MessageNotification]) -> Bool {
-
- var result: Bool = true
- if silentList.count == 0 {
- result = imdatabase.cancelAllConversationNoDisturbing()
- } else {
- for notification in silentList {
- guard let convId = notification.convId,
- let convType = notification.convType else {
- continue
- }
- result = imdatabase.setConversationNoDisturbing(convId: convId, convType: convType)
- if result == false {
- break
- }
- }
- }
-
- if result {
- mainQueueTask {
- self.conversationListeners.forEach{ $0.conversationChanged() }
- }
- }
-
- return result
- }
-
- func getNoDisturbing(targetId: Int, convType: ConversationType) -> Bool {
- let convId: String
- switch convType {
- case .single:
- convId = targetId.string.convId(add: .single)
- case .system:
- convId = targetId.string.convId(add: .system)
- case .group:
- convId = targetId.string.convId(add: .group)
- case .microServer:
- convId = targetId.string.convId(add: .microServer)
- }
- return imdatabase.findNoDisturbingExist(convId: convId, convType: convType)
- }
-
- }
|