@@ -3,25 +3,14 @@ package chat.rocket.core.internal
3
3
import chat.rocket.common.internal.ISO8601Date
4
4
import chat.rocket.common.util.Logger
5
5
import chat.rocket.core.model.attachment.Attachment
6
- import chat.rocket.core.model.attachment.AudioAttachment
7
- import chat.rocket.core.model.attachment.AuthorAttachment
8
6
import chat.rocket.core.model.attachment.Color
9
- import chat.rocket.core.model.attachment.ColorAttachment
10
- import chat.rocket.core.model.attachment.DEFAULT_COLOR
11
7
import chat.rocket.core.model.attachment.Field
12
- import chat.rocket.core.model.attachment.FileAttachment
13
- import chat.rocket.core.model.attachment.GenericFileAttachment
14
- import chat.rocket.core.model.attachment.ImageAttachment
15
- import chat.rocket.core.model.attachment.MessageAttachment
16
- import chat.rocket.core.model.attachment.VideoAttachment
17
8
import com.squareup.moshi.JsonAdapter
18
- import com.squareup.moshi.JsonDataException
19
9
import com.squareup.moshi.JsonReader
20
10
import com.squareup.moshi.JsonWriter
21
11
import com.squareup.moshi.Moshi
22
12
import com.squareup.moshi.Types
23
13
import chat.rocket.core.model.attachment.actions.Action
24
- import chat.rocket.core.model.attachment.actions.ActionsAttachment
25
14
import chat.rocket.core.model.attachment.actions.ButtonAction
26
15
import java.lang.reflect.Type
27
16
@@ -143,45 +132,50 @@ class AttachmentAdapter(moshi: Moshi, private val logger: Logger) : JsonAdapter<
143
132
}
144
133
reader.endObject()
145
134
146
- return when {
147
- imageUrl != null -> {
148
- var preview: String? = null
149
- imagePreview?.let {
150
- preview = " data:${imageType!! } ;base64,$it "
151
- }
152
- ImageAttachment (title, description, text, titleLink, titleLinkDownload, imageUrl, imageType, imageSize, preview)
153
- }
154
- videoUrl != null -> {
155
- VideoAttachment (title, description, text, titleLink, titleLinkDownload, videoUrl, videoType, videoSize)
156
- }
157
- audioUrl != null -> {
158
- AudioAttachment (title, description, text, titleLink, titleLinkDownload, audioUrl, audioType, audioSize)
159
- }
160
- titleLink != null -> {
161
- GenericFileAttachment (title, description, text, titleLink, titleLink, titleLinkDownload)
162
- }
163
- text != null && color != null && fallback != null -> {
164
- ColorAttachment (color, text, fallback, fields)
165
- }
166
- text != null -> {
167
- MessageAttachment (authorName, authorIcon, text, thumbUrl, color, messageLink, attachments, timestamp)
168
- }
169
- authorLink != null -> {
170
- AuthorAttachment (authorLink, authorIcon, authorName, fields)
171
- }
172
- fields != null -> {
173
- ColorAttachment (color ? : DEFAULT_COLOR , text ? : " " , fallback, fields)
174
- }
175
- actions != null -> {
176
- ActionsAttachment (title, actions, buttonAlignment = buttonAlignment ? : " vertical" )
177
- }
178
- else -> {
179
- logger.debug {
180
- " Invalid Attachment type: supported are file and message at ${reader.path} - type: $type "
181
- }
182
- null
135
+ if (isAllNull(title, type, description, authorName, text, thumbUrl, color, titleLink, titleLinkDownload,
136
+ imageUrl, imageType, imageSize, videoUrl, videoType, videoSize, audioUrl, audioType, audioSize,
137
+ messageLink, attachments, timestamp, authorIcon, authorLink, imagePreview, fields, fallback,
138
+ buttonAlignment, actions)) {
139
+ logger.debug {
140
+ " Empty attachment"
183
141
}
142
+ return null
184
143
}
144
+
145
+ return Attachment (
146
+ title = title,
147
+ type = type,
148
+ description = description,
149
+ authorName = authorName,
150
+ text = text,
151
+ thumbUrl = thumbUrl,
152
+ color = color,
153
+ titleLink = titleLink,
154
+ titleLinkDownload = titleLinkDownload,
155
+ imageUrl = imageUrl,
156
+ imageType = imageType,
157
+ imageSize = imageSize,
158
+ videoUrl = videoUrl,
159
+ videoType = videoType,
160
+ videoSize = videoSize,
161
+ audioUrl = audioUrl,
162
+ audioType = audioType,
163
+ audioSize = audioSize,
164
+ messageLink = messageLink,
165
+ attachments = attachments,
166
+ timestamp = timestamp,
167
+ authorIcon = authorIcon,
168
+ authorLink = authorLink,
169
+ imagePreview = imagePreview,
170
+ fields = fields,
171
+ fallback = fallback,
172
+ buttonAlignment = buttonAlignment,
173
+ actions = actions
174
+ )
175
+ }
176
+
177
+ private fun isAllNull (vararg params : Any? ): Boolean {
178
+ return params.isEmpty()
185
179
}
186
180
187
181
private fun parseFields (reader : JsonReader ): List <Field >? {
@@ -258,103 +252,41 @@ class AttachmentAdapter(moshi: Moshi, private val logger: Logger) : JsonAdapter<
258
252
}
259
253
}
260
254
261
- override fun toJson (writer : JsonWriter , value : Attachment ? ) {
262
- if (value == null ) {
255
+ override fun toJson (writer : JsonWriter , attachment : Attachment ? ) {
256
+ if (attachment == null ) {
263
257
writer.nullValue()
264
258
} else {
265
- when (value) {
266
- is ColorAttachment -> writeColorAttachment(writer, value)
267
- is MessageAttachment -> writeMessageAttachment(writer, value)
268
- is FileAttachment -> writeFileAttachment(writer, value)
269
- is AuthorAttachment -> writeAuthorAttachment(writer, value)
270
- is ActionsAttachment -> writeActionsAttachment(writer, value)
271
- }
272
- }
273
- }
274
-
275
- private fun writeColorAttachment (writer : JsonWriter , attachment : ColorAttachment ) {
276
- writer.beginObject()
277
- with (writer) {
278
- name(" color" ).value(attachment.color.rawColor)
279
- name(" text" ).value(attachment.text)
280
- name(" fallback" ).value(attachment.fallback)
281
- attachment.fields?.let { writeFields(writer, it) }
282
- }
283
- writer.endObject()
284
- }
259
+ with (writer) {
260
+ beginObject()
261
+ name(" title" ).value(attachment.title)
262
+ name(" description" ).value(attachment.description)
263
+ name(" text" ).value(attachment.text)
264
+ name(" title_link" ).value(attachment.titleLink)
265
+ name(" title_link_download" ).value(attachment.titleLinkDownload)
266
+ name(" image_url" ).value(attachment.imageUrl)
267
+ name(" image_size" ).value(attachment.imageSize)
268
+ name(" image_type" ).value(attachment.imageType)
269
+ name(" image_preview" ).value(attachment.imagePreview)
270
+ name(" video_url" ).value(attachment.videoUrl)
271
+ name(" video_size" ).value(attachment.videoType)
272
+ name(" video_type" ).value(attachment.videoUrl)
273
+ name(" audio_url" ).value(attachment.audioUrl)
274
+ name(" audio_size" ).value(attachment.audioSize)
275
+ name(" audio_type" ).value(attachment.audioType)
276
+ name(" author_link" ).value(attachment.authorLink)
277
+ name(" author_icon" ).value(attachment.authorIcon)
278
+ name(" author_name" ).value(attachment.authorName)
279
+ name(" button_alignment" ).value(attachment.buttonAlignment)
280
+ name(" color" ).value(attachment.color?.rawColor)
281
+ name(" fallback" ).value(attachment.fallback)
282
+ name(" thumbUrl" ).value(attachment.thumbUrl)
283
+ name(" message_link" ).value(attachment.messageLink)
284
+ name(" ts" ).value(attachment.timestamp)
285
285
286
- private fun writeMessageAttachment (writer : JsonWriter , attachment : MessageAttachment ) {
287
- writer.beginObject()
288
- with (writer) {
289
- name(" author_name" ).value(attachment.author)
290
- name(" author_icon" ).value(attachment.icon)
291
- name(" text" ).value(attachment.text)
292
- name(" thumbUrl" ).value(attachment.thumbUrl)
293
- name(" color" ).value(attachment.color?.toString())
294
- name(" message_link" ).value(attachment.url)
295
- name(" ts" ).value(attachment.timestamp)
296
- }
297
- writer.endObject()
298
- }
299
-
300
- private fun writeFileAttachment (writer : JsonWriter , attachment : FileAttachment ) {
301
- writer.beginObject()
302
- writer.name(" title" ).value(attachment.title)
303
- writer.name(" description" ).value(attachment.description)
304
- writer.name(" text" ).value(attachment.text)
305
- writer.name(" title_link" ).value(attachment.titleLink)
306
- writer.name(" title_link_download" ).value(attachment.titleLinkDownload)
307
- when (attachment) {
308
- is AudioAttachment -> writeAudioAttachment(writer, attachment)
309
- is VideoAttachment -> writeVideoAttachment(writer, attachment)
310
- is ImageAttachment -> writeImageAttachment(writer, attachment)
311
- is GenericFileAttachment -> writeGenericFileAttachment(writer, attachment)
312
- }
313
- writer.endObject()
314
- }
315
-
316
- private fun writeGenericFileAttachment (writer : JsonWriter , attachment : GenericFileAttachment ) {
317
- with (writer) {
318
- name(" title" ).value(attachment.title)
319
- name(" titleLink" ).value(attachment.url)
320
- name(" titleLinkDownload" ).value(attachment.titleLinkDownload)
321
- }
322
- }
323
-
324
- private fun writeAudioAttachment (writer : JsonWriter , attachment : AudioAttachment ) {
325
- with (writer) {
326
- name(" audio_url" ).value(attachment.url)
327
- name(" audio_size" ).value(attachment.size)
328
- name(" audio_type" ).value(attachment.type)
329
- }
330
- }
331
-
332
- private fun writeVideoAttachment (writer : JsonWriter , attachment : VideoAttachment ) {
333
- with (writer) {
334
- name(" video_url" ).value(attachment.url)
335
- name(" video_size" ).value(attachment.size)
336
- name(" video_type" ).value(attachment.type)
337
- }
338
- }
339
-
340
- private fun writeImageAttachment (writer : JsonWriter , attachment : ImageAttachment ) {
341
- with (writer) {
342
- name(" image_url" ).value(attachment.url)
343
- name(" image_size" ).value(attachment.size)
344
- name(" image_type" ).value(attachment.type)
345
- name(" image_preview" ).value(attachment.imagePreview)
346
- }
347
- }
348
-
349
- private fun writeAuthorAttachment (writer : JsonWriter , attachment : AuthorAttachment ) {
350
- writer.beginObject()
351
- with (writer) {
352
- name(" author_link" ).value(attachment.url)
353
- name(" author_icon" ).value(attachment.authorIcon)
354
- name(" author_name" ).value(attachment.authorName)
355
- attachment.fields?.let { writeFields(writer, it) }
286
+ attachment.actions?.let { writeActions(writer, it) }
287
+ attachment.fields?.let { writeFields(writer, it) }
288
+ }
356
289
}
357
- writer.endObject()
358
290
}
359
291
360
292
private fun writeFields (writer : JsonWriter , fields : List <Field >) {
@@ -371,16 +303,6 @@ class AttachmentAdapter(moshi: Moshi, private val logger: Logger) : JsonAdapter<
371
303
}
372
304
}
373
305
374
- private fun writeActionsAttachment (writer : JsonWriter , attachment : ActionsAttachment ) {
375
- writer.beginObject()
376
- with (writer) {
377
- name(" title" ).value(attachment.title)
378
- name(" button_alignment" ).value(attachment.buttonAlignment)
379
- attachment.actions?.let { writeActions(writer, it) }
380
- }
381
- writer.endObject()
382
- }
383
-
384
306
private fun writeActions (writer : JsonWriter , actions : List <Action >) {
385
307
if (actions.isNotEmpty()) {
386
308
writer.name(" actions" )
@@ -402,10 +324,6 @@ class AttachmentAdapter(moshi: Moshi, private val logger: Logger) : JsonAdapter<
402
324
writer.endArray()
403
325
}
404
326
}
405
-
406
- private fun checkNonNull (field : Any? , fieldName : String ) {
407
- if (field == null ) throw JsonDataException (" $fieldName is null" )
408
- }
409
327
}
410
328
411
329
class AttachmentAdapterFactory (private val logger : Logger ) : JsonAdapter.Factory {
0 commit comments