diff --git a/libsession/src/main/java/org/session/libsession/messaging/messages/control/ClosedGroupUpdate.kt b/libsession/src/main/java/org/session/libsession/messaging/messages/control/ClosedGroupUpdate.kt index 9b0ba71cf6..f72c8a1dce 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/messages/control/ClosedGroupUpdate.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/messages/control/ClosedGroupUpdate.kt @@ -141,7 +141,7 @@ class ClosedGroupUpdate() : ControlMessage() { // extension functions to class ClosedGroupSenderKey -private fun ClosedGroupSenderKey.Companion.fromProto(proto: SignalServiceProtos.ClosedGroupUpdate.SenderKey): org.session.libsignal.service.loki.protocol.closedgroups.ClosedGroupSenderKey { +private fun ClosedGroupSenderKey.Companion.fromProto(proto: SignalServiceProtos.ClosedGroupUpdate.SenderKey): ClosedGroupSenderKey { return ClosedGroupSenderKey(chainKey = proto.chainKey.toByteArray(), keyIndex = proto.keyIndex, publicKey = proto.publicKey.toByteArray()) } diff --git a/libsession/src/main/java/org/session/libsession/messaging/messages/visible/Attachment.kt b/libsession/src/main/java/org/session/libsession/messaging/messages/visible/Attachment.kt new file mode 100644 index 0000000000..3e1530f34e --- /dev/null +++ b/libsession/src/main/java/org/session/libsession/messaging/messages/visible/Attachment.kt @@ -0,0 +1,71 @@ +package org.session.libsession.messaging.messages.visible + +import android.util.Size +import android.webkit.MimeTypeMap +import org.session.libsignal.service.internal.push.SignalServiceProtos +import java.io.File +import java.net.URL +import kotlin.math.absoluteValue + +class Attachment : VisibleMessage() { + + var fileName: String? = null + var contentType: String? = null + var key: ByteArray? = null + var digest: ByteArray? = null + var kind: Kind? = null + var caption: String? = null + var size: Size? = null + var sizeInBytes: Int? = 0 + var url: String? = null + + companion object { + fun fromProto(proto: SignalServiceProtos.AttachmentPointer): Attachment? { + val result = Attachment() + result.fileName = proto.fileName + fun inferContentType(): String { + val fileName = result.fileName ?: return "application/octet-stream" //TODO find equivalent to OWSMimeTypeApplicationOctetStream + val fileExtension = File(fileName).extension + val mimeTypeMap = MimeTypeMap.getSingleton() + return mimeTypeMap.getMimeTypeFromExtension(fileExtension) ?: "application/octet-stream" //TODO check that it's correct + } + result.contentType = proto.contentType ?: inferContentType() + result.key = proto.key.toByteArray() + result.digest = proto.digest.toByteArray() + val kind: Kind + if (proto.hasFlags() && (proto.flags and SignalServiceProtos.AttachmentPointer.Flags.VOICE_MESSAGE_VALUE) > 0) { + kind = Kind.VOICEMESSAGE + } else { + kind = Kind.GENERIC + } + result.kind = kind + result.caption = if (proto.hasCaption()) proto.caption else null + val size: Size + if (proto.hasWidth() && proto.width > 0 && proto.hasHeight() && proto.height > 0) { + size = Size(proto.width, proto.height) + } else { + size = Size(0,0) //TODO check that it's equivalent to swift: CGSize.zero + } + result.size = size + result.sizeInBytes = if (proto.size > 0) proto.size else null + result. url = proto.url + return result + } + } + + enum class Kind { + VOICEMESSAGE, + GENERIC + } + + // validation + override fun isValid(): Boolean { + if (!super.isValid()) return false + // key and digest can be nil for open group attachments + return (contentType != null && kind != null && size != null && sizeInBytes != null && url != null) + } + + override fun toProto(transaction: String): SignalServiceProtos.AttachmentPointer? { + TODO("Not implemented") + } +} \ No newline at end of file diff --git a/libsession/src/main/java/org/session/libsession/messaging/messages/visible/attachments/Attachment.kt b/libsession/src/main/java/org/session/libsession/messaging/messages/visible/attachments/Attachment.kt deleted file mode 100644 index f50ffb6baa..0000000000 --- a/libsession/src/main/java/org/session/libsession/messaging/messages/visible/attachments/Attachment.kt +++ /dev/null @@ -1,17 +0,0 @@ -package org.session.libsession.messaging.messages.visible.attachments - -import org.session.libsession.messaging.messages.visible.BaseVisibleMessage -import org.session.libsignal.service.internal.push.SignalServiceProtos - -internal class Attachment : BaseVisibleMessage() { - - companion object { - fun fromProto(proto: SignalServiceProtos.Content): Attachment? { - TODO("Not yet implemented") - } - } - - override fun toProto(): SignalServiceProtos.Content? { - TODO("Not yet implemented") - } -} \ No newline at end of file