Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Participant attributes #448

Merged
merged 7 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Sources/LiveKit/Core/SignalClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -495,11 +495,15 @@ extension SignalClient {
try await _sendRequest(r)
}

func sendUpdateParticipant(metadata: String? = nil, name: String? = nil) async throws {
func sendUpdateParticipant(name: String? = nil,
metadata: String? = nil,
attributes: [String: String]? = nil) async throws
{
let r = Livekit_SignalRequest.with {
$0.updateMetadata = Livekit_UpdateParticipantMetadata.with {
$0.metadata = metadata ?? ""
$0.name = name ?? ""
$0.metadata = metadata ?? ""
$0.attributes = attributes ?? [:]
}
}

Expand Down
6 changes: 6 additions & 0 deletions Sources/LiveKit/Participant/LocalParticipant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ public class LocalParticipant: Participant {
_state.mutate { $0.name = name }
}

public func set(attributes: [String: String]) async throws {
let room = try requireRoom()
try await room.signalClient.sendUpdateParticipant(attributes: attributes)
_state.mutate { $0.attributes = attributes }
}

func sendTrackSubscriptionPermissions() async throws {
let room = try requireRoom()
guard room._state.connectionState == .connected else { return }
Expand Down
21 changes: 21 additions & 0 deletions Sources/LiveKit/Participant/Participant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public class Participant: NSObject, ObservableObject, Loggable {
@objc
public var metadata: String? { _state.metadata }

@objc
public var attributes: [String: String] { _state.attributes }

@objc
public var connectionQuality: ConnectionQuality { _state.connectionQuality }

Expand Down Expand Up @@ -91,6 +94,7 @@ public class Participant: NSObject, ObservableObject, Loggable {
var connectionQuality: ConnectionQuality = .unknown
var permissions = ParticipantPermissions()
var trackPublications = [Track.Sid: TrackPublication]()
var attributes = [String: String]()
}

let _state: StateSync<State>
Expand Down Expand Up @@ -141,6 +145,22 @@ public class Participant: NSObject, ObservableObject, Loggable {
}
}

// attributes updated
if newState.attributes != oldState.attributes {
// Compute diff of attributes
let attributesDiff = computeAttributesDiff(oldValues: oldState.attributes, newValues: newState.attributes)
if !attributesDiff.isEmpty {
// Notfy ParticipantDelegate
self.delegates.notify(label: { "participant.didUpdateAttributes: \(String(describing: attributesDiff))" }) {
$0.participant?(self, didUpdateAttributes: attributesDiff)
}
// Notify RoomDelegate
room.delegates.notify(label: { "room.didUpdateAttributes: \(String(describing: attributesDiff))" }) {
$0.room?(room, participant: self, didUpdateAttributes: attributesDiff)
}
}
}

if newState.connectionQuality != oldState.connectionQuality {
self.delegates.notify(label: { "participant.didUpdate connectionQuality: \(self.connectionQuality)" }) {
$0.participant?(self, didUpdateConnectionQuality: self.connectionQuality)
Expand Down Expand Up @@ -193,6 +213,7 @@ public class Participant: NSObject, ObservableObject, Loggable {
$0.metadata = info.metadata
$0.joinedAt = Date(timeIntervalSince1970: TimeInterval(info.joinedAt))
$0.kind = info.kind.toLKType()
$0.attributes = info.attributes
}

self.info = info
Expand Down
3 changes: 3 additions & 0 deletions Sources/LiveKit/Protocols/ParticipantDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public protocol ParticipantDelegate: AnyObject {
@objc optional
func participant(_ participant: Participant, didUpdatePermissions permissions: ParticipantPermissions)

@objc optional
func participant(_ participant: Participant, didUpdateAttributes attributes: [String: String])

// MARK: - TrackPublication

/// `muted` state has updated for the ``Participant``'s ``TrackPublication``.
Expand Down
3 changes: 3 additions & 0 deletions Sources/LiveKit/Protocols/RoomDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ public protocol RoomDelegate: AnyObject {
@objc optional
func room(_ room: Room, participant: Participant, didUpdatePermissions permissions: ParticipantPermissions)

@objc optional
func room(_ room: Room, participant: Participant, didUpdateAttributes attributes: [String: String])

// MARK: - Track Publications

/// The ``LocalParticipant`` has published a ``LocalTrack``.
Expand Down
13 changes: 13 additions & 0 deletions Sources/LiveKit/Support/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,16 @@ extension MutableCollection {
}
}
}

func computeAttributesDiff(oldValues: [String: String], newValues: [String: String]) -> [String: String] {
var allKeys = Set(oldValues.keys).union(newValues.keys)
var diff = [String: String]()

for key in allKeys {
if oldValues[key] != newValues[key] {
diff[key] = newValues[key] ?? ""
}
}

return diff
}
27 changes: 27 additions & 0 deletions Tests/LiveKitTests/FunctionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,31 @@ class FunctionTests: XCTestCase {
print("merged: \(merged)")
XCTAssert(merged == 5 ... 20)
}

func testAttributesUpdated() {
let oldValues: [String: String] = ["a": "value", "b": "initial", "c": "value"]
let newValues: [String: String] = ["a": "value", "b": "updated", "c": "value"]

let diff = computeAttributesDiff(oldValues: oldValues, newValues: newValues)
XCTAssertEqual(diff.count, 1)
XCTAssertEqual(diff["b"], "updated")
}

func testAttributesNew() {
let newValues: [String: String] = ["a": "value", "b": "value", "c": "value"]
let oldValues: [String: String] = ["a": "value", "b": "value"]

let diff = computeAttributesDiff(oldValues: oldValues, newValues: newValues)
XCTAssertEqual(diff.count, 1)
XCTAssertEqual(diff["c"], "value")
}

func testAttributesRemoved() {
let newValues: [String: String] = ["a": "value", "b": "value"]
let oldValues: [String: String] = ["a": "value", "b": "value", "c": "value"]

let diff = computeAttributesDiff(oldValues: oldValues, newValues: newValues)
XCTAssertEqual(diff.count, 1)
XCTAssertEqual(diff["c"], "")
}
}