PLT-3193 Add channel notification preferences for push and email noti… (#5500)

* PLT-3193 Add channel notification preferences for push and email notifications

* unit tests, model validation and localization

* Feedback review

* Adding back allowFromCache check

* Setting push and email to use default settings

* Move props as constants

* address feedback
Этот коммит содержится в:
enahum
2017-02-23 11:08:48 -03:00
коммит произвёл GitHub
родитель ca7d3b6e7b
Коммит 748a416961
12 изменённых файлов: 247 добавлений и 68 удалений

Просмотреть файл

@@ -1536,7 +1536,7 @@ func TestUpdateNotifyProps(t *testing.T) {
data := make(map[string]string) data := make(map[string]string)
data["channel_id"] = channel1.Id data["channel_id"] = channel1.Id
data["user_id"] = user.Id data["user_id"] = user.Id
data["desktop"] = model.CHANNEL_NOTIFY_MENTION data[model.DESKTOP_NOTIFY_PROP] = model.CHANNEL_NOTIFY_MENTION
//timeBeforeUpdate := model.GetMillis() //timeBeforeUpdate := model.GetMillis()
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
@@ -1544,46 +1544,65 @@ func TestUpdateNotifyProps(t *testing.T) {
// test updating desktop // test updating desktop
if result, err := Client.UpdateNotifyProps(data); err != nil { if result, err := Client.UpdateNotifyProps(data); err != nil {
t.Fatal(err) t.Fatal(err)
} else if notifyProps := result.Data.(map[string]string); notifyProps["desktop"] != model.CHANNEL_NOTIFY_MENTION { } else if notifyProps := result.Data.(map[string]string); notifyProps[model.DESKTOP_NOTIFY_PROP] != model.CHANNEL_NOTIFY_MENTION {
t.Fatal("NotifyProps[\"desktop\"] did not update properly") t.Fatal("NotifyProps[\"desktop\"] did not update properly")
} else if notifyProps["mark_unread"] != model.CHANNEL_MARK_UNREAD_ALL { } else if notifyProps[model.MARK_UNREAD_NOTIFY_PROP] != model.CHANNEL_MARK_UNREAD_ALL {
t.Fatalf("NotifyProps[\"mark_unread\"] changed to %v", notifyProps["mark_unread"]) t.Fatalf("NotifyProps[\"mark_unread\"] changed to %v", notifyProps[model.MARK_UNREAD_NOTIFY_PROP])
} }
// test an empty update // test an empty update
delete(data, "desktop") delete(data, model.DESKTOP_NOTIFY_PROP)
if result, err := Client.UpdateNotifyProps(data); err != nil { if result, err := Client.UpdateNotifyProps(data); err != nil {
t.Fatal(err) t.Fatal(err)
} else if notifyProps := result.Data.(map[string]string); notifyProps["mark_unread"] != model.CHANNEL_MARK_UNREAD_ALL { } else if notifyProps := result.Data.(map[string]string); notifyProps[model.MARK_UNREAD_NOTIFY_PROP] != model.CHANNEL_MARK_UNREAD_ALL {
t.Fatalf("NotifyProps[\"mark_unread\"] changed to %v", notifyProps["mark_unread"]) t.Fatalf("NotifyProps[\"mark_unread\"] changed to %v", notifyProps[model.MARK_UNREAD_NOTIFY_PROP])
} else if notifyProps["desktop"] != model.CHANNEL_NOTIFY_MENTION { } else if notifyProps[model.DESKTOP_NOTIFY_PROP] != model.CHANNEL_NOTIFY_MENTION {
t.Fatalf("NotifyProps[\"desktop\"] changed to %v", notifyProps["desktop"]) t.Fatalf("NotifyProps[\"desktop\"] changed to %v", notifyProps[model.DESKTOP_NOTIFY_PROP])
} }
// test updating mark unread // test updating mark unread
data["mark_unread"] = model.CHANNEL_MARK_UNREAD_MENTION data[model.MARK_UNREAD_NOTIFY_PROP] = model.CHANNEL_MARK_UNREAD_MENTION
if result, err := Client.UpdateNotifyProps(data); err != nil { if result, err := Client.UpdateNotifyProps(data); err != nil {
t.Fatal(err) t.Fatal(err)
} else if notifyProps := result.Data.(map[string]string); notifyProps["mark_unread"] != model.CHANNEL_MARK_UNREAD_MENTION { } else if notifyProps := result.Data.(map[string]string); notifyProps[model.MARK_UNREAD_NOTIFY_PROP] != model.CHANNEL_MARK_UNREAD_MENTION {
t.Fatal("NotifyProps[\"mark_unread\"] did not update properly") t.Fatal("NotifyProps[\"mark_unread\"] did not update properly")
} else if notifyProps["desktop"] != model.CHANNEL_NOTIFY_MENTION { } else if notifyProps[model.DESKTOP_NOTIFY_PROP] != model.CHANNEL_NOTIFY_MENTION {
t.Fatalf("NotifyProps[\"desktop\"] changed to %v", notifyProps["desktop"]) t.Fatalf("NotifyProps[\"desktop\"] changed to %v", notifyProps[model.DESKTOP_NOTIFY_PROP])
} }
// test updating both // test updating both
data["desktop"] = model.CHANNEL_NOTIFY_NONE data[model.DESKTOP_NOTIFY_PROP] = model.CHANNEL_NOTIFY_NONE
data["mark_unread"] = model.CHANNEL_MARK_UNREAD_MENTION data[model.MARK_UNREAD_NOTIFY_PROP] = model.CHANNEL_MARK_UNREAD_MENTION
if result, err := Client.UpdateNotifyProps(data); err != nil { if result, err := Client.UpdateNotifyProps(data); err != nil {
t.Fatal(err) t.Fatal(err)
} else if notifyProps := result.Data.(map[string]string); notifyProps["desktop"] != model.CHANNEL_NOTIFY_NONE { } else if notifyProps := result.Data.(map[string]string); notifyProps[model.DESKTOP_NOTIFY_PROP] != model.CHANNEL_NOTIFY_NONE {
t.Fatal("NotifyProps[\"desktop\"] did not update properly") t.Fatal("NotifyProps[\"desktop\"] did not update properly")
} else if notifyProps["mark_unread"] != model.CHANNEL_MARK_UNREAD_MENTION { } else if notifyProps[model.MARK_UNREAD_NOTIFY_PROP] != model.CHANNEL_MARK_UNREAD_MENTION {
t.Fatal("NotifyProps[\"mark_unread\"] did not update properly") t.Fatal("NotifyProps[\"mark_unread\"] did not update properly")
} }
// test updating push notification preferences
delete(data, model.DESKTOP_NOTIFY_PROP)
delete(data, model.MARK_UNREAD_NOTIFY_PROP)
data[model.PUSH_NOTIFY_PROP] = model.CHANNEL_NOTIFY_MENTION
if result, err := Client.UpdateNotifyProps(data); err != nil {
t.Fatal(err)
} else if notifyProps := result.Data.(map[string]string); notifyProps[model.PUSH_NOTIFY_PROP] != model.CHANNEL_NOTIFY_MENTION {
t.Fatal("NotifyProps[\"push\"] did not update properly")
}
// test updating email preferences
delete(data, model.PUSH_NOTIFY_PROP)
data[model.EMAIL_NOTIFY_PROP] = "true"
if result, err := Client.UpdateNotifyProps(data); err != nil {
t.Fatal(err)
} else if notifyProps := result.Data.(map[string]string); notifyProps[model.EMAIL_NOTIFY_PROP] != "true" {
t.Fatal("NotifyProps[\"email\"] did not update properly")
}
// test error cases // test error cases
data["user_id"] = "junk" data["user_id"] = "junk"
if _, err := Client.UpdateNotifyProps(data); err == nil { if _, err := Client.UpdateNotifyProps(data); err == nil {
@@ -1606,24 +1625,38 @@ func TestUpdateNotifyProps(t *testing.T) {
t.Fatal("Should have errored - bad channel id") t.Fatal("Should have errored - bad channel id")
} }
data["desktop"] = "junk" data[model.DESKTOP_NOTIFY_PROP] = "junk"
data["mark_unread"] = model.CHANNEL_MARK_UNREAD_ALL data[model.MARK_UNREAD_NOTIFY_PROP] = model.CHANNEL_MARK_UNREAD_ALL
if _, err := Client.UpdateNotifyProps(data); err == nil { if _, err := Client.UpdateNotifyProps(data); err == nil {
t.Fatal("Should have errored - bad desktop notify level") t.Fatal("Should have errored - bad desktop notify level")
} }
data["desktop"] = model.CHANNEL_NOTIFY_ALL data[model.DESKTOP_NOTIFY_PROP] = model.CHANNEL_NOTIFY_ALL
data["mark_unread"] = "junk" data[model.MARK_UNREAD_NOTIFY_PROP] = "junk"
if _, err := Client.UpdateNotifyProps(data); err == nil { if _, err := Client.UpdateNotifyProps(data); err == nil {
t.Fatal("Should have errored - bad mark unread level") t.Fatal("Should have errored - bad mark unread level")
} }
data[model.DESKTOP_NOTIFY_PROP] = model.CHANNEL_NOTIFY_ALL
data[model.MARK_UNREAD_NOTIFY_PROP] = model.CHANNEL_MARK_UNREAD_ALL
data[model.PUSH_NOTIFY_PROP] = "junk"
data[model.EMAIL_NOTIFY_PROP] = "true"
if _, err := Client.UpdateNotifyProps(data); err == nil {
t.Fatal("Should have errored - bad push level")
}
data[model.PUSH_NOTIFY_PROP] = model.CHANNEL_NOTIFY_ALL
data[model.EMAIL_NOTIFY_PROP] = "junk"
if _, err := Client.UpdateNotifyProps(data); err == nil {
t.Fatal("Should have errored - bad email notification option")
}
th.LoginBasic2() th.LoginBasic2()
data["channel_id"] = channel1.Id data["channel_id"] = channel1.Id
data["user_id"] = user2.Id data["user_id"] = user2.Id
data["desktop"] = model.CHANNEL_NOTIFY_MENTION data[model.DESKTOP_NOTIFY_PROP] = model.CHANNEL_NOTIFY_MENTION
data["mark_unread"] = model.CHANNEL_MARK_UNREAD_MENTION data[model.MARK_UNREAD_NOTIFY_PROP] = model.CHANNEL_MARK_UNREAD_MENTION
if _, err := Client.UpdateNotifyProps(data); err == nil { if _, err := Client.UpdateNotifyProps(data); err == nil {
t.Fatal("Should have errored - user not in channel") t.Fatal("Should have errored - user not in channel")
} }

Просмотреть файл

@@ -226,18 +226,27 @@ func UpdateChannelMemberNotifyProps(data map[string]string, channelId string, us
} }
// update whichever notify properties have been provided, but don't change the others // update whichever notify properties have been provided, but don't change the others
if markUnread, exists := data["mark_unread"]; exists { if markUnread, exists := data[model.MARK_UNREAD_NOTIFY_PROP]; exists {
member.NotifyProps["mark_unread"] = markUnread member.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP] = markUnread
} }
if desktop, exists := data["desktop"]; exists { if desktop, exists := data[model.DESKTOP_NOTIFY_PROP]; exists {
member.NotifyProps["desktop"] = desktop member.NotifyProps[model.DESKTOP_NOTIFY_PROP] = desktop
}
if email, exists := data[model.EMAIL_NOTIFY_PROP]; exists {
member.NotifyProps[model.EMAIL_NOTIFY_PROP] = email
}
if push, exists := data[model.PUSH_NOTIFY_PROP]; exists {
member.NotifyProps[model.PUSH_NOTIFY_PROP] = push
} }
if result := <-Srv.Store.Channel().UpdateMember(member); result.Err != nil { if result := <-Srv.Store.Channel().UpdateMember(member); result.Err != nil {
return nil, result.Err return nil, result.Err
} else { } else {
InvalidateCacheForUser(userId) InvalidateCacheForUser(userId)
InvalidateCacheForChannelMembersNotifyProps(channelId)
return member, nil return member, nil
} }
} }

