[APIv2/Vulcan] Fix getting sent messages with unknown recipient

This commit is contained in:
Kacper Ziubryniewicz 2019-11-13 21:01:09 +01:00
parent 058345b9c9
commit dfdc6817a1
4 changed files with 50 additions and 28 deletions

View File

@ -95,6 +95,15 @@ fun String.swapFirstLastName(): String {
} }
} }
fun String.getFirstLastName(): Pair<String, String>? {
return this.split(" ").let {
if (it.size >= 2) Pair(it[0], it[1])
else null
}
}
fun String.getLastFirstName() = this.getFirstLastName()
fun changeStringCase(s: String): String { fun changeStringCase(s: String): String {
val delimiters = " '-/" val delimiters = " '-/"
val sb = StringBuilder() val sb = StringBuilder()

View File

@ -46,13 +46,10 @@ class VulcanApiMessagesInbox(override val data: DataVulcan, val onSuccess: () ->
val senderLoginId = message.getString("NadawcaId") ?: return@forEach val senderLoginId = message.getString("NadawcaId") ?: return@forEach
val senderId = data.teacherList val senderId = data.teacherList
.singleOrNull { it.loginId == senderLoginId }?.id ?: { .singleOrNull { it.loginId == senderLoginId }?.id ?: {
val senderName = message.getString("Nadawca") ?: "" val senderName = message.getString("Nadawca") ?: ""
val senderNameSplit = senderName.split(" ")
if (senderNameSplit.size >= 2) {
val senderLastName = senderNameSplit[0]
val senderFirstName = senderNameSplit[1]
senderName.getLastFirstName()?.let { (senderLastName, senderFirstName) ->
val teacherObject = Teacher( val teacherObject = Teacher(
profileId, profileId,
-1 * Utils.crc16(senderName.toByteArray()).toLong(), -1 * Utils.crc16(senderName.toByteArray()).toLong(),
@ -62,8 +59,8 @@ class VulcanApiMessagesInbox(override val data: DataVulcan, val onSuccess: () ->
) )
data.teacherList.put(teacherObject.id, teacherObject) data.teacherList.put(teacherObject.id, teacherObject)
teacherObject.id teacherObject.id
} else -1 }
}.invoke() }.invoke() ?: -1
val sentDate = message.getLong("DataWyslaniaUnixEpoch")?.let { it * 1000 } val sentDate = message.getLong("DataWyslaniaUnixEpoch")?.let { it * 1000 }
?: -1 ?: -1

View File

@ -14,6 +14,8 @@ import pl.szczodrzynski.edziennik.data.db.modules.messages.Message
import pl.szczodrzynski.edziennik.data.db.modules.messages.Message.TYPE_SENT import pl.szczodrzynski.edziennik.data.db.modules.messages.Message.TYPE_SENT
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.utils.Utils
import pl.szczodrzynski.edziennik.utils.models.Date import pl.szczodrzynski.edziennik.utils.models.Date
class VulcanApiMessagesSent(override val data: DataVulcan, val onSuccess: () -> Unit) : VulcanApi(data) { class VulcanApiMessagesSent(override val data: DataVulcan, val onSuccess: () -> Unit) : VulcanApi(data) {
@ -23,11 +25,11 @@ class VulcanApiMessagesSent(override val data: DataVulcan, val onSuccess: () ->
init { init {
data.profile?.also { profile -> data.profile?.also { profile ->
val startDate: String = when (profile.empty) { val startDate: Long = when (profile.empty) {
true -> profile.getSemesterStart(profile.currentSemester).stringY_m_d true -> profile.getSemesterStart(profile.currentSemester).inUnix
else -> Date.getToday().stepForward(0, -1, 0).stringY_m_d else -> Date.getToday().stepForward(0, -1, 0).inUnix
} }
val endDate: String = profile.getSemesterEnd(profile.currentSemester).stringY_m_d val endDate: Long = profile.getSemesterEnd(profile.currentSemester).inUnix
apiGet(TAG, VULCAN_API_ENDPOINT_MESSAGES_SENT, parameters = mapOf( apiGet(TAG, VULCAN_API_ENDPOINT_MESSAGES_SENT, parameters = mapOf(
"DataPoczatkowa" to startDate, "DataPoczatkowa" to startDate,
@ -43,23 +45,27 @@ class VulcanApiMessagesSent(override val data: DataVulcan, val onSuccess: () ->
val unreadBy = message.getInt("Nieprzeczytane") ?: 0 val unreadBy = message.getInt("Nieprzeczytane") ?: 0
val sentDate = message.getLong("DataWyslaniaUnixEpoch")?.let { it * 1000 } ?: -1 val sentDate = message.getLong("DataWyslaniaUnixEpoch")?.let { it * 1000 } ?: -1
val messageObject = Message(
profileId,
id,
subject,
body,
TYPE_SENT,
-1,
-1
)
message.getJsonArray("Adresaci")?.asJsonObjectList() message.getJsonArray("Adresaci")?.asJsonObjectList()
?.onEach { recipient -> ?.onEach { receiver ->
val recipientLoginId = recipient.getString("LoginId") val receiverLoginId = receiver.getString("LoginId")
?: return@onEach
val recipientId = data.teacherList.singleOrNull { it.loginId == recipientLoginId }?.id
?: return@onEach ?: return@onEach
val receiverId = data.teacherList.singleOrNull { it.loginId == receiverLoginId }?.id
?: {
val receiverName = receiver.getString("Nazwa") ?: ""
receiverName.getLastFirstName()?.let { (receiverLastName, receiverFirstName) ->
val teacherObject = Teacher(
profileId,
-1 * Utils.crc16(receiverName.toByteArray()).toLong(),
receiverFirstName,
receiverLastName,
receiverLoginId
)
data.teacherList.put(teacherObject.id, teacherObject)
teacherObject.id
}
}.invoke() ?: -1
val readDate: Long = when (readBy) { val readDate: Long = when (readBy) {
0 -> 0 0 -> 0
@ -71,7 +77,7 @@ class VulcanApiMessagesSent(override val data: DataVulcan, val onSuccess: () ->
val messageRecipientObject = MessageRecipient( val messageRecipientObject = MessageRecipient(
profileId, profileId,
recipientId, receiverId,
-1, -1,
readDate, readDate,
id id
@ -80,6 +86,16 @@ class VulcanApiMessagesSent(override val data: DataVulcan, val onSuccess: () ->
data.messageRecipientList.add(messageRecipientObject) data.messageRecipientList.add(messageRecipientObject)
} }
val messageObject = Message(
profileId,
id,
subject,
body,
TYPE_SENT,
-1,
-1
)
data.messageIgnoreList.add(messageObject) data.messageIgnoreList.add(messageObject)
data.metadataList.add(Metadata( data.metadataList.add(Metadata(
profileId, profileId,

View File

@ -97,7 +97,7 @@ class MessageFragment : Fragment(), CoroutineScope {
val msg = app.db.messageDao().getById(App.profileId, messageId)?.also { val msg = app.db.messageDao().getById(App.profileId, messageId)?.also {
it.recipients = app.db.messageRecipientDao().getAllByMessageId(it.profileId, it.id) it.recipients = app.db.messageRecipientDao().getAllByMessageId(it.profileId, it.id)
if (it.body != null && !it.seen) { if (it.body != null && !it.seen) {
app.db.metadataDao().setSeen(it.profileId, message, true) app.db.metadataDao().setSeen(it.profileId, it, true)
} }
} }
msg msg