GetStream / stream-chat-swift

💬 iOS Chat SDK in Swift - Build your own app chat experience for iOS using the official Stream Chat API
https://getstream.io/chat/sdk/ios/
Other
859 stars 211 forks source link

Can't Add Members To Existing Group Chat #2190

Closed berkayturanci closed 2 years ago

berkayturanci commented 2 years ago

What did you do?

I am trying to add a feature to the app where channel owners can add members to existing chat groups.

What did you expect to happen?

I expect to see new members can be added to the existing groups by group owners.

What happened instead?

I got HTTP 403 with code 17 which do not allow me to do that.

GetStream Environment

GetStream Chat version: Tried with 4.15.0 and 4.19.0 GetStream Chat frameworks: StreamChat, StreamChatUI iOS version: 15.5 Swift version: Swift 5 Xcode version: 13.4.1 Device: All Devices

Additional context

I got two problem actually. With 4.15.0 I can understand who creates the channel by checking member.role value which is "owner". However with 4.19.0 I cannot understand who created the channel since member.role is now "member". I saw that there was a change in the 4.16.0 version but cannot figure out the latest usage. I can make this control by checking channel.createdBy part but is there any other solution?

Second problem is that, I cannot add members to existing channels. System gives HTTP 403 with code 17 which shows user do not have permission for that. However from the "Roles and Permission" dashboards I gave both "Update Channel Members" and "Update Own Channel Members" to all roles. Still got this error from the response. We uses permission v2.

Lastly I checked the demo app where I can add members to the groups with demo app.

🛠 2022-07-26 08:56:03.781 [DEBUG] [com.stream.api-client (QOS: UNSPECIFIED)] [APIClient.swift:193] [executeRequest(endpoint:completion:)] > Making URL request: POST channelUpdate("messaging/!members-MbVW3qjuvOSJnvagSCUjQd6N4AU96XOVqEwXday49Qs")
Headers:
Optional(["Stream-Auth-Type": "jwt", "Authorization": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiOTI5MTk3NjgiLCJleHAiOjE2NTg5MDEzMTh9.f1xCBCYpMgcs2SM_LVnWSkKWFGTvD-kr3stq6XEE-tA"])
Body:
{
  "add_members" : [
    "77702184"
  ],
  "hide_history" : false
}
Query items:
<Empty>
🛠 2022-07-26 08:56:04.254 [DEBUG] [com.apple.NSURLSession-delegate] [RequestDecoder.swift:46] [decodeRequestResponse(data:response:error:)] > URL request response: <NSHTTPURLResponse: 0x600001091e00> { URL: https://chat.stream-io-api.com/channels/messaging/!members-MbVW3qjuvOSJnvagSCUjQd6N4AU96XOVqEwXday49Qs?api_key=5u7xasgag3yv } { Status Code: 403, Headers {
    "Access-Control-Allow-Origin" =     (
        "*"
    );
    "Cache-Control" =     (
        "no-cache"
    );
    "Content-Length" =     (
        289
    );
    "Content-Type" =     (
        "application/json;charset=utf-8"
    );
    Date =     (
        "Tue, 26 Jul 2022 05:56:04 GMT"
    );
    Server =     (
        "stream-edge"
    );
    Vary =     (
        "Accept-Encoding"
    );
    "access-control-allow-headers" =     (
        "x-requested-with, content-type, accept, origin, authorization, x-csrftoken, x-stream-client, stream-auth-type, x-csrf-token, content-encoding, x-client-request-id"
    );
    "access-control-allow-methods" =     (
        "GET, POST, PUT, PATCH, DELETE, OPTIONS"
    );
    "access-control-max-age" =     (
        86400
    );
    "x-envoy-upstream-service-time" =     (
        101
    );
    "x-ratelimit-limit" =     (
        300
    );
    "x-ratelimit-remaining" =     (
        299
    );
    "x-ratelimit-reset" =     (
        1658815020
    );
} }, data:
{
  "code" : 17,
  "message" : "UpdateChannel failed with error: \"cannot add members to the distinct channel they don't belong to, please create a new distinct channel with the desired members\"",
  "more_info" : "https:\/\/getstream.io\/chat\/docs\/api_errors_response",
  "StatusCode" : 403,
  "duration" : "0.00ms"

})
🚨 2022-07-26 08:56:04.254 [ERROR] [com.apple.NSURLSession-delegate] [RequestDecoder.swift:64] [decodeRequestResponse(data:response:error:)] > API request failed with status code: 403, code: 17 response:
{
  "code" : 17,
  "message" : "UpdateChannel failed with error: \"cannot add members to the distinct channel they don't belong to, please create a new distinct channel with the desired members\"",
  "more_info" : "https:\/\/getstream.io\/chat\/docs\/api_errors_response",
  "StatusCode" : 403,
  "duration" : "0.00ms"
})
Screen Shot 2022-07-26 at 09 07 15 Screen Shot 2022-07-26 at 09 06 46

The user who tries to add member to the group has owner ability.

Screen Shot 2022-07-26 at 09 14 27
berkayturanci commented 2 years ago

Now I see the problem in here;

"You cannot add/remove members for channels created this way."

Screen Shot 2022-07-26 at 09 20 48

Still is there a way to understand the owner of the channel with the latest SDK version?

polqf commented 2 years ago

Hi @berkayturanci,

It is important to note that if you are creating a 1-1 channel (as shown in your last comment) you cannot mutate the members given the nature of the channel.

In order to do so, you need to create a group channel. This can help:

// A unique identifier of a channel
let id = UUID().uuidString

// A `ChannelId` for the new channel. `ChannelId` is a combination of channel's type and a unique identifier of the channel.
let cid = ChannelId(type: .messaging, id: id)

let controller = chatClient.channelController(
    createChannelWithId: cid, // Assign `cid` for the new channel
    members: [chatClient.currentUserId], // Add users to the new channel
    extraData: .init(name: "My new channel", imageURL: nil) // Set the name for the channel
)
berkayturanci commented 2 years ago

Thanks, @polqf, it resolves my add member problem. Is there a way to understand which user is the channel owner by just looking at the channel members? (my first question)

hugobernalstream commented 2 years ago

Hi @berkayturanci,

To check who is the channel owner, from you member list controller, you could create a query to get the channel members and then filter to get the owner. From the query you'll get a list of ChatChannelMember and each member contains a property named memberRole.

Quering Members ChatChannelMember

Regards, Hugo

berkayturanci commented 2 years ago

Hello @hugobernalstream,

Actually, I tried the same thing but the creator of the channel doesn't have the owner - member role as you can see from the screenshot;

Screen Shot 2022-08-12 at 10 35 56

That is why I try to understand the owner by looking at the channel, createdBy by variable.

Screen Shot 2022-08-12 at 10 39 35

So in our case, "Ege" is the creator of the channel and should have owner permission where he doesn't.

tbarbugli commented 2 years ago

@berkayturanci you are right, the user that owns the channel is the one referenced in channel.createdBy.

Owning a channel does not change the role for a member but you can configure your app so that owners have additional grants (eg. add/remove members).

nuno-vieira commented 2 years ago

Hi @berkayturanci,

Were you able to configure your app to do what you want?

Thank you, Nuno

nuno-vieira commented 2 years ago

Closing this one for inactivity.

Let us know if you have any further questions.

Best, Nuno

berkayturanci commented 2 years ago

Yes, I resolved the problem. Thanks @nuno-vieira