|
| 1 | +// |
| 2 | +// MutedUsersManagerInterface.swift |
| 3 | +// |
| 4 | +// Copyright (c) PubNub Inc. |
| 5 | +// All rights reserved. |
| 6 | +// |
| 7 | +// This source code is licensed under the license found in the |
| 8 | +// LICENSE file in the root directory of this source tree. |
| 9 | +// |
| 10 | + |
| 11 | +import Foundation |
| 12 | +import PubNubChat |
| 13 | + |
| 14 | +/// Defines a protocol for an object capable of muting and unmuting users. |
| 15 | +public protocol MutedUsersManagerInterface { |
| 16 | + /// The current set of muted users. |
| 17 | + var mutedUsers: Set<String> { get } |
| 18 | + |
| 19 | + /// Add a user to the list of muted users |
| 20 | + /// |
| 21 | + /// - Parameters: |
| 22 | + /// - userId: The ID of the user to mute |
| 23 | + /// - completion: The `Result` of an asynchronous call: |
| 24 | + /// - **Success**: The operation succeeded, returns nothing |
| 25 | + /// - **Failure**: An `Error` describing the failure |
| 26 | + func muteUser(userId: String, completion: ((Swift.Result<Void, Error>) -> Void)?) |
| 27 | + |
| 28 | + /// Removes a user from the list of muted users |
| 29 | + /// |
| 30 | + /// - Parameters: |
| 31 | + /// - userId: The ID of the muted user |
| 32 | + /// - completion: The `Result` of an asynchronous call: |
| 33 | + /// - **Success**: The operation succeeded, returns nothing |
| 34 | + /// - **Failure**: An `Error` describing the failure |
| 35 | + func unmuteUser(userId: String, completion: ((Swift.Result<Void, Error>) -> Void)?) |
| 36 | +} |
| 37 | + |
| 38 | +/// Provides a default implementation for ``MutedUsersManagerInterface`` methods requiring a completion handler, |
| 39 | +/// allowing the completion handler to be `nil` by default. This simplifies usage for cases where no action is needed after the method completes. |
| 40 | +extension MutedUsersManagerInterface { |
| 41 | + /// Add a user to the list of muted users |
| 42 | + /// |
| 43 | + /// - Parameter userId: The ID of the user to mute |
| 44 | + func muteUser(userId: String) { |
| 45 | + muteUser(userId: userId, completion: nil) |
| 46 | + } |
| 47 | + |
| 48 | + /// Removes a user from the list of muted users |
| 49 | + /// |
| 50 | + /// - Parameter userId: The ID of the muted user |
| 51 | + func unmuteUser(userId: String) { |
| 52 | + unmuteUser(userId: userId, completion: nil) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +class MutedUsersManagerImpl: MutedUsersManagerInterface { |
| 57 | + let underlying: PubNubChat.MutedUsersManager |
| 58 | + |
| 59 | + init(underlying: PubNubChat.MutedUsersManager) { |
| 60 | + self.underlying = underlying |
| 61 | + } |
| 62 | + |
| 63 | + var mutedUsers: Set<String> { |
| 64 | + underlying.mutedUsers |
| 65 | + } |
| 66 | + |
| 67 | + func muteUser(userId: String, completion: ((Swift.Result<Void, Error>) -> Void)? = nil) { |
| 68 | + underlying.muteUser(userId: userId).async( |
| 69 | + caller: self |
| 70 | + ) { (result: FutureResult<MutedUsersManagerImpl, PubNubChat.KotlinUnit>) in |
| 71 | + switch result.result { |
| 72 | + case .success: |
| 73 | + completion?(.success(())) |
| 74 | + case let .failure(error): |
| 75 | + completion?(.failure(error)) |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + func unmuteUser(userId: String, completion: ((Swift.Result<Void, Error>) -> Void)? = nil) { |
| 81 | + underlying.unmuteUser(userId: userId).async( |
| 82 | + caller: self |
| 83 | + ) { (result: FutureResult<MutedUsersManagerImpl, PubNubChat.KotlinUnit>) in |
| 84 | + switch result.result { |
| 85 | + case .success: |
| 86 | + completion?(.success(())) |
| 87 | + case let .failure(error): |
| 88 | + completion?(.failure(error)) |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments