From feec22bf722aeb83bdf6603c34230939e2406554 Mon Sep 17 00:00:00 2001 From: Brice Date: Tue, 1 Dec 2020 17:35:47 +1100 Subject: [PATCH] Profile implementation --- .../messaging/messages/visible/Profile.kt | 53 ++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/libsession/src/main/java/org/session/libsession/messaging/messages/visible/Profile.kt b/libsession/src/main/java/org/session/libsession/messaging/messages/visible/Profile.kt index 193966e003..417599962c 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/messages/visible/Profile.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/messages/visible/Profile.kt @@ -1,16 +1,65 @@ package org.session.libsession.messaging.messages.visible +import com.google.protobuf.ByteString +import org.session.libsignal.libsignal.logging.Log import org.session.libsignal.service.internal.push.SignalServiceProtos class Profile() : VisibleMessage() { + var displayName: String? = null + var profileKey: ByteArray? = null + var profilePictureURL: String? = null + companion object { + const val TAG = "Profile" + fun fromProto(proto: SignalServiceProtos.DataMessage): Profile? { - TODO("Not yet implemented") + val profileProto = proto.profile ?: return null + val displayName = profileProto.displayName ?: return null + val profileKey = proto.profileKey + val profilePictureURL = profileProto.profilePictureURL + profileKey?.let { + val profilePictureURL = profilePictureURL + profilePictureURL?.let { + return Profile(displayName = displayName, profileKey = profileKey.toByteArray(), profilePictureURL = profilePictureURL) + } + return Profile(displayName) + } + } } + //constructor + internal constructor(displayName: String, profileKey: ByteArray? = nil, profilePictureURL: String? = nil) : this() { + this.displayName = displayName + this.profileKey = profileKey + this.profilePictureURL = profilePictureURL + } + + fun toProto(): SignalServiceProtos.DataMessage? { + return this.toProto("") + } + override fun toProto(transaction: String): SignalServiceProtos.DataMessage? { - return null + val displayName = displayName + if (displayName == null) { + Log.w(TAG, "Couldn't construct link preview proto from: $this") + return null + } + val dataMessageProto = SignalServiceProtos.DataMessage.newBuilder() + val profileProto = SignalServiceProtos.LokiUserProfile.newBuilder() + profileProto.displayName = displayName + val profileKey = profileKey + profileKey?.let { dataMessageProto.profileKey = ByteString.copyFrom(profileKey) } + val profilePictureURL = profilePictureURL + profilePictureURL?.let { profileProto.profilePictureURL = profilePictureURL } + // Build + try { + dataMessageProto.profile = profileProto.build() + return dataMessageProto.build() + } catch (e: Exception) { + Log.w(TAG, "Couldn't construct profile proto from: $this") + return null + } } } \ No newline at end of file