From 758af8dcaf2d3ba85e12ef2130ee686b350be806 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Mon, 7 Feb 2022 21:12:57 -0500 Subject: [PATCH] rejig notification policies around mute, opt in, default always and conversations around default, never, optin --- constants/attributes.go | 7 ++----- constants/globals.go | 9 ++++++++ utils/contacts.go | 3 +-- utils/eventHandler.go | 13 ++++-------- utils/notifications.go | 46 +++++++++++++++++++++++++++-------------- utils/settings.go | 6 +++--- 6 files changed, 49 insertions(+), 35 deletions(-) diff --git a/constants/attributes.go b/constants/attributes.go index 32a5bfa..2fbe174 100644 --- a/constants/attributes.go +++ b/constants/attributes.go @@ -18,11 +18,8 @@ const PeerOnline = "peer-online" // Description is used on server contacts, const Description = "description" -// NotificationOptIn is the attribute label for conversations. When App NotificationPolicy is OptIn a true value here opts in -const NotificationOptIn = "notification-opt-in" - -// NotificationOptOut is the attribute label for conversations. When App NotificationPolicy is OptOut a true value here opts out -const NotificationOptOut = "notification-opt-out" +// ConversationNotificationPolicy is the attribute label for conversations. When App NotificationPolicy is OptIn a true value here opts in +const ConversationNotificationPolicy = "notification-policy" const StateProfilePane = "state-profile-pane" const StateSelectedConversation = "state-selected-conversation" diff --git a/constants/globals.go b/constants/globals.go index aacfd0e..31cf2be 100644 --- a/constants/globals.go +++ b/constants/globals.go @@ -22,3 +22,12 @@ const ( // NotificationConversation enum for message["notification"] that means emit a notification event with Conversation handle included NotificationConversation = NotificationType("ContactInfo") ) + +const ( + // ConversationNotificationPolicyDefault enum for conversations indicating to use global notification policy + ConversationNotificationPolicyDefault = "ConversationNotificationPolicy.Default" + // ConversationNotificationPolicyOptIn enum for conversation indicating to opt in to nofitications when allowed + ConversationNotificationPolicyOptIn = "ConversationNotificationPolicy.OptIn" + // ConversationNotificationPolicyNever enum for conversation indicating to opt in to never do notifications + ConversationNotificationPolicyNever = "ConversationNotificationPolicy.Never" +) diff --git a/utils/contacts.go b/utils/contacts.go index 3961246..928bed3 100644 --- a/utils/contacts.go +++ b/utils/contacts.go @@ -16,6 +16,5 @@ type Contact struct { GroupServer string `json:"groupServer"` IsArchived bool `json:"isArchived"` Identifier int `json:"identifier"` - NotificationOptIn bool `json:"notificationOptIn""` - NotificationOptOut bool `json:"notificationOptOut""` + NotificationPolicy string `json:"notificationPolicy""` } diff --git a/utils/eventHandler.go b/utils/eventHandler.go index 5b6ff12..9437835 100644 --- a/utils/eventHandler.go +++ b/utils/eventHandler.go @@ -220,13 +220,9 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string { lastMessage, _ := profile.GetMostRecentMessages(conversationInfo.ID, 0, 0, 1) - notificationOptIn := false - if notificationOptInAttr, exists := conversationInfo.GetAttribute(attr.LocalScope, attr.ProfileZone, constants2.NotificationOptIn); exists { - notificationOptIn = notificationOptInAttr == "true" - } - notificationOptOut := false - if notificationOptOutAttr, exists := conversationInfo.GetAttribute(attr.LocalScope, attr.ProfileZone, constants2.NotificationOptOut); exists { - notificationOptOut = notificationOptOutAttr == "true" + notificationPolicy := constants2.ConversationNotificationPolicyDefault + if notificationPolicyAttr, exists := conversationInfo.GetAttribute(attr.LocalScope, attr.ProfileZone, constants2.ConversationNotificationPolicy); exists { + notificationPolicy = notificationPolicyAttr } contacts = append(contacts, Contact{ @@ -245,8 +241,7 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string { IsGroup: conversationInfo.IsGroup(), GroupServer: groupServer, IsArchived: isArchived == event.True, - NotificationOptIn: notificationOptIn, - NotificationOptOut: notificationOptOut, + NotificationPolicy: notificationPolicy, }) } } diff --git a/utils/notifications.go b/utils/notifications.go index 5b5a50a..8abb5d7 100644 --- a/utils/notifications.go +++ b/utils/notifications.go @@ -8,30 +8,44 @@ import ( func determineNotification(ci *model.Conversation) constants.NotificationType { settings := ReadGlobalSettings() - switch settings.NotificationPolicy { - case NotificationPolicyNone: + case NotificationPolicyMute: return constants.NotificationNone - case NotificationPolicyOptOut: - if ci != nil { - if optOut, exists := ci.GetAttribute(attr.LocalScope, attr.ProfileZone, constants.NotificationOptOut); exists && optOut == "true" { - return constants.NotificationNone - } - } - if settings.NotificationContent == "NotificationContent.ContactInfo" { - return constants.NotificationConversation - } - return constants.NotificationEvent case NotificationPolicyOptIn: if ci != nil { - if optIn, exists := ci.GetAttribute(attr.LocalScope, attr.ProfileZone, constants.NotificationOptIn); exists && optIn == "true" { - if settings.NotificationContent == "NotificationContent.ContactInfo" { - return constants.NotificationConversation + if policy, exists := ci.GetAttribute(attr.LocalScope, attr.ProfileZone, constants.ConversationNotificationPolicy); exists { + switch policy { + case constants.ConversationNotificationPolicyDefault: + return constants.NotificationNone + case constants.ConversationNotificationPolicyNever: + return constants.NotificationNone + case constants.ConversationNotificationPolicyOptIn: + return notificationContentToNotificationType(settings.NotificationContent) } - return constants.NotificationEvent } } return constants.NotificationNone + case NotificationPolicyDefaultAll: + if ci != nil { + if policy, exists := ci.GetAttribute(attr.LocalScope, attr.ProfileZone, constants.ConversationNotificationPolicy); exists { + switch policy { + case constants.ConversationNotificationPolicyNever: + return constants.NotificationNone + case constants.ConversationNotificationPolicyDefault: + fallthrough + case constants.ConversationNotificationPolicyOptIn: + return notificationContentToNotificationType(settings.NotificationContent) + } + } + } + return notificationContentToNotificationType(settings.NotificationContent) } return constants.NotificationNone } + +func notificationContentToNotificationType(notificationContent string) constants.NotificationType { + if notificationContent == "NotificationContent.ContactInfo" { + return constants.NotificationConversation + } + return constants.NotificationEvent +} diff --git a/utils/settings.go b/utils/settings.go index 5f6599e..6b0b15e 100644 --- a/utils/settings.go +++ b/utils/settings.go @@ -29,9 +29,9 @@ const saltFile = "SALT" type NotificationPolicy string const ( - NotificationPolicyNone = NotificationPolicy("NotificationPolicy.None") - NotificationPolicyOptOut = NotificationPolicy("NotificationPolicy.OptOut") - NotificationPolicyOptIn = NotificationPolicy("NotificationPolicy.OptIn") + NotificationPolicyMute = NotificationPolicy("NotificationPolicy.Mute") + NotificationPolicyOptIn = NotificationPolicy("NotificationPolicy.OptIn") + NotificationPolicyDefaultAll = NotificationPolicy("NotificationPolicy.DefaultAll") ) type GlobalSettings struct {