From dd99771c0b622b018c593c3b5d2a9c449e2d6366 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Wed, 30 Oct 2019 21:13:49 +0100 Subject: [PATCH] [APIv2/Idziennik] Add Messages - received and sent. --- .../pl/szczodrzynski/edziennik/Extensions.kt | 7 ++ .../edziennik/api/v2/Constants.kt | 7 ++ .../api/v2/idziennik/IdziennikFeatures.kt | 10 +- .../api/v2/idziennik/data/IdziennikData.kt | 10 ++ .../data/api/IdziennikApiMessagesInbox.kt | 94 +++++++++++++++++++ .../data/api/IdziennikApiMessagesSent.kt | 85 +++++++++++++++++ 6 files changed, 208 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/api/v2/idziennik/data/api/IdziennikApiMessagesInbox.kt create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/api/v2/idziennik/data/api/IdziennikApiMessagesSent.kt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt b/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt index b0167959..c03db59c 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt @@ -21,6 +21,7 @@ import pl.szczodrzynski.navlib.R import pl.szczodrzynski.navlib.getColorFromRes import java.text.SimpleDateFormat import java.util.* +import java.util.zip.CRC32 fun List.byId(id: Long) = firstOrNull { it.id == id } @@ -317,4 +318,10 @@ fun String.crc16(): Int { } crc = crc and 0xffff return crc + 32768 +} + +fun String.crc32(): Long { + val crc = CRC32() + crc.update(toByteArray()) + return crc.value } \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Constants.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Constants.kt index 49b24503..b34b6900 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Constants.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Constants.kt @@ -5,12 +5,15 @@ package pl.szczodrzynski.edziennik.api.v2 import android.os.Build +import pl.szczodrzynski.edziennik.BuildConfig const val GET = 0 const val POST = 1 val SYSTEM_USER_AGENT = System.getProperty("http.agent") ?: "Dalvik/2.1.0 Android" +val SERVER_USER_AGENT = "Szkolny.eu/${BuildConfig.VERSION_NAME} $SYSTEM_USER_AGENT" + val LIBRUS_USER_AGENT = "$SYSTEM_USER_AGENT LibrusMobileApp" const val SYNERGIA_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Gecko/20100101 Firefox/62.0" const val LIBRUS_CLIENT_ID = "wmSyUMo8llDAs4y9tJVYY92oyZ6h4lAt7KCuy0Gv" @@ -61,6 +64,10 @@ const val IDZIENNIK_WEB_MESSAGES_LIST = "mod_komunikator/WS_wiadomosci.asmx/Pobi val IDZIENNIK_API_USER_AGENT = SYSTEM_USER_AGENT const val IDZIENNIK_API_URL = "https://iuczniowie.progman.pl/idziennik/api" const val IDZIENNIK_API_CURRENT_REGISTER = "Uczniowie/\$STUDENT_ID/AktualnyDziennik" +const val IDZIENNIK_API_GRADES = "Uczniowie/\$STUDENT_ID/Oceny/" /* + semester */ +const val IDZIENNIK_API_MESSAGES_INBOX = "Wiadomosci/Odebrane" +const val IDZIENNIK_API_MESSAGES_SENT = "Wiadomosci/Wyslane" + val MOBIDZIENNIK_USER_AGENT = SYSTEM_USER_AGENT diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/idziennik/IdziennikFeatures.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/idziennik/IdziennikFeatures.kt index eac360e4..c83c9690 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/idziennik/IdziennikFeatures.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/idziennik/IdziennikFeatures.kt @@ -46,19 +46,19 @@ val IdziennikFeatures = listOf( ENDPOINT_IDZIENNIK_WEB_ANNOUNCEMENTS to LOGIN_METHOD_IDZIENNIK_WEB ), listOf(LOGIN_METHOD_IDZIENNIK_WEB)), - Feature(LOGIN_TYPE_IDZIENNIK, FEATURE_MESSAGES_INBOX, listOf( + /*Feature(LOGIN_TYPE_IDZIENNIK, FEATURE_MESSAGES_INBOX, listOf( ENDPOINT_IDZIENNIK_WEB_MESSAGES_INBOX to LOGIN_METHOD_IDZIENNIK_WEB - ), listOf(LOGIN_METHOD_IDZIENNIK_WEB)), + ), listOf(LOGIN_METHOD_IDZIENNIK_WEB)).withPriority(2), Feature(LOGIN_TYPE_IDZIENNIK, FEATURE_MESSAGES_SENT, listOf( ENDPOINT_IDZIENNIK_WEB_MESSAGES_SENT to LOGIN_METHOD_IDZIENNIK_WEB - ), listOf(LOGIN_METHOD_IDZIENNIK_WEB)), + ), listOf(LOGIN_METHOD_IDZIENNIK_WEB)).withPriority(2),*/ Feature(LOGIN_TYPE_IDZIENNIK, FEATURE_MESSAGES_INBOX, listOf( ENDPOINT_IDZIENNIK_API_MESSAGES_INBOX to LOGIN_METHOD_IDZIENNIK_API - ), listOf(LOGIN_METHOD_IDZIENNIK_WEB)), + ), listOf(LOGIN_METHOD_IDZIENNIK_API)), Feature(LOGIN_TYPE_IDZIENNIK, FEATURE_MESSAGES_SENT, listOf( ENDPOINT_IDZIENNIK_API_MESSAGES_SENT to LOGIN_METHOD_IDZIENNIK_API - ), listOf(LOGIN_METHOD_IDZIENNIK_WEB)), + ), listOf(LOGIN_METHOD_IDZIENNIK_API)), Feature(LOGIN_TYPE_IDZIENNIK, FEATURE_LUCKY_NUMBER, listOf( ENDPOINT_IDZIENNIK_API_CURRENT_REGISTER to LOGIN_METHOD_IDZIENNIK_API diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/idziennik/data/IdziennikData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/idziennik/data/IdziennikData.kt index 4f3b3e66..a3de93a7 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/idziennik/data/IdziennikData.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/idziennik/data/IdziennikData.kt @@ -7,6 +7,8 @@ package pl.szczodrzynski.edziennik.api.v2.idziennik.data import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.api.v2.idziennik.* import pl.szczodrzynski.edziennik.api.v2.idziennik.data.api.IdziennikApiCurrentRegister +import pl.szczodrzynski.edziennik.api.v2.idziennik.data.api.IdziennikApiMessagesInbox +import pl.szczodrzynski.edziennik.api.v2.idziennik.data.api.IdziennikApiMessagesSent import pl.szczodrzynski.edziennik.api.v2.idziennik.data.web.* import pl.szczodrzynski.edziennik.utils.Utils @@ -70,6 +72,14 @@ class IdziennikData(val data: DataIdziennik, val onSuccess: () -> Unit) { data.startProgress(R.string.edziennik_progress_endpoint_lucky_number) IdziennikApiCurrentRegister(data) { onSuccess() } } + ENDPOINT_IDZIENNIK_API_MESSAGES_INBOX -> { + data.startProgress(R.string.edziennik_progress_endpoint_messages_inbox) + IdziennikApiMessagesInbox(data) { onSuccess() } + } + ENDPOINT_IDZIENNIK_API_MESSAGES_SENT -> { + data.startProgress(R.string.edziennik_progress_endpoint_messages_outbox) + IdziennikApiMessagesSent(data) { onSuccess() } + } else -> onSuccess() } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/idziennik/data/api/IdziennikApiMessagesInbox.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/idziennik/data/api/IdziennikApiMessagesInbox.kt new file mode 100644 index 00000000..a1352a60 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/idziennik/data/api/IdziennikApiMessagesInbox.kt @@ -0,0 +1,94 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-10-30. + */ + +package pl.szczodrzynski.edziennik.api.v2.idziennik.data.api + +import com.google.gson.JsonArray +import pl.szczodrzynski.edziennik.api.v2.IDZIENNIK_API_MESSAGES_INBOX +import pl.szczodrzynski.edziennik.api.v2.idziennik.DataIdziennik +import pl.szczodrzynski.edziennik.api.v2.idziennik.ENDPOINT_IDZIENNIK_API_MESSAGES_INBOX +import pl.szczodrzynski.edziennik.api.v2.idziennik.data.IdziennikApi +import pl.szczodrzynski.edziennik.asJsonObjectList +import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS +import pl.szczodrzynski.edziennik.data.db.modules.messages.Message +import pl.szczodrzynski.edziennik.data.db.modules.messages.Message.TYPE_DELETED +import pl.szczodrzynski.edziennik.data.db.modules.messages.Message.TYPE_RECEIVED +import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageRecipient +import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata +import pl.szczodrzynski.edziennik.getBoolean +import pl.szczodrzynski.edziennik.getString +import pl.szczodrzynski.edziennik.utils.Utils.crc32 +import pl.szczodrzynski.edziennik.utils.models.Date + +class IdziennikApiMessagesInbox(override val data: DataIdziennik, + val onSuccess: () -> Unit) : IdziennikApi(data) { + companion object { + private const val TAG = "IdziennikApiMessagesInbox" + } + + init { + apiGet(TAG, IDZIENNIK_API_MESSAGES_INBOX) { json -> + if (json !is JsonArray) { + onSuccess() + return@apiGet + } + + json.asJsonObjectList()?.forEach { jMessage -> + val subject = jMessage.getString("tytul") + if (subject?.contains("(") == true && subject.startsWith("iDziennik - ")) + return@forEach + if (subject?.startsWith("Uwaga dla ucznia (klasa:") == true) + return@forEach + + val messageIdStr = jMessage.getString("id") + val messageId = crc32((messageIdStr + "0").toByteArray()) + + var body = "[META:$messageIdStr;-1]" + body += jMessage.getString("tresc")?.replace("\n".toRegex(), "
") + + val readDate = if (jMessage.getBoolean("odczytana") == true) Date.fromIso(jMessage.getString("wersjaRekordu")) else 0 + val sentDate = Date.fromIso(jMessage.getString("dataWyslania")) + + val sender = jMessage.getAsJsonObject("nadawca") + val rTeacher = data.getTeacher( + sender.getString("imie") ?: "", + sender.getString("nazwisko") ?: "" + ) + rTeacher.loginId = sender.getString("id") + ":" + sender.getString("usr") + + val message = Message( + profileId, + messageId, + subject, + body, + if (jMessage.getBoolean("rekordUsuniety") == true) TYPE_DELETED else TYPE_RECEIVED, + rTeacher.id, + -1 + ) + + val messageRecipient = MessageRecipient( + profileId, + -1 /* me */, + -1, + readDate, + /*messageId*/ messageId + ) + + data.messageList.add(message) + data.messageRecipientList.add(messageRecipient) + data.messageMetadataList.add(Metadata( + profileId, + Metadata.TYPE_MESSAGE, + message.id, + readDate > 0, + readDate > 0 || profile?.empty ?: false, + sentDate + )) + } + + data.setSyncNext(ENDPOINT_IDZIENNIK_API_MESSAGES_INBOX, SYNC_ALWAYS) + onSuccess() + } + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/idziennik/data/api/IdziennikApiMessagesSent.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/idziennik/data/api/IdziennikApiMessagesSent.kt new file mode 100644 index 00000000..8a4ca455 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/idziennik/data/api/IdziennikApiMessagesSent.kt @@ -0,0 +1,85 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-10-30. + */ + +package pl.szczodrzynski.edziennik.api.v2.idziennik.data.api + +import com.google.gson.JsonArray +import pl.szczodrzynski.edziennik.DAY +import pl.szczodrzynski.edziennik.MainActivity.Companion.DRAWER_ITEM_MESSAGES +import pl.szczodrzynski.edziennik.api.v2.IDZIENNIK_API_MESSAGES_SENT +import pl.szczodrzynski.edziennik.api.v2.idziennik.DataIdziennik +import pl.szczodrzynski.edziennik.api.v2.idziennik.ENDPOINT_IDZIENNIK_API_MESSAGES_SENT +import pl.szczodrzynski.edziennik.api.v2.idziennik.data.IdziennikApi +import pl.szczodrzynski.edziennik.asJsonObjectList +import pl.szczodrzynski.edziennik.data.db.modules.messages.Message +import pl.szczodrzynski.edziennik.data.db.modules.messages.Message.TYPE_SENT +import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageRecipient +import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata +import pl.szczodrzynski.edziennik.utils.Utils.crc32 +import pl.szczodrzynski.edziennik.utils.models.Date + +class IdziennikApiMessagesSent(override val data: DataIdziennik, + val onSuccess: () -> Unit) : IdziennikApi(data) { + companion object { + private const val TAG = "IdziennikApiMessagesSent" + } + + init { + apiGet(TAG, IDZIENNIK_API_MESSAGES_SENT) { json -> + if (json !is JsonArray) { + onSuccess() + return@apiGet + } + + json.asJsonObjectList()?.forEach { jMessage -> + val messageIdStr = jMessage.get("id").asString + val messageId = crc32((messageIdStr + "1").toByteArray()) + + val subject = jMessage.get("tytul").asString + + var body = "[META:$messageIdStr;-1]" + body += jMessage.get("tresc").asString.replace("\n".toRegex(), "
") + + val sentDate = Date.fromIso(jMessage.get("dataWyslania").asString) + + val message = Message( + profileId, + messageId, + subject, + body, + TYPE_SENT, + -1, + -1 + ) + + for (recipientEl in jMessage.getAsJsonArray("odbiorcy")) { + val recipient = recipientEl.asJsonObject + var firstName = recipient.get("imie").asString + var lastName = recipient.get("nazwisko").asString + if (firstName.isEmpty() || lastName.isEmpty()) { + firstName = "usunięty" + lastName = "użytkownik" + } + val rTeacher = data.getTeacher(firstName, lastName) + rTeacher.loginId = recipient.get("id").asString + ":" + recipient.get("usr").asString + + val messageRecipient = MessageRecipient( + profileId, + rTeacher.id, + -1, + -1, + /*messageId*/ messageId + ) + data.messageRecipientIgnoreList.add(messageRecipient) + } + + data.messageList.add(message) + data.metadataList.add(Metadata(profileId, Metadata.TYPE_MESSAGE, message.id, true, true, sentDate)) + } + + data.setSyncNext(ENDPOINT_IDZIENNIK_API_MESSAGES_SENT, DAY, DRAWER_ITEM_MESSAGES) + onSuccess() + } + } +}