From aeed735521e4ab5bfdc2db7cb5c4bf0da63ed334 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Sat, 5 Feb 2022 17:39:15 +0100 Subject: [PATCH] [Mobidziennik] Fix syncing message lists. --- .../mobidziennik/DataMobidziennik.kt | 27 +++++++++++++++++ .../data/web/MobidziennikWebMessagesInbox.kt | 19 ++++++++---- .../data/web/MobidziennikWebMessagesSent.kt | 29 +++++++++++++------ 3 files changed, 61 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/DataMobidziennik.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/DataMobidziennik.kt index 3553d72d..cee3cda5 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/DataMobidziennik.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/DataMobidziennik.kt @@ -7,10 +7,12 @@ package pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik import android.util.LongSparseArray import pl.szczodrzynski.edziennik.App import pl.szczodrzynski.edziennik.data.api.LOGIN_METHOD_MOBIDZIENNIK_WEB +import pl.szczodrzynski.edziennik.data.api.Regexes import pl.szczodrzynski.edziennik.data.api.models.Data import pl.szczodrzynski.edziennik.data.db.entity.LoginStore import pl.szczodrzynski.edziennik.data.db.entity.Profile import pl.szczodrzynski.edziennik.ext.currentTimeUnix +import pl.szczodrzynski.edziennik.ext.get import pl.szczodrzynski.edziennik.ext.isNotNullNorEmpty import pl.szczodrzynski.edziennik.utils.models.Date import pl.szczodrzynski.edziennik.utils.models.Time @@ -35,6 +37,31 @@ class DataMobidziennik(app: App, profile: Profile?, loginStore: LoginStore) : Da override fun generateUserCode() = "$loginServerName:$loginUsername:$studentId" + fun parseDateTime(dateStr: String): Pair { + // pt, 4 lut, 09:11 + val dateParts = dateStr.split(',', ' ').filter { it.isNotEmpty() } + // [pt], [4], [lut], [09:11] + val date = Date.getToday() + date.day = dateParts[1].toIntOrNull() ?: 1 + date.month = when (dateParts[2]) { + "sty" -> 1 + "lut" -> 2 + "mar" -> 3 + "kwi" -> 4 + "maj" -> 5 + "cze" -> 6 + "lip" -> 7 + "sie" -> 8 + "wrz" -> 9 + "paź" -> 10 + "lis" -> 11 + "gru" -> 12 + else -> 1 + } + val time = Time.fromH_m(dateParts[3]) + return date to time + } + val teachersMap = LongSparseArray() val subjectsMap = LongSparseArray() diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/web/MobidziennikWebMessagesInbox.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/web/MobidziennikWebMessagesInbox.kt index ed9c2ae1..b3819784 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/web/MobidziennikWebMessagesInbox.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/web/MobidziennikWebMessagesInbox.kt @@ -36,6 +36,10 @@ class MobidziennikWebMessagesInbox(override val data: DataMobidziennik, val doc = Jsoup.parse(text) + val today = Date.getToday() + var currentYear = today.year + var currentMonth = today.month + val list = doc.getElementsByClass("spis").first()?.getElementsByClass("podswietl") list?.forEach { item -> val id = item.attr("rel").toLongOrNull() ?: return@forEach @@ -47,15 +51,20 @@ class MobidziennikWebMessagesInbox(override val data: DataMobidziennik, } val subject = subjectEl?.ownText() ?: "" - val addedDateEl = item.select("td:eq(1) small").first() - val addedDate = Date.fromIsoHm(addedDateEl?.text()) + val addedDateEl = item.select("td:eq(4)").first() + val (date, time) = data.parseDateTime(addedDateEl?.text()?.trim() ?: "") + if (date.month > currentMonth) { + currentYear-- + } + currentMonth = date.month + date.year = currentYear - val senderEl = item.select("td:eq(2)").first() + val senderEl = item.select("td:eq(3)").first() val senderName = senderEl?.ownText().fixName() val senderId = data.teacherList.singleOrNull { it.fullNameLastFirst == senderName }?.id data.messageRecipientIgnoreList.add(MessageRecipient(profileId, -1, id)) - val isRead = item.select("td:eq(3) span").first()?.hasClass("wiadomosc_przeczytana") == true + val isRead = item.select("td:eq(5) span").first()?.hasClass("wiadomosc_przeczytana") == true val message = Message( profileId = profileId, @@ -64,7 +73,7 @@ class MobidziennikWebMessagesInbox(override val data: DataMobidziennik, subject = subject, body = null, senderId = senderId, - addedDate = addedDate + addedDate = date.combineWith(time) ) if (hasAttachments) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/web/MobidziennikWebMessagesSent.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/web/MobidziennikWebMessagesSent.kt index c6508a26..7f249cf6 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/web/MobidziennikWebMessagesSent.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/web/MobidziennikWebMessagesSent.kt @@ -40,23 +40,29 @@ class MobidziennikWebMessagesSent(override val data: DataMobidziennik, val doc = Jsoup.parse(text) + val today = Date.getToday() + var currentYear = today.year + var currentMonth = today.month + val list = doc.getElementsByClass("spis").first()?.getElementsByClass("podswietl") list?.forEach { item -> val id = item.attr("rel").toLongOrNull() ?: return@forEach val subjectEl = item.select("td:eq(0)").first() - var hasAttachments = false - if (subjectEl?.getElementsByTag("a")?.size ?: 0 > 0) { - hasAttachments = true - } val subject = subjectEl?.ownText() ?: "" - val readByString = item.select("td:eq(2)").first()?.text() ?: "" + val attachmentsEl = item.select("td:eq(1)").first() + var hasAttachments = false + if (attachmentsEl?.getElementsByTag("a")?.size ?: 0 > 0) { + hasAttachments = true + } + + val readByString = item.select("td:eq(4)").first()?.text() ?: "" val (readBy, sentTo) = Regexes.MOBIDZIENNIK_MESSAGE_SENT_READ_BY.find(readByString).let { (it?.get(1)?.toIntOrNull() ?: 0) to (it?.get(2)?.toIntOrNull() ?: 0) } - val recipientEl = item.select("td:eq(1) a span").first() + val recipientEl = item.select("td:eq(2) a span").first() val recipientNames = recipientEl?.ownText()?.split(", ") val readState = when (readBy) { 0 -> 0 @@ -69,8 +75,13 @@ class MobidziennikWebMessagesSent(override val data: DataMobidziennik, data.messageRecipientIgnoreList.add(MessageRecipient(profileId, recipientId, -1, readState, id)) } - val addedDateEl = item.select("td:eq(3) small").first() - val addedDate = Date.fromIsoHm(addedDateEl?.text()) + val addedDateEl = item.select("td:eq(3)").first() + val (date, time) = data.parseDateTime(addedDateEl?.text()?.trim() ?: "") + if (date.month > currentMonth) { + currentYear-- + } + currentMonth = date.month + date.year = currentYear val message = Message( profileId = profileId, @@ -79,7 +90,7 @@ class MobidziennikWebMessagesSent(override val data: DataMobidziennik, subject = subject, body = null, senderId = null, - addedDate = addedDate + addedDate = date.combineWith(time) ) if (hasAttachments)