-
Notifications
You must be signed in to change notification settings - Fork 319
Add opt-in fast event parsing (message.new)
#6344
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
Merged
Merged
Changes from 20 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
dd9c4de
Implement new JSON parsers.
VelikovPetar 369ce5e
Implement new JSON parsers.
VelikovPetar 7682c1d
Implement new JSON parsers.
VelikovPetar 6f11575
Api dump.
VelikovPetar 836b8a2
Remove unused adapters
VelikovPetar a336361
Fix nullable fields parsing
VelikovPetar 0e9714b
Chnage package
VelikovPetar 9667294
Add tests
VelikovPetar a72a379
Merge branch 'v6' into feature/new_event_parsing
VelikovPetar 4ee8f8f
Merge branch 'v6' into feature/new_event_parsing
VelikovPetar 06ab234
Address CodeRabbit review remarks on direct JSON parsers
VelikovPetar 6d9fd73
Merge branch 'v6' into feature/new_event_parsing
VelikovPetar 963a212
Merge branch 'v6' into feature/new_event_parsing
VelikovPetar 3de3c12
Improve tests.
VelikovPetar 99a8ba0
Add MessageBufferConfig.
VelikovPetar a8c53db
Merge branch 'v6' into feature/new_event_parsing
VelikovPetar a21bdc3
Merge branch 'v6' into feature/new_event_parsing
VelikovPetar 461870e
Merge branch 'v6' into feature/new_event_parsing
VelikovPetar 29ca2ca
Merge branch 'v6' into feature/new_event_parsing
VelikovPetar a506a8b
Add `ChatClient.Build.fastEventParsing` for enabling the new event pa…
VelikovPetar c533286
Revert "Add MessageBufferConfig."
VelikovPetar af32544
Remove leftover logs
VelikovPetar 6bf8637
Address PR remarks
VelikovPetar ac694de
Merge branch 'v6' into feature/new_event_parsing
VelikovPetar e89bd08
Address PR remarks
VelikovPetar 329b6d4
Merge branch 'v6' into feature/new_event_parsing
VelikovPetar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
...ndroid-client/src/main/java/io/getstream/chat/android/client/parser2/DirectEventParser.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| /* | ||
| * Copyright (c) 2014-2026 Stream.io Inc. All rights reserved. | ||
| * | ||
| * Licensed under the Stream License; | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://github.com/GetStream/stream-chat-android/blob/main/LICENSE | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.getstream.chat.android.client.parser2 | ||
|
|
||
| import com.squareup.moshi.JsonAdapter | ||
| import com.squareup.moshi.JsonReader | ||
| import com.squareup.moshi.Moshi | ||
| import io.getstream.chat.android.client.events.ChatEvent | ||
| import io.getstream.chat.android.client.parser2.adapters.DateAdapter | ||
| import io.getstream.chat.android.client.parser2.direct.AttachmentAdapter | ||
| import io.getstream.chat.android.client.parser2.direct.ChannelInfoAdapter | ||
| import io.getstream.chat.android.client.parser2.direct.DeviceAdapter | ||
| import io.getstream.chat.android.client.parser2.direct.LocationAdapter | ||
| import io.getstream.chat.android.client.parser2.direct.MessageAdapter | ||
| import io.getstream.chat.android.client.parser2.direct.MessageModerationDetailsAdapter | ||
| import io.getstream.chat.android.client.parser2.direct.MessageReminderInfoAdapter | ||
| import io.getstream.chat.android.client.parser2.direct.ModerationAdapter | ||
| import io.getstream.chat.android.client.parser2.direct.NewMessageEventAdapter | ||
| import io.getstream.chat.android.client.parser2.direct.OptionAdapter | ||
| import io.getstream.chat.android.client.parser2.direct.PollAdapter | ||
| import io.getstream.chat.android.client.parser2.direct.PrivacySettingsAdapter | ||
| import io.getstream.chat.android.client.parser2.direct.ReactionAdapter | ||
| import io.getstream.chat.android.client.parser2.direct.ReactionGroupAdapter | ||
| import io.getstream.chat.android.client.parser2.direct.UserAdapter | ||
| import io.getstream.chat.android.models.EventType | ||
| import io.getstream.chat.android.models.MessageTransformer | ||
| import io.getstream.chat.android.models.UserId | ||
| import io.getstream.chat.android.models.UserTransformer | ||
| import okio.Buffer | ||
| import java.util.Date | ||
|
|
||
| /** | ||
| * Routes incoming JSON events to hand-written [JsonAdapter]s that parse directly into domain models, | ||
| * bypassing the DTO intermediate layer. Returns `null` for unsupported event types so the caller | ||
| * can fall back to the existing DTO path. | ||
| */ | ||
| internal class DirectEventParser( | ||
| private val currentUserIdProvider: () -> UserId?, | ||
| private val messageTransformer: MessageTransformer, | ||
| private val userTransformer: UserTransformer, | ||
| ) { | ||
|
|
||
| // region Leaf adapters | ||
|
|
||
| private val moshi by lazy { Moshi.Builder().add(Date::class.java, DateAdapter()).build() } | ||
| private val dateAdapter by lazy { moshi.adapter(Date::class.java) } | ||
| private val deviceAdapter by lazy { DeviceAdapter() } | ||
| private val privacySettingsAdapter by lazy { PrivacySettingsAdapter() } | ||
| private val attachmentAdapter by lazy { AttachmentAdapter() } | ||
| private val channelInfoAdapter by lazy { ChannelInfoAdapter() } | ||
| private val moderationDetailsAdapter by lazy { MessageModerationDetailsAdapter() } | ||
| private val moderationAdapter by lazy { ModerationAdapter() } | ||
| private val optionAdapter by lazy { OptionAdapter() } | ||
| private val locationAdapter by lazy { LocationAdapter(dateAdapter) } | ||
| private val reactionGroupAdapter by lazy { ReactionGroupAdapter(dateAdapter) } | ||
|
|
||
| // endregion | ||
|
|
||
| // region Composed adapters | ||
|
|
||
| private val userAdapter by lazy { | ||
| UserAdapter(deviceAdapter, privacySettingsAdapter, dateAdapter, userTransformer) | ||
| } | ||
| private val reactionAdapter by lazy { ReactionAdapter(userAdapter, dateAdapter) } | ||
| private val pollAdapter by lazy { | ||
| PollAdapter(userAdapter, optionAdapter, dateAdapter, currentUserIdProvider) | ||
| } | ||
| private val reminderAdapter by lazy { MessageReminderInfoAdapter(dateAdapter) } | ||
| private val messageAdapter by lazy { | ||
| MessageAdapter( | ||
| attachmentAdapter, channelInfoAdapter, reactionAdapter, | ||
| reactionGroupAdapter, userAdapter, moderationDetailsAdapter, moderationAdapter, | ||
| pollAdapter, reminderAdapter, locationAdapter, dateAdapter, messageTransformer, | ||
| ) | ||
| } | ||
|
|
||
| // endregion | ||
|
|
||
| // region Event adapters | ||
|
|
||
| private val newMessageEventAdapter by lazy { | ||
| NewMessageEventAdapter(messageAdapter, userAdapter) | ||
| } | ||
|
|
||
| // endregion | ||
|
|
||
| /** Registry mapping event type strings to their direct adapters. */ | ||
| private val adapterMap: Map<String, JsonAdapter<out ChatEvent>> by lazy { | ||
| mapOf(EventType.MESSAGE_NEW to newMessageEventAdapter) | ||
| } | ||
|
|
||
| /** | ||
| * Attempts to parse [raw] JSON into a [ChatEvent] using a direct adapter. | ||
| * Returns `null` if the event type is not supported by any direct adapter. | ||
| */ | ||
| fun parse(raw: String): ChatEvent? { | ||
| val type = extractType(raw) ?: return null | ||
| val adapter = adapterMap[type] ?: return null | ||
| return adapter.fromJson(raw) | ||
| } | ||
|
|
||
| companion object { | ||
| /** | ||
| * Extracts the `"type"` field value from the top level of a JSON object | ||
| * using a streaming [JsonReader]. Stops as soon as the field is found. | ||
| */ | ||
| @Suppress("NestedBlockDepth") | ||
| internal fun extractType(raw: String): String? { | ||
| if (raw.isBlank()) return null | ||
| val reader = JsonReader.of(Buffer().writeUtf8(raw)) | ||
| return try { | ||
| reader.use { | ||
| if (it.peek() != JsonReader.Token.BEGIN_OBJECT) return null | ||
| it.beginObject() | ||
| while (it.hasNext()) { | ||
| if (it.nextName() == "type") { | ||
| return if (it.peek() == JsonReader.Token.NULL) { | ||
| it.nextNull<String>() | ||
| } else { | ||
| it.nextString() | ||
| } | ||
| } else { | ||
| it.skipValue() | ||
| } | ||
| } | ||
| null | ||
| } | ||
| } catch (@Suppress("TooGenericExceptionCaught", "SwallowedException") _: Exception) { | ||
|
andremion marked this conversation as resolved.
Outdated
|
||
| null | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.