[gateway] add support for data messages#237
Conversation
## Walkthrough
The changes introduce a sealed class hierarchy for message content, supporting both text and data messages with explicit typing. The `port` property for data messages is updated from `Short` to `UShort` throughout the codebase. Message construction and handling logic is updated to accommodate these changes, ensuring proper type conversions and backward compatibility.
## Changes
| File(s) | Change Summary |
|---------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------|
| `.../domain/MessageContent.kt` | Changed `port` property in `MessageContent.Data` from `Short` to `UShort`. |
| `.../modules/gateway/GatewayApi.kt` | Introduced sealed class `MessageContent` (`Text`, `Data`). Updated `Message` class to use new content structure, added compatibility logic. |
| `.../modules/gateway/GatewayService.kt` | Injected `LogsService` for error logging. Updated message processing to handle `Text` and `Data` content types distinctly. |
| `.../modules/localserver/routes/MessagesRoutes.kt` | Changed conversion of `port` to use `toUShort()` instead of `toShort()` in message construction. |
| `.../modules/messages/MessagesService.kt` | Explicitly converts `content.port` to `Short` with `.toShort()` when sending data SMS messages. |
| `android-sms-gateway/dev-docs` | Added new empty file `dev-docs`. |
| `README.md` | Updated phrasing of "Data SMS support" feature description, changed emoji, and added a link to detailed feature page. |
| `.../modules/gateway/Module.kt` | Renamed constant `NAME` to `MODULE_NAME` without changing value or usage. |
## Sequence Diagram(s)
```mermaid
sequenceDiagram
participant Client
participant GatewayApi
participant GatewayService
participant MessagesService
participant SmsManager
Client->>GatewayApi: Sends message (text or data)
GatewayApi->>GatewayService: Parses message into MessageContent (Text/Data)
GatewayService->>MessagesService: Processes message content
alt Data message
MessagesService->>SmsManager: sendDataMessage(data, port.toShort())
else Text message
MessagesService->>SmsManager: sendTextMessage(text)
end |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
app/src/main/java/me/capcom/smsgateway/modules/gateway/GatewayApi.kt (1)
203-208: Consider safer error handling in the computed property.The computed
contentproperty throws aRuntimeExceptionwhen no valid content is found, which could cause runtime crashes. Consider returning a nullable type or using a sealed class for error states to enable better error handling at compile time.- val content: MessageContent - get() = this._dataMessage - ?: this._textMessage - ?: _message?.let { MessageContent.Text(it) } - ?: throw RuntimeException("Invalid message content") + val content: MessageContent? + get() = this._dataMessage + ?: this._textMessage + ?: _message?.let { MessageContent.Text(it) }Alternatively, consider using a validation method that can be called during deserialization to fail early with better context.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (5)
app/src/main/java/me/capcom/smsgateway/domain/MessageContent.kt(1 hunks)app/src/main/java/me/capcom/smsgateway/modules/gateway/GatewayApi.kt(2 hunks)app/src/main/java/me/capcom/smsgateway/modules/gateway/GatewayService.kt(1 hunks)app/src/main/java/me/capcom/smsgateway/modules/localserver/routes/MessagesRoutes.kt(1 hunks)app/src/main/java/me/capcom/smsgateway/modules/messages/MessagesService.kt(1 hunks)
🔇 Additional comments (6)
app/src/main/java/me/capcom/smsgateway/modules/messages/MessagesService.kt (1)
372-372: LGTM: Necessary type conversion for SMS API compatibility.The conversion from
UShorttoShortis required because theSmsManager.sendDataMessage()API expects a signed short, while the domain model now correctly usesUShortfor port values. The conversion is safe as both types can represent the full valid port range (0-65535).app/src/main/java/me/capcom/smsgateway/modules/localserver/routes/MessagesRoutes.kt (1)
141-141: LGTM: Correct type conversion aligning with domain model.The conversion to
UShortcorrectly aligns with the updatedMessageContent.Datadefinition. The existing port validation (lines 79-85) ensures the value is within the valid range (0-65535) before this conversion, making the operation safe.app/src/main/java/me/capcom/smsgateway/domain/MessageContent.kt (1)
10-10: LGTM: Improved semantic correctness for port representation.Changing the port type from
ShorttoUShortis semantically correct since port numbers are inherently unsigned values ranging from 0 to 65535. This improves type safety and makes the domain model more accurate.app/src/main/java/me/capcom/smsgateway/modules/gateway/GatewayService.kt (1)
208-214: LGTM: Proper handling of new message content types.The
whenexpression correctly maps the APIMessageContenttypes to domain types, handling bothTextandDatamessage variants. The mapping preserves all necessary data (text for Text messages, data and port for Data messages) and integrates well with the new sealed class hierarchy.app/src/main/java/me/capcom/smsgateway/modules/gateway/GatewayApi.kt (2)
175-184: LGTM: Well-designed sealed class hierarchy.The
MessageContentsealed class provides excellent type safety and clear separation between text and data messages. UsingUShortfor the port in theDataclass is semantically correct for representing port numbers (0-65535 range).
186-202: LGTM: Backward-compatible message structure.The multiple nullable properties with
@SerializedNameannotations provide good backward compatibility while supporting the new message content types. The priority order (_dataMessage → _textMessage → _message) in the computed property is logical and maintains expected behavior.
0c84460 to
429f7d6
Compare
🤖 Pull request artifacts
|
Summary by CodeRabbit
New Features
Bug Fixes
Documentation