[APIv2/Librus] Fix getting messages with unknown recipient

This commit is contained in:
Kacper Ziubryniewicz 2019-11-13 00:00:28 +01:00
parent a8f89abf7d
commit 9f871c077b

View File

@ -13,6 +13,7 @@ import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_MESSAGES_SENT
import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusMessages import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusMessages
import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS
import pl.szczodrzynski.edziennik.data.db.modules.messages.Message 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.MessageRecipient 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.metadata.Metadata
import pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher import pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher
@ -20,7 +21,7 @@ import pl.szczodrzynski.edziennik.singleOrNull
import pl.szczodrzynski.edziennik.utils.Utils import pl.szczodrzynski.edziennik.utils.Utils
import pl.szczodrzynski.edziennik.utils.models.Date import pl.szczodrzynski.edziennik.utils.models.Date
class LibrusMessagesGetList(override val data: DataLibrus, private val type: Int = Message.TYPE_RECEIVED, class LibrusMessagesGetList(override val data: DataLibrus, private val type: Int = TYPE_RECEIVED,
archived: Boolean = false, val onSuccess: () -> Unit) : LibrusMessages(data) { archived: Boolean = false, val onSuccess: () -> Unit) : LibrusMessages(data) {
companion object { companion object {
const val TAG = "LibrusMessagesGetList" const val TAG = "LibrusMessagesGetList"
@ -28,7 +29,7 @@ class LibrusMessagesGetList(override val data: DataLibrus, private val type: Int
init { init {
val endpoint = when (type) { val endpoint = when (type) {
Message.TYPE_RECEIVED -> "Inbox/action/GetList" TYPE_RECEIVED -> "Inbox/action/GetList"
Message.TYPE_SENT -> "Outbox/action/GetList" Message.TYPE_SENT -> "Outbox/action/GetList"
else -> null else -> null
} }
@ -46,34 +47,38 @@ class LibrusMessagesGetList(override val data: DataLibrus, private val type: Int
else -> 0 else -> 0
} }
val sentDate = Date.fromIso(element.select("sendDate").text().trim()) val sentDate = Date.fromIso(element.select("sendDate").text().trim())
var senderId: Long = -1
var receiverId: Long = -1
when (type) { val recipientFirstName = element.select(when (type) {
Message.TYPE_RECEIVED -> { TYPE_RECEIVED -> "senderFirstName"
val senderFirstName = element.select("senderFirstName").text().trim() else -> "receiverFirstName"
val senderLastName = element.select("senderLastName").text().trim() }).text().trim()
senderId = data.teacherList.singleOrNull {
it.name == senderFirstName && it.surname == senderLastName
}?.id ?: -1
}
Message.TYPE_SENT -> { val recipientLastName = element.select(when (type) {
val receiverFirstName = element.select("receiverFirstName").text().trim() TYPE_RECEIVED -> "senderLastName"
val receiverLastName = element.select("receiverLastName").text().trim() else -> "receiverLastName"
receiverId = data.teacherList.singleOrNull { }).text().trim()
it.name == receiverFirstName && it.surname == receiverLastName
val recipientId = data.teacherList.singleOrNull {
it.name == recipientFirstName && it.surname == recipientLastName
}?.id ?: { }?.id ?: {
val teacherObject = Teacher( val teacherObject = Teacher(
profileId, profileId,
-1 * Utils.crc16("$receiverFirstName $receiverLastName".toByteArray()).toLong(), -1 * Utils.crc16("$recipientFirstName $recipientLastName".toByteArray()).toLong(),
receiverFirstName, recipientFirstName,
receiverLastName recipientLastName
) )
data.teacherList.put(teacherObject.id, teacherObject) data.teacherList.put(teacherObject.id, teacherObject)
teacherObject.id teacherObject.id
}.invoke() }.invoke()
val senderId = when (type) {
TYPE_RECEIVED -> recipientId
else -> -1
} }
val receiverId = when (type) {
TYPE_RECEIVED -> -1
else -> recipientId
} }
val notified = when (type) { val notified = when (type) {
@ -112,7 +117,7 @@ class LibrusMessagesGetList(override val data: DataLibrus, private val type: Int
} }
when (type) { when (type) {
Message.TYPE_RECEIVED -> data.setSyncNext(ENDPOINT_LIBRUS_MESSAGES_RECEIVED, SYNC_ALWAYS) TYPE_RECEIVED -> data.setSyncNext(ENDPOINT_LIBRUS_MESSAGES_RECEIVED, SYNC_ALWAYS)
Message.TYPE_SENT -> data.setSyncNext(ENDPOINT_LIBRUS_MESSAGES_SENT, DAY, DRAWER_ITEM_MESSAGES) Message.TYPE_SENT -> data.setSyncNext(ENDPOINT_LIBRUS_MESSAGES_SENT, DAY, DRAWER_ITEM_MESSAGES)
} }
onSuccess() onSuccess()