fix bug of not checking sender and receiver
This commit is contained in:
@@ -239,12 +239,6 @@ extension AuthSessionStore {
|
|||||||
if returnedUser != nil {
|
if returnedUser != nil {
|
||||||
self.friendsArr.append(returnedUser!)
|
self.friendsArr.append(returnedUser!)
|
||||||
|
|
||||||
// if this friend do not exist in the dict, add it to show up in my circle
|
|
||||||
// TODO: prolly don't need this function to mess with dictionary
|
|
||||||
if self.friendMessagesDict[returnedUser!.id!] == nil {
|
|
||||||
self.friendMessagesDict[returnedUser!.id!] = []
|
|
||||||
}
|
|
||||||
|
|
||||||
self.objectWillChange.send()
|
self.objectWillChange.send()
|
||||||
|
|
||||||
print("added this user to the array of users for user's circle\(returnedUser)")
|
print("added this user to the array of users for user's circle\(returnedUser)")
|
||||||
@@ -289,38 +283,52 @@ extension AuthSessionStore {
|
|||||||
// SOLUTION: composite with array
|
// SOLUTION: composite with array
|
||||||
db.collection("messages").whereField("senderIdReceiverIdComposite", arrayContains: userId).order(by: "sentTimestamp", descending: true).limit(to: 100)
|
db.collection("messages").whereField("senderIdReceiverIdComposite", arrayContains: userId).order(by: "sentTimestamp", descending: true).limit(to: 100)
|
||||||
.addSnapshotListener { querySnapshot, error in
|
.addSnapshotListener { querySnapshot, error in
|
||||||
guard let snapshot = querySnapshot else {
|
guard let documents = querySnapshot?.documents else {
|
||||||
print("Error fetching snapshots: \(error!)")
|
print("error in fetching messages: \(error!)")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
try? snapshot.documentChanges.forEach { diff in
|
print("going through all messages now that the query found changes")
|
||||||
// only need to modify array on new additions...
|
|
||||||
if (diff.type == .added) {
|
// clearing dict to allow clean list of messages to be put forth
|
||||||
let currMessage = try diff.document.data(as: Message.self)
|
//optimize this? but also saving on memory and same db reads
|
||||||
print("new message received! \(currMessage)")
|
self.friendMessagesDict.removeAll()
|
||||||
|
|
||||||
|
self.messagesArr = documents.compactMap { (queryDocumentSnapshot) -> Message? in
|
||||||
|
do {
|
||||||
|
let currMessage = try queryDocumentSnapshot.data(as: Message.self)
|
||||||
|
print("new message received! \(currMessage!.sentTimestamp)")
|
||||||
|
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
if currMessage != nil { // not really possible but just check
|
||||||
|
// if the user doesn't exist for the dictionary, then add it
|
||||||
|
// this means it's most likely someone new (never had user_friend relationship before) messaging for the user's inbox
|
||||||
|
// TODO: prolly want to make a call to get this sender user details for the inbox, but they should either be in the friendsArr or their are not a friend so won't be there
|
||||||
|
// also add in any messages where I am the sender
|
||||||
|
if currMessage!.senderId == userId { // if I am the sender
|
||||||
|
if self.friendMessagesDict[currMessage!.receiverId] == nil {
|
||||||
|
self.friendMessagesDict[currMessage!.receiverId] = [currMessage!]
|
||||||
|
} else {
|
||||||
|
self.friendMessagesDict[currMessage!.receiverId]?.append(currMessage!)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else { // if I am receiving
|
||||||
|
if self.friendMessagesDict[currMessage!.senderId] == nil {
|
||||||
|
self.friendMessagesDict[currMessage!.senderId] = [currMessage!]
|
||||||
|
} else {
|
||||||
|
self.friendMessagesDict[currMessage!.senderId]?.append(currMessage!)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if currMessage != nil { // not really possible but just check
|
|
||||||
// if the user doesn't exist for the dictionary, then add it
|
|
||||||
// this means it's most likely someone new (never had user_friend relationship before) messaging for the user's inbox
|
|
||||||
// TODO: is the firestore ordering working? maybe need clientside ordering here...just maybe...keep array of messages sorted, but this should automatically be sorted?
|
|
||||||
if self.friendMessagesDict[currMessage!.senderId] == nil {
|
|
||||||
self.friendMessagesDict[currMessage!.senderId] = [currMessage!]
|
|
||||||
} else { // [2, 1] => [2, 1]
|
|
||||||
self.friendMessagesDict[currMessage!.senderId]?.append(currMessage!)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//self.objectWillChange.send()// TODO: maybe don't need? value type friendsMessagesDict? so publishes changes?
|
self.objectWillChange.send()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return currMessage
|
||||||
|
} catch {
|
||||||
|
print(error)
|
||||||
}
|
}
|
||||||
// no need to a alter view model as of now
|
return nil
|
||||||
if (diff.type == .modified) {
|
|
||||||
print("Modified city: \(diff.document.data())")
|
|
||||||
}
|
|
||||||
// no feature for deleting messages as of now
|
|
||||||
if (diff.type == .removed) {
|
|
||||||
print("Removed city: \(diff.document.data())")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -294,9 +294,10 @@ extension CircleGridView {
|
|||||||
// TODO: protect against force unwraps
|
// TODO: protect against force unwraps
|
||||||
var AVPlayerItems: [AVPlayerItem] = []
|
var AVPlayerItems: [AVPlayerItem] = []
|
||||||
let messagesRelatedToFriend = self.authSessionStore.friendMessagesDict[friend.id!]!
|
let messagesRelatedToFriend = self.authSessionStore.friendMessagesDict[friend.id!]!
|
||||||
print("have \(messagesRelatedToFriend.count) messages to play")
|
|
||||||
|
|
||||||
for message in messagesRelatedToFriend {
|
for message in messagesRelatedToFriend {
|
||||||
|
print("message: the sender is \(message.senderId) and senttime: \(message.sentTimestamp)")
|
||||||
// if it's starting to get to my messages then don't play
|
// if it's starting to get to my messages then don't play
|
||||||
if message.senderId == self.authSessionStore.user?.id {
|
if message.senderId == self.authSessionStore.user?.id {
|
||||||
break
|
break
|
||||||
@@ -311,6 +312,8 @@ extension CircleGridView {
|
|||||||
|
|
||||||
// start playing if there are messages to listen to
|
// start playing if there are messages to listen to
|
||||||
if AVPlayerItems.count > 0 {
|
if AVPlayerItems.count > 0 {
|
||||||
|
print("have \(AVPlayerItems.count) messages to play")
|
||||||
|
|
||||||
// reverse the items because we want to listen to the most recent messages in order
|
// reverse the items because we want to listen to the most recent messages in order
|
||||||
AVPlayerItems = AVPlayerItems.reversed()
|
AVPlayerItems = AVPlayerItems.reversed()
|
||||||
queuePlayer = AVQueuePlayer(items: AVPlayerItems)
|
queuePlayer = AVQueuePlayer(items: AVPlayerItems)
|
||||||
|
|||||||
Reference in New Issue
Block a user