Skip to content

Commit

Permalink
Integrate top zaps with PostStatsUpdater
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandarIlic committed Apr 24, 2024
1 parent d447902 commit e34776f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
17 changes: 17 additions & 0 deletions app/src/main/kotlin/net/primal/android/feed/db/NoteZapDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ import kotlinx.coroutines.flow.Flow
@Dao
interface NoteZapDao {

@Insert
fun insert(data: NoteZapData)

@Query(
"""
DELETE FROM NoteZapData
WHERE zapSenderId = :senderId AND zapReceiverId = :receiverId AND noteId = :noteId
AND (zapRequestAt = :timestamp OR zapReceiptAt = :timestamp)
""",
)
fun delete(
senderId: String,
receiverId: String,
noteId: String,
timestamp: Long,
)

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun upsertAll(data: List<NoteZapData>)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ class PostRepository @Inject constructor(
@Throws(NostrPublishException::class)
suspend fun likePost(postId: String, postAuthorId: String) {
val userId = activeAccountStore.activeUserId()
val statsUpdater = PostStatsUpdater(postId = postId, userId = userId, database = database)
val statsUpdater = PostStatsUpdater(
postId = postId,
userId = userId,
postAuthorId = postAuthorId,
database = database,
)

try {
statsUpdater.increaseLikeStats()
Expand All @@ -55,7 +60,12 @@ class PostRepository @Inject constructor(
postRawNostrEvent: String,
) {
val userId = activeAccountStore.activeUserId()
val statsUpdater = PostStatsUpdater(postId = postId, userId = userId, database = database)
val statsUpdater = PostStatsUpdater(
postId = postId,
userId = userId,
postAuthorId = postAuthorId,
database = database,
)

try {
statsUpdater.increaseRepostStats()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package net.primal.android.feed.repository

import androidx.room.withTransaction
import kotlin.time.Duration.Companion.milliseconds
import net.primal.android.db.PrimalDatabase
import net.primal.android.feed.db.NoteZapData
import net.primal.android.feed.db.PostStats
import net.primal.android.profile.db.PostUserStats
import net.primal.android.wallet.utils.CurrencyConversionUtils.toBtc

class PostStatsUpdater(
val postId: String,
val userId: String,
val postAuthorId: String,
val database: PrimalDatabase,
) {

private val timestamp: Long = System.currentTimeMillis().milliseconds.inWholeSeconds

private val postStats: PostStats by lazy {
database.postStats().find(postId = postId)
?: PostStats(postId = postId)
Expand All @@ -33,7 +39,7 @@ class PostStatsUpdater(
database.postUserStats().upsert(data = postUserStats.copy(reposted = true))
}

suspend fun increaseZapStats(amountInSats: Int) =
suspend fun increaseZapStats(amountInSats: Int, zapComment: String) =
database.withTransaction {
database.postStats().upsert(
data = postStats.copy(
Expand All @@ -42,11 +48,29 @@ class PostStatsUpdater(
),
)
database.postUserStats().upsert(data = postUserStats.copy(zapped = true))

database.noteZaps().insert(
data = NoteZapData(
zapSenderId = userId,
zapReceiverId = postAuthorId,
noteId = postId,
zapRequestAt = timestamp,
zapReceiptAt = timestamp,
amountInBtc = amountInSats.toBtc(),
message = zapComment,
),
)
}

suspend fun revertStats() =
database.withTransaction {
database.postStats().upsert(data = postStats)
database.postUserStats().upsert(data = postUserStats)
database.noteZaps().delete(
noteId = postId,
senderId = userId,
receiverId = postAuthorId,
timestamp = timestamp,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ class ZapHandler @Inject constructor(
val statsUpdater = target.buildPostStatsUpdaterIfApplicable(userId)

try {
statsUpdater?.increaseZapStats(amountInSats = zapAmountInSats.toInt())
statsUpdater?.increaseZapStats(
amountInSats = zapAmountInSats.toInt(),
zapComment = zapComment,
)

val userZapRequestEvent = notary.signZapRequestNostrEvent(
userId = userId,
comment = zapComment,
Expand Down Expand Up @@ -96,8 +100,9 @@ class ZapHandler @Inject constructor(
private fun ZapTarget.buildPostStatsUpdaterIfApplicable(userId: String) =
when (this) {
is ZapTarget.Note -> PostStatsUpdater(
postId = this.id,
userId = userId,
postId = this.id,
postAuthorId = this.authorPubkey,
database = database,
)

Expand Down

0 comments on commit e34776f

Please sign in to comment.