Skip to content

Commit d7f9a63

Browse files
feat: create one to one conversation #WPB-19057
* rearrange sample onMessage processes to be more readable
1 parent 4a1923b commit d7f9a63

File tree

1 file changed

+104
-74
lines changed

1 file changed

+104
-74
lines changed

sample/sample-kotlin/src/main/kotlin/com/wire/integrations/sample/SampleEventsHandler.kt

Lines changed: 104 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -32,92 +32,28 @@ class SampleEventsHandler : WireEventsHandlerSuspending() {
3232
override suspend fun onMessage(wireMessage: WireMessage.Text) {
3333
logger.info("Received Text Message : $wireMessage")
3434

35-
if (wireMessage.text.contains("create-one2one")) {
36-
// Expected message: `create-one2one [USER_ID] [DOMAIN]
37-
val split = wireMessage.text.split(" ")
38-
39-
manager.createOneToOneConversation(
40-
userId = QualifiedId(
41-
id = UUID.fromString(split[1]),
42-
domain = split[2]
43-
)
44-
)
45-
35+
if (isCreateOneToOneConversation(text = wireMessage.text)) {
36+
processCreateOneToOneConversation(wireMessage = wireMessage)
4637
return
4738
}
4839

49-
if (wireMessage.text.contains("create-conversation")) {
50-
// Expected message: `create-conversation [NAME] [USER_ID] [DOMAIN]`
51-
val split = wireMessage.text.split(" ")
52-
53-
logger.info("conversation_name: ${split[1]}")
54-
manager.createGroupConversation(
55-
name = split[1],
56-
userIds = listOf(
57-
QualifiedId(
58-
id = UUID.fromString(split[2]),
59-
domain = split[3]
60-
)
61-
)
62-
)
63-
40+
if (isCreateGroupConversation(text = wireMessage.text)) {
41+
processCreateGroupConversation(wireMessage = wireMessage)
6442
return
6543
}
6644

67-
if (wireMessage.text.contains("asset-image")) {
68-
val resourcePath = javaClass.classLoader.getResource("banana-icon.png")?.path
69-
?: throw IllegalStateException("Test resource 'banana-icon.png' not found")
70-
val asset = File(resourcePath)
71-
val originalData = asset.readBytes()
72-
73-
manager.sendAssetSuspending(
74-
conversationId = wireMessage.conversationId,
75-
asset = AssetResource(originalData),
76-
name = asset.name,
77-
mimeType = "image/png",
78-
retention = AssetRetention.VOLATILE
79-
)
80-
45+
if (isAssetImage(text = wireMessage.text)) {
46+
processAssetImage(wireMessage = wireMessage)
8147
return
8248
}
8349

84-
if (wireMessage.text.contains("asset-audio")) {
85-
val resourcePath = javaClass.classLoader.getResource("sample_audio_6s.mp3")?.path
86-
?: throw IllegalStateException("Test resource 'sample_audio_6s.mp3' not found")
87-
val asset = File(resourcePath)
88-
val originalData = asset.readBytes()
89-
90-
manager.sendAssetSuspending(
91-
conversationId = wireMessage.conversationId,
92-
asset = AssetResource(originalData),
93-
metadata = getSampleAudioMetadata(),
94-
name = asset.name,
95-
mimeType = "audio/mp3",
96-
retention = AssetRetention.VOLATILE
97-
)
98-
50+
if (isAssetAudio(text = wireMessage.text)) {
51+
processAssetAudio(wireMessage = wireMessage)
9952
return
10053
}
10154

102-
if (wireMessage.text.contains("asset-video")) {
103-
val resourcePath = javaClass.classLoader.getResource("sample_video_5s.mp4")?.path
104-
?: throw IllegalStateException("Test resource 'sample_video_5s.mp4' not found")
105-
val asset = File(resourcePath)
106-
val originalData = asset.readBytes()
107-
108-
manager.sendAssetSuspending(
109-
conversationId = wireMessage.conversationId,
110-
asset = AssetResource(originalData),
111-
metadata = AssetMetadata.Video(
112-
width = 1920,
113-
height = 1080,
114-
durationMs = 6000L
115-
),
116-
name = asset.name,
117-
mimeType = "video/mp4",
118-
retention = AssetRetention.VOLATILE
119-
)
120-
55+
if (isAssetVideo(text = wireMessage.text)) {
56+
processAssetVideo(wireMessage = wireMessage)
12157
return
12258
}
12359

@@ -243,4 +179,98 @@ class SampleEventsHandler : WireEventsHandlerSuspending() {
243179
normalizedLoudness = Base64.getDecoder().decode(base64Loudness)
244180
)
245181
}
182+
183+
private fun isCreateOneToOneConversation(text: String): Boolean =
184+
text.contains("create-one2one-conversation")
185+
186+
private fun isCreateGroupConversation(text: String): Boolean =
187+
text.contains("create-group-conversation")
188+
189+
private fun isAssetImage(text: String): Boolean =
190+
text.contains("asset-image")
191+
192+
private fun isAssetAudio(text: String): Boolean =
193+
text.contains("asset-audio")
194+
195+
private fun isAssetVideo(text: String): Boolean =
196+
text.contains("asset-video")
197+
198+
private fun processCreateOneToOneConversation(wireMessage: WireMessage.Text) {
199+
// Expected message: `create-one2one [USER_ID] [DOMAIN]
200+
val split = wireMessage.text.split(" ")
201+
202+
manager.createOneToOneConversation(
203+
userId = QualifiedId(
204+
id = UUID.fromString(split[1]),
205+
domain = split[2]
206+
)
207+
)
208+
}
209+
210+
private fun processCreateGroupConversation(wireMessage: WireMessage.Text) {
211+
// Expected message: `create-conversation [NAME] [USER_ID] [DOMAIN]`
212+
val split = wireMessage.text.split(" ")
213+
214+
logger.info("conversation_name: ${split[1]}")
215+
manager.createGroupConversation(
216+
name = split[1],
217+
userIds = listOf(
218+
QualifiedId(
219+
id = UUID.fromString(split[2]),
220+
domain = split[3]
221+
)
222+
)
223+
)
224+
}
225+
226+
private suspend fun processAssetImage(wireMessage: WireMessage.Text) {
227+
val resourcePath = javaClass.classLoader.getResource("banana-icon.png")?.path
228+
?: throw IllegalStateException("Test resource 'banana-icon.png' not found")
229+
val asset = File(resourcePath)
230+
val originalData = asset.readBytes()
231+
232+
manager.sendAssetSuspending(
233+
conversationId = wireMessage.conversationId,
234+
asset = AssetResource(originalData),
235+
name = asset.name,
236+
mimeType = "image/png",
237+
retention = AssetRetention.VOLATILE
238+
)
239+
}
240+
241+
private suspend fun processAssetAudio(wireMessage: WireMessage.Text) {
242+
val resourcePath = javaClass.classLoader.getResource("sample_audio_6s.mp3")?.path
243+
?: throw IllegalStateException("Test resource 'sample_audio_6s.mp3' not found")
244+
val asset = File(resourcePath)
245+
val originalData = asset.readBytes()
246+
247+
manager.sendAssetSuspending(
248+
conversationId = wireMessage.conversationId,
249+
asset = AssetResource(originalData),
250+
metadata = getSampleAudioMetadata(),
251+
name = asset.name,
252+
mimeType = "audio/mp3",
253+
retention = AssetRetention.VOLATILE
254+
)
255+
}
256+
257+
private suspend fun processAssetVideo(wireMessage: WireMessage.Text) {
258+
val resourcePath = javaClass.classLoader.getResource("sample_video_5s.mp4")?.path
259+
?: throw IllegalStateException("Test resource 'sample_video_5s.mp4' not found")
260+
val asset = File(resourcePath)
261+
val originalData = asset.readBytes()
262+
263+
manager.sendAssetSuspending(
264+
conversationId = wireMessage.conversationId,
265+
asset = AssetResource(originalData),
266+
metadata = AssetMetadata.Video(
267+
width = 1920,
268+
height = 1080,
269+
durationMs = 6000L
270+
),
271+
name = asset.name,
272+
mimeType = "video/mp4",
273+
retention = AssetRetention.VOLATILE
274+
)
275+
}
246276
}

0 commit comments

Comments
 (0)