File tree Expand file tree Collapse file tree
app/src/main/kotlin/net/primal/android/user/subscriptions
remote/src/commonMain/kotlin/net/primal/data/remote/api/stream
repository/src/commonMain/kotlin/net/primal/data/repository/streams
domain/primal/src/commonMain/kotlin/net/primal/domain/streams Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -138,6 +138,7 @@ class SubscriptionsManager @Inject constructor(
138138
139139 private fun launchStreamsFromFollowsSubscription (userId : String ) =
140140 scope.launch {
141+ streamRepository.syncLiveEventsFromFollows(userId = userId)
141142 streamRepository.startLiveEventsFromFollowsSubscription(userId = userId)
142143 }
143144
Original file line number Diff line number Diff line change @@ -17,5 +17,7 @@ interface LiveStreamApi {
1717
1818 suspend fun subscribeToLiveEventsFromFollows (userId : String ): Flow <NostrEvent >
1919
20+ suspend fun getLiveEventsFromFollows (userId : String ): List <NostrEvent >
21+
2022 suspend fun findLiveStream (body : FindLiveStreamRequestBody ): FindLiveStreamResponse
2123}
Original file line number Diff line number Diff line change @@ -73,6 +73,14 @@ class LiveStreamApiImpl(
7373 }
7474 }
7575
76+ override suspend fun getLiveEventsFromFollows (userId : String ): List <NostrEvent > =
77+ primalApiClient.query(
78+ message = PrimalCacheFilter (
79+ primalVerb = PrimalVerb .LIVE_EVENTS_FROM_FOLLOWS .id,
80+ optionsJson = LiveEventsFromFollowsRequest (pubkey = userId).encodeToJsonString(),
81+ ),
82+ ).filterNostrEvents(NostrEventKind .LiveActivity )
83+
7684 override suspend fun findLiveStream (body : FindLiveStreamRequestBody ): FindLiveStreamResponse {
7785 val queryResult = primalApiClient.query(
7886 message = PrimalCacheFilter (
Original file line number Diff line number Diff line change @@ -112,9 +112,35 @@ class StreamRepositoryImpl(
112112 return @withContext job
113113 }
114114
115+ override suspend fun syncLiveEventsFromFollows (userId : String ) =
116+ withContext(dispatcherProvider.io()) {
117+ val currentLiveStreams = liveStreamApi.getLiveEventsFromFollows(userId = userId)
118+ .mapNotNull { it.asStreamData() }
119+
120+ currentLiveStreams.forEach { liveActivity ->
121+ scope.launch {
122+ profileRepository.fetchMissingProfiles(profileIds = listOf (liveActivity.mainHostId))
123+ }
124+ }
125+
126+ database.withTransaction {
127+ database.streams().upsertStreamData(data = currentLiveStreams)
128+ database.streamFollows().deleteAllByOwnerId(ownerId = userId)
129+ currentLiveStreams
130+ .filter { it.isLive() }
131+ .forEach { liveActivity ->
132+ database.streamFollows().upsert(
133+ data = StreamFollowsCrossRef (
134+ streamATag = liveActivity.aTag,
135+ ownerId = userId,
136+ ),
137+ )
138+ }
139+ }
140+ }
141+
115142 override suspend fun startLiveEventsFromFollowsSubscription (userId : String ) =
116143 withContext(dispatcherProvider.io()) {
117- database.streamFollows().deleteAllByOwnerId(ownerId = userId)
118144 liveStreamApi.subscribeToLiveEventsFromFollows(userId = userId)
119145 .collect { liveActivityEvent ->
120146 val liveActivity = liveActivityEvent.asStreamData() ? : return @collect
Original file line number Diff line number Diff line change @@ -35,6 +35,8 @@ interface StreamRepository {
3535 streamContentModerationMode : StreamContentModerationMode ,
3636 ): Job
3737
38+ suspend fun syncLiveEventsFromFollows (userId : String )
39+
3840 suspend fun startLiveEventsFromFollowsSubscription (userId : String )
3941
4042 fun observeLiveEventsFromFollows (userId : String ): Flow <List <Stream >>
You can’t perform that action at this time.
0 commit comments