[APIv2/Librus] Add getting message info

This commit is contained in:
Kacper Ziubryniewicz 2019-11-12 00:28:55 +01:00
parent 74db524db6
commit c6e2519dcc
9 changed files with 124 additions and 11 deletions

View File

@ -21,7 +21,7 @@ open class EdziennikTask(override val profileId: Int, val request: Any) : IApiTa
fun sync() = EdziennikTask(-1, SyncRequest())
fun syncProfile(profileId: Int, viewIds: List<Pair<Int, Int>>? = null, arguments: JsonObject? = null) = EdziennikTask(profileId, SyncProfileRequest(viewIds, arguments))
fun syncProfileList(profileList: List<Int>) = EdziennikTask(-1, SyncProfileListRequest(profileList))
fun messageGet(profileId: Int, messageId: Int) = EdziennikTask(profileId, MessageGetRequest(messageId))
fun messageGet(profileId: Int, messageId: Long) = EdziennikTask(profileId, MessageGetRequest(messageId))
fun announcementsRead(profileId: Int) = EdziennikTask(profileId, AnnouncementsReadRequest())
}
@ -87,6 +87,6 @@ open class EdziennikTask(override val profileId: Int, val request: Any) : IApiTa
class SyncRequest
data class SyncProfileRequest(val viewIds: List<Pair<Int, Int>>? = null, val arguments: JsonObject? = null)
data class SyncProfileListRequest(val profileList: List<Int>)
data class MessageGetRequest(val messageId: Int)
data class MessageGetRequest(val messageId: Long)
class AnnouncementsReadRequest
}
}

View File

@ -61,7 +61,7 @@ class Idziennik(val app: App, val profile: Profile?, val loginStore: LoginStore,
}
}
override fun getMessage(messageId: Int) {
override fun getMessage(messageId: Long) {
}

View File

@ -8,7 +8,7 @@ import com.google.gson.JsonObject
interface EdziennikInterface {
fun sync(featureIds: List<Int>, viewId: Int? = null, arguments: JsonObject? = null)
fun getMessage(messageId: Int)
fun getMessage(messageId: Long)
fun markAllAnnouncementsAsRead()
fun firstLogin()
fun cancel()

View File

@ -10,10 +10,12 @@ import pl.szczodrzynski.edziennik.api.v2.*
import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikCallback
import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikInterface
import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusData
import pl.szczodrzynski.edziennik.api.v2.librus.data.messages.LibrusMessagesGetMessage
import pl.szczodrzynski.edziennik.api.v2.librus.data.synergia.LibrusSynergiaMarkAllAnnouncementsAsRead
import pl.szczodrzynski.edziennik.api.v2.librus.firstlogin.LibrusFirstLogin
import pl.szczodrzynski.edziennik.api.v2.librus.login.LibrusLogin
import pl.szczodrzynski.edziennik.api.v2.librus.login.LibrusLoginApi
import pl.szczodrzynski.edziennik.api.v2.librus.login.LibrusLoginMessages
import pl.szczodrzynski.edziennik.api.v2.librus.login.LibrusLoginSynergia
import pl.szczodrzynski.edziennik.api.v2.models.ApiError
import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore
@ -78,8 +80,14 @@ class Librus(val app: App, val profile: Profile?, val loginStore: LoginStore, va
}
}
override fun getMessage(messageId: Int) {
override fun getMessage(messageId: Long) {
LibrusLoginApi(data) {
LibrusLoginMessages(data) {
LibrusMessagesGetMessage(data, messageId) {
completed()
}
}
}
}
override fun markAllAnnouncementsAsRead() {

View File

@ -187,6 +187,6 @@ class LibrusApiTimetables(override val data: DataLibrus,
System.currentTimeMillis()
))
}
data.lessonNewList += lessonObject
data.lessonNewList.add(lessonObject)
}
}

View File

@ -0,0 +1,105 @@
/*
* Copyright (c) Kacper Ziubryniewicz 2019-11-11
*/
package pl.szczodrzynski.edziennik.api.v2.librus.data.messages
import android.util.Base64
import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus
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.Message.TYPE_RECEIVED
import pl.szczodrzynski.edziennik.data.db.modules.messages.Message.TYPE_SENT
import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageRecipient
import pl.szczodrzynski.edziennik.utils.models.Date
class LibrusMessagesGetMessage(override val data: DataLibrus, val messageId: Long, val onSuccess: () -> Unit) : LibrusMessages(data) {
companion object {
const val TAG = "LibrusMessagesGetMessage"
}
init {
messagesGet(TAG, "GetMessage", parameters = mapOf(
"messageId" to messageId,
"archive" to 0
)) { doc ->
val message = doc.select("response GetMessage data").first()
val body = Base64.decode(message.select("Message").text(), Base64.DEFAULT)
.toString().apply {
replace("\n", "")
replace("<!\\[CDATA\\[", "")
replace("]]>", "")
}
val messageObject = data.db.messageDao().getById(profileId, messageId).apply {
this.body = body
clearAttachments()
message.select("attachments ArrayItem").forEach {
val attachmentId = it.select("id").text().toLong()
val attachmentName = it.select("filename").text()
addAttachment(attachmentId, attachmentName, -1)
}
}
when (messageObject.type) {
TYPE_RECEIVED -> {
val senderLoginId = message.select("senderId").text()
val readDateText = message.select("readDate").text()
val readDate = when (readDateText.isNotEmpty()) {
true -> Date.fromIso(readDateText)
else -> 0
}
val messageRecipientObject = MessageRecipient(
profileId,
-1,
-1,
readDate,
messageObject.id
)
data.messageRecipientList.add(messageRecipientObject)
data.db.teacherDao().updateLoginId(profileId, messageObject.senderId, senderLoginId)
}
TYPE_SENT -> {
val teachers = data.db.teacherDao().getAllNow(profileId)
message.select("receivers ArrayItem").forEach { receiver ->
val receiverFirstName = receiver.select("firstName").text()
val receiverLastName = receiver.select("lastName").text()
val receiverLoginId = receiver.select("receiverId").text()
val receiverId = teachers.singleOrNull {
it.name.equals(receiverFirstName, true) && it.surname.equals(receiverLastName, true)
}?.id ?: -1
val readDateText = message.select("readed").text()
val readDate = when (readDateText.isNotEmpty()) {
true -> Date.fromIso(readDateText)
else -> 0
}
val messageRecipientObject = MessageRecipient(
profileId,
-1,
-1,
readDate,
messageObject.id
)
data.messageRecipientList.add(messageRecipientObject)
data.db.teacherDao().updateLoginId(profileId, receiverId, receiverLoginId)
}
}
}
if (!messageObject.seen) data.db.metadataDao().setSeen(profileId, messageObject, true)
data.messageList.add(messageObject as Message)
onSuccess()
}
}
}

View File

@ -61,7 +61,7 @@ class Mobidziennik(val app: App, val profile: Profile?, val loginStore: LoginSto
}
}
override fun getMessage(messageId: Int) {
override fun getMessage(messageId: Long) {
}

View File

@ -61,7 +61,7 @@ class Template(val app: App, val profile: Profile?, val loginStore: LoginStore,
}
}
override fun getMessage(messageId: Int) {
override fun getMessage(messageId: Long) {
}

View File

@ -61,7 +61,7 @@ class Vulcan(val app: App, val profile: Profile?, val loginStore: LoginStore, va
}
}
override fun getMessage(messageId: Int) {
override fun getMessage(messageId: Long) {
}