Skip to content

Fetch members for a team #715

@WcaleNieWolny

Description

@WcaleNieWolny

What are you trying to achieve?

I am trying to call all members of team red. Currently, there is no good way to do that.
There exists a https://chat.stream-io-api.com/api/v2/users API on the StreamVideo backend, but there is no call to easily access it from the SDK.

If possible, how can you achieve this currently?

    // Fetch members for a team
    private func getMembersForTeam(teamName: String) async throws -> [String] {
        guard let streamVideo = streamVideo else {
            throw NSError(domain: "StreamCallPlugin", code: -1, userInfo: [NSLocalizedDescriptionKey: "StreamVideo not initialized"])
        }
        
        guard let credentials = SecureUserRepository.shared.loadCurrentUser() else {
            throw NSError(domain: "StreamCallPlugin", code: -1, userInfo: [NSLocalizedDescriptionKey: "Credentials not found"])
        }
        
        // Create URL for the Stream Chat API
        var urlComponents = URLComponents(string: "https://chat.stream-io-api.com/api/v2/users")!
        
        // Create the filter payload
        let filterPayload: [String: Any] = [
            "filter_conditions": [
                "teams": [
                    "$in": [teamName]
                ]
            ]
        ]
        
        // Convert payload to JSON string
        let jsonData = try JSONSerialization.data(withJSONObject: filterPayload)
        let jsonString = String(data: jsonData, encoding: .utf8)!
        
        // Add payload as query parameter
        urlComponents.queryItems = [URLQueryItem(name: "payload", value: jsonString)]
        
        // Create URL request
        var request = URLRequest(url: urlComponents.url!)
        request.httpMethod = "GET"
        
        request.addValue("Bearer \(credentials.tokenValue)", forHTTPHeaderField: "Authorization")
        
        // Perform the request
        let (data, response) = try await URLSession.shared.data(for: request)
        
        // Check response status
        guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
            throw NSError(domain: "StreamCallPlugin", code: -1, userInfo: [NSLocalizedDescriptionKey: "Failed to fetch team members"])
        }
        
        // Parse the response
        let responseObject = try JSONSerialization.jsonObject(with: data) as? [String: Any]
        guard let users = responseObject?["users"] as? [[String: Any]] else {
            throw NSError(domain: "StreamCallPlugin", code: -1, userInfo: [NSLocalizedDescriptionKey: "Invalid response format"])
        }
        
        // Extract user IDs
        let memberIds = users.compactMap { $0["id"] as? String }
        return memberIds
    }

What would be the better way?

Get a proper SDK to do this

GetStream Environment

**GetStream Video version: 1.16.0
GetStream Video frameworks: StreamVideo, StreamVideoSwiftUI
iOS version: 18.4
Swift version:

> swift --version
swift-driver version: 1.115.1 Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1)
Target: x86_64-apple-macosx15.0

Xcode version: Version 16.2 (16C5032a)
Device: iPad pro (12.9-inch) (5th generation)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions