-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented basic message receiving functionality
- Loading branch information
Showing
21 changed files
with
572 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...eApp/src/commonMain/kotlin/com/github/aeoliux/violet/api/scraping/messages/AllMessages.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.github.aeoliux.violet.api.scraping.messages | ||
|
||
import com.fleeksoft.ksoup.Ksoup | ||
import com.github.aeoliux.violet.api.ApiClient | ||
import com.github.aeoliux.violet.api.localDateTimeFormat | ||
import io.ktor.client.request.get | ||
import io.ktor.client.statement.bodyAsText | ||
import kotlinx.datetime.LocalDateTime | ||
|
||
data class MessageLabel( | ||
val url: String, | ||
val sender: String, | ||
val topic: String, | ||
val sentAt: LocalDateTime?, | ||
val hasAttachment: Boolean, | ||
) | ||
|
||
typealias MessagesList = LinkedHashMap<MessageCategories, List<MessageLabel>> | ||
//fun MessagesList.init() { | ||
// this[MessageCategories.Received] = emptyList() | ||
// this[MessageCategories.Sent] = emptyList() | ||
// this[MessageCategories.Bin] = emptyList() | ||
//} | ||
|
||
suspend fun ApiClient.getMessages(): MessagesList { | ||
val categories = listOf( | ||
MessageCategories.Received, | ||
MessageCategories.Sent, | ||
MessageCategories.Bin | ||
) | ||
|
||
return categories.fold(MessagesList()) { acc, category -> | ||
val url = "https://synergia.librus.pl/wiadomosci/${category.categoryId}" | ||
val body = client.get(url).bodyAsText() | ||
val soup = Ksoup.parse(body) | ||
|
||
val labels = soup.select( | ||
"#formWiadomosci > div > div > table > tbody > tr > td:nth-child(2) > table:nth-child(2) > tbody > tr" | ||
).fold(emptyList<MessageLabel>()) { list, label -> | ||
if (label.getAllElements().size > 2) { | ||
val hasAttachment = label.select("td:nth-child(2)").html().startsWith("<img ") | ||
val date = label.select("td:nth-child(5)").html() | ||
|
||
val senderData = label.select("td:nth-child(3) > a") | ||
val messageUrl = senderData.attr("href") | ||
val sender = senderData.html() | ||
|
||
val topic = label.select("td:nth-child(4) > a").html() | ||
|
||
val messageLabel = MessageLabel( | ||
url = messageUrl, | ||
sender = sender, | ||
topic = topic, | ||
sentAt = LocalDateTime.parse(date, localDateTimeFormat), | ||
hasAttachment = hasAttachment | ||
) | ||
|
||
list.plus(messageLabel) | ||
} else | ||
list | ||
} | ||
|
||
acc[category] = labels | ||
acc | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...seApp/src/commonMain/kotlin/com/github/aeoliux/violet/api/scraping/messages/GetMessage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.github.aeoliux.violet.api.scraping.messages | ||
|
||
import com.fleeksoft.ksoup.Ksoup | ||
import com.github.aeoliux.violet.api.ApiClient | ||
import com.github.aeoliux.violet.api.localDateTimeFormat | ||
import io.ktor.client.request.get | ||
import io.ktor.client.statement.bodyAsText | ||
import kotlinx.datetime.LocalDateTime | ||
|
||
|
||
suspend fun ApiClient.getMessage(url: String): Message { | ||
val url = "https://synergia.librus.pl$url" | ||
val soup = Ksoup.parse( | ||
client.get(url).bodyAsText() | ||
) | ||
|
||
val messageData = soup.select("#formWiadomosci > div > div > table > tbody > tr > td:nth-child(2)") | ||
val meta = messageData.select("table:nth-child(2) > tbody > tr > td:nth-child(2)") | ||
val content = messageData.select("div").html().replace("<br>", "") | ||
|
||
var topic = "" | ||
var date = "" | ||
var sender: String? = null | ||
if (meta.size == 2) { | ||
topic = meta[0].html() | ||
date = meta[1].html() | ||
} else { | ||
sender = meta[0].html() | ||
topic = meta[1].html() | ||
date = meta[2].html() | ||
} | ||
|
||
return Message( | ||
sender = sender, | ||
date = LocalDateTime.parse(date, localDateTimeFormat), | ||
topic = topic, | ||
content = content, | ||
attachments = emptyList() | ||
) | ||
} |
11 changes: 11 additions & 0 deletions
11
composeApp/src/commonMain/kotlin/com/github/aeoliux/violet/api/scraping/messages/Message.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.github.aeoliux.violet.api.scraping.messages | ||
|
||
import kotlinx.datetime.LocalDateTime | ||
|
||
data class Message( | ||
val sender: String?, | ||
val date: LocalDateTime, | ||
val topic: String, | ||
val content: String, | ||
val attachments: List<String> | ||
) |
15 changes: 15 additions & 0 deletions
15
...rc/commonMain/kotlin/com/github/aeoliux/violet/api/scraping/messages/MessageCategories.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.github.aeoliux.violet.api.scraping.messages | ||
|
||
import kotlin.enums.EnumEntries | ||
|
||
enum class MessageCategories(val categoryId: Int) { | ||
Received(5), | ||
Sent(6), | ||
Bin(7); | ||
|
||
companion object { | ||
fun fromInt(n: Int): MessageCategories { | ||
return MessageCategories.entries.first { n == it.categoryId } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
composeApp/src/commonMain/kotlin/com/github/aeoliux/violet/app/appState/FetchMessage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.github.aeoliux.violet.app.appState | ||
|
||
import com.github.aeoliux.violet.api.scraping.messages.Message | ||
import com.github.aeoliux.violet.api.scraping.messages.getMessage | ||
|
||
suspend fun AppState.fetchMessage(url: String): Message? { | ||
this.logIn() | ||
return try { | ||
this.client.value.getMessage(url) | ||
} catch (e: Exception) { | ||
this.alertTitle.value = "Fetching message error" | ||
this.alertMessage.value = e.toString() | ||
this.showAlert.value = true | ||
|
||
null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
composeApp/src/commonMain/kotlin/com/github/aeoliux/violet/app/components/ComboBox.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.github.aeoliux.violet.app.components | ||
|
||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.wrapContentHeight | ||
import androidx.compose.material3.Card | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
|
||
@Composable | ||
fun <T> ComboBox( | ||
options: List<String>, | ||
values: List<T>, | ||
selectedIndex: Int = 0, | ||
onChange: (opt: T) -> Unit = {} | ||
) { | ||
var selectedIndex by remember { mutableStateOf(selectedIndex) } | ||
|
||
Card( | ||
Modifier.fillMaxWidth() | ||
.wrapContentHeight() | ||
.padding(start = 10.dp, end = 10.dp, top = 2.dp, bottom = 2.dp) | ||
) { | ||
ExpandableList( | ||
header = { Text( | ||
text = options[selectedIndex], | ||
modifier = Modifier.padding(15.dp) | ||
) }, | ||
) { | ||
options.forEachIndexed { index, it -> | ||
Row( | ||
Modifier | ||
.fillMaxSize() | ||
.clickable { | ||
selectedIndex = index | ||
onChange(values[selectedIndex]) | ||
}, | ||
verticalAlignment = Alignment.CenterVertically | ||
) { | ||
Text( | ||
modifier = Modifier | ||
.padding(15.dp), | ||
text = it | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.