Просмотреть файл

@@ -581,11 +581,11 @@ func ImportUserChannels(user *model.User, team *model.Team, data *[]UserChannelI
notifyProps := member.NotifyProps notifyProps := member.NotifyProps
if cdata.NotifyProps.Desktop != nil { if cdata.NotifyProps.Desktop != nil {
notifyProps["desktop"] = *cdata.NotifyProps.Desktop notifyProps[model.DESKTOP_NOTIFY_PROP] = *cdata.NotifyProps.Desktop
} }
if cdata.NotifyProps.MarkUnread != nil { if cdata.NotifyProps.MarkUnread != nil {
notifyProps["mark_unread"] = *cdata.NotifyProps.MarkUnread notifyProps[model.MARK_UNREAD_NOTIFY_PROP] = *cdata.NotifyProps.MarkUnread
} }
if _, err := UpdateChannelMemberNotifyProps(notifyProps, channel.Id, user.Id); err != nil { if _, err := UpdateChannelMemberNotifyProps(notifyProps, channel.Id, user.Id); err != nil {

Просмотреть файл

@@ -1157,7 +1157,7 @@ func TestImportImportUser(t *testing.T) {
// Check channel member properties. // Check channel member properties.
if channelMember, err := GetChannelMember(channel.Id, user.Id); err != nil { if channelMember, err := GetChannelMember(channel.Id, user.Id); err != nil {
t.Fatalf("Failed to get channel member from database.") t.Fatalf("Failed to get channel member from database.")
} else if channelMember.Roles != "channel_user" || channelMember.NotifyProps["desktop"] != "default" || channelMember.NotifyProps["mark_unread"] != "all" { } else if channelMember.Roles != "channel_user" || channelMember.NotifyProps[model.DESKTOP_NOTIFY_PROP] != "default" || channelMember.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP] != "all" {
t.Fatalf("Channel member properties not as expected") t.Fatalf("Channel member properties not as expected")
} }
@@ -1171,8 +1171,8 @@ func TestImportImportUser(t *testing.T) {
Name: &channelName, Name: &channelName,
Roles: ptrStr("channel_user channel_admin"), Roles: ptrStr("channel_user channel_admin"),
NotifyProps: &UserChannelNotifyPropsImportData{ NotifyProps: &UserChannelNotifyPropsImportData{
Desktop: ptrStr("mention"), Desktop: ptrStr(model.USER_NOTIFY_MENTION),
MarkUnread: ptrStr("mention"), MarkUnread: ptrStr(model.USER_NOTIFY_MENTION),
}, },
}, },
}, },
@@ -1191,7 +1191,7 @@ func TestImportImportUser(t *testing.T) {
if channelMember, err := GetChannelMember(channel.Id, user.Id); err != nil { if channelMember, err := GetChannelMember(channel.Id, user.Id); err != nil {
t.Fatalf("Failed to get channel member Desktop from database.") t.Fatalf("Failed to get channel member Desktop from database.")
} else if channelMember.Roles != "channel_user channel_admin" && channelMember.NotifyProps["desktop"] == "mention" && channelMember.NotifyProps["mark_unread"] == "mention" { } else if channelMember.Roles != "channel_user channel_admin" && channelMember.NotifyProps[model.DESKTOP_NOTIFY_PROP] == model.USER_NOTIFY_MENTION && channelMember.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP] == model.USER_NOTIFY_MENTION {
t.Fatalf("Channel member properties not as expected") t.Fatalf("Channel member properties not as expected")
} }
@@ -1226,7 +1226,7 @@ func TestImportImportUser(t *testing.T) {
} }
// Check their values. // Check their values.
user, err = GetUserByUsername(username); user, err = GetUserByUsername(username)
if err != nil { if err != nil {
t.Fatalf("Failed to get user from database.") t.Fatalf("Failed to get user from database.")
} }

