Skip to content
Snippets Groups Projects
Commit 98b79822 authored by Bruno Muniz's avatar Bruno Muniz :apple:
Browse files

Trying to fix group issue

parent c4a6b634
No related branches found
No related tags found
2 merge requests!40v1.1.2b166,!38Using new database structure
...@@ -124,12 +124,16 @@ extension Session { ...@@ -124,12 +124,16 @@ extension Session {
message.date = Date() message.date = Date()
if let message = try? dbManager.saveMessage(message) { if let message = try? dbManager.saveMessage(message) {
send(message: message) if let recipientId = message.recipientId {
send(message: message)
} else {
send(groupMessage: message)
}
} }
} }
} }
private func send(message: Message) { func send(message: Message) {
var message = message var message = message
var reply: Reply? var reply: Reply?
......
...@@ -25,17 +25,49 @@ extension Session { ...@@ -25,17 +25,49 @@ extension Session {
members: [Contact], members: [Contact],
_ completion: @escaping (Result<GroupInfo, Error>) -> Void _ completion: @escaping (Result<GroupInfo, Error>) -> Void
) { ) {
guard let manager = client.groupManager else { fatalError("A group manager was not created") } guard let manager = client.groupManager else {
fatalError("A group manager was not created")
let me = client.bindings.meMarshalled }
let memberIds = members.map { $0.id }
manager.create(me: me, name: name, welcome: welcome, with: memberIds) { [weak self] in manager.create(
me: myId,
name: name,
welcome: welcome,
with: members.map { $0.id }) { [weak self] result in
guard let self = self else { return } guard let self = self else { return }
switch $0 { switch result {
case .success(let group): case .success(let group):
completion(.success(self.processGroupCreation(group, memberIds: memberIds, welcome: welcome))) try! self.dbManager.saveGroup(group)
members
.map { GroupMember(groupId: group.id, contactId: $0.id) }
.forEach { try! self.dbManager.saveGroupMember($0) }
// TODO: Add saveBulkGroupMembers to the database
if let welcome = welcome {
let message = Message(
networkId: nil,
senderId: self.myId,
recipientId: nil,
groupId: group.id,
date: group.createdAt,
status: .received,
isUnread: false,
text: welcome,
replyMessageId: nil,
roundURL: nil,
fileTransferId: nil
)
try! self.dbManager.saveMessage(message)
}
let query = GroupInfo.Query(groupId: group.id)
let info = try! self.dbManager.fetchGroupInfos(query).first
completion(.success(info!))
case .failure(let error): case .failure(let error):
completion(.failure(error)) completion(.failure(error))
} }
...@@ -48,9 +80,35 @@ extension Session { ...@@ -48,9 +80,35 @@ extension Session {
/// ///
_ = try! dbManager.saveGroup(group) _ = try! dbManager.saveGroup(group)
/// Save the members /// Which of those members are not my friends?
/// ///
memberIds.forEach { _ = try! dbManager.saveGroupMember(.init(groupId: group.id, contactId: $0)) } let friendsParticipating = try! dbManager.fetchContacts(Contact.Query(id: Set(memberIds)))
/// Save the strangers as contacts
///
let friendIds = friendsParticipating.map(\.id)
memberIds.forEach {
if !friendIds.contains($0) {
try! dbManager.saveContact(.init(
id: $0,
marshaled: nil,
username: nil,
email: nil,
phone: nil,
nickname: nil,
photo: nil,
authStatus: .stranger,
isRecent: false,
createdAt: Date()
))
}
}
/// Save group members relation
///
memberIds.forEach {
try! dbManager.saveGroupMember(.init(groupId: group.id, contactId: $0))
}
/// Save the welcome message (if any) /// Save the welcome message (if any)
/// ///
...@@ -70,9 +128,8 @@ extension Session { ...@@ -70,9 +128,8 @@ extension Session {
)) ))
} }
/// Buzz if the group was not created by me
/// if inappnotifications {
if group.leaderId != client.bindings.myId, inappnotifications {
DeviceFeedback.sound(.contactAdded) DeviceFeedback.sound(.contactAdded)
DeviceFeedback.shake(.notification) DeviceFeedback.shake(.notification)
} }
...@@ -103,15 +160,15 @@ extension Session { ...@@ -103,15 +160,15 @@ extension Session {
do { do {
message = try dbManager.saveMessage(message) message = try dbManager.saveMessage(message)
send(message: message) send(groupMessage: message)
} catch { } catch {
log(string: error.localizedDescription, type: .error) log(string: error.localizedDescription, type: .error)
} }
} }
private func send(message: Message) { func send(groupMessage: Message) {
guard let manager = client.groupManager else { fatalError("A group manager was not created") } guard let manager = client.groupManager else { fatalError("A group manager was not created") }
var message = message var message = groupMessage
var reply: Reply? var reply: Reply?
if let replyId = message.replyMessageId, if let replyId = message.replyMessageId,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment