DtoMappers.kt (3633B)
1 package org.gnunet.gnunetmessenger.ipc 2 3 import org.gnunet.gnunetmessenger.model.* 4 5 // DTO -> Local Model Mappers 6 fun ChatAccountDto.toLocal(): ChatAccount { 7 return ChatAccount( 8 key = this.key ?: "", 9 name = this.name ?: "", 10 pointer = 0 11 ) 12 } 13 14 fun ChatContactDto.toLocal(): ChatContact { 15 return ChatContact( 16 chatContext = this.chatContext?.toLocal() ?: ChatContext(ChatContextType.CONTACT, null, false, false), 17 name = this.name ?: "", 18 key = this.key ?: "", 19 blocked = this.isBlocked, 20 userPointer = this.userPointer?.takeIf { it.isNotEmpty() } 21 ) 22 } 23 24 fun ChatGroupDto.toLocal(): ChatGroup { 25 return ChatGroup( 26 chatContext = this.chatContext?.toLocal() ?: ChatContext(ChatContextType.GROUP, null, true, false), 27 name = this.name ?: "", 28 userPointer = this.userPointer?.takeIf { it.isNotEmpty() } 29 ) 30 } 31 32 fun ChatContextDto.toLocal(): ChatContext { 33 val type = ChatContextType.fromCode(chatContextType) 34 return ChatContext( 35 chatContextType = type, 36 userPointer = userPointer?.takeIf { it.isNotEmpty() }, 37 isGroup = isGroup, 38 isPlatform = isPlatform, 39 nativeContextPointer = nativeContextPointer?.takeIf { it.isNotEmpty() } 40 ?: userPointer?.takeIf { it.isNotEmpty() } 41 ) 42 } 43 44 fun ChatMessageDto.toLocal(ctx: ChatContext): ChatMessage { 45 val kindEnum = MessageKind.fromCode(kind) 46 val typeEnum = if (type < 0) null else ChatMessageType.fromCode(type) 47 val senderContact = if (!senderName.isNullOrEmpty() || !senderKey.isNullOrEmpty()) { 48 ChatContact( 49 chatContext = ChatContext(ChatContextType.CONTACT, null, false, false), 50 name = senderName ?: "", 51 key = senderKey ?: "" 52 ) 53 } else { 54 null 55 } 56 return ChatMessage( 57 chatContext = ctx, 58 text = text ?: "", 59 timestamp = timestamp, 60 sender = senderContact, 61 kind = kindEnum, 62 type = typeEnum 63 ) 64 } 65 66 fun ChatUriDto.toLocal(): ChatUri { 67 return ChatUri( 68 error = if (isValid) "" else (uri ?: "Invalid URI") 69 ) 70 } 71 72 // Local Model -> DTO Mappers 73 fun ChatAccount.toDto(): ChatAccountDto = 74 ChatAccountDto().apply { 75 key = this@toDto.key 76 name = this@toDto.name 77 } 78 79 fun ChatContact.toDto(): ChatContactDto = 80 ChatContactDto().apply { 81 chatContext = this@toDto.chatContext.toDto() 82 name = this@toDto.name 83 key = this@toDto.key 84 isBlocked = this@toDto.blocked 85 userPointer = this@toDto.userPointer 86 } 87 88 fun ChatGroup.toDto(): ChatGroupDto = 89 ChatGroupDto().apply { 90 chatContext = this@toDto.chatContext.toDto() 91 name = this@toDto.name 92 userPointer = this@toDto.userPointer 93 } 94 95 fun ChatContext.toDto(): ChatContextDto = 96 ChatContextDto().apply { 97 chatContextType = this@toDto.chatContextType?.code ?: 0 98 userPointer = this@toDto.userPointer 99 isGroup = this@toDto.isGroup 100 isPlatform = this@toDto.isPlatform 101 nativeContextPointer = this@toDto.nativeContextPointer 102 } 103 104 fun ChatMessage.toDto(): ChatMessageDto = 105 ChatMessageDto().apply { 106 chatContext = this@toDto.chatContext.toDto() 107 text = this@toDto.text 108 timestamp = this@toDto.timestamp 109 senderKey = this@toDto.sender?.key 110 senderName = this@toDto.sender?.name 111 kind = this@toDto.kind.code 112 type = this@toDto.type?.code ?: -1 113 } 114 115 fun ChatUri.toDto(): ChatUriDto = 116 ChatUriDto().apply { 117 uri = if (error.isEmpty()) "valid" else error 118 isValid = error.isEmpty() 119 }