sok dolog

This commit is contained in:
2026-01-17 15:51:30 +01:00
parent a926a9d667
commit dcb08a5e84
6 changed files with 223 additions and 22 deletions
+42 -16
View File
@@ -64,13 +64,14 @@ class Conversation(val ID: Int)
{
return """
<div class="conversation">
<p style="flex-grow: 1; background: ">$ID</p>
<p style="flex-grow: 1;">$ID</p>
<p style="flex-grow: 9;">${messages.last().text}</p>
<button style="flex-grow: 1;" onclick="open_message($ID)">reply</button>
<div>
""".trimIndent()
}
override fun toString(): String
fun sendOne(admin: Boolean = false): String
{
newMsg = false
@@ -80,7 +81,7 @@ class Conversation(val ID: Int)
{
if (!message.read)
{
returnString.append(message.toString())
returnString.append(message.toString().also { if ( !admin )message.read = true })
returnString.append("\n")
}
}
@@ -88,13 +89,13 @@ class Conversation(val ID: Int)
return returnString.toString()
}
fun sendAll(): String
fun sendAll(admin: Boolean = false): String
{
val returnString = StringBuilder()
for (message in messages.reversed())
{
returnString.append(message.toString())
returnString.append(message.toString().also { if (!admin) message.read = true })
returnString.append("\n")
}
@@ -102,16 +103,21 @@ class Conversation(val ID: Int)
}
}
data class Message(val user: Boolean, val text: String)
class Message(val user: Boolean, messagetext: String)
{
var read: Boolean = false
val text: String
init {
text = if (!user) "That's a great question/opinion/response! I'm so proud that you came up with that. You are " +
"very close to the correct solution. " + messagetext + " Thank you for your attention in this matter."
else messagetext
}
override fun toString(): String
{
read = true
return """
<div class="message-${if (user) "user" else "scott"}">
<div class="message message-${if (user) "user" else "scott"}">
<p>$text</p>
</div>
""".trimIndent()
@@ -131,12 +137,32 @@ fun runEmbeddedServer()
staticFiles("/resources", File(resourcesPath))
staticFiles("/", File(resourcesPath+"/index.html"))
staticFiles("/admin", File(resourcesPath+"/admin.html"))
staticFiles("/about", File(resourcesPath+"/about.html"))
route("/admin")
{
get("/api/all_messages") {
call.respondText(ConversationHandler.listAllConversations())
}
get("/api/all_messages/{id}") {
ConversationHandler.conversations.find {
it.ID == call.parameters["id"]?.toInt()
}.also{ call.respondText(it?.sendAll(true) ?: "") }
}
put("/api/write/{id}")
{
try {
ConversationHandler.conversations.find {
it.ID == call.parameters["id"]?.toInt()
}?.write(Message(false, call.receiveText()))
call.respond(HttpStatusCode.OK)
} catch (e: Exception) { call.respond(HttpStatusCode.NotFound) }
}
}
put("/api/write/{id}")
@@ -152,18 +178,18 @@ fun runEmbeddedServer()
}
get("/api/init") {
ConversationHandler.newConv()
println(ConversationHandler.conversations.last().ID)
call.respondText(ConversationHandler.conversations.last().ID.toString())
}
get("/api/all_messages/{id}") {
ConversationHandler.conversations.find {
it.ID == call.parameters["id"]?.toInt()
}.also{ call.respondText(it?.sendAll() ?: "") }
}
get("/api/init") {
ConversationHandler.newConv()
println(ConversationHandler.conversations.last().ID)
call.respondText(ConversationHandler.conversations.last().ID.toString())
}
get("/api/reinit/{id}") {
ConversationHandler.newConv(call.parameters["id"]?.toInt() ?: -1)
println(ConversationHandler.conversations.last().ID)
@@ -174,7 +200,7 @@ fun runEmbeddedServer()
{
ConversationHandler.conversations.find {
it.ID == call.parameters["id"]?.toInt()
}.also { if (it?.newMsg?:false) call.respondText(it.toString()) else call.respondText("") }
}.also { if (it?.newMsg?:false) call.respondText(it.sendOne()) else call.respondText("") }
}
}
}.start(wait = true)