[APIv2/Vulcan] Add getting received messages

This commit is contained in:
Kacper Ziubryniewicz 2019-11-02 00:05:14 +01:00
parent 64019dccf7
commit 33c009befe
5 changed files with 94 additions and 15 deletions

View File

@ -60,6 +60,16 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore)
val profileId
get() = profile?.id ?: -1
val syncStartDate: Date
get() = when (profile?.empty) {
true -> profile.getSemesterStart(profile.currentSemester)
else -> Date.getToday().stepForward(0, -1, 0)
}
val syncEndDate: Date
get() = profile?.getSemesterEnd(profile.currentSemester)
?: Date.getToday().stepForward(0, 1, 0)
/**
* A callback passed to all [Feature]s and [LoginMethod]s
*/
@ -316,8 +326,7 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore)
db.notificationDao().addAll(notifications)
onSuccess()
}
}
catch (e: Exception) {
} catch (e: Exception) {
error(ApiError(TAG, EXCEPTION_NOTIFY_AND_SYNC)
.withThrowable(e))
}

View File

@ -65,6 +65,10 @@ class VulcanData(val data: DataVulcan, val onSuccess: () -> Unit) {
data.startProgress(R.string.edziennik_progress_endpoint_attendance)
VulcanApiAttendance(data) { onSuccess() }
}
ENDPOINT_VULCAN_API_MESSAGES_INBOX -> {
data.startProgress(R.string.edziennik_progress_endpoint_messages_inbox)
VulcanApiMessagesInbox(data) { onSuccess() }
}
else -> onSuccess()
}
}

View File

@ -22,12 +22,9 @@ class VulcanApiAttendance(override val data: DataVulcan, val onSuccess: () -> Un
data.db.attendanceTypeDao().getAllNow(profileId).toSparseArray(data.attendanceTypes) { it.id }
}
val startDate: String = profile.getSemesterStart(profile.currentSemester).stringY_m_d
val endDate: String = profile.getSemesterEnd(profile.currentSemester).stringY_m_d
apiGet(TAG, VULCAN_API_ENDPOINT_ATTENDANCE, parameters = mapOf(
"DataPoczatkowa" to startDate,
"DataKoncowa" to endDate,
"DataPoczatkowa" to data.syncStartDate.stringY_m_d,
"DataKoncowa" to data.syncEndDate.stringY_m_d,
"IdOddzial" to data.studentClassId,
"IdUczen" to data.studentId,
"IdOkresKlasyfikacyjny" to data.studentSemesterId

View File

@ -26,19 +26,13 @@ class VulcanApiEvents(override val data: DataVulcan, private val isHomework: Boo
init { data.profile?.also { profile ->
val startDate: String = when (profile.empty) {
true -> profile.getSemesterStart(profile.currentSemester).stringY_m_d
else -> Date.getToday().stepForward(0, -1, 0).stringY_m_d
}
val endDate: String = profile.getSemesterEnd(profile.currentSemester).stringY_m_d
val endpoint = when (isHomework) {
true -> VULCAN_API_ENDPOINT_HOMEWORK
else -> VULCAN_API_ENDPOINT_EVENTS
}
apiGet(TAG, endpoint, parameters = mapOf(
"DataPoczatkowa" to startDate,
"DataKoncowa" to endDate,
"DataPoczatkowa" to data.syncStartDate.stringY_m_d,
"DataKoncowa" to data.syncEndDate.stringY_m_d,
"IdOddzial" to data.studentClassId,
"IdUczen" to data.studentId,
"IdOkresKlasyfikacyjny" to data.studentSemesterId

View File

@ -0,0 +1,75 @@
/*
* Copyright (c) Kacper Ziubryniewicz 2019-11-01
*/
package pl.szczodrzynski.edziennik.api.v2.vulcan.data.api
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.api.v2.VULCAN_API_ENDPOINT_MESSAGES_RECEIVED
import pl.szczodrzynski.edziennik.api.v2.vulcan.DataVulcan
import pl.szczodrzynski.edziennik.api.v2.vulcan.ENDPOINT_VULCAN_API_MESSAGES_INBOX
import pl.szczodrzynski.edziennik.api.v2.vulcan.data.VulcanApi
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.MessageRecipient
import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata
class VulcanApiMessagesInbox(override val data: DataVulcan, val onSuccess: () -> Unit) : VulcanApi(data) {
companion object {
const val TAG = "VulcanApiMessagesInbox"
}
init {
apiGet(TAG, VULCAN_API_ENDPOINT_MESSAGES_RECEIVED, parameters = mapOf(
"DataPoczatkowa" to data.syncStartDate.inUnix,
"DataKoncowa" to data.syncEndDate.inUnix,
"LoginId" to data.studentLoginId,
"IdUczen" to data.studentId
)) { json, _ ->
json.getJsonArray("Data").asJsonObjectList()?.forEach { message ->
val id = message.getLong("WiadomoscId") ?: return@forEach
val subject = message.getString("Tytul") ?: ""
val body = message.getString("Tresc") ?: ""
val senderLoginId = message.getString("NadawcaId") ?: return@forEach
val senderId = data.teacherList
.singleOrNull { it.loginId == senderLoginId }?.id ?: return@forEach
val addedDate = message.getLong("DataWyslaniaUnixEpoch")?.let { it * 1000 } ?: -1
val readDate = message.getLong("DataPrzeczytaniaUnixEpoch")?.let { it * 1000 } ?: -1
val messageObject = Message(
profileId,
id,
subject,
body,
Message.TYPE_RECEIVED,
senderId,
-1
)
val messageRecipientObject = MessageRecipient(
profileId,
-1,
-1,
readDate,
id
)
data.messageList.add(messageObject)
data.messageRecipientList.add(messageRecipientObject)
data.metadataList.add(Metadata(
profileId,
Metadata.TYPE_MESSAGE,
id,
readDate > 0,
readDate > 0,
addedDate
))
}
data.setSyncNext(ENDPOINT_VULCAN_API_MESSAGES_INBOX, SYNC_ALWAYS)
onSuccess()
}
}
}