Просмотреть файл

@@ -26,6 +26,7 @@ import (
func SendNotifications(post *model.Post, team *model.Team, channel *model.Channel, sender *model.User) ([]string, *model.AppError) { func SendNotifications(post *model.Post, team *model.Team, channel *model.Channel, sender *model.User) ([]string, *model.AppError) {
pchan := Srv.Store.User().GetAllProfilesInChannel(channel.Id, true) pchan := Srv.Store.User().GetAllProfilesInChannel(channel.Id, true)
cmnchan := Srv.Store.Channel().GetAllChannelMembersNotifyPropsForChannel(channel.Id, true)
var fchan store.StoreChannel var fchan store.StoreChannel
if len(post.FileIds) != 0 { if len(post.FileIds) != 0 {
@@ -39,6 +40,13 @@ func SendNotifications(post *model.Post, team *model.Team, channel *model.Channe
profileMap = result.Data.(map[string]*model.User) profileMap = result.Data.(map[string]*model.User)
} }
var channelMemberNotifyPropsMap map[string]model.StringMap
if result := <-cmnchan; result.Err != nil {
return nil, result.Err
} else {
channelMemberNotifyPropsMap = result.Data.(map[string]model.StringMap)
}
mentionedUserIds := make(map[string]bool) mentionedUserIds := make(map[string]bool)
allActivityPushUserIds := []string{} allActivityPushUserIds := []string{}
hereNotification := false hereNotification := false
@@ -94,7 +102,7 @@ func SendNotifications(post *model.Post, team *model.Team, channel *model.Channe
// find which users in the channel are set up to always receive mobile notifications // find which users in the channel are set up to always receive mobile notifications
for _, profile := range profileMap { for _, profile := range profileMap {
if profile.NotifyProps["push"] == model.USER_NOTIFY_ALL && if profile.NotifyProps[model.PUSH_NOTIFY_PROP] == model.USER_NOTIFY_ALL &&
(post.UserId != profile.Id || post.Props["from_webhook"] == "true") && (post.UserId != profile.Id || post.Props["from_webhook"] == "true") &&
!post.IsSystemMessage() { !post.IsSystemMessage() {
allActivityPushUserIds = append(allActivityPushUserIds, profile.Id) allActivityPushUserIds = append(allActivityPushUserIds, profile.Id)
@@ -137,7 +145,12 @@ func SendNotifications(post *model.Post, team *model.Team, channel *model.Channe
if utils.Cfg.EmailSettings.SendEmailNotifications { if utils.Cfg.EmailSettings.SendEmailNotifications {
for _, id := range mentionedUsersList { for _, id := range mentionedUsersList {
userAllowsEmails := profileMap[id].NotifyProps["email"] != "false" userAllowsEmails := profileMap[id].NotifyProps[model.EMAIL_NOTIFY_PROP] != "false"
if channelEmail, ok := channelMemberNotifyPropsMap[id][model.EMAIL_NOTIFY_PROP]; ok {
if channelEmail != model.CHANNEL_NOTIFY_DEFAULT {
userAllowsEmails = channelEmail != "false"
}
}
var status *model.Status var status *model.Status
var err *model.AppError var err *model.AppError
@@ -245,7 +258,7 @@ func SendNotifications(post *model.Post, team *model.Team, channel *model.Channe
} }
if DoesStatusAllowPushNotification(profileMap[id], status, post.ChannelId) { if DoesStatusAllowPushNotification(profileMap[id], status, post.ChannelId) {
sendPushNotification(post, profileMap[id], channel, senderName[id], true) sendPushNotification(post, profileMap[id], channel, senderName[id], channelMemberNotifyPropsMap[id], true)
} }
} }
@@ -258,7 +271,7 @@ func SendNotifications(post *model.Post, team *model.Team, channel *model.Channe
} }
if DoesStatusAllowPushNotification(profileMap[id], status, post.ChannelId) { if DoesStatusAllowPushNotification(profileMap[id], status, post.ChannelId) {
sendPushNotification(post, profileMap[id], channel, senderName[id], false) sendPushNotification(post, profileMap[id], channel, senderName[id], channelMemberNotifyPropsMap[id], false)
} }
} }
} }
@@ -442,7 +455,7 @@ func GetMessageForNotification(post *model.Post, translateFunc i18n.TranslateFun
} }
} }
func sendPushNotification(post *model.Post, user *model.User, channel *model.Channel, senderName string, wasMentioned bool) *model.AppError { func sendPushNotification(post *model.Post, user *model.User, channel *model.Channel, senderName string, channelNotifyProps model.StringMap, wasMentioned bool) *model.AppError {
sessions, err := getMobileAppSessions(user.Id) sessions, err := getMobileAppSessions(user.Id)
if err != nil { if err != nil {
return err return err
@@ -450,6 +463,14 @@ func sendPushNotification(post *model.Post, user *model.User, channel *model.Cha
var channelName string var channelName string
if channelNotify, ok := channelNotifyProps[model.PUSH_NOTIFY_PROP]; ok {
if channelNotify == model.USER_NOTIFY_NONE {
return nil
} else if channelNotify == model.USER_NOTIFY_MENTION && !wasMentioned {
return nil
}
}
if channel.Type == model.CHANNEL_DIRECT { if channel.Type == model.CHANNEL_DIRECT {
channelName = senderName channelName = senderName
} else { } else {

Просмотреть файл

@@ -134,6 +134,18 @@ func InvalidateCacheForChannelMembersSkipClusterSend(channelId string) {
Srv.Store.Channel().InvalidateMemberCount(channelId) Srv.Store.Channel().InvalidateMemberCount(channelId)
} }
func InvalidateCacheForChannelMembersNotifyProps(channelId string) {
InvalidateCacheForChannelMembersNotifyPropsSkipClusterSend(channelId)
if cluster := einterfaces.GetClusterInterface(); cluster != nil {
cluster.InvalidateCacheForChannelMembersNotifyProps(channelId)
}
}
func InvalidateCacheForChannelMembersNotifyPropsSkipClusterSend(channelId string) {
Srv.Store.Channel().InvalidateCacheForChannelMembersNotifyProps(channelId)
}
func InvalidateCacheForChannelByNameSkipClusterSend(teamId, name string) { func InvalidateCacheForChannelByNameSkipClusterSend(teamId, name string) {
Srv.Store.Channel().InvalidateChannelByName(teamId, name) Srv.Store.Channel().InvalidateChannelByName(teamId, name)
} }

Просмотреть файл

@@ -17,6 +17,7 @@ type ClusterInterface interface {
InvalidateCacheForChannel(channelId string) InvalidateCacheForChannel(channelId string)
InvalidateCacheForChannelByName(teamId, name string) InvalidateCacheForChannelByName(teamId, name string)
InvalidateCacheForChannelMembers(channelId string) InvalidateCacheForChannelMembers(channelId string)
InvalidateCacheForChannelMembersNotifyProps(channelId string)
InvalidateCacheForChannelPosts(channelId string) InvalidateCacheForChannelPosts(channelId string)
InvalidateCacheForWebhook(webhookId string) InvalidateCacheForWebhook(webhookId string)
Publish(event *model.WebSocketEvent) Publish(event *model.WebSocketEvent)

Просмотреть файл

@@ -3519,10 +3519,18 @@
"id": "model.channel_member.is_valid.role.app_error", "id": "model.channel_member.is_valid.role.app_error",
"translation": "Invalid role" "translation": "Invalid role"
}, },
{
"id": "model.channel_member.is_valid.email_value.app_error",
"translation": "Invalid email notification value"
},
{ {
"id": "model.channel_member.is_valid.unread_level.app_error", "id": "model.channel_member.is_valid.unread_level.app_error",
"translation": "Invalid mark unread level" "translation": "Invalid mark unread level"
}, },
{
"id": "model.channel_member.is_valid.push_level.app_error",
"translation": "Invalid push notification level"
},
{ {
"id": "model.channel_member.is_valid.user_id.app_error", "id": "model.channel_member.is_valid.user_id.app_error",
"translation": "Invalid user id" "translation": "Invalid user id"

Просмотреть файл

@@ -88,18 +88,32 @@ func (o *ChannelMember) IsValid() *AppError {
return NewLocAppError("ChannelMember.IsValid", "model.channel_member.is_valid.user_id.app_error", nil, "") return NewLocAppError("ChannelMember.IsValid", "model.channel_member.is_valid.user_id.app_error", nil, "")
} }
notifyLevel := o.NotifyProps["desktop"] notifyLevel := o.NotifyProps[DESKTOP_NOTIFY_PROP]
if len(notifyLevel) > 20 || !IsChannelNotifyLevelValid(notifyLevel) { if len(notifyLevel) > 20 || !IsChannelNotifyLevelValid(notifyLevel) {
return NewLocAppError("ChannelMember.IsValid", "model.channel_member.is_valid.notify_level.app_error", return NewLocAppError("ChannelMember.IsValid", "model.channel_member.is_valid.notify_level.app_error",
nil, "notify_level="+notifyLevel) nil, "notify_level="+notifyLevel)
} }
markUnreadLevel := o.NotifyProps["mark_unread"] markUnreadLevel := o.NotifyProps[MARK_UNREAD_NOTIFY_PROP]
if len(markUnreadLevel) > 20 || !IsChannelMarkUnreadLevelValid(markUnreadLevel) { if len(markUnreadLevel) > 20 || !IsChannelMarkUnreadLevelValid(markUnreadLevel) {
return NewLocAppError("ChannelMember.IsValid", "model.channel_member.is_valid.unread_level.app_error", return NewLocAppError("ChannelMember.IsValid", "model.channel_member.is_valid.unread_level.app_error",
nil, "mark_unread_level="+markUnreadLevel) nil, "mark_unread_level="+markUnreadLevel)
} }
if pushLevel, ok := o.NotifyProps[PUSH_NOTIFY_PROP]; ok {
if len(pushLevel) > 20 || !IsChannelNotifyLevelValid(pushLevel) {
return NewLocAppError("ChannelMember.IsValid", "model.channel_member.is_valid.push_level.app_error",
nil, "push_notification_level="+pushLevel)
}
}
if sendEmail, ok := o.NotifyProps[EMAIL_NOTIFY_PROP]; ok {
if len(sendEmail) > 20 || !IsSendEmailValid(sendEmail) {
return NewLocAppError("ChannelMember.IsValid", "model.channel_member.is_valid.email_value.app_error",
nil, "push_notification_level="+sendEmail)
}
}
return nil return nil
} }
@@ -126,9 +140,15 @@ func IsChannelMarkUnreadLevelValid(markUnreadLevel string) bool {
return markUnreadLevel == CHANNEL_MARK_UNREAD_ALL || markUnreadLevel == CHANNEL_MARK_UNREAD_MENTION return markUnreadLevel == CHANNEL_MARK_UNREAD_ALL || markUnreadLevel == CHANNEL_MARK_UNREAD_MENTION
} }
func IsSendEmailValid(sendEmail string) bool {
return sendEmail == CHANNEL_NOTIFY_DEFAULT || sendEmail == "true" || sendEmail == "false"
}
func GetDefaultChannelNotifyProps() StringMap { func GetDefaultChannelNotifyProps() StringMap {
return StringMap{ return StringMap{
"desktop": CHANNEL_NOTIFY_DEFAULT, DESKTOP_NOTIFY_PROP: CHANNEL_NOTIFY_DEFAULT,
"mark_unread": CHANNEL_MARK_UNREAD_ALL, MARK_UNREAD_NOTIFY_PROP: CHANNEL_MARK_UNREAD_ALL,
PUSH_NOTIFY_PROP: CHANNEL_NOTIFY_DEFAULT,
EMAIL_NOTIFY_PROP: CHANNEL_NOTIFY_DEFAULT,
} }
} }

Просмотреть файл

@@ -19,6 +19,11 @@ const (
USER_NOTIFY_ALL = "all" USER_NOTIFY_ALL = "all"
USER_NOTIFY_MENTION = "mention" USER_NOTIFY_MENTION = "mention"
USER_NOTIFY_NONE = "none" USER_NOTIFY_NONE = "none"
DESKTOP_NOTIFY_PROP = "desktop"
MARK_UNREAD_NOTIFY_PROP = "mark_unread"
PUSH_NOTIFY_PROP = "push"
EMAIL_NOTIFY_PROP = "email"
DEFAULT_LOCALE = "en" DEFAULT_LOCALE = "en"
USER_AUTH_SERVICE_EMAIL = "email" USER_AUTH_SERVICE_EMAIL = "email"
USER_AUTH_SERVICE_USERNAME = "username" USER_AUTH_SERVICE_USERNAME = "username"

Просмотреть файл

@@ -25,6 +25,9 @@ const (
ALL_CHANNEL_MEMBERS_FOR_USER_CACHE_SIZE = model.SESSION_CACHE_SIZE ALL_CHANNEL_MEMBERS_FOR_USER_CACHE_SIZE = model.SESSION_CACHE_SIZE
ALL_CHANNEL_MEMBERS_FOR_USER_CACHE_SEC = 900 // 15 mins ALL_CHANNEL_MEMBERS_FOR_USER_CACHE_SEC = 900 // 15 mins
ALL_CHANNEL_MEMBERS_NOTIFY_PROPS_FOR_CHANNEL_CACHE_SIZE = model.SESSION_CACHE_SIZE
ALL_CHANNEL_MEMBERS_NOTIFY_PROPS_FOR_CHANNEL_CACHE_SEC = 1800 // 30 mins
CHANNEL_MEMBERS_COUNTS_CACHE_SIZE = model.CHANNEL_CACHE_SIZE CHANNEL_MEMBERS_COUNTS_CACHE_SIZE = model.CHANNEL_CACHE_SIZE
CHANNEL_MEMBERS_COUNTS_CACHE_SEC = 1800 // 30 mins CHANNEL_MEMBERS_COUNTS_CACHE_SEC = 1800 // 30 mins
@@ -37,12 +40,14 @@ type SqlChannelStore struct {
var channelMemberCountsCache = utils.NewLru(CHANNEL_MEMBERS_COUNTS_CACHE_SIZE) var channelMemberCountsCache = utils.NewLru(CHANNEL_MEMBERS_COUNTS_CACHE_SIZE)
var allChannelMembersForUserCache = utils.NewLru(ALL_CHANNEL_MEMBERS_FOR_USER_CACHE_SIZE) var allChannelMembersForUserCache = utils.NewLru(ALL_CHANNEL_MEMBERS_FOR_USER_CACHE_SIZE)
var allChannelMembersNotifyPropsForChannelCache = utils.NewLru(ALL_CHANNEL_MEMBERS_NOTIFY_PROPS_FOR_CHANNEL_CACHE_SIZE)
var channelCache = utils.NewLru(model.CHANNEL_CACHE_SIZE) var channelCache = utils.NewLru(model.CHANNEL_CACHE_SIZE)
var channelByNameCache = utils.NewLru(model.CHANNEL_CACHE_SIZE) var channelByNameCache = utils.NewLru(model.CHANNEL_CACHE_SIZE)
func ClearChannelCaches() { func ClearChannelCaches() {
channelMemberCountsCache.Purge() channelMemberCountsCache.Purge()
allChannelMembersForUserCache.Purge() allChannelMembersForUserCache.Purge()
allChannelMembersNotifyPropsForChannelCache.Purge()
channelCache.Purge() channelCache.Purge()
channelByNameCache.Purge() channelByNameCache.Purge()
} }
@@ -919,6 +924,69 @@ func (s SqlChannelStore) GetAllChannelMembersForUser(userId string, allowFromCac
return storeChannel return storeChannel
} }
func (us SqlChannelStore) InvalidateCacheForChannelMembersNotifyProps(channelId string) {
allChannelMembersNotifyPropsForChannelCache.Remove(channelId)
}
type allChannelMemberNotifyProps struct {
UserId string
NotifyProps model.StringMap
}
func (s SqlChannelStore) GetAllChannelMembersNotifyPropsForChannel(channelId string, allowFromCache bool) StoreChannel {
storeChannel := make(StoreChannel, 1)
go func() {
result := StoreResult{}
metrics := einterfaces.GetMetricsInterface()
if allowFromCache {
if cacheItem, ok := allChannelMembersNotifyPropsForChannelCache.Get(channelId); ok {
if metrics != nil {
metrics.IncrementMemCacheHitCounter("All Channel Members Notify Props for Channel")
}
result.Data = cacheItem.(map[string]model.StringMap)
storeChannel <- result
close(storeChannel)
return
} else {
if metrics != nil {
metrics.IncrementMemCacheMissCounter("All Channel Members Notify Props for Channel")
}
}
} else {
if metrics != nil {
metrics.IncrementMemCacheMissCounter("All Channel Members Notify Props for Channel")
}
}
var data []allChannelMemberNotifyProps
_, err := s.GetReplica().Select(&data, `
SELECT ChannelMembers.UserId, ChannelMembers.NotifyProps
FROM Channels, ChannelMembers
WHERE Channels.Id = ChannelMembers.ChannelId AND ChannelMembers.ChannelId = :ChannelId`, map[string]interface{}{"ChannelId": channelId})
if err != nil {
result.Err = model.NewLocAppError("SqlChannelStore.GetAllChannelMembersPropsForChannel", "store.sql_channel.get_members.app_error", nil, "channelId="+channelId+", err="+err.Error())
} else {
props := make(map[string]model.StringMap)
for i := range data {
props[data[i].UserId] = data[i].NotifyProps
}
result.Data = props
allChannelMembersNotifyPropsForChannelCache.AddWithExpiresInSecs(channelId, props, ALL_CHANNEL_MEMBERS_NOTIFY_PROPS_FOR_CHANNEL_CACHE_SEC)
}
storeChannel <- result
close(storeChannel)
}()
return storeChannel
}
func (us SqlChannelStore) InvalidateMemberCount(channelId string) { func (us SqlChannelStore) InvalidateMemberCount(channelId string) {
channelMemberCountsCache.Remove(channelId) channelMemberCountsCache.Remove(channelId)
} }

Просмотреть файл

@@ -110,6 +110,8 @@ type ChannelStore interface {
GetAllChannelMembersForUser(userId string, allowFromCache bool) StoreChannel GetAllChannelMembersForUser(userId string, allowFromCache bool) StoreChannel
InvalidateAllChannelMembersForUser(userId string) InvalidateAllChannelMembersForUser(userId string)
IsUserInChannelUseCache(userId string, channelId string) bool IsUserInChannelUseCache(userId string, channelId string) bool
GetAllChannelMembersNotifyPropsForChannel(channelId string, allowFromCache bool) StoreChannel
InvalidateCacheForChannelMembersNotifyProps(channelId string)
GetMemberForPost(postId string, userId string) StoreChannel GetMemberForPost(postId string, userId string) StoreChannel
InvalidateMemberCount(channelId string) InvalidateMemberCount(channelId string)
GetMemberCountFromCache(channelId string) int64 GetMemberCountFromCache(channelId string) int64