Skip to content

Commit

Permalink
Accepting or rejecting requests updates the corresponding db row
Browse files Browse the repository at this point in the history
Closes #48
  • Loading branch information
TimCastelijns committed Jan 14, 2019
1 parent e0ce17b commit 9a0af7c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class MessageEventHandler(
private val getStarsDataUseCase: GetStarsDataUseCase,
private val acceptUserUseCase: AcceptUserUseCase,
private val rejectUserUseCase: RejectUserUseCase,
private val updateAccessRequestUseCase: UpdateAccessRequestUseCase,
private val syncStarsDataUseCase: SyncStarsDataUseCase,
private val setReminderUseCase: SetReminderUseCase,
private val updateUseCase: UpdateUseCase,
Expand Down Expand Up @@ -53,7 +54,7 @@ class MessageEventHandler(
handleMessage(event.message)
}

private suspend fun handleMessage(message: Message)= coroutineScope {
private suspend fun handleMessage(message: Message) = coroutineScope {
if (message.isCommand()) {
val time = measureTimeMillis {
launch { processCommandMessage(message) }.join()
Expand Down Expand Up @@ -152,10 +153,12 @@ class MessageEventHandler(
}

if (username != null) {
acceptUserByName(username)
// acceptUserByName(username)
actor.acceptReply("Accepting by name is currently not supported", messageId)
} else {
actor.provideLatestAccessRequestee()?.let { requestee ->
acceptUserByName(requestee.name)
updateAccessRequest(requestee, user, true)
} ?: actor.acceptMessage(messageFormatter.asRequesteeNotFound())
}
}
Expand All @@ -174,10 +177,12 @@ class MessageEventHandler(
}

if (username != null) {
rejectUserByName(username)
// rejectUserByName(username)
actor.acceptReply("Rejecting by name is currently not supported", messageId)
} else {
actor.provideLatestAccessRequestee()?.let { requestee ->
rejectUserByName(requestee.name)
updateAccessRequest(requestee, user, false)
} ?: actor.acceptMessage(messageFormatter.asRequesteeNotFound())
}
}
Expand All @@ -199,6 +204,11 @@ class MessageEventHandler(
}
}

private fun updateAccessRequest(requestee: User, user: User, accessGranted: Boolean) =
updateAccessRequestUseCase.execute(
UpdateAccessRequestParams(requestee.id, true, user.name, accessGranted)
)

private fun processLeaveCommand(user: User) {
if (user.id == 1843331L) {
actor.acceptMessage(messageFormatter.asLeavingString())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.timcastelijns.room15bot.bot.usecases

import com.timcastelijns.room15bot.data.db.AccessRequestDao

class UpdateAccessRequestUseCase(
private val accessRequestDao: AccessRequestDao
) : UseCase<UpdateAccessRequestParams, Unit> {

override fun execute(params: UpdateAccessRequestParams) {
accessRequestDao.update(
params.userId,
params.processed,
params.processedBy,
params.accessGranted
)
}

}

data class UpdateAccessRequestParams(
val userId: Long,
val processed: Boolean,
val processedBy: String,
val accessGranted: Boolean
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.timcastelijns.room15bot.data.db

import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.update
import java.time.Instant

class AccessRequestDao {

Expand All @@ -10,6 +12,18 @@ class AccessRequestDao {
AccessRequests.insert {
it[this.userId] = userId
it[this.username] = username
it[this.createdAt] = Instant.now().toEpochMilli()
}
}
}

fun update(userId: Long, processed: Boolean, processedBy: String, accessGranted: Boolean) {
transaction {
AccessRequests.update({ AccessRequests.userId eq userId }) {
it[this.processed] = processed
it[this.processedBy] = processedBy
it[this.processedAt] = Instant.now().toEpochMilli()
it[this.accessGranted] = accessGranted
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/com/timcastelijns/room15bot/data/db/Schema.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.timcastelijns.room15bot.data.db

import org.jetbrains.exposed.dao.IntIdTable
import org.jetbrains.exposed.dao.LongIdTable
import java.time.Instant

object StarredMessages : LongIdTable() {
val username = varchar("username", 255)
Expand All @@ -21,6 +20,8 @@ object AccessRequests: IntIdTable() {
val userId = long("userId")
val username = varchar("username", 255)
val processed = bool("processed").default(false)
val processedAt = long("processedAt").nullable()
val processedBy = varchar("processedBy", 255).nullable()
val timestamp = long("timestamp").default(Instant.now().toEpochMilli())
val createdAt = long("createdAt")
val accessGranted = bool("accessGranted").nullable()
}
1 change: 1 addition & 0 deletions src/main/kotlin/com/timcastelijns/room15bot/di/module.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ private val module: Module = module {
factory<AcceptUserUseCase>()
factory<RejectUserUseCase>()
factory<CreateAccessRequestUseCase>()
factory<UpdateAccessRequestUseCase>()

factory { UpdateUseCase(Runtime.getRuntime()) }

Expand Down

0 comments on commit 9a0af7c

Please sign in to comment.