forked from github/szkolny
[API/Mobidziennik] Add sent messages endpoint.
This commit is contained in:
parent
169a900f01
commit
626bbfa7a4
@ -54,6 +54,9 @@ object Regexes {
|
||||
val MOBIDZIENNIK_MESSAGE_ATTACHMENT by lazy {
|
||||
"""href="https://.+?\.mobidziennik.pl/.+?&(?:amp;)?zalacznik=([0-9]+)"(?:.+?<small.+?\(([0-9.]+)\s(M|K|G|)B\))*""".toRegex(DOT_MATCHES_ALL)
|
||||
}
|
||||
val MOBIDZIENNIK_MESSAGE_SENT_READ_BY by lazy {
|
||||
"""([0-9]+)/([0-9]+)""".toRegex()
|
||||
}
|
||||
|
||||
val MOBIDZIENNIK_MESSAGE_RECIPIENTS_JSON by lazy {
|
||||
"""odbiorcy: (\[.+?]),${'$'}""".toRegex(RegexOption.MULTILINE)
|
||||
|
@ -52,6 +52,10 @@ class MobidziennikData(val data: DataMobidziennik, val onSuccess: () -> Unit) {
|
||||
data.startProgress(R.string.edziennik_progress_endpoint_messages_inbox)
|
||||
MobidziennikWebMessagesInbox(data, lastSync, onSuccess)
|
||||
}
|
||||
ENDPOINT_MOBIDZIENNIK_WEB_MESSAGES_SENT -> {
|
||||
data.startProgress(R.string.edziennik_progress_endpoint_messages_outbox)
|
||||
MobidziennikWebMessagesSent(data, lastSync, onSuccess)
|
||||
}
|
||||
ENDPOINT_MOBIDZIENNIK_WEB_MESSAGES_ALL -> {
|
||||
data.startProgress(R.string.edziennik_progress_endpoint_messages)
|
||||
MobidziennikWebMessagesAll(data, lastSync, onSuccess)
|
||||
|
@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright (c) Kuba Szczodrzyński 2020-2-18.
|
||||
*/
|
||||
|
||||
package pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik.data.web
|
||||
|
||||
import org.jsoup.Jsoup
|
||||
import pl.szczodrzynski.edziennik.DAY
|
||||
import pl.szczodrzynski.edziennik.MainActivity.Companion.DRAWER_ITEM_MESSAGES
|
||||
import pl.szczodrzynski.edziennik.data.api.Regexes
|
||||
import pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik.DataMobidziennik
|
||||
import pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik.ENDPOINT_MOBIDZIENNIK_WEB_MESSAGES_SENT
|
||||
import pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik.data.MobidziennikWeb
|
||||
import pl.szczodrzynski.edziennik.data.db.entity.Message
|
||||
import pl.szczodrzynski.edziennik.data.db.entity.MessageRecipient
|
||||
import pl.szczodrzynski.edziennik.data.db.entity.Metadata
|
||||
import pl.szczodrzynski.edziennik.data.db.entity.SYNC_ALWAYS
|
||||
import pl.szczodrzynski.edziennik.fixName
|
||||
import pl.szczodrzynski.edziennik.get
|
||||
import pl.szczodrzynski.edziennik.singleOrNull
|
||||
import pl.szczodrzynski.edziennik.utils.models.Date
|
||||
|
||||
class MobidziennikWebMessagesSent(override val data: DataMobidziennik,
|
||||
override val lastSync: Long?,
|
||||
val onSuccess: (endpointId: Int) -> Unit
|
||||
) : MobidziennikWeb(data, lastSync) {
|
||||
companion object {
|
||||
private const val TAG = "MobidziennikWebMessagesSent"
|
||||
}
|
||||
|
||||
init {
|
||||
webGet(TAG, "/dziennik/wiadomosciwyslane") { text ->
|
||||
MobidziennikLuckyNumberExtractor(data, text)
|
||||
|
||||
if (text.contains("Brak wiadomości wysłanych.")) {
|
||||
data.setSyncNext(ENDPOINT_MOBIDZIENNIK_WEB_MESSAGES_SENT, SYNC_ALWAYS)
|
||||
onSuccess(ENDPOINT_MOBIDZIENNIK_WEB_MESSAGES_SENT)
|
||||
return@webGet
|
||||
}
|
||||
|
||||
val doc = Jsoup.parse(text)
|
||||
|
||||
val list = doc.getElementsByClass("spis").first().getElementsByClass("podswietl")
|
||||
for (item in list) {
|
||||
val id = item.attr("rel").toLongOrNull() ?: continue
|
||||
|
||||
val subjectEl = item.select("td:eq(0)").first()
|
||||
var hasAttachments = false
|
||||
if (subjectEl.getElementsByTag("a").size != 0) {
|
||||
hasAttachments = true
|
||||
}
|
||||
val subject = subjectEl.ownText()
|
||||
|
||||
val readByString = item.select("td:eq(2)").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 recipientNames = recipientEl.ownText().split(", ")
|
||||
val readState = when (readBy) {
|
||||
0 -> 0
|
||||
sentTo -> 1
|
||||
else -> -1
|
||||
}.toLong()
|
||||
for (recipientName in recipientNames) {
|
||||
val name = recipientName.fixName()
|
||||
val recipientId = data.teacherList.singleOrNull { it.fullNameLastFirst == name }?.id ?: -1
|
||||
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 message = Message(
|
||||
profileId,
|
||||
id,
|
||||
subject,
|
||||
null,
|
||||
Message.TYPE_SENT,
|
||||
-1,
|
||||
-1
|
||||
)
|
||||
|
||||
if (hasAttachments)
|
||||
message.setHasAttachments()
|
||||
|
||||
data.messageIgnoreList.add(message)
|
||||
data.setSeenMetadataList.add(
|
||||
Metadata(
|
||||
profileId,
|
||||
Metadata.TYPE_MESSAGE,
|
||||
message.id,
|
||||
true,
|
||||
true,
|
||||
addedDate
|
||||
))
|
||||
}
|
||||
|
||||
data.setSyncNext(ENDPOINT_MOBIDZIENNIK_WEB_MESSAGES_SENT, 1*DAY, DRAWER_ITEM_MESSAGES)
|
||||
onSuccess(ENDPOINT_MOBIDZIENNIK_WEB_MESSAGES_SENT)
|
||||
}
|
||||
}
|
||||
}
|
@ -18,8 +18,9 @@ public class MessageRecipient {
|
||||
@ColumnInfo(name = "messageRecipientReplyId")
|
||||
public long replyId = -1;
|
||||
/**
|
||||
* -1 for not read
|
||||
* 0 for read, date unknown
|
||||
* -1 for unknown
|
||||
* 0 for not read
|
||||
* 1 for read, date unknown
|
||||
* time in millis for read, date known
|
||||
*/
|
||||
@ColumnInfo(name = "messageRecipientReadDate")
|
||||
|
Loading…
x
Reference in New Issue
Block a user