ConferenceInvitationService.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // ConferenceInvitationService.swift
  3. // TUIRoomKit
  4. //
  5. // Created by jeremiawang on 2024/8/8.
  6. //
  7. import Foundation
  8. import RTCRoomEngine
  9. import Combine
  10. import Factory
  11. class ConferenceInvitationService: NSObject {
  12. @WeakLazyInjected(\.conferenceStore) var store: ConferenceStore?
  13. typealias InviteUsersResult = ([String: NSNumber])
  14. typealias InvitationfetchResult = ([TUIInvitation], String)
  15. private let timeout: Double = 60
  16. private let invitationManager = TUIRoomEngine.sharedInstance().getExtension(extensionType: .conferenceInvitationManager) as? TUIConferenceInvitationManager
  17. override init() {
  18. super.init()
  19. invitationManager?.addObserver(self)
  20. }
  21. deinit {
  22. invitationManager?.removeObserver(self)
  23. }
  24. func inviteUsers(roomId: String, userIdList: [String]) -> AnyPublisher<InviteUsersResult, RoomError> {
  25. return Future<InviteUsersResult, RoomError> { [weak self] promise in
  26. guard let self = self else { return }
  27. self.invitationManager?.inviteUsers(roomId, userIdList: userIdList, timeout: timeout, extensionInfo: "") {dic in
  28. promise(.success((dic)))
  29. } onError: { error, message in
  30. promise(.failure(RoomError(error: error, message: message)))
  31. }
  32. }
  33. .eraseToAnyPublisher()
  34. }
  35. func accept(roomId: String) -> AnyPublisher<String, RoomError> {
  36. return Future<String, RoomError> { [weak self] promise in
  37. guard let self = self else { return }
  38. self.invitationManager?.accept(roomId) {
  39. promise(.success(roomId))
  40. } onError: { error, message in
  41. promise(.failure(RoomError(error: error, message: message)))
  42. }
  43. }
  44. .eraseToAnyPublisher()
  45. }
  46. func reject(roomId: String, reason: TUIInvitationRejectedReason) -> AnyPublisher<String, RoomError> {
  47. return Future<String, RoomError> { [weak self] promise in
  48. guard let self = self else { return }
  49. self.invitationManager?.reject(roomId, reason: reason) {
  50. promise(.success(roomId))
  51. } onError: { error, message in
  52. promise(.failure(RoomError(error: error, message: message)))
  53. }
  54. }
  55. .eraseToAnyPublisher()
  56. }
  57. func getInvitationList(roomId: String, cursor: String, count: Int = 20) -> AnyPublisher<InvitationfetchResult, RoomError> {
  58. return Future<InvitationfetchResult, RoomError> { [weak self] promise in
  59. guard let self = self else { return }
  60. self.invitationManager?.getInvitationList(roomId, cursor: cursor, count: count) {invitations, cursor in
  61. promise(.success((invitations, cursor)))
  62. } onError: { error, message in
  63. promise(.failure(RoomError(error: error, message: message)))
  64. }
  65. }
  66. .eraseToAnyPublisher()
  67. }
  68. }
  69. extension ConferenceInvitationService: TUIConferenceInvitationObserver {
  70. func onReceiveInvitation(roomInfo: TUIRoomInfo, invitation: TUIInvitation, extensionInfo: String) {
  71. }
  72. func onInvitationHandledByOtherDevice(roomInfo: TUIRoomInfo, accepted: Bool) {
  73. guard let store = self.store else { return }
  74. store.dispatch(action: InvitationViewActions.dismissInvitationView())
  75. store.dispatch(action: InvitationObserverActions.stopCallingBellAndVibration())
  76. }
  77. func onInvitationCancelled(roomInfo: TUIRoomInfo, invitation: TUIInvitation) {
  78. }
  79. func onInvitationAccepted(roomInfo: TUIRoomInfo, invitation: TUIInvitation) {
  80. }
  81. func onInvitationRejected(roomInfo: TUIRoomInfo, invitation: TUIInvitation, reason: TUIInvitationRejectedReason) {
  82. }
  83. func onInvitationTimeout(roomInfo: TUIRoomInfo, invitation: TUIInvitation) {
  84. guard let store = self.store else { return }
  85. store.dispatch(action: InvitationViewActions.dismissInvitationView())
  86. store.dispatch(action: InvitationObserverActions.stopCallingBellAndVibration())
  87. }
  88. func onInvitationRevokedByAdmin(roomInfo: TUIRoomInfo, invitation: TUIInvitation, admin: TUIUserInfo) {
  89. }
  90. func onInvitationAdded(roomId: String, invitation: TUIInvitation) {
  91. guard let store = self.store else { return }
  92. let currentRoomId = store.selectCurrent(RoomSelectors.getRoomId)
  93. guard currentRoomId == roomId else { return }
  94. store.dispatch(action: ConferenceInvitationActions.addInvitation(payload: invitation))
  95. }
  96. func onInvitationRemoved(roomId: String, invitation: TUIInvitation) {
  97. guard let store = self.store else { return }
  98. let currentRoomId = store.selectCurrent(RoomSelectors.getRoomId)
  99. guard currentRoomId == roomId else { return }
  100. store.dispatch(action: ConferenceInvitationActions.removeInvitation(payload: invitation.invitee.userId))
  101. }
  102. func onInvitationStatusChanged(roomId: String, invitation: TUIInvitation) {
  103. guard let store = self.store else { return }
  104. let currentRoomId = store.selectCurrent(RoomSelectors.getRoomId)
  105. guard currentRoomId == roomId else { return }
  106. store.dispatch(action: ConferenceInvitationActions.changeInvitationStatus(payload: invitation))
  107. }
  108. }