GetStream / stream-chat-android

:speech_balloon: Android Chat SDK ➜ Stream Chat API. UI component libraries for chat apps. Kotlin & Jetpack Compose messaging SDK for Android chat
https://getstream.io/chat/sdk/android/
Other
1.44k stars 269 forks source link

User Mention Error #5118

Closed JanFicko closed 8 months ago

JanFicko commented 9 months ago

Describe the bug When mentioning a user then posting a message an error is returned from BE:

API call failed (attempt 1). Giving up for now, will retry when connection recovers. Error was NetworkError(message=SendMessage failed with error: "message.mentioned_users must contain at maximum 25 items"

It doesn't specifically matter what you search for, the error is always the same.

If a normal message is posted (without any mentions) it works fine.

SDK version

To Reproduce Steps to reproduce the behavior:

  1. Go to message list
  2. Click on InputField
  3. Starting searching for users with '@' prefix
  4. Pick a user
  5. Post a message

Expected behavior A message with mentioned user should be posted in the message list.

Device:

Screenshots Screenshot 2023-12-15 at 10 12 03

Screenshot 2023-12-15 at 10 12 22

DanielNovak commented 9 months ago

Are you able to reproduce this in our sample application? (compose or XML) I tried the described steps but it seems to work fine on our end. That error is from backend and it looks like as is you were trying to mention more than 25 users 🤔 .

DanielNovak commented 9 months ago

Are you doing some own customisation? Can you share the logs?

JanFicko commented 9 months ago

@DanielNovak I tried reproducing it in the sample app, but couldn't find a channel with more than 25 users. It appears that seems to be the problem. In my cases, I've only ever tagged (mentioned) single user and have gotten the error.

My theory is that after you start typing '@' all users in that channel get saved in a list then sent together with a message to the back-end.

I've been testing this with just default component MessagesScreen from UI compose library.

JanFicko commented 9 months ago

This is what request body looks like when sending a message with 1 mentioned user looks like (IDs have been ommited):

{
   "message":{
      "attachments":[

      ],
      "cid":"<messaging_id>",
      "html":"",
      "id":"<id>",
      "mentioned_users":[
         "b2f12f8...f2561c",
         "873e1ac...9477f5d6",
         "3c5adc...b4d037f0d",
         "a157197b...fb8138687",
         "4da4ac...cbb7a75288",
         "576e5...ab8b1bb",
         "6809eb...377ebe4",
         "291684f...7d15468",
         "b0e5107...bfc726",
         "244bca70...68292775",
         "b063b1b...cbe223f115",
         "d53ca9...15d4a",
         "168a5a...3448",
         "525e...7185545",
         "a8f96ce...3c2a9e9",
         "5fa509d...1c9907400",
         "f8376805...bad616",
         "81fe659...6070a",
         "eb7a3bfb...23c16b2",
         "d6ed9f5...4b6777a",
         "389e67...9ea73",
         "f0975f...07e158a",
         "96df139...3e6f9b1fd",
         "6b728c...6fdb4e3d",
         "212e9b...3066990ab",
         "3cb322...0af34c0a",
         "c4084c...cb32406",
         "2f4b854...4d4696",
         "706bd3...92be07d9",
         "fe7a0...cec356c2",
         "eb78da...969d26",
         "b077a49e...1e12e8",
         "8123f...11e3e8",
         "c0228a...4369c0",
         "dc8b55...743c61a",
         "10e4...e9a9d1",
         "3068aec...f34",
         "5193...536ebb8",
         "5531c42...ce34229",
         "02cb15...08dc879",
         "f2cc80c...f02b7",
         "e1d79ca...b3334486",
         "b5aac7...6559995",
         "b4b7799...1e18456cd",
         "fb06d...0c0a9f",
         "7752c0b...bed845",
         "a21e16...fbf17",
         "c639f...bae837e",
         "46d682...1b5fbe",
         "6b7a958...40ab779",
         "a5ca5...9b67f4",
         "b9915e...c5c3b750",
         "7bd9d...9d4a6a98",
         "b22d3e...71d375b1",
         "e02942...c2eaa62085",
         "511352...94aa8ee",
         "036e4e...4810a7b",
         "9992...c2f2b52",
         "84a46f2...1fe1fc9",
         "3e20ed...c1e68ca5",
         "7a04d5...f1249",
         "c359fc...5373f",
         "b3ce85...8856b"
      ],
      "pinned":false,
      "shadowed":false,
      "show_in_channel":false,
      "silent":false,
      "text":"@Mention User testing mentions",
      "thread_participants":[

      ]
   },
   "skip_push":false,
   "skip_enrich_url":false
}
DanielNovak commented 9 months ago

Thank you. I will test this now.

DanielNovak commented 9 months ago

This seems to work fine even in 25+ channels. Are you maybe overriding MessageComposer somewhere and changing the onMentionSelected default value or calling it too many times somewhere? This is by default set to: onMentionSelected = { viewModel.selectMention(it) }. So whenever a mention is selected the viewmodel will add it to the list of the currently selected mentions. Can you maybe put a breakpoint somewhere around this code and make sure that it's not being executed too many times?

The selectMention() function simply keeps adding users to the current list, the implementation is here: https://github.com/GetStream/stream-chat-android/blob/8ad4b16625f92b83b23460ab961c9321defad827/stream-chat-android-ui-common/src/main/kotlin/io/getstream/chat/android/ui/common/feature/messages/composer/MessageComposerController.kt#L719

And then when you submit the message, the filterMentions() function is executed: https://github.com/GetStream/stream-chat-android/blob/8ad4b16625f92b83b23460ab961c9321defad827/stream-chat-android-ui-common/src/main/kotlin/io/getstream/chat/android/ui/common/feature/messages/composer/MessageComposerController.kt#L637

This goes through the message text and filters out the selectedMentions - it returns only those mentions that are still present in the final text.

So when looking at this it would mean that onMentionSelected in MessageComposer was executed maybe somewhere by your code for 25+ users? Can you take a look at this part of the code? Or are you maybe overriding the mentionPopupContent in MessageComposer?

DanielNovak commented 8 months ago

@JanFicko Did you maybe look into this? From our side we don't see any issues yet (also not from other customers).

JanFicko commented 8 months ago

@JanFicko Did you maybe look into this? From our side we don't see any issues yet (also not from other customers).

Sorry for keeping you waiting, we were away for holidays. Will take another look today/tomorrow and will report back.

JanFicko commented 8 months ago

@DanielNovak Okay, I found where the issue is, and it's on our side.

Right now we're in the testing phase, and our Devs/QAs are creating a lot of new accounts, so it happened that initial ones had null name field, so username is blank for those.

So when you want to mention a user, you pick one from the mentioned list pop-up. Afterwards, when user clicks on send button, message gets additionally prepared through prepareMessage() message function here: https://github.com/GetStream/stream-chat-android/blob/8ad4b16625f92b83b23460ab961c9321defad827/stream-chat-android-client/src/main/java/io/getstream/chat/android/client/interceptor/message/internal/PrepareMessageLogicImpl.kt#L52

Which also calls populateMentions(): https://github.com/GetStream/stream-chat-android/blob/8ad4b16625f92b83b23460ab961c9321defad827/stream-chat-android-client/src/main/java/io/getstream/chat/android/client/extensions/internal/Message.kt#L63

And this functions checks message text against all members of the channel: https://github.com/GetStream/stream-chat-android/blob/8ad4b16625f92b83b23460ab961c9321defad827/stream-chat-android-client/src/main/java/io/getstream/chat/android/client/extensions/internal/Message.kt#L69

And here all users, which have blank name, get added to the mentions user list, as it essentialy checks if text contains "@". In happy case it would be something like "@John Doe".

As we currently have 66 users with blank user name, this then exceeds maximum 25 mentioned user limit.

Thanks for the pointers! We can now handle this error case on our side.

DanielNovak commented 8 months ago

Thank you for the detailed explanation. I am glad to hear that you fixed it 👍 .