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 conversation timer update - WPB-11663 #2380

Open
wants to merge 2 commits 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
Copy link
Collaborator

Choose a reason for hiding this comment

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

question: I think I may have left this comment on another PR but just incase I didn't, what do you think about having separate builders for each notification type? Is there a reason to group them together in to one?

Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,18 @@ struct NewSystemMessageNotificationBuilder: NotificationBuilder {
case .conversationDeleted:
// TODO: [WPB-11658]
break
case .messageTimerUpdate:
// TODO: [WPB-11663]
break
case let .messageTimerUpdate(timeoutValue):
var timeoutStrValue: String?

if let timeoutValue {
let timerInMilliseconds = Double(timeoutValue)
let timeoutValue = timerInMilliseconds / 1000
let timeout: MessageDestructionTimeoutValue = .init(rawValue: timeoutValue)

timeoutStrValue = timeout.displayString
}

return buildTimerUpdateNotification(timeout: timeoutStrValue)
}

return UNMutableNotificationContent()
Expand Down Expand Up @@ -140,6 +149,32 @@ struct NewSystemMessageNotificationBuilder: NotificationBuilder {
return content
}

private func buildTimerUpdateNotification(timeout: String?) -> UNMutableNotificationContent {
let content = UNMutableNotificationContent()

if let title = makeTitle() {
content.title = title
}

let bodyFormat: NotificationBody.SystemMessageBodyFormat = if let timeout {
.setMessageTimer(senderName: context.senderName, timeoutValue: timeout)
} else {
.turnedOffMessageTimer(senderName: context.senderName)
}

let body = NotificationBody.newSystemMessage(
bodyFormat
)

content.body = body.make()
content.categoryIdentifier = makeCategory()
content.sound = makeSound()
content.userInfo = makeUserInfo()
content.threadIdentifier = context.conversationID.uuid.transportString()

return content
}

// MARK: - Helpers

private func makeTitle() -> String? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ struct NewSystemMessageNotificationBodyComposer {
""
case let .removedYou(senderName):
senderName != nil ? "\(senderName!) removed you" : "Someone removed you"
case let .setMessageTimer(senderName):
// TODO: [WPB-11663]
""
case let .setMessageTimer(senderName, timeoutValue):
senderName != nil ? "\(senderName!) set the message timer to \(timeoutValue)" :
"Someone set the message timer to \(timeoutValue)"
case let .addedYou(senderName):
// TODO: [WPB-11661]
""
case let .turnedOffMessageTimer(senderName):
// TODO: [WPB-11663]
""
senderName != nil ? "\(senderName!) turned off the message timer" : "Someone turned off the message timer"
case let .deletedGroup(senderName):
// TODO: [WPB-11658]
""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ extension NotificationBody {
case removedYou(senderName: String?)
/// `[sender name] set the message timer to [value]` or `Someone set the message timer to [value]` if sender is
/// nil
case setMessageTimer(senderName: String?)
case setMessageTimer(senderName: String?, timeoutValue: String)
/// `[sender name] turned off the message timer` or `Someone turned off the message timer` if sender is nil
case turnedOffMessageTimer(senderName: String?)
/// `[sender name] deleted the group` or `Someone deleted the group` if sender is nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ final class NewSystemMessageNotificationBuilderTests: XCTestCase {
}
}

func testGenerateNewSystemMessageNotifications_Is_Group_Conversation_And_Is_Team_User() async throws {
func testGenerateNewSystemMessageNotifications_For_Group_Conversation_And_Team_User() async throws {

// Mock

Expand All @@ -84,7 +84,9 @@ final class NewSystemMessageNotificationBuilderTests: XCTestCase {
await setupMock(isGroup: isGroup, isTeam: isTeam, selfUserID: .mockID4)

let systemMessages: [NewSystemMessageNotificationBuilder.SystemMessage] = [
.memberLeave(removedUserIDs: [.mockID4]) // concerns self user
.memberLeave(removedUserIDs: [.mockID4]), // concerns self user
.messageTimerUpdate(newTimer: Scaffolding.timeoutValue), // enabled timer
.messageTimerUpdate(newTimer: nil) // disabled timer
]

for systemMessage in systemMessages {
Expand All @@ -109,7 +111,7 @@ final class NewSystemMessageNotificationBuilderTests: XCTestCase {
}
}

func testGenerateNewSystemMessageNotifications_Is_Group_Conversation_And_Is_Personal_User() async throws {
func testGenerateNewSystemMessageNotifications_For_Group_Conversation_And_Personal_User() async throws {

// Mock

Expand All @@ -119,7 +121,9 @@ final class NewSystemMessageNotificationBuilderTests: XCTestCase {
await setupMock(isGroup: isGroup, isTeam: isTeam, selfUserID: .mockID4)

let systemMessages: [NewSystemMessageNotificationBuilder.SystemMessage] = [
.memberLeave(removedUserIDs: [.mockID4]) // concerns self user
.memberLeave(removedUserIDs: [.mockID4]), // concerns self user
.messageTimerUpdate(newTimer: Scaffolding.timeoutValue), // enabled timer
.messageTimerUpdate(newTimer: nil) // disabled timer
]

for systemMessage in systemMessages {
Expand All @@ -144,7 +148,7 @@ final class NewSystemMessageNotificationBuilderTests: XCTestCase {
}
}

func testGenerateNewSystemMessageNotifications_Is_OneOnOne_Conversation_And_Team() async throws {
func testGenerateNewSystemMessageNotifications_For_OneOnOne_Conversation_And_Team() async throws {

// Mock

Expand All @@ -154,7 +158,9 @@ final class NewSystemMessageNotificationBuilderTests: XCTestCase {
await setupMock(isGroup: isGroup, isTeam: isTeam, selfUserID: .mockID4)

let systemMessages: [NewSystemMessageNotificationBuilder.SystemMessage] = [
.memberLeave(removedUserIDs: [.mockID4]) // concerns self user
.memberLeave(removedUserIDs: [.mockID4]), // concerns self user
.messageTimerUpdate(newTimer: Scaffolding.timeoutValue), // enabled timer
.messageTimerUpdate(newTimer: nil) // disabled timer
]

for systemMessage in systemMessages {
Expand Down Expand Up @@ -237,8 +243,15 @@ final class NewSystemMessageNotificationBuilderTests: XCTestCase {
break
case .conversationDeleted:
break
case .messageTimerUpdate:
break
case let .messageTimerUpdate(timeoutValue):
if let timeoutValue {
XCTAssertEqual(
notificationContent.body,
"\(Scaffolding.senderName) set the message timer to \(timeoutValue / 1000) seconds"
)
} else {
XCTAssertEqual(notificationContent.body, "\(Scaffolding.senderName) turned off the message timer")
}
}

// Category
Expand Down Expand Up @@ -295,6 +308,7 @@ final class NewSystemMessageNotificationBuilderTests: XCTestCase {
}

private enum Scaffolding {
static let timeoutValue: Int64 = 10_000
static let senderName = "User1"
static let conversationName = "Conversation1"
static let teamName = "Team1"
Expand Down
Loading