Skip to content

Commit 1ab310d

Browse files
committed
Fix sharing location for other users active location messages
1 parent 1295343 commit 1ab310d

File tree

3 files changed

+45
-33
lines changed

3 files changed

+45
-33
lines changed

Sources/StreamChat/Controllers/CurrentUserController/CurrentUserController.swift

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright © 2024 Stream.io Inc. All rights reserved.
2+
// Copyright © 2025 Stream.io Inc. All rights reserved.
33
//
44

55
import CoreData
@@ -37,6 +37,9 @@ public class CurrentChatUserController: DataController, DelegateCallable, DataSt
3737
return _basePublishers as? BasePublishers ?? .init(controller: self)
3838
}
3939

40+
/// The observer for the active live location messages.
41+
private var activeLiveLocationMessagesObserver: BackgroundListDatabaseObserver<ChatMessage, MessageDTO>?
42+
4043
/// Used for observing the current user changes in a database.
4144
private lazy var currentUserObserver = createUserObserver()
4245
.onChange { [weak self] change in
@@ -47,6 +50,22 @@ public class CurrentChatUserController: DataController, DelegateCallable, DataSt
4750
}
4851
$0.currentUserController(self, didChangeCurrentUser: change)
4952
}
53+
54+
/// Only when we have access to the currentUserId is when we should
55+
/// create the observer for the active live location messages.
56+
if self?.activeLiveLocationMessagesObserver == nil {
57+
let observer = self?.createActiveLiveLocationMessagesObserver()
58+
self?.activeLiveLocationMessagesObserver = observer
59+
try? observer?.startObserving()
60+
observer?.onDidChange = { [weak self] _ in
61+
self?.delegateCallback { [weak self] in
62+
guard let self = self else { return }
63+
let messages = Array(observer?.items ?? [])
64+
self.isSharingLiveLocation = !messages.isEmpty
65+
$0.currentUserController(self, didChangeActiveLiveLocationMessages: messages)
66+
}
67+
}
68+
}
5069
}
5170
.onFieldChange(\.unreadCount) { [weak self] change in
5271
self?.delegateCallback { [weak self] in
@@ -58,20 +77,6 @@ public class CurrentChatUserController: DataController, DelegateCallable, DataSt
5877
}
5978
}
6079

61-
/// The observer for the active live location messages.
62-
private lazy var activeLiveLocationMessagesObserver: BackgroundListDatabaseObserver<ChatMessage, MessageDTO> = {
63-
let observer = createActiveLiveLocationMessagesObserver()
64-
observer.onDidChange = { [weak self] _ in
65-
self?.delegateCallback { [weak self] in
66-
guard let self = self else { return }
67-
let messages = Array(observer.items)
68-
self.isSharingLiveLocation = !messages.isEmpty
69-
$0.currentUserController(self, didChangeActiveLiveLocationMessages: messages)
70-
}
71-
}
72-
return observer
73-
}()
74-
7580
/// A flag to indicate whether the current user is sharing his live location.
7681
private var isSharingLiveLocation = false {
7782
didSet {
@@ -160,7 +165,6 @@ public class CurrentChatUserController: DataController, DelegateCallable, DataSt
160165

161166
do {
162167
try currentUserObserver.startObserving()
163-
try activeLiveLocationMessagesObserver.startObserving()
164168
state = .localDataFetched
165169
} catch {
166170
log.error("""
@@ -268,8 +272,11 @@ public extension CurrentChatUserController {
268272
///
269273
/// - Parameter location: The new location to be updated.
270274
func updateLiveLocation(_ location: LocationAttachmentInfo) {
275+
guard let messages = activeLiveLocationMessagesObserver?.items, !messages.isEmpty else {
276+
return
277+
}
278+
271279
locationUpdatesThrottler.execute { [weak self] in
272-
let messages = self?.activeLiveLocationMessagesObserver.items ?? []
273280
for message in messages {
274281
guard let cid = message.cid else { continue }
275282
let messageController = self?.client.messageController(cid: cid, messageId: message.id)
@@ -442,12 +449,15 @@ private extension CurrentChatUserController {
442449
)
443450
}
444451

445-
func createActiveLiveLocationMessagesObserver() -> BackgroundListDatabaseObserver<ChatMessage, MessageDTO> {
446-
environment.currentUserActiveLiveLocationMessagesObserverBuilder(
452+
func createActiveLiveLocationMessagesObserver() -> BackgroundListDatabaseObserver<ChatMessage, MessageDTO>? {
453+
guard let currentUserId = client.currentUserId else {
454+
return nil
455+
}
456+
return environment.currentUserActiveLiveLocationMessagesObserverBuilder(
447457
client.databaseContainer,
448458
MessageDTO.activeLiveLocationMessagesFetchRequest(
449-
channelId: nil,
450-
currentUserId: client.currentUserId
459+
currentUserId: currentUserId,
460+
channelId: nil
451461
),
452462
{ try $0.asModel() },
453463
NSFetchedResultsController<MessageDTO>.self

Sources/StreamChat/Database/DTOs/MessageDTO.swift

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright © 2024 Stream.io Inc. All rights reserved.
2+
// Copyright © 2025 Stream.io Inc. All rights reserved.
33
//
44

55
import CoreData
@@ -577,9 +577,11 @@ class MessageDTO: NSManagedObject {
577577
return try load(request, context: context)
578578
}
579579

580+
/// Fetches all active location messages in a channel or all channels of the current user.
581+
/// If `channelId` is nil, it will fetch all messages independent of the channel.
580582
static func activeLiveLocationMessagesFetchRequest(
581-
channelId: ChannelId?,
582-
currentUserId: UserId?
583+
currentUserId: UserId,
584+
channelId: ChannelId?
583585
) -> NSFetchRequest<MessageDTO> {
584586
let request = NSFetchRequest<MessageDTO>(entityName: MessageDTO.entityName)
585587
MessageDTO.applyPrefetchingState(to: request)
@@ -590,11 +592,9 @@ class MessageDTO: NSManagedObject {
590592
ascending: true
591593
)]
592594
var predicates: [NSPredicate] = [
593-
.init(format: "ANY attachments.isActiveLocationAttachment == YES")
595+
.init(format: "ANY attachments.isActiveLocationAttachment == YES"),
596+
.init(format: "user.id == %@", currentUserId)
594597
]
595-
if let currentUserId {
596-
predicates.append(.init(format: "user.id == %@", currentUserId))
597-
}
598598
if let channelId {
599599
predicates.append(.init(format: "channel.cid == %@", channelId.rawValue))
600600
}
@@ -603,13 +603,11 @@ class MessageDTO: NSManagedObject {
603603
}
604604

605605
static func loadActiveLiveLocationMessages(
606+
currentUserId: UserId,
606607
channelId: ChannelId?,
607608
context: NSManagedObjectContext
608609
) throws -> [MessageDTO] {
609-
let request = activeLiveLocationMessagesFetchRequest(
610-
channelId: channelId,
611-
currentUserId: context.currentUser?.user.id
612-
)
610+
let request = activeLiveLocationMessagesFetchRequest(currentUserId: currentUserId, channelId: channelId)
613611
return try load(request, context: context)
614612
}
615613

Sources/StreamChat/Repositories/MessageRepository.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright © 2024 Stream.io Inc. All rights reserved.
2+
// Copyright © 2025 Stream.io Inc. All rights reserved.
33
//
44

55
import CoreData
@@ -289,7 +289,11 @@ class MessageRepository {
289289
let context = database.backgroundReadOnlyContext
290290
context.perform {
291291
do {
292+
guard let currentUserId = context.currentUser?.user.id else {
293+
return completion(.failure(ClientError.CurrentUserDoesNotExist()))
294+
}
292295
let messages = try MessageDTO.loadActiveLiveLocationMessages(
296+
currentUserId: currentUserId,
293297
channelId: channelId,
294298
context: context
295299
)

0 commit comments

Comments
 (0)