@@ -63,26 +63,26 @@ public struct ChatConfig {
6363 public var retentionPolicy : ChatRetentionPolicy
6464 public var usersCollection : String
6565 public var userId : String ?
66+ public var userEmail : String ?
6667 public var acceptLargeImages : Bool
6768 public var primaryColor : String ?
68- public var hasAdminPrivileges : Bool
6969
7070 public init (
7171 ditto: Ditto ,
7272 retentionPolicy: ChatRetentionPolicy = . init( days: 30 ) ,
7373 usersCollection: String = " users " ,
7474 userId: String ? = nil ,
75+ userEmail: String ? = nil ,
7576 acceptLargeImages: Bool = true ,
76- primaryColor: String ? = nil ,
77- hasAdminPrivileges: Bool = false
77+ primaryColor: String ? = nil
7878 ) {
7979 self . ditto = ditto
8080 self . retentionPolicy = retentionPolicy
8181 self . usersCollection = usersCollection
8282 self . userId = userId
83+ self . userEmail = userEmail
8384 self . acceptLargeImages = acceptLargeImages
8485 self . primaryColor = primaryColor
85- self . hasAdminPrivileges = hasAdminPrivileges
8686 }
8787}
8888
@@ -95,13 +95,27 @@ public struct ChatRetentionPolicy {
9595 }
9696}
9797
98+ struct AdminRole : DittoDecodable {
99+ init ( value: [ String : Any ? ] ) {
100+ _id = value [ " _id " ] as? String
101+ email = value [ " email " ] as? String
102+ }
103+
104+ public var _id : String ?
105+ public var email : String ?
106+ }
107+
98108@MainActor
99109public class DittoChat : DittoSwiftChat , ObservableObject {
100110 @Published private( set) public var publicRoomsPublisher : AnyPublisher < [ Room ] , Never >
101111 public var retentionPolicy : ChatRetentionPolicy = . init( days: 30 )
102112 public var acceptLargeImages : Bool
103113 public var primaryColor : String ?
104- public var hasAdminPrivileges : Bool
114+ public var hasAdminPrivileges : Bool {
115+ return !roles. isEmpty
116+ }
117+ private var rolesCancellable : AnyCancellable ?
118+ private var roles : [ AdminRole ] = [ ]
105119
106120 private var localStore : LocalDataInterface
107121 internal var p2pStore : DittoDataInterface
@@ -110,14 +124,22 @@ public class DittoChat: DittoSwiftChat, ObservableObject {
110124 let localStore : LocalService = LocalService ( )
111125 self . acceptLargeImages = config. acceptLargeImages
112126 self . primaryColor = config. primaryColor
113- self . hasAdminPrivileges = config. hasAdminPrivileges
114127 self . localStore = localStore
115128 self . p2pStore = DittoService ( privateStore: localStore, ditto: config. ditto, usersCollection: config. usersCollection, chatRetentionPolicy: config. retentionPolicy)
116129 self . publicRoomsPublisher = p2pStore. publicRoomsPublisher. eraseToAnyPublisher ( )
117130 self . retentionPolicy = config. retentionPolicy
118131 if let userId = config. userId {
119132 self . setCurrentUser ( withConfig: UserConfig ( id: userId) )
120133 }
134+ if let email = config. userEmail {
135+ do {
136+ try setupRolesSubscription (
137+ email: email
138+ )
139+ } catch {
140+ // TODO: Handle errors
141+ }
142+ }
121143 }
122144
123145 // MARK: Carry over from previous public things
@@ -134,6 +156,24 @@ public class DittoChat: DittoSwiftChat, ObservableObject {
134156 return room
135157 }
136158
159+ func setupRolesSubscription( email: String ) throws {
160+ try p2pStore. ditto. sync. registerSubscription (
161+ query: " SELECT * FROM `roles` WHERE email = :email " ,
162+ arguments: [ " email " : email]
163+ )
164+
165+ rolesCancellable = p2pStore. ditto. store
166+ . observePublisher (
167+ query: " SELECT * FROM `roles` WHERE email = :email " ,
168+ mapTo: AdminRole . self
169+ )
170+ . catch { error in
171+ assertionFailure ( " ERROR with \( #function) " + error. localizedDescription)
172+ return Empty < [ AdminRole ] , Never > ( )
173+ }
174+ . assign ( to: \. roles, on: self )
175+ }
176+
137177 public func allUsersPublisher( ) -> AnyPublisher < [ ChatUser ] , Never > {
138178 p2pStore. allUsersPublisher ( )
139179 }
0 commit comments