mirror of
https://github.com/szkolny-eu/szkolny-android.git
synced 2024-11-24 19:04:38 -06:00
[API/Librus] Implement downloading messages using Synergia endpoints.
This commit is contained in:
parent
a6ce3a5068
commit
1a8134459a
@ -148,6 +148,9 @@ object Regexes {
|
||||
val LIBRUS_ATTACHMENT_KEY by lazy {
|
||||
"""singleUseKey=([0-9A-z_]+)""".toRegex()
|
||||
}
|
||||
val LIBRUS_MESSAGE_ID by lazy {
|
||||
"""/wiadomosci/[0-9]+/[0-9]+/([0-9]+?)/""".toRegex()
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -86,8 +86,8 @@ class Librus(val app: App, val profile: Profile?, val loginStore: LoginStore, va
|
||||
}
|
||||
|
||||
override fun getMessage(message: MessageFull) {
|
||||
login(LOGIN_METHOD_LIBRUS_MESSAGES) {
|
||||
LibrusMessagesGetMessage(data, message) {
|
||||
login(LOGIN_METHOD_LIBRUS_SYNERGIA) {
|
||||
LibrusSynergiaGetMessage(data, message) {
|
||||
completed()
|
||||
}
|
||||
}
|
||||
|
@ -50,6 +50,8 @@ const val ENDPOINT_LIBRUS_API_CLASS_FREE_DAYS = 1130
|
||||
const val ENDPOINT_LIBRUS_SYNERGIA_INFO = 2010
|
||||
const val ENDPOINT_LIBRUS_SYNERGIA_GRADES = 2020
|
||||
const val ENDPOINT_LIBRUS_SYNERGIA_HOMEWORK = 2030
|
||||
const val ENDPOINT_LIBRUS_SYNERGIA_MESSAGES_RECEIVED = 2040
|
||||
const val ENDPOINT_LIBRUS_SYNERGIA_MESSAGES_SENT = 2050
|
||||
const val ENDPOINT_LIBRUS_MESSAGES_RECEIVED = 3010
|
||||
const val ENDPOINT_LIBRUS_MESSAGES_SENT = 3020
|
||||
const val ENDPOINT_LIBRUS_MESSAGES_TRASH = 3030
|
||||
|
@ -8,6 +8,7 @@ import pl.szczodrzynski.edziennik.R
|
||||
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.*
|
||||
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.api.*
|
||||
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.messages.LibrusMessagesGetList
|
||||
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.synergia.LibrusSynergiaGetMessages
|
||||
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.synergia.LibrusSynergiaHomework
|
||||
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.synergia.LibrusSynergiaInfo
|
||||
import pl.szczodrzynski.edziennik.data.db.entity.Message
|
||||
@ -201,6 +202,14 @@ class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) {
|
||||
data.startProgress(R.string.edziennik_progress_endpoint_student_info)
|
||||
LibrusSynergiaInfo(data, lastSync, onSuccess)
|
||||
}
|
||||
ENDPOINT_LIBRUS_SYNERGIA_MESSAGES_RECEIVED -> {
|
||||
data.startProgress(R.string.edziennik_progress_endpoint_messages_inbox)
|
||||
LibrusSynergiaGetMessages(data, type = Message.TYPE_RECEIVED, lastSync = lastSync, onSuccess = onSuccess)
|
||||
}
|
||||
ENDPOINT_LIBRUS_SYNERGIA_MESSAGES_SENT -> {
|
||||
data.startProgress(R.string.edziennik_progress_endpoint_messages_outbox)
|
||||
LibrusSynergiaGetMessages(data, type = Message.TYPE_SENT, lastSync = lastSync, onSuccess = onSuccess)
|
||||
}
|
||||
|
||||
/**
|
||||
* MESSAGES
|
||||
|
@ -0,0 +1,160 @@
|
||||
package pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.synergia
|
||||
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
import org.jsoup.Jsoup
|
||||
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.DataLibrus
|
||||
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.LibrusSynergia
|
||||
import pl.szczodrzynski.edziennik.data.api.events.MessageGetEvent
|
||||
import pl.szczodrzynski.edziennik.data.db.entity.Message
|
||||
import pl.szczodrzynski.edziennik.data.db.entity.Metadata
|
||||
import pl.szczodrzynski.edziennik.data.db.entity.Teacher
|
||||
import pl.szczodrzynski.edziennik.data.db.full.MessageFull
|
||||
import pl.szczodrzynski.edziennik.data.db.full.MessageRecipientFull
|
||||
import pl.szczodrzynski.edziennik.get
|
||||
import pl.szczodrzynski.edziennik.isNotNullNorEmpty
|
||||
import pl.szczodrzynski.edziennik.singleOrNull
|
||||
import pl.szczodrzynski.edziennik.swapFirstLastName
|
||||
import pl.szczodrzynski.edziennik.utils.models.Date
|
||||
|
||||
class LibrusSynergiaGetMessage(override val data: DataLibrus,
|
||||
private val messageObject: MessageFull,
|
||||
val onSuccess: () -> Unit) : LibrusSynergia(data, null) {
|
||||
companion object {
|
||||
const val TAG = "LibrusSynergiaGetMessage"
|
||||
}
|
||||
|
||||
init {
|
||||
val endpoint = when (messageObject.type) {
|
||||
Message.TYPE_SENT -> "wiadomosci/1/6/${messageObject.id}/f0"
|
||||
else -> "wiadomosci/1/5/${messageObject.id}/f0"
|
||||
}
|
||||
|
||||
data.profile?.also { profile ->
|
||||
synergiaGet(TAG, endpoint) { text ->
|
||||
val doc = Jsoup.parse(text)
|
||||
|
||||
val messageElement = doc.select(".container-message tr")[0].child(1)
|
||||
val detailsElement = messageElement.child(1)
|
||||
val readElement = messageElement.children().last()
|
||||
|
||||
val body = messageElement.select(".container-message-content").html()
|
||||
|
||||
messageObject.apply {
|
||||
this.body = body
|
||||
|
||||
clearAttachments()
|
||||
if (messageElement.children().size >= 5) {
|
||||
messageElement.child(3).select("tr").forEachIndexed { i, attachment ->
|
||||
if (i == 0) return@forEachIndexed // Skip the header
|
||||
val filename = attachment.child(0).text().trim()
|
||||
val attachmentId = "wiadomosci\\\\/pobierz_zalacznik\\\\/[0-9]+?\\\\/([0-9]+)\"".toRegex()
|
||||
.find(attachment.select("img").attr("onclick"))?.get(1)
|
||||
?: return@forEachIndexed
|
||||
addAttachment(attachmentId.toLong(), filename, -1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val messageRecipientList = mutableListOf<MessageRecipientFull>()
|
||||
|
||||
when (messageObject.type) {
|
||||
Message.TYPE_RECEIVED -> {
|
||||
val senderFullName = detailsElement.child(0).select(".left").text()
|
||||
val senderGroupName = "\\[(.+?)]".toRegex().find(senderFullName)?.get(1)?.trim()
|
||||
|
||||
data.teacherList.singleOrNull { it.id == messageObject.senderId }?.apply {
|
||||
setTeacherType(when (senderGroupName) {
|
||||
/* https://api.librus.pl/2.0/Messages/Role */
|
||||
"Pomoc techniczna Librus", "SuperAdministrator" -> Teacher.TYPE_SUPER_ADMIN
|
||||
"Administrator szkoły" -> Teacher.TYPE_SCHOOL_ADMIN
|
||||
"Dyrektor Szkoły" -> Teacher.TYPE_PRINCIPAL
|
||||
"Nauczyciel" -> Teacher.TYPE_TEACHER
|
||||
"Rodzic", "Opiekun" -> Teacher.TYPE_PARENT
|
||||
"Sekretariat" -> Teacher.TYPE_SECRETARIAT
|
||||
"Uczeń" -> Teacher.TYPE_STUDENT
|
||||
"Pedagog/Psycholog szkolny" -> Teacher.TYPE_PEDAGOGUE
|
||||
"Pracownik biblioteki" -> Teacher.TYPE_LIBRARIAN
|
||||
"Inny specjalista" -> Teacher.TYPE_SPECIALIST
|
||||
"Jednostka Nadrzędna" -> {
|
||||
typeDescription = "Jednostka Nadrzędna"
|
||||
Teacher.TYPE_OTHER
|
||||
}
|
||||
"Jednostka Samorządu Terytorialnego" -> {
|
||||
typeDescription = "Jednostka Samorządu Terytorialnego"
|
||||
Teacher.TYPE_OTHER
|
||||
}
|
||||
else -> Teacher.TYPE_OTHER
|
||||
})
|
||||
}
|
||||
|
||||
val readDateText = readElement.select(".left").text()
|
||||
val readDate = when (readDateText.isNotNullNorEmpty()) {
|
||||
true -> Date.fromIso(readDateText)
|
||||
else -> 0
|
||||
}
|
||||
|
||||
val messageRecipientObject = MessageRecipientFull(
|
||||
profileId = profileId,
|
||||
id = -1,
|
||||
messageId = messageObject.id,
|
||||
readDate = readDate
|
||||
)
|
||||
|
||||
messageRecipientObject.fullName = profile.accountName
|
||||
?: profile.studentNameLong
|
||||
|
||||
messageRecipientList.add(messageRecipientObject)
|
||||
}
|
||||
|
||||
Message.TYPE_SENT -> {
|
||||
|
||||
readElement.select("tr").forEachIndexed { i, receiver ->
|
||||
if (i == 0) return@forEachIndexed // Skip the header
|
||||
|
||||
val receiverFullName = receiver.child(0).text()
|
||||
val receiverName = receiverFullName.split('(')[0].swapFirstLastName()
|
||||
|
||||
val teacher = data.teacherList.singleOrNull { it.fullName == receiverName }
|
||||
val receiverId = teacher?.id ?: -1
|
||||
|
||||
val readDate = when (val readDateText = receiver.child(1).text().trim()) {
|
||||
"NIE" -> 0
|
||||
else -> Date.fromIso(readDateText)
|
||||
}
|
||||
|
||||
val messageRecipientObject = MessageRecipientFull(
|
||||
profileId = profileId,
|
||||
id = receiverId,
|
||||
messageId = messageObject.id,
|
||||
readDate = readDate
|
||||
)
|
||||
|
||||
messageRecipientObject.fullName = receiverName
|
||||
|
||||
messageRecipientList.add(messageRecipientObject)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!messageObject.seen) {
|
||||
data.setSeenMetadataList.add(Metadata(
|
||||
messageObject.profileId,
|
||||
Metadata.TYPE_MESSAGE,
|
||||
messageObject.id,
|
||||
true,
|
||||
true
|
||||
))
|
||||
}
|
||||
|
||||
messageObject.recipients = messageRecipientList
|
||||
data.messageRecipientList.addAll(messageRecipientList)
|
||||
|
||||
data.messageList.add(messageObject)
|
||||
data.messageListReplace = true
|
||||
|
||||
EventBus.getDefault().postSticky(MessageGetEvent(messageObject))
|
||||
onSuccess()
|
||||
}
|
||||
} ?: onSuccess()
|
||||
}
|
||||
}
|
@ -0,0 +1,116 @@
|
||||
package pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.synergia
|
||||
|
||||
import org.jsoup.Jsoup
|
||||
import pl.szczodrzynski.edziennik.*
|
||||
import pl.szczodrzynski.edziennik.data.api.ERROR_NOT_IMPLEMENTED
|
||||
import pl.szczodrzynski.edziennik.data.api.Regexes
|
||||
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.*
|
||||
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.LibrusSynergia
|
||||
import pl.szczodrzynski.edziennik.data.db.entity.*
|
||||
import pl.szczodrzynski.edziennik.utils.Utils
|
||||
import pl.szczodrzynski.edziennik.utils.models.Date
|
||||
|
||||
class LibrusSynergiaGetMessages(override val data: DataLibrus,
|
||||
override val lastSync: Long?,
|
||||
private val type: Int = Message.TYPE_RECEIVED,
|
||||
archived: Boolean = false,
|
||||
val onSuccess: (Int) -> Unit) : LibrusSynergia(data, lastSync) {
|
||||
companion object {
|
||||
const val TAG = "LibrusSynergiaGetMessages"
|
||||
}
|
||||
|
||||
init {
|
||||
val endpoint = when (type) {
|
||||
Message.TYPE_RECEIVED -> "wiadomosci/5"
|
||||
Message.TYPE_SENT -> "wiadomosci/6"
|
||||
else -> null
|
||||
}
|
||||
val endpointId = when (type) {
|
||||
Message.TYPE_RECEIVED -> ENDPOINT_LIBRUS_SYNERGIA_MESSAGES_RECEIVED
|
||||
else -> ENDPOINT_LIBRUS_SYNERGIA_MESSAGES_SENT
|
||||
}
|
||||
|
||||
if (endpoint != null) {
|
||||
synergiaGet(TAG, endpoint) { text ->
|
||||
val doc = Jsoup.parse(text)
|
||||
|
||||
fun getRecipientId(name: String): Long = data.teacherList.singleOrNull {
|
||||
it.fullNameLastFirst == name
|
||||
}?.id ?: {
|
||||
val teacherObject = Teacher(
|
||||
profileId,
|
||||
-1 * Utils.crc16(name.swapFirstLastName().toByteArray()).toLong(),
|
||||
name.splitName()?.second!!,
|
||||
name.splitName()?.first!!
|
||||
)
|
||||
data.teacherList.put(teacherObject.id, teacherObject)
|
||||
teacherObject.id
|
||||
}.invoke()
|
||||
|
||||
doc.select(".decorated.stretch tbody > tr").forEach { messageElement ->
|
||||
val url = messageElement.select("a").first().attr("href")
|
||||
val id = Regexes.LIBRUS_MESSAGE_ID.find(url)?.get(1)?.toLong() ?: return@forEach
|
||||
val subject = messageElement.child(3).text()
|
||||
val sentDate = Date.fromIso(messageElement.child(4).text())
|
||||
val recipientName = messageElement.child(2).text().split('(')[0].fixName()
|
||||
val recipientId = getRecipientId(recipientName)
|
||||
val read = messageElement.child(2).attr("style").isNotEmpty()
|
||||
|
||||
val senderId = when (type) {
|
||||
Message.TYPE_RECEIVED -> recipientId
|
||||
else -> null
|
||||
}
|
||||
|
||||
val receiverId = when (type) {
|
||||
Message.TYPE_RECEIVED -> -1
|
||||
else -> recipientId
|
||||
}
|
||||
|
||||
val notified = when (type) {
|
||||
Message.TYPE_SENT -> true
|
||||
else -> profile?.empty ?: false
|
||||
}
|
||||
|
||||
val messageObject = Message(
|
||||
profileId = profileId,
|
||||
id = id,
|
||||
type = type,
|
||||
subject = subject,
|
||||
body = null,
|
||||
senderId = senderId,
|
||||
addedDate = sentDate
|
||||
)
|
||||
|
||||
val messageRecipientObject = MessageRecipient(
|
||||
profileId,
|
||||
receiverId,
|
||||
-1,
|
||||
if (read) 1 else 0,
|
||||
id
|
||||
)
|
||||
|
||||
messageObject.hasAttachments = !messageElement.child(1).select("img").isEmpty()
|
||||
|
||||
data.messageList.add(messageObject)
|
||||
data.messageRecipientList.add(messageRecipientObject)
|
||||
data.setSeenMetadataList.add(Metadata(
|
||||
profileId,
|
||||
Metadata.TYPE_MESSAGE,
|
||||
id,
|
||||
notified,
|
||||
notified
|
||||
))
|
||||
}
|
||||
|
||||
when (type) {
|
||||
Message.TYPE_RECEIVED -> data.setSyncNext(ENDPOINT_LIBRUS_MESSAGES_RECEIVED, SYNC_ALWAYS)
|
||||
Message.TYPE_SENT -> data.setSyncNext(ENDPOINT_LIBRUS_MESSAGES_SENT, DAY, MainActivity.DRAWER_ITEM_MESSAGES)
|
||||
}
|
||||
onSuccess(endpointId)
|
||||
}
|
||||
} else {
|
||||
data.error(TAG, ERROR_NOT_IMPLEMENTED)
|
||||
onSuccess(endpointId)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user