Storing and Counting followers of the main account.

This commit is contained in:
Vitor Pamplona
2023-01-14 10:16:36 -05:00
parent d130a43358
commit f580fdd216
3 changed files with 28 additions and 5 deletions

View File

@@ -18,7 +18,7 @@ class User(val pubkey: ByteArray) {
val follows = Collections.synchronizedSet(mutableSetOf<User>()) val follows = Collections.synchronizedSet(mutableSetOf<User>())
val taggedPosts = Collections.synchronizedSet(mutableSetOf<Note>()) val taggedPosts = Collections.synchronizedSet(mutableSetOf<Note>())
var follower: Number? = null val followers = Collections.synchronizedSet(mutableSetOf<User>())
fun toBestDisplayName(): String { fun toBestDisplayName(): String {
return bestDisplayName() ?: bestUsername() ?: pubkeyDisplayHex return bestDisplayName() ?: bestUsername() ?: pubkeyDisplayHex
@@ -37,9 +37,26 @@ class User(val pubkey: ByteArray) {
return info.picture ?: "https://robohash.org/${pubkeyHex}.png" return info.picture ?: "https://robohash.org/${pubkeyHex}.png"
} }
fun follow(user: User) {
follows.add(user)
user.followers.add(this)
}
fun unfollow(user: User) {
follows.remove(user)
user.followers.remove(this)
}
fun updateFollows(newFollows: List<User>, updateAt: Long) { fun updateFollows(newFollows: List<User>, updateAt: Long) {
follows.clear() val toBeAdded = newFollows - follows
follows.addAll(newFollows) val toBeRemoved = follows - newFollows
toBeAdded.forEach {
follow(it)
}
toBeRemoved.forEach {
unfollow(it)
}
updatedFollowsAt = updateAt updatedFollowsAt = updateAt
live.refresh() live.refresh()

View File

@@ -2,8 +2,11 @@ package com.vitorpamplona.amethyst.service
import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.service.model.ReactionEvent
import com.vitorpamplona.amethyst.service.model.RepostEvent
import java.util.Collections import java.util.Collections
import nostr.postr.JsonFilter import nostr.postr.JsonFilter
import nostr.postr.events.TextNoteEvent
object NostrSingleEventDataSource: NostrDataSource("SingleEventFeed") { object NostrSingleEventDataSource: NostrDataSource("SingleEventFeed") {
val eventsToWatch = Collections.synchronizedList(mutableListOf<String>()) val eventsToWatch = Collections.synchronizedList(mutableListOf<String>())
@@ -15,7 +18,9 @@ object NostrSingleEventDataSource: NostrDataSource("SingleEventFeed") {
return null return null
} }
// downloads all the reactions to a given event.
return JsonFilter( return JsonFilter(
kinds = listOf(TextNoteEvent.kind, ReactionEvent.kind, RepostEvent.kind),
tags = mapOf("e" to reactionsToWatch) tags = mapOf("e" to reactionsToWatch)
) )
} }
@@ -39,6 +44,7 @@ object NostrSingleEventDataSource: NostrDataSource("SingleEventFeed") {
return null return null
} }
// downloads linked events to this event.
return JsonFilter( return JsonFilter(
ids = interestedEvents ids = interestedEvents
) )

View File

@@ -118,11 +118,11 @@ fun ProfileContent(accountUser: User?, modifier: Modifier = Modifier) {
Text(" @${accountUser?.bestUsername()}", color = Color.LightGray) Text(" @${accountUser?.bestUsername()}", color = Color.LightGray)
Row(modifier = Modifier.padding(top = 15.dp)) { Row(modifier = Modifier.padding(top = 15.dp)) {
Row() { Row() {
Text("${accountUser?.follows?.size}", fontWeight = FontWeight.Bold) Text("${accountUser?.follows?.size ?: "--"}", fontWeight = FontWeight.Bold)
Text(" Following") Text(" Following")
} }
Row(modifier = Modifier.padding(start = 10.dp)) { Row(modifier = Modifier.padding(start = 10.dp)) {
Text("${accountUser?.follower ?: "--"}", fontWeight = FontWeight.Bold) Text("${accountUser?.followers?.size ?: "--"}", fontWeight = FontWeight.Bold)
Text(" Followers") Text(" Followers")
} }
} }