|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2026 Stream.io Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Stream License; |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://github.com/GetStream/stream-chat-android/blob/main/LICENSE |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.getstream.chat.android.client.parser2 |
| 18 | + |
| 19 | +import com.squareup.moshi.JsonAdapter |
| 20 | +import com.squareup.moshi.JsonReader |
| 21 | +import com.squareup.moshi.Moshi |
| 22 | +import io.getstream.chat.android.client.events.ChatEvent |
| 23 | +import io.getstream.chat.android.client.parser2.adapters.DateAdapter |
| 24 | +import io.getstream.chat.android.client.parser2.direct.AttachmentAdapter |
| 25 | +import io.getstream.chat.android.client.parser2.direct.ChannelInfoAdapter |
| 26 | +import io.getstream.chat.android.client.parser2.direct.DeviceAdapter |
| 27 | +import io.getstream.chat.android.client.parser2.direct.LocationAdapter |
| 28 | +import io.getstream.chat.android.client.parser2.direct.MessageAdapter |
| 29 | +import io.getstream.chat.android.client.parser2.direct.MessageModerationDetailsAdapter |
| 30 | +import io.getstream.chat.android.client.parser2.direct.MessageReminderInfoAdapter |
| 31 | +import io.getstream.chat.android.client.parser2.direct.ModerationAdapter |
| 32 | +import io.getstream.chat.android.client.parser2.direct.NewMessageEventAdapter |
| 33 | +import io.getstream.chat.android.client.parser2.direct.OptionAdapter |
| 34 | +import io.getstream.chat.android.client.parser2.direct.PollAdapter |
| 35 | +import io.getstream.chat.android.client.parser2.direct.PrivacySettingsAdapter |
| 36 | +import io.getstream.chat.android.client.parser2.direct.ReactionAdapter |
| 37 | +import io.getstream.chat.android.client.parser2.direct.ReactionGroupAdapter |
| 38 | +import io.getstream.chat.android.client.parser2.direct.UserAdapter |
| 39 | +import io.getstream.chat.android.models.EventType |
| 40 | +import io.getstream.chat.android.models.MessageTransformer |
| 41 | +import io.getstream.chat.android.models.UserId |
| 42 | +import io.getstream.chat.android.models.UserTransformer |
| 43 | +import io.getstream.log.taggedLogger |
| 44 | +import okio.Buffer |
| 45 | +import java.util.Date |
| 46 | + |
| 47 | +/** |
| 48 | + * Routes incoming JSON events to hand-written [JsonAdapter]s that parse directly into domain models, |
| 49 | + * bypassing the DTO intermediate layer. Returns `null` for unsupported event types so the caller |
| 50 | + * can fall back to the existing DTO path. |
| 51 | + */ |
| 52 | +internal class DirectEventParser( |
| 53 | + private val currentUserIdProvider: () -> UserId?, |
| 54 | + private val messageTransformer: MessageTransformer, |
| 55 | + private val userTransformer: UserTransformer, |
| 56 | +) { |
| 57 | + |
| 58 | + // region Leaf adapters |
| 59 | + |
| 60 | + private val moshi by lazy { Moshi.Builder().add(Date::class.java, DateAdapter()).build() } |
| 61 | + private val dateAdapter by lazy { moshi.adapter(Date::class.java) } |
| 62 | + private val deviceAdapter by lazy { DeviceAdapter() } |
| 63 | + private val privacySettingsAdapter by lazy { PrivacySettingsAdapter() } |
| 64 | + private val attachmentAdapter by lazy { AttachmentAdapter() } |
| 65 | + private val channelInfoAdapter by lazy { ChannelInfoAdapter() } |
| 66 | + private val moderationDetailsAdapter by lazy { MessageModerationDetailsAdapter() } |
| 67 | + private val moderationAdapter by lazy { ModerationAdapter() } |
| 68 | + private val optionAdapter by lazy { OptionAdapter() } |
| 69 | + private val locationAdapter by lazy { LocationAdapter(dateAdapter) } |
| 70 | + private val reactionGroupAdapter by lazy { ReactionGroupAdapter(dateAdapter) } |
| 71 | + |
| 72 | + // endregion |
| 73 | + |
| 74 | + // region Composed adapters |
| 75 | + |
| 76 | + private val userAdapter by lazy { |
| 77 | + UserAdapter(deviceAdapter, privacySettingsAdapter, dateAdapter, userTransformer) |
| 78 | + } |
| 79 | + private val reactionAdapter by lazy { ReactionAdapter(userAdapter, dateAdapter) } |
| 80 | + private val pollAdapter by lazy { |
| 81 | + PollAdapter(userAdapter, optionAdapter, dateAdapter, currentUserIdProvider) |
| 82 | + } |
| 83 | + private val reminderAdapter by lazy { MessageReminderInfoAdapter(dateAdapter) } |
| 84 | + private val messageAdapter by lazy { |
| 85 | + MessageAdapter( |
| 86 | + attachmentAdapter, channelInfoAdapter, reactionAdapter, |
| 87 | + reactionGroupAdapter, userAdapter, moderationDetailsAdapter, moderationAdapter, |
| 88 | + pollAdapter, reminderAdapter, locationAdapter, dateAdapter, messageTransformer, |
| 89 | + ) |
| 90 | + } |
| 91 | + |
| 92 | + // endregion |
| 93 | + |
| 94 | + // region Event adapters |
| 95 | + |
| 96 | + private val newMessageEventAdapter by lazy { |
| 97 | + NewMessageEventAdapter(messageAdapter, userAdapter) |
| 98 | + } |
| 99 | + |
| 100 | + // endregion |
| 101 | + |
| 102 | + /** Registry mapping event type strings to their direct adapters. */ |
| 103 | + private val adapterMap: Map<String, JsonAdapter<out ChatEvent>> by lazy { |
| 104 | + mapOf(EventType.MESSAGE_NEW to newMessageEventAdapter) |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Attempts to parse [raw] JSON into a [ChatEvent] using a direct adapter. |
| 109 | + * Returns `null` if the event type is not supported by any direct adapter. |
| 110 | + */ |
| 111 | + fun parse(raw: String): ChatEvent? { |
| 112 | + val type = extractType(raw) ?: return null |
| 113 | + val adapter = adapterMap[type] ?: return null |
| 114 | + return adapter.fromJson(raw) |
| 115 | + } |
| 116 | + |
| 117 | + companion object { |
| 118 | + |
| 119 | + private val logger by taggedLogger("DirectEventParser") |
| 120 | + |
| 121 | + /** |
| 122 | + * Extracts the `"type"` field value from the top level of a JSON object |
| 123 | + * using a streaming [JsonReader]. Stops as soon as the field is found. |
| 124 | + */ |
| 125 | + @Suppress("NestedBlockDepth") |
| 126 | + internal fun extractType(raw: String): String? { |
| 127 | + if (raw.isBlank()) return null |
| 128 | + val reader = JsonReader.of(Buffer().writeUtf8(raw)) |
| 129 | + return try { |
| 130 | + reader.use { |
| 131 | + if (it.peek() != JsonReader.Token.BEGIN_OBJECT) return null |
| 132 | + it.beginObject() |
| 133 | + while (it.hasNext()) { |
| 134 | + if (it.nextName() == "type") { |
| 135 | + return if (it.peek() == JsonReader.Token.NULL) { |
| 136 | + it.nextNull<String>() |
| 137 | + } else { |
| 138 | + it.nextString() |
| 139 | + } |
| 140 | + } else { |
| 141 | + it.skipValue() |
| 142 | + } |
| 143 | + } |
| 144 | + null |
| 145 | + } |
| 146 | + } catch (@Suppress("TooGenericExceptionCaught") e: Exception) { |
| 147 | + logger.v { "extractType failed; falling back to DTO path: ${e.message}" } |
| 148 | + null |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments