[APIv2/Librus] Add getting received and sent messages

This commit is contained in:
Kacper Ziubryniewicz 2019-10-26 00:14:14 +02:00
parent e38dc011bd
commit 454e8caa0d
5 changed files with 149 additions and 1 deletions

View File

@ -45,6 +45,7 @@ const val ERROR_LOGIN_DATA_INVALID = 102
const val ERROR_PROFILE_MISSING = 105
const val ERROR_INVALID_LOGIN_MODE = 110
const val ERROR_LOGIN_METHOD_NOT_SATISFIED = 111
const val ERROR_NOT_IMPLEMENTED = 112
const val ERROR_NO_STUDENTS_IN_ACCOUNT = 115

View File

@ -7,8 +7,10 @@ package pl.szczodrzynski.edziennik.api.v2.librus.data
import pl.szczodrzynski.edziennik.R
import pl.szczodrzynski.edziennik.api.v2.librus.*
import pl.szczodrzynski.edziennik.api.v2.librus.data.api.*
import pl.szczodrzynski.edziennik.api.v2.librus.data.messages.LibrusMessagesGetList
import pl.szczodrzynski.edziennik.api.v2.librus.data.synergia.LibrusSynergiaHomework
import pl.szczodrzynski.edziennik.api.v2.librus.data.synergia.LibrusSynergiaInfo
import pl.szczodrzynski.edziennik.data.db.modules.messages.Message
import pl.szczodrzynski.edziennik.utils.Utils
class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) {
@ -37,6 +39,9 @@ class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) {
private fun useEndpoint(endpointId: Int, onSuccess: () -> Unit) {
Utils.d(TAG, "Using endpoint $endpointId")
when (endpointId) {
/**
* API
*/
ENDPOINT_LIBRUS_API_ME -> {
data.startProgress(R.string.edziennik_progress_endpoint_student_info)
LibrusApiMe(data) { onSuccess() }
@ -127,6 +132,9 @@ class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) {
LibrusApiTeacherFreeDays(data) { onSuccess() }
}
/**
* SYNERGIA
*/
ENDPOINT_LIBRUS_SYNERGIA_HOMEWORK -> {
data.startProgress(R.string.edziennik_progress_endpoint_homework)
LibrusSynergiaHomework(data) { onSuccess() }
@ -135,6 +143,19 @@ class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) {
data.startProgress(R.string.edziennik_progress_endpoint_student_info)
LibrusSynergiaInfo(data) { onSuccess() }
}
/**
* MESSAGES
*/
ENDPOINT_LIBRUS_MESSAGES_RECEIVED -> {
data.startProgress(R.string.edziennik_progress_endpoint_messages_inbox)
LibrusMessagesGetList(data, type = Message.TYPE_RECEIVED) { onSuccess() }
}
ENDPOINT_LIBRUS_MESSAGES_SENT -> {
data.startProgress(R.string.edziennik_progress_endpoint_messages_outbox)
LibrusMessagesGetList(data, type = Message.TYPE_SENT) { onSuccess() }
}
else -> onSuccess()
}
}

View File

@ -0,0 +1,125 @@
/*
* Copyright (c) Kacper Ziubryniewicz 2019-10-24
*/
package pl.szczodrzynski.edziennik.api.v2.librus.data.messages
import pl.szczodrzynski.edziennik.DAY
import pl.szczodrzynski.edziennik.HOUR
import pl.szczodrzynski.edziennik.MainActivity.Companion.DRAWER_ITEM_MESSAGES
import pl.szczodrzynski.edziennik.api.v2.ERROR_NOT_IMPLEMENTED
import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus
import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_MESSAGES_RECEIVED
import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_MESSAGES_SENT
import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusMessages
import pl.szczodrzynski.edziennik.data.db.modules.messages.Message
import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageRecipient
import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata
import pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher
import pl.szczodrzynski.edziennik.singleOrNull
import pl.szczodrzynski.edziennik.utils.Utils
import pl.szczodrzynski.edziennik.utils.models.Date
class LibrusMessagesGetList(override val data: DataLibrus, private val type: Int = Message.TYPE_RECEIVED,
archived: Boolean = false, val onSuccess: () -> Unit) : LibrusMessages(data) {
companion object {
const val TAG = "LibrusMessagesGetList"
}
init {
val endpoint = when (type) {
Message.TYPE_RECEIVED -> "Inbox/action/GetList"
Message.TYPE_SENT -> "Outbox/action/GetList"
else -> null
}
if (endpoint != null) {
messagesGet(TAG, endpoint, parameters = mapOf(
"archive" to if (archived) 1 else 0
)) { doc ->
doc.select("GetList data").firstOrNull()?.children()?.forEach { element ->
val id = element.select("messageId").text().toLong()
val subject = element.select("topic").text().trim()
val readDateText = element.select("readDate").text().trim()
val readDate = when (readDateText.isNotBlank()) {
true -> Date.fromIso(readDateText)
else -> 0
}
val sentDate = Date.fromIso(element.select("sendDate").text().trim())
var senderId: Long = -1
var receiverId: Long = -1
when (type) {
Message.TYPE_RECEIVED -> {
val senderFirstName = element.select("senderFirstName").text().trim()
val senderLastName = element.select("senderLastName").text().trim()
senderId = data.teacherList.singleOrNull {
it.name == senderFirstName && it.surname == senderLastName
}?.id ?: -1
}
Message.TYPE_SENT -> {
val receiverFirstName = element.select("receiverFirstName").text().trim()
val receiverLastName = element.select("receiverLastName").text().trim()
receiverId = data.teacherList.singleOrNull {
it.name == receiverFirstName && it.surname == receiverLastName
}?.id ?: {
val teacherObject = Teacher(
profileId,
-1 * Utils.crc16("$receiverFirstName $receiverLastName".toByteArray()).toLong(),
receiverFirstName,
receiverLastName
)
data.teacherList.put(teacherObject.id, teacherObject)
teacherObject.id
}.invoke()
}
}
val notified = when (type) {
Message.TYPE_SENT -> true
else -> readDate > 0 || profile?.empty ?: false
}
val messageObject = Message(
profileId,
id,
subject,
null,
type,
senderId,
-1
)
val messageRecipientObject = MessageRecipient(
profileId,
receiverId,
-1,
readDate,
id
)
data.messageList.add(messageObject)
data.messageRecipientList.add(messageRecipientObject)
data.metadataList.add(Metadata(
profileId,
Metadata.TYPE_MESSAGE,
id,
notified,
notified,
sentDate
))
}
when (type) {
Message.TYPE_RECEIVED -> data.setSyncNext(ENDPOINT_LIBRUS_MESSAGES_RECEIVED, 2 * HOUR, DRAWER_ITEM_MESSAGES)
Message.TYPE_SENT -> data.setSyncNext(ENDPOINT_LIBRUS_MESSAGES_SENT, DAY, DRAWER_ITEM_MESSAGES)
}
onSuccess()
}
} else {
data.error(TAG, ERROR_NOT_IMPLEMENTED)
onSuccess()
}
}
}

View File

@ -96,7 +96,7 @@ public class MessagesListFragment extends Fragment {
return;
}
if (app.profile.getLoginStoreType() == LOGIN_TYPE_LIBRUS && app.profile.getStudentData("accountPassword", null) == null) {
if (app.profile.getLoginStoreType() == LOGIN_TYPE_LIBRUS && app.profile.getStudentData("accountPassword", null) == null && false) {
new MaterialDialog.Builder(activity)
.title("Wiadomości w systemie Synergia")
.content("Moduł Wiadomości w aplikacji Szkolny.eu jest przeglądarką zasobów szkolnego konta Synergia. Z tego powodu, musisz wpisać swoje hasło do tego konta, aby móc korzystać z tej funkcji.")

View File

@ -963,4 +963,5 @@
<string name="edziennik_progress_endpoint_event_types">Pobieranie kategorii wydarzeń...</string>
<string name="edziennik_progress_endpoint_notice_types">Pobieranie kategorii uwag...</string>
<string name="edziennik_progress_endpoint_pt_meetings">Pobieranie zebrań z rodzicami...</string>
<string name="edziennik_progress_endpoint_messages_outbox">Pobieranie wiadomości wysłanych...</string>
</resources>