forked from github/szkolny
[Vulcan/Hebe] Add getting messages.
This commit is contained in:
parent
f49e39e858
commit
e86b47fb1b
@ -133,6 +133,7 @@ const val VULCAN_HEBE_ENDPOINT_ADDRESSBOOK = "api/mobile/addressbook"
|
||||
const val VULCAN_HEBE_ENDPOINT_EXAMS = "api/mobile/exam"
|
||||
const val VULCAN_HEBE_ENDPOINT_GRADES = "api/mobile/grade"
|
||||
const val VULCAN_HEBE_ENDPOINT_HOMEWORK = "api/mobile/homework"
|
||||
const val VULCAN_HEBE_ENDPOINT_MESSAGES = "api/mobile/message"
|
||||
|
||||
const val EDUDZIENNIK_USER_AGENT = "Szkolny.eu/${BuildConfig.VERSION_NAME}"
|
||||
|
||||
|
@ -91,6 +91,20 @@ class Vulcan(val app: App, val profile: Profile?, val loginStore: LoginStore, va
|
||||
}
|
||||
|
||||
override fun getMessage(message: MessageFull) {
|
||||
if (loginStore.mode != LOGIN_MODE_VULCAN_API) {
|
||||
login(LOGIN_METHOD_VULCAN_HEBE) {
|
||||
if (message.seen) {
|
||||
EventBus.getDefault().postSticky(MessageGetEvent(message))
|
||||
completed()
|
||||
return@login
|
||||
}
|
||||
VulcanApiMessagesChangeStatus(data, message) {
|
||||
completed()
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
login(LOGIN_METHOD_VULCAN_API) {
|
||||
if (message.attachmentIds != null) {
|
||||
VulcanApiMessagesChangeStatus(data, message) {
|
||||
|
@ -26,6 +26,8 @@ const val ENDPOINT_VULCAN_HEBE_TIMETABLE = 3020
|
||||
const val ENDPOINT_VULCAN_HEBE_EXAMS = 3030
|
||||
const val ENDPOINT_VULCAN_HEBE_GRADES = 3040
|
||||
const val ENDPOINT_VULCAN_HEBE_HOMEWORK = 3060
|
||||
const val ENDPOINT_VULCAN_HEBE_MESSAGES_INBOX = 3090
|
||||
const val ENDPOINT_VULCAN_HEBE_MESSAGES_SENT = 3100
|
||||
|
||||
val VulcanFeatures = listOf(
|
||||
// timetable
|
||||
@ -72,6 +74,12 @@ val VulcanFeatures = listOf(
|
||||
Feature(LOGIN_TYPE_VULCAN, FEATURE_MESSAGES_SENT, listOf(
|
||||
ENDPOINT_VULCAN_API_MESSAGES_SENT to LOGIN_METHOD_VULCAN_API
|
||||
), listOf(LOGIN_METHOD_VULCAN_API)),
|
||||
Feature(LOGIN_TYPE_VULCAN, FEATURE_MESSAGES_INBOX, listOf(
|
||||
ENDPOINT_VULCAN_HEBE_MESSAGES_INBOX to LOGIN_METHOD_VULCAN_HEBE
|
||||
), listOf(LOGIN_METHOD_VULCAN_HEBE)),
|
||||
Feature(LOGIN_TYPE_VULCAN, FEATURE_MESSAGES_SENT, listOf(
|
||||
ENDPOINT_VULCAN_HEBE_MESSAGES_SENT to LOGIN_METHOD_VULCAN_HEBE
|
||||
), listOf(LOGIN_METHOD_VULCAN_HEBE)),
|
||||
|
||||
// push config
|
||||
Feature(LOGIN_TYPE_VULCAN, FEATURE_PUSH_CONFIG, listOf(
|
||||
|
@ -9,6 +9,7 @@ import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.*
|
||||
import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.data.api.*
|
||||
import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.data.hebe.*
|
||||
import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.data.web.VulcanWebLuckyNumber
|
||||
import pl.szczodrzynski.edziennik.data.db.entity.Message
|
||||
import pl.szczodrzynski.edziennik.utils.Utils
|
||||
|
||||
class VulcanData(val data: DataVulcan, val onSuccess: () -> Unit) {
|
||||
@ -125,6 +126,14 @@ class VulcanData(val data: DataVulcan, val onSuccess: () -> Unit) {
|
||||
data.startProgress(R.string.edziennik_progress_endpoint_homework)
|
||||
VulcanHebeHomework(data, lastSync, onSuccess)
|
||||
}
|
||||
ENDPOINT_VULCAN_HEBE_MESSAGES_INBOX -> {
|
||||
data.startProgress(R.string.edziennik_progress_endpoint_messages_inbox)
|
||||
VulcanHebeMessages(data, lastSync, onSuccess).getMessages(Message.TYPE_RECEIVED)
|
||||
}
|
||||
ENDPOINT_VULCAN_HEBE_MESSAGES_SENT -> {
|
||||
data.startProgress(R.string.edziennik_progress_endpoint_messages_outbox)
|
||||
VulcanHebeMessages(data, lastSync, onSuccess).getMessages(Message.TYPE_SENT)
|
||||
}
|
||||
else -> onSuccess(endpointId)
|
||||
}
|
||||
}
|
||||
|
@ -42,9 +42,9 @@ open class VulcanHebe(open val data: DataVulcan, open val lastSync: Long?) {
|
||||
val profile
|
||||
get() = data.profile
|
||||
|
||||
fun getDateTime(json: JsonObject?, key: String): Long {
|
||||
fun getDateTime(json: JsonObject?, key: String, default: Long = System.currentTimeMillis()): Long {
|
||||
val date = json.getJsonObject(key)
|
||||
return date.getLong("Timestamp") ?: return System.currentTimeMillis()
|
||||
return date.getLong("Timestamp") ?: return default
|
||||
}
|
||||
|
||||
fun getDate(json: JsonObject?, key: String): Date? {
|
||||
|
@ -0,0 +1,127 @@
|
||||
package pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.data.hebe
|
||||
|
||||
import androidx.core.util.set
|
||||
import com.google.gson.JsonObject
|
||||
import pl.szczodrzynski.edziennik.*
|
||||
import pl.szczodrzynski.edziennik.MainActivity.Companion.DRAWER_ITEM_MESSAGES
|
||||
import pl.szczodrzynski.edziennik.data.api.VULCAN_HEBE_ENDPOINT_MESSAGES
|
||||
import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.DataVulcan
|
||||
import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.ENDPOINT_VULCAN_HEBE_MESSAGES_INBOX
|
||||
import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.ENDPOINT_VULCAN_HEBE_MESSAGES_SENT
|
||||
import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.data.VulcanHebe
|
||||
import pl.szczodrzynski.edziennik.data.db.entity.*
|
||||
import pl.szczodrzynski.edziennik.data.db.entity.Message.Companion.TYPE_DELETED
|
||||
import pl.szczodrzynski.edziennik.data.db.entity.Message.Companion.TYPE_RECEIVED
|
||||
import pl.szczodrzynski.edziennik.data.db.entity.Message.Companion.TYPE_SENT
|
||||
import pl.szczodrzynski.navlib.crc16
|
||||
import kotlin.text.replace
|
||||
|
||||
class VulcanHebeMessages(
|
||||
override val data: DataVulcan,
|
||||
override val lastSync: Long?,
|
||||
val onSuccess: (endpointId: Int) -> Unit
|
||||
) : VulcanHebe(data, lastSync) {
|
||||
companion object {
|
||||
const val TAG = "VulcanHebeMessagesInbox"
|
||||
}
|
||||
|
||||
private fun getPersonId(json: JsonObject): Long {
|
||||
val senderLoginId = json.getInt("LoginId") ?: return -1
|
||||
/*if (senderLoginId == data.studentLoginId)
|
||||
return -1*/
|
||||
|
||||
val senderName = json.getString("Address") ?: return -1
|
||||
val senderNameSplit = senderName.splitName()
|
||||
val senderLoginIdStr = senderLoginId.toString()
|
||||
val teacher = data.teacherList.singleOrNull { it.loginId == senderLoginIdStr }
|
||||
?: Teacher(
|
||||
profileId,
|
||||
-1 * crc16(senderName).toLong(),
|
||||
senderNameSplit?.second ?: "",
|
||||
senderNameSplit?.first ?: "",
|
||||
senderLoginIdStr
|
||||
).also {
|
||||
it.setTeacherType(Teacher.TYPE_OTHER)
|
||||
data.teacherList[it.id] = it
|
||||
}
|
||||
return teacher.id
|
||||
}
|
||||
|
||||
fun getMessages(messageType: Int) {
|
||||
val folder = when (messageType) {
|
||||
TYPE_RECEIVED -> 1
|
||||
TYPE_SENT -> 2
|
||||
TYPE_DELETED -> 3
|
||||
else -> 1
|
||||
}
|
||||
val endpointId = when (messageType) {
|
||||
TYPE_RECEIVED -> ENDPOINT_VULCAN_HEBE_MESSAGES_INBOX
|
||||
TYPE_SENT -> ENDPOINT_VULCAN_HEBE_MESSAGES_SENT
|
||||
else -> ENDPOINT_VULCAN_HEBE_MESSAGES_INBOX
|
||||
}
|
||||
apiGetList(
|
||||
TAG,
|
||||
VULCAN_HEBE_ENDPOINT_MESSAGES,
|
||||
HebeFilterType.BY_PERSON,
|
||||
folder = folder,
|
||||
lastSync = lastSync
|
||||
) { list, _ ->
|
||||
list.forEach { message ->
|
||||
val id = message.getLong("Id") ?: return@forEach
|
||||
val subject = message.getString("Subject") ?: return@forEach
|
||||
val body = message.getString("Content") ?: return@forEach
|
||||
|
||||
val sender = message.getJsonObject("Sender") ?: return@forEach
|
||||
|
||||
val sentDate = getDateTime(message, "DateSent")
|
||||
val readDate = getDateTime(message, "DateRead", default = 0)
|
||||
|
||||
val messageObject = Message(
|
||||
profileId = profileId,
|
||||
id = id,
|
||||
type = messageType,
|
||||
subject = subject,
|
||||
body = body.replace("\n", "<br>"),
|
||||
senderId = if (messageType == TYPE_RECEIVED) getPersonId(sender) else null,
|
||||
addedDate = sentDate
|
||||
)
|
||||
|
||||
val receivers = message.getJsonArray("Receiver")
|
||||
?.asJsonObjectList()
|
||||
?: return@forEach
|
||||
val receiverReadDate =
|
||||
if (receivers.size == 1) readDate
|
||||
else -1
|
||||
|
||||
for (receiver in receivers) {
|
||||
val messageRecipientObject = MessageRecipient(
|
||||
profileId,
|
||||
if (messageType == TYPE_SENT) getPersonId(receiver) else -1,
|
||||
-1,
|
||||
receiverReadDate,
|
||||
id
|
||||
)
|
||||
data.messageRecipientList.add(messageRecipientObject)
|
||||
}
|
||||
|
||||
data.messageList.add(messageObject)
|
||||
data.setSeenMetadataList.add(
|
||||
Metadata(
|
||||
profileId,
|
||||
Metadata.TYPE_MESSAGE,
|
||||
id,
|
||||
readDate > 0 || messageType == TYPE_SENT,
|
||||
readDate > 0 || messageType == TYPE_SENT
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
data.setSyncNext(
|
||||
endpointId,
|
||||
if (messageType == TYPE_RECEIVED) SYNC_ALWAYS else 1 * DAY,
|
||||
if (messageType == TYPE_RECEIVED) null else DRAWER_ITEM_MESSAGES
|
||||
)
|
||||
onSuccess(endpointId)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user