Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: generate notification user contact join - WPB-11660 #2383

Open
wants to merge 1 commit into
base: refactor/generate-notification-conversation-member-leave
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ struct ConversationNotificationBuilder: NotificationBuilder {
return UNMutableNotificationContent()
}

builder = await NewUserMessageNotificationBuilder(
builder = await ConversationUserMessageNotificationBuilder(
message: genericMessage,
conversationID: mlsMessageEvent.conversationID,
senderID: mlsMessageEvent.senderID
Expand All @@ -103,7 +103,7 @@ struct ConversationNotificationBuilder: NotificationBuilder {
return UNMutableNotificationContent()
}

builder = await NewUserMessageNotificationBuilder(
builder = await ConversationUserMessageNotificationBuilder(
message: genericMessage,
conversationID: proteusMessageEvent.conversationID,
senderID: proteusMessageEvent.senderID
Expand All @@ -112,7 +112,7 @@ struct ConversationNotificationBuilder: NotificationBuilder {
case let .memberLeave(memberLeaveEvent):
let removedUserIDs = Set(memberLeaveEvent.removedUserIDs.compactMap(\.uuid))

builder = await NewSystemMessageNotificationBuilder(
builder = await ConversationSystemMessageNotificationBuilder(
systemMessage: .memberLeave(removedUserIDs: removedUserIDs),
conversationID: memberLeaveEvent.conversationID,
senderID: memberLeaveEvent.senderID
Expand All @@ -121,37 +121,37 @@ struct ConversationNotificationBuilder: NotificationBuilder {
case let .memberJoin(memberJoinEvent):
let addedUserIDs = Set(memberJoinEvent.members.compactMap(\.id))

builder = await NewSystemMessageNotificationBuilder(
builder = await ConversationSystemMessageNotificationBuilder(
systemMessage: .memberJoin(addedUserIDs: addedUserIDs),
conversationID: memberJoinEvent.conversationID,
senderID: memberJoinEvent.senderID
)

case let .create(conversationCreateEvent):

builder = await NewSystemMessageNotificationBuilder(
builder = await ConversationSystemMessageNotificationBuilder(
systemMessage: .conversationCreated,
conversationID: conversationCreateEvent.conversationID,
senderID: conversationCreateEvent.senderID
)

case let .delete(conversationDeleteEvent):

builder = await NewSystemMessageNotificationBuilder(
builder = await ConversationSystemMessageNotificationBuilder(
systemMessage: .conversationDeleted,
conversationID: conversationDeleteEvent.conversationID,
senderID: conversationDeleteEvent.senderID
)

case let .messageTimerUpdate(messageTimerUpdateEvent):

builder = await NewSystemMessageNotificationBuilder(
builder = await ConversationSystemMessageNotificationBuilder(
systemMessage: .messageTimerUpdate(newTimer: messageTimerUpdateEvent.newTimer),
conversationID: messageTimerUpdateEvent.conversationID,
senderID: messageTimerUpdateEvent.senderID
)

default: // TODO: [WPB-11175] - Generate notifications for other events
default:
return UNMutableNotificationContent()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import WireAPI
import WireDataModel

struct NewSystemMessageNotificationBuilder: NotificationBuilder {
struct ConversationSystemMessageNotificationBuilder: NotificationBuilder {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused by the naming, what is the relevance of "system message"?


enum SystemMessage {
case memberLeave(removedUserIDs: Set<UUID>)
Expand Down Expand Up @@ -127,7 +127,7 @@ struct NewSystemMessageNotificationBuilder: NotificationBuilder {
content.title = title
}

let body = NotificationBody.newSystemMessage(
let body = NotificationBody.conversationSystemMessage(
.removedYou(senderName: context.senderName)
)

Expand Down Expand Up @@ -167,7 +167,7 @@ struct NewSystemMessageNotificationBuilder: NotificationBuilder {
}

return NotificationTitle
.newMessage(format)
.conversationMessage(format)
.make()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import WireAPI
import WireDataModel

struct NewUserMessageNotificationBuilder: NotificationBuilder {
struct ConversationUserMessageNotificationBuilder: NotificationBuilder {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this was renamed from another PR, but it still confuses me slightly. I think the structure of the builders could follow the structure of events, so ConversationEventNotificationBuilder builds notifications for all conversation events. Internally, there could be separate builders for each events, i.e NewMessageNotificationBuilder or ConversationMemberJoinNotificationBuilder, et.c


private enum AssetType {
case image
Expand Down Expand Up @@ -143,19 +143,19 @@ struct NewUserMessageNotificationBuilder: NotificationBuilder {

let body: NotificationBody = switch assetType {
case .image:
.newUserMessage(
.conversationUserMessage(
.sharedPicture(senderName: isGroupConversation ? senderName : nil)
)
case .video:
.newUserMessage(
.conversationUserMessage(
.sharedVideo(senderName: isGroupConversation ? senderName : nil)
)
case .audio:
.newUserMessage(
.conversationUserMessage(
.sharedAudio(senderName: isGroupConversation ? senderName : nil)
)
case .fileUpload:
.newUserMessage(
.conversationUserMessage(
.sharedFile(senderName: isGroupConversation ? senderName : nil)
)
}
Expand All @@ -177,7 +177,7 @@ struct NewUserMessageNotificationBuilder: NotificationBuilder {
content.title = title
}

let body = NotificationBody.newUserMessage(
let body = NotificationBody.conversationUserMessage(
.ping(senderName: context.isGroupConversation ? senderName : nil)
)

Expand All @@ -194,7 +194,7 @@ struct NewUserMessageNotificationBuilder: NotificationBuilder {
let content = UNMutableNotificationContent()

// No title for hidden message, only a body.
let body: NotificationBody = .newUserMessage(.hidden)
let body: NotificationBody = .conversationUserMessage(.hidden)
content.body = body.make()
content.categoryIdentifier = makeCategory()
content.sound = makeSound()
Expand Down Expand Up @@ -241,7 +241,7 @@ struct NewUserMessageNotificationBuilder: NotificationBuilder {
.text(content: text, senderName: senderName)
}

let body = NotificationBody.newUserMessage(
let body = NotificationBody.conversationUserMessage(
format
)

Expand All @@ -263,7 +263,7 @@ struct NewUserMessageNotificationBuilder: NotificationBuilder {
content.title = title
}

let body = NotificationBody.newUserMessage(
let body = NotificationBody.conversationUserMessage(
.sharedLocation(senderName: isGroupConversation ? senderName : nil)
)

Expand Down Expand Up @@ -310,7 +310,7 @@ struct NewUserMessageNotificationBuilder: NotificationBuilder {
.sentWithUnknownSender
}

let body = NotificationBody.newUserMessage(
let body = NotificationBody.conversationUserMessage(
format
)

Expand Down Expand Up @@ -351,7 +351,7 @@ struct NewUserMessageNotificationBuilder: NotificationBuilder {
}

return NotificationTitle
.newMessage(format)
.conversationMessage(format)
.make()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// Wire
// Copyright (C) 2025 Wire Swiss GmbH
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.
//

import UserNotifications

struct UserConnectionNotificationBuilder: NotificationBuilder {

enum ConnectionStatus {
case joined
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is for the case where someone in your address book creates an account on Wire. But I don't think we get this event anymore, in which case I don't think we need to support this notification. Best to check with BE.

case pending
case accepted
}

struct Context {
let connectionStatus: ConnectionStatus
let username: String
}

private let context: Context

init(
connectionStatus: ConnectionStatus,
username: String
) {
self.context = Context(
connectionStatus: connectionStatus,
username: username
)
}

func shouldBuildNotification() async -> Bool {
true
}

func buildContent() async -> UNMutableNotificationContent {
switch context.connectionStatus {
case .joined:
buildUserJoinNotification()
case .pending:
// TODO: [WPB-11659] To implement
UNMutableNotificationContent()
case .accepted:
// TODO: [WPB-11659] To implement
UNMutableNotificationContent()
}
}

// MARK: - Build notifications

private func buildUserJoinNotification() -> UNMutableNotificationContent {
let content = UNMutableNotificationContent()

let body = NotificationBody.userConnection(
.userJoined(username: context.username)
)

content.body = body.make()
content.categoryIdentifier = makeCategory()
content.sound = makeSound()

return content
}

// MARK: - Helpers

private func makeSound(type: NotificationSound = .default) -> UNNotificationSound {
let notificationSoundName = UNNotificationSoundName(type.rawValue)
return UNNotificationSound(named: notificationSoundName)
}

private func makeCategory() -> String {
switch context.connectionStatus {
case .joined:
NotificationCategory.nonActionable.rawValue
case .pending, .accepted:
// TODO: [WPB-11659] To implement
""
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// Wire
// Copyright (C) 2025 Wire Swiss GmbH
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.
//

import UserNotifications
import WireAPI
import WireDataModel

struct UserNotificationBuilder: NotificationBuilder {

private let event: UserEvent

init(
event: UserEvent
) {
self.event = event
}

func shouldBuildNotification() async -> Bool {
true
}

func buildContent() async -> UNMutableNotificationContent {
let builder: NotificationBuilder

switch event {
case let .connection(userConnectionEvent):
let isPending = userConnectionEvent.connection.status == .pending

builder = UserConnectionNotificationBuilder(
connectionStatus: isPending ? .pending : .accepted,
username: userConnectionEvent.userName
)

case let .contactJoin(userContactJoinEvent):

builder = UserConnectionNotificationBuilder(
connectionStatus: .joined,
username: userContactJoinEvent.name
)

default:
return UNMutableNotificationContent()
}

guard await builder.shouldBuildNotification() else {
return UNMutableNotificationContent()
}

return await builder.buildContent()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import Foundation

struct NewMessageNotificationTitleComposer {
struct ConversationMessageNotificationTitleComposer {
let format: NotificationTitle.MessageTitleFormat

// TODO: [WPB-15153] - Localize strings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import Foundation

struct NewSystemMessageNotificationBodyComposer {
struct ConversationSystemMessageNotificationBodyComposer {
let format: NotificationBody.SystemMessageBodyFormat

// TODO: [WPB-15153] - Localize strings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import Foundation

struct NewUserMessageNotificationBodyComposer {
struct ConversationUserMessageNotificationBodyComposer {
let format: NotificationBody.UserMessageBodyFormat

// TODO: [WPB-15153] - Localize strings
Expand Down
Loading
Loading