Refactors old user zap object.

This commit is contained in:
Vitor Pamplona 2024-08-15 17:08:15 -04:00
parent 7513036d47
commit 68b15ba483
3 changed files with 20 additions and 44 deletions

View File

@ -1,38 +0,0 @@
/**
* Copyright (c) 2024 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.quartz.events.zaps
import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.ui.screen.ZapReqResponse
import com.vitorpamplona.quartz.events.LnZapEventInterface
object UserZaps {
fun forProfileFeed(zaps: Map<Note, Note?>?): List<ZapReqResponse> {
if (zaps == null) return emptyList()
return (
zaps
.mapNotNull { entry -> entry.value?.let { ZapReqResponse(entry.key, it) } }
.sortedBy { (it.zapEvent.event as? LnZapEventInterface)?.amount() }
.reversed()
)
}
}

View File

@ -20,16 +20,30 @@
*/
package com.vitorpamplona.amethyst.ui.dal
import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.model.User
import com.vitorpamplona.amethyst.ui.screen.ZapReqResponse
import com.vitorpamplona.quartz.events.zaps.UserZaps
import com.vitorpamplona.quartz.events.LnZapEventInterface
class UserProfileZapsFeedFilter(
val user: User,
) : FeedFilter<ZapReqResponse>() {
override fun feedKey(): String = user.pubkeyHex
override fun feed(): List<ZapReqResponse> = UserZaps.forProfileFeed(user.zaps)
override fun feed(): List<ZapReqResponse> = forProfileFeed(user.zaps)
override fun limit() = 400
companion object {
fun forProfileFeed(zaps: Map<Note, Note?>?): List<ZapReqResponse> {
if (zaps == null) return emptyList()
return (
zaps
.mapNotNull { entry -> entry.value?.let { ZapReqResponse(entry.key, it) } }
.sortedBy { (it.zapEvent.event as? LnZapEventInterface)?.amount() }
.reversed()
)
}
}
}

View File

@ -21,16 +21,16 @@
package com.vitorpamplona.amethyst.service.zaps
import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.ui.dal.UserProfileZapsFeedFilter
import com.vitorpamplona.quartz.encoders.HexKey
import com.vitorpamplona.quartz.events.LnZapEventInterface
import com.vitorpamplona.quartz.events.zaps.UserZaps
import io.mockk.every
import io.mockk.mockk
import org.junit.Assert
import org.junit.Test
import java.math.BigDecimal
class UserZapsTest {
class UserProfileZapsFeedFilterTest {
@Test
fun nothing() {
Assert.assertEquals(1, 1)
@ -38,7 +38,7 @@ class UserZapsTest {
@Test
fun user_without_zaps() {
val actual = UserZaps.forProfileFeed(zaps = null)
val actual = UserProfileZapsFeedFilter.forProfileFeed(zaps = null)
Assert.assertEquals(emptyList<Pair<Note, Note>>(), actual)
}
@ -53,7 +53,7 @@ class UserZapsTest {
zapRequest to mockZapNoteWith("user-1", amount = 200),
)
val actual = UserZaps.forProfileFeed(zaps)
val actual = UserProfileZapsFeedFilter.forProfileFeed(zaps)
Assert.assertEquals(1, actual.count())
Assert.assertEquals(zapRequest, actual.first().zapRequest)