From b599d679c4d76166f4f4cfebef183b6932b7b435 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Mon, 22 Feb 2021 22:43:36 +0100 Subject: [PATCH] [Vulcan/Hebe] Implement sending messages. --- .../pl/szczodrzynski/edziennik/Extensions.kt | 6 ++ .../edziennik/data/api/Constants.kt | 1 + .../data/api/edziennik/vulcan/Vulcan.kt | 10 +++ .../vulcan/data/hebe/VulcanHebeSendMessage.kt | 82 +++++++++++++++++++ .../edziennik/data/db/entity/Teacher.kt | 4 + 5 files changed, 103 insertions(+) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/hebe/VulcanHebeSendMessage.kt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt b/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt index d4c6a2e0..a34f6af5 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt @@ -538,6 +538,12 @@ fun String.md5(): String { return BigInteger(1, md.digest(toByteArray())).toString(16).padStart(32, '0') } +fun String.sha1Hex(): String { + val md = MessageDigest.getInstance("SHA-1") + md.update(toByteArray()) + return md.digest().joinToString("") { "%02x".format(it) } +} + fun String.sha256(): ByteArray { val md = MessageDigest.getInstance("SHA-256") md.update(toByteArray()) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Constants.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Constants.kt index b98c0f33..f16c4f43 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Constants.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Constants.kt @@ -139,6 +139,7 @@ const val VULCAN_HEBE_ENDPOINT_NOTICES = "api/mobile/note" const val VULCAN_HEBE_ENDPOINT_ATTENDANCE = "api/mobile/lesson" const val VULCAN_HEBE_ENDPOINT_MESSAGES = "api/mobile/message" const val VULCAN_HEBE_ENDPOINT_MESSAGES_STATUS = "api/mobile/message/status" +const val VULCAN_HEBE_ENDPOINT_MESSAGES_SEND = "api/mobile/message" const val VULCAN_HEBE_ENDPOINT_LUCKY_NUMBER = "api/mobile/school/lucky" const val EDUDZIENNIK_USER_AGENT = "Szkolny.eu/${BuildConfig.VERSION_NAME}" diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/Vulcan.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/Vulcan.kt index 9dfc1572..d6c2c770 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/Vulcan.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/Vulcan.kt @@ -14,6 +14,7 @@ import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.data.api.VulcanApiAt import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.data.api.VulcanApiMessagesChangeStatus import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.data.api.VulcanApiSendMessage import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.data.hebe.VulcanHebeMessagesChangeStatus +import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.data.hebe.VulcanHebeSendMessage import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.firstlogin.VulcanFirstLogin import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.login.VulcanLogin import pl.szczodrzynski.edziennik.data.api.events.AttachmentGetEvent @@ -135,6 +136,15 @@ class Vulcan(val app: App, val profile: Profile?, val loginStore: LoginStore, va } override fun sendMessage(recipients: List, subject: String, text: String) { + if (loginStore.mode != LOGIN_MODE_VULCAN_API) { + login(LOGIN_METHOD_VULCAN_HEBE) { + VulcanHebeSendMessage(data, recipients, subject, text) { + completed() + } + } + return + } + login(LOGIN_METHOD_VULCAN_API) { VulcanApiSendMessage(data, recipients, subject, text) { completed() diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/hebe/VulcanHebeSendMessage.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/hebe/VulcanHebeSendMessage.kt new file mode 100644 index 00000000..8f5f5afa --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/hebe/VulcanHebeSendMessage.kt @@ -0,0 +1,82 @@ +/* + * Copyright (c) Kuba SzczodrzyƄski 2021-2-22. + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.data.hebe + +import com.google.gson.JsonObject +import org.greenrobot.eventbus.EventBus +import pl.szczodrzynski.edziennik.* +import pl.szczodrzynski.edziennik.data.api.VULCAN_HEBE_ENDPOINT_MESSAGES_SEND +import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.DataVulcan +import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.data.VulcanHebe +import pl.szczodrzynski.edziennik.data.api.events.MessageSentEvent +import pl.szczodrzynski.edziennik.data.db.entity.Message +import pl.szczodrzynski.edziennik.data.db.entity.Metadata +import pl.szczodrzynski.edziennik.data.db.entity.Teacher + +class VulcanHebeSendMessage( + override val data: DataVulcan, + val recipients: List, + val subject: String, + val text: String, + val onSuccess: () -> Unit +) : VulcanHebe(data, null) { + companion object { + const val TAG = "VulcanHebeSendMessage" + } + + init { + val recipientsArray = JsonArray() + recipients.forEach { teacher -> + recipientsArray += JsonObject( + "Address" to teacher.fullNameLastFirst, + "LoginId" to (teacher.loginId?.toIntOrNull() ?: return@forEach), + "Initials" to teacher.initialsLastFirst, + "AddressHash" to teacher.fullNameLastFirst.sha1Hex() + ) + } + + val senderName = (profile?.accountName ?: profile?.studentNameLong) + ?.swapFirstLastName() ?: "" + val sender = JsonObject( + "Address" to senderName, + "LoginId" to data.studentLoginId.toString(), + "Initials" to senderName.getNameInitials(), + "AddressHash" to senderName.sha1Hex() + ) + + apiPost( + TAG, + VULCAN_HEBE_ENDPOINT_MESSAGES_SEND, + payload = JsonObject( + "Status" to 1, + "Sender" to sender, + "DateSent" to null, + "DateRead" to null, + "Content" to text, + "Receiver" to recipientsArray, + "Id" to 0, + "Subject" to subject, + "Attachments" to null, + "Self" to null + ) + ) { json: JsonObject, _ -> + val messageId = json.getLong("Id") + + if (messageId == null) { + // TODO error + return@apiPost + } + + VulcanHebeMessages(data, null) { + val message = data.messageList.firstOrNull { it.type == Message.TYPE_SENT && it.subject == subject } + val metadata = data.metadataList.firstOrNull { it.thingType == Metadata.TYPE_MESSAGE && it.thingId == messageId } + val event = MessageSentEvent(data.profileId, message, message?.addedDate) + + EventBus.getDefault().postSticky(event) + onSuccess() + }.getMessages(Message.TYPE_SENT) + } + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Teacher.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Teacher.kt index 27431a4f..c41b8a7a 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Teacher.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Teacher.kt @@ -11,6 +11,7 @@ import androidx.room.Entity import androidx.room.Ignore import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.fixName +import pl.szczodrzynski.edziennik.getNameInitials import pl.szczodrzynski.edziennik.join import java.util.* @@ -180,6 +181,9 @@ open class Teacher { @delegate:Ignore val fullNameLastFirst by lazy { "$surname $name".fixName() } + @delegate:Ignore + val initialsLastFirst by lazy { fullNameLastFirst.getNameInitials() } + val shortName: String get() = (if (name == null || name?.length == 0) "" else name!![0].toString()) + "." + surname