From e34776fafb12a57144572fd338e6db1ec670b83f Mon Sep 17 00:00:00 2001 From: Aleksandar Ilic Date: Wed, 24 Apr 2024 15:53:34 +0200 Subject: [PATCH] Integrate top zaps with PostStatsUpdater --- .../net/primal/android/feed/db/NoteZapDao.kt | 17 ++++++++++++ .../android/feed/repository/PostRepository.kt | 14 ++++++++-- .../feed/repository/PostStatsUpdater.kt | 26 ++++++++++++++++++- .../primal/android/wallet/zaps/ZapHandler.kt | 9 +++++-- 4 files changed, 61 insertions(+), 5 deletions(-) diff --git a/app/src/main/kotlin/net/primal/android/feed/db/NoteZapDao.kt b/app/src/main/kotlin/net/primal/android/feed/db/NoteZapDao.kt index d46fc247..f057b73a 100644 --- a/app/src/main/kotlin/net/primal/android/feed/db/NoteZapDao.kt +++ b/app/src/main/kotlin/net/primal/android/feed/db/NoteZapDao.kt @@ -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) diff --git a/app/src/main/kotlin/net/primal/android/feed/repository/PostRepository.kt b/app/src/main/kotlin/net/primal/android/feed/repository/PostRepository.kt index 071eccfb..4476fb6e 100644 --- a/app/src/main/kotlin/net/primal/android/feed/repository/PostRepository.kt +++ b/app/src/main/kotlin/net/primal/android/feed/repository/PostRepository.kt @@ -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() @@ -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() diff --git a/app/src/main/kotlin/net/primal/android/feed/repository/PostStatsUpdater.kt b/app/src/main/kotlin/net/primal/android/feed/repository/PostStatsUpdater.kt index 19b59596..1ae4d216 100644 --- a/app/src/main/kotlin/net/primal/android/feed/repository/PostStatsUpdater.kt +++ b/app/src/main/kotlin/net/primal/android/feed/repository/PostStatsUpdater.kt @@ -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) @@ -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( @@ -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, + ) } } diff --git a/app/src/main/kotlin/net/primal/android/wallet/zaps/ZapHandler.kt b/app/src/main/kotlin/net/primal/android/wallet/zaps/ZapHandler.kt index f633925f..5addbb38 100644 --- a/app/src/main/kotlin/net/primal/android/wallet/zaps/ZapHandler.kt +++ b/app/src/main/kotlin/net/primal/android/wallet/zaps/ZapHandler.kt @@ -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, @@ -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, )