From 60347559c7fb857bd5afcd93e65b6e220625da79 Mon Sep 17 00:00:00 2001 From: enahum Date: Tue, 27 Sep 2016 11:19:50 -0300 Subject: PLT-3734 Cleaning up shouldSendEvent function (#4024) * PLT-3734 Cleaning up shouldSendEvent function * Fix LHS unread highlight and jewel mentions --- api/channel.go | 22 +++++++++++----- api/command_expand_collapse.go | 2 +- api/command_msg.go | 2 +- api/post.go | 16 ++++++------ api/post_test.go | 2 +- api/status.go | 9 ++++--- api/status_test.go | 2 +- api/team.go | 8 +++--- api/user.go | 19 ++++++++++---- api/user_test.go | 2 +- api/web_hub.go | 59 ++++++++++++------------------------------ api/webrtc.go | 2 +- api/websocket_test.go | 9 ++++--- 13 files changed, 77 insertions(+), 77 deletions(-) (limited to 'api') diff --git a/api/channel.go b/api/channel.go index 2950b8831..e2ddc72bc 100644 --- a/api/channel.go +++ b/api/channel.go @@ -167,7 +167,7 @@ func CreateDirectChannel(userId string, otherUserId string) (*model.Channel, *mo return nil, result.Err } } else { - message := model.NewWebSocketEvent("", channel.Id, userId, model.WEBSOCKET_EVENT_DIRECT_ADDED) + message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_DIRECT_ADDED, "", channel.Id, "", nil) message.Add("teammate_id", otherUserId) go Publish(message) @@ -579,7 +579,9 @@ func AddUserToChannel(user *model.User, channel *model.Channel) (*model.ChannelM go func() { InvalidateCacheForUser(user.Id) - message := model.NewWebSocketEvent(channel.TeamId, channel.Id, user.Id, model.WEBSOCKET_EVENT_USER_ADDED) + message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_USER_ADDED, "", channel.Id, "", nil) + message.Add("user_id", user.Id) + message.Add("team_id", channel.TeamId) go Publish(message) }() @@ -789,7 +791,8 @@ func deleteChannel(c *Context, w http.ResponseWriter, r *http.Request) { go func() { InvalidateCacheForChannel(channel.Id) - message := model.NewWebSocketEvent(c.TeamId, channel.Id, c.Session.UserId, model.WEBSOCKET_EVENT_CHANNEL_DELETED) + message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_CHANNEL_DELETED, c.TeamId, "", "", nil) + message.Add("channel_id", channel.Id) go Publish(message) post := &model.Post{ @@ -827,7 +830,7 @@ func setLastViewedAt(c *Context, w http.ResponseWriter, r *http.Request) { Srv.Store.Preference().Save(&model.Preferences{preference}) - message := model.NewWebSocketEvent(c.TeamId, id, c.Session.UserId, model.WEBSOCKET_EVENT_CHANNEL_VIEWED) + message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_CHANNEL_VIEWED, c.TeamId, "", c.Session.UserId, nil) message.Add("channel_id", id) go Publish(message) @@ -882,7 +885,7 @@ func updateLastViewedAt(c *Context, w http.ResponseWriter, r *http.Request) { Srv.Store.Preference().Save(&model.Preferences{preference}) - message := model.NewWebSocketEvent(c.TeamId, id, c.Session.UserId, model.WEBSOCKET_EVENT_CHANNEL_VIEWED) + message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_CHANNEL_VIEWED, c.TeamId, "", c.Session.UserId, nil) message.Add("channel_id", id) go Publish(message) @@ -1111,10 +1114,17 @@ func RemoveUserFromChannel(userIdToRemove string, removerUserId string, channel InvalidateCacheForUser(userIdToRemove) - message := model.NewWebSocketEvent(channel.TeamId, channel.Id, userIdToRemove, model.WEBSOCKET_EVENT_USER_REMOVED) + message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_USER_REMOVED, "", channel.Id, "", nil) + message.Add("user_id", userIdToRemove) message.Add("remover_id", removerUserId) go Publish(message) + // because the removed user no longer belongs to the channel we need to send a separate websocket event + userMsg := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_USER_REMOVED, "", "", userIdToRemove, nil) + userMsg.Add("channel_id", channel.Id) + userMsg.Add("remover_id", removerUserId) + go Publish(userMsg) + return nil } diff --git a/api/command_expand_collapse.go b/api/command_expand_collapse.go index c56845a9e..ee018cf8f 100644 --- a/api/command_expand_collapse.go +++ b/api/command_expand_collapse.go @@ -69,7 +69,7 @@ func setCollapsePreference(c *Context, value string) *model.CommandResponse { return &model.CommandResponse{Text: c.T("api.command_expand_collapse.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL} } - socketMessage := model.NewWebSocketEvent("", "", c.Session.UserId, model.WEBSOCKET_EVENT_PREFERENCE_CHANGED) + socketMessage := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_PREFERENCE_CHANGED, "", "", c.Session.UserId, nil) socketMessage.Add("preference", pref.ToJson()) go Publish(socketMessage) diff --git a/api/command_msg.go b/api/command_msg.go index d7208f121..aac657385 100644 --- a/api/command_msg.go +++ b/api/command_msg.go @@ -79,7 +79,7 @@ func (me *msgProvider) DoCommand(c *Context, channelId string, message string) * targetChannelId = channel.Data.(*model.Channel).Id } - makeDirectChannelVisible(c.TeamId, targetChannelId) + makeDirectChannelVisible(targetChannelId) if len(parsedMessage) > 0 { post := &model.Post{} post.Message = parsedMessage diff --git a/api/post.go b/api/post.go index 0b43aef0d..1286a23d9 100644 --- a/api/post.go +++ b/api/post.go @@ -286,11 +286,11 @@ func handlePostEvents(c *Context, post *model.Post, triggerWebhooks bool) { } if channel.Type == model.CHANNEL_DIRECT { - go makeDirectChannelVisible(c.TeamId, post.ChannelId) + go makeDirectChannelVisible(post.ChannelId) } } -func makeDirectChannelVisible(teamId string, channelId string) { +func makeDirectChannelVisible(channelId string) { var members []model.ChannelMember if result := <-Srv.Store.Channel().GetMembers(channelId); result.Err != nil { l4g.Error(utils.T("api.post.make_direct_channel_visible.get_members.error"), channelId, result.Err.Message) @@ -320,7 +320,7 @@ func makeDirectChannelVisible(teamId string, channelId string) { if saveResult := <-Srv.Store.Preference().Save(&model.Preferences{*preference}); saveResult.Err != nil { l4g.Error(utils.T("api.post.make_direct_channel_visible.save_pref.error"), member.UserId, otherUserId, saveResult.Err.Message) } else { - message := model.NewWebSocketEvent(teamId, channelId, member.UserId, model.WEBSOCKET_EVENT_PREFERENCE_CHANGED) + message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_PREFERENCE_CHANGED, "", "", member.UserId, nil) message.Add("preference", preference.ToJson()) go Publish(message) @@ -335,7 +335,7 @@ func makeDirectChannelVisible(teamId string, channelId string) { if updateResult := <-Srv.Store.Preference().Save(&model.Preferences{preference}); updateResult.Err != nil { l4g.Error(utils.T("api.post.make_direct_channel_visible.update_pref.error"), member.UserId, otherUserId, updateResult.Err.Message) } else { - message := model.NewWebSocketEvent(teamId, channelId, member.UserId, model.WEBSOCKET_EVENT_PREFERENCE_CHANGED) + message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_PREFERENCE_CHANGED, "", "", member.UserId, nil) message.Add("preference", preference.ToJson()) go Publish(message) @@ -778,7 +778,7 @@ func sendNotifications(c *Context, post *model.Post, team *model.Team, channel * } } - message := model.NewWebSocketEvent(c.TeamId, post.ChannelId, post.UserId, model.WEBSOCKET_EVENT_POSTED) + message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_POSTED, "", post.ChannelId, "", nil) message.Add("post", post.ToJson()) message.Add("channel_type", channel.Type) message.Add("channel_display_name", channel.DisplayName) @@ -1103,7 +1103,7 @@ func SendEphemeralPost(teamId, userId string, post *model.Post) { post.Filenames = []string{} } - message := model.NewWebSocketEvent(teamId, post.ChannelId, userId, model.WEBSOCKET_EVENT_EPHEMERAL_MESSAGE) + message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_EPHEMERAL_MESSAGE, "", post.ChannelId, userId, nil) message.Add("post", post.ToJson()) go Publish(message) @@ -1164,7 +1164,7 @@ func updatePost(c *Context, w http.ResponseWriter, r *http.Request) { } else { rpost := result.Data.(*model.Post) - message := model.NewWebSocketEvent(c.TeamId, rpost.ChannelId, c.Session.UserId, model.WEBSOCKET_EVENT_POST_EDITED) + message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_POST_EDITED, "", rpost.ChannelId, "", nil) message.Add("post", rpost.ToJson()) go Publish(message) @@ -1445,7 +1445,7 @@ func deletePost(c *Context, w http.ResponseWriter, r *http.Request) { return } - message := model.NewWebSocketEvent(c.TeamId, post.ChannelId, c.Session.UserId, model.WEBSOCKET_EVENT_POST_DELETED) + message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_POST_DELETED, "", post.ChannelId, "", nil) message.Add("post", post.ToJson()) go Publish(message) diff --git a/api/post_test.go b/api/post_test.go index 8f8bca899..7b7832148 100644 --- a/api/post_test.go +++ b/api/post_test.go @@ -837,7 +837,7 @@ func TestMakeDirectChannelVisible(t *testing.T) { channel := Client.Must(Client.CreateDirectChannel(user2.Id)).Data.(*model.Channel) - makeDirectChannelVisible(team.Id, channel.Id) + makeDirectChannelVisible(channel.Id) if result, err := Client.GetPreference(model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW, user2.Id); err != nil { t.Fatal("Errored trying to set direct channel to be visible for user1") diff --git a/api/status.go b/api/status.go index 892b1fd68..a1a5ac496 100644 --- a/api/status.go +++ b/api/status.go @@ -110,8 +110,9 @@ func SetStatusOnline(userId string, sessionId string, manual bool) { } if broadcast { - event := model.NewWebSocketEvent("", "", status.UserId, model.WEBSOCKET_EVENT_STATUS_CHANGE) + event := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_STATUS_CHANGE, "", "", "", nil) event.Add("status", model.STATUS_ONLINE) + event.Add("user_id", status.UserId) go Publish(event) } } @@ -130,8 +131,9 @@ func SetStatusOffline(userId string, manual bool) { l4g.Error(utils.T("api.status.save_status.error"), userId, result.Err) } - event := model.NewWebSocketEvent("", "", status.UserId, model.WEBSOCKET_EVENT_STATUS_CHANGE) + event := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_STATUS_CHANGE, "", "", "", nil) event.Add("status", model.STATUS_OFFLINE) + event.Add("user_id", status.UserId) go Publish(event) } @@ -166,8 +168,9 @@ func SetStatusAwayIfNeeded(userId string, manual bool) { l4g.Error(utils.T("api.status.save_status.error"), userId, result.Err) } - event := model.NewWebSocketEvent("", "", status.UserId, model.WEBSOCKET_EVENT_STATUS_CHANGE) + event := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_STATUS_CHANGE, "", "", "", nil) event.Add("status", model.STATUS_AWAY) + event.Add("user_id", status.UserId) go Publish(event) } diff --git a/api/status_test.go b/api/status_test.go index 81edd2103..6e4fb30ca 100644 --- a/api/status_test.go +++ b/api/status_test.go @@ -121,7 +121,7 @@ func TestStatuses(t *testing.T) { for { select { case resp := <-WebSocketClient.EventChannel: - if resp.Event == model.WEBSOCKET_EVENT_STATUS_CHANGE && resp.UserId == th.BasicUser2.Id { + if resp.Event == model.WEBSOCKET_EVENT_STATUS_CHANGE && resp.Data["user_id"].(string) == th.BasicUser2.Id { status := resp.Data["status"].(string) if status == model.STATUS_ONLINE { onlineHit = true diff --git a/api/team.go b/api/team.go index 0f3475098..2be7b8545 100644 --- a/api/team.go +++ b/api/team.go @@ -304,8 +304,8 @@ func JoinUserToTeam(team *model.Team, user *model.User) *model.AppError { RemoveAllSessionsForUserId(user.Id) InvalidateCacheForUser(user.Id) - // This message goes to every channel, so the channelId is irrelevant - go Publish(model.NewWebSocketEvent("", "", user.Id, model.WEBSOCKET_EVENT_NEW_USER)) + // This message goes to everyone, so the teamId, channelId and userId are irrelevant + go Publish(model.NewWebSocketEvent(model.WEBSOCKET_EVENT_NEW_USER, "", "", "", nil)) return nil } @@ -357,7 +357,9 @@ func LeaveTeam(team *model.Team, user *model.User) *model.AppError { RemoveAllSessionsForUserId(user.Id) InvalidateCacheForUser(user.Id) - go Publish(model.NewWebSocketEvent(team.Id, "", user.Id, model.WEBSOCKET_EVENT_LEAVE_TEAM)) + message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_LEAVE_TEAM, team.Id, "", "", nil) + message.Add("user_id", user.Id) + go Publish(message) return nil } diff --git a/api/user.go b/api/user.go index e8040f74e..5b2024315 100644 --- a/api/user.go +++ b/api/user.go @@ -269,8 +269,8 @@ func CreateUser(user *model.User) (*model.User, *model.AppError) { ruser.Sanitize(map[string]bool{}) - // This message goes to every channel, so the channelId is irrelevant - go Publish(model.NewWebSocketEvent("", "", ruser.Id, model.WEBSOCKET_EVENT_NEW_USER)) + // This message goes to everyone, so the teamId, channelId and userId are irrelevant + go Publish(model.NewWebSocketEvent(model.WEBSOCKET_EVENT_NEW_USER, "", "", "", nil)) return ruser, nil } @@ -1289,8 +1289,11 @@ func uploadProfileImage(c *Context, w http.ResponseWriter, r *http.Request) { } else { user := result.Data.(*model.User) user = sanitizeProfile(c, user) - message := model.NewWebSocketEvent("", "", c.Session.UserId, model.WEBSOCKET_EVENT_USER_UPDATED) + omitUsers := make(map[string]bool, 1) + omitUsers[user.Id] = true + message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_USER_UPDATED, "", "", "", omitUsers) message.Add("user", user) + go Publish(message) } @@ -1340,7 +1343,9 @@ func updateUser(c *Context, w http.ResponseWriter, r *http.Request) { updatedUser := rusers[0] updatedUser = sanitizeProfile(c, updatedUser) - message := model.NewWebSocketEvent("", "", user.Id, model.WEBSOCKET_EVENT_USER_UPDATED) + omitUsers := make(map[string]bool, 1) + omitUsers[user.Id] = true + message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_USER_UPDATED, "", "", "", omitUsers) message.Add("user", updatedUser) go Publish(message) @@ -2496,8 +2501,12 @@ func userTyping(req *model.WebSocketRequest) (map[string]interface{}, *model.App parentId = "" } - event := model.NewWebSocketEvent("", channelId, req.Session.UserId, model.WEBSOCKET_EVENT_TYPING) + omitUsers := make(map[string]bool, 1) + omitUsers[req.Session.UserId] = true + + event := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_TYPING, "", channelId, "", omitUsers) event.Add("parent_id", parentId) + event.Add("user_id", req.Session.UserId) go Publish(event) return nil, nil diff --git a/api/user_test.go b/api/user_test.go index 7f36083a6..3800f5d9e 100644 --- a/api/user_test.go +++ b/api/user_test.go @@ -1793,7 +1793,7 @@ func TestUserTyping(t *testing.T) { for { select { case resp := <-WebSocketClient2.EventChannel: - if resp.Event == model.WEBSOCKET_EVENT_TYPING && resp.UserId == th.BasicUser.Id { + if resp.Event == model.WEBSOCKET_EVENT_TYPING && resp.Data["user_id"].(string) == th.BasicUser.Id { eventHit = true } case <-stop: diff --git a/api/web_hub.go b/api/web_hub.go index 18c218634..4a9719d80 100644 --- a/api/web_hub.go +++ b/api/web_hub.go @@ -64,7 +64,7 @@ func InvalidateCacheForChannel(channelId string) { func (h *Hub) Register(webConn *WebConn) { h.register <- webConn - msg := model.NewWebSocketEvent("", "", webConn.UserId, model.WEBSOCKET_EVENT_HELLO) + msg := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_HELLO, "", "", webConn.UserId, nil) msg.Add("server_version", fmt.Sprintf("%v.%v.%v", model.CurrentVersion, model.BuildNumber, utils.CfgHash)) go Publish(msg) } @@ -146,52 +146,25 @@ func (h *Hub) Start() { } func shouldSendEvent(webCon *WebConn, msg *model.WebSocketEvent) bool { - if webCon.UserId == msg.UserId { - // Don't need to tell the user they are typing - if msg.Event == model.WEBSOCKET_EVENT_TYPING { - return false - } - - // We have to make sure the user is in the channel. Otherwise system messages that - // post about users in channels they are not in trigger warnings. - if len(msg.ChannelId) > 0 { - allowed := webCon.IsMemberOfChannel(msg.ChannelId) - - if !allowed { - return false - } - } - } else { - // Don't share a user's view or preference events with other users - if msg.Event == model.WEBSOCKET_EVENT_CHANNEL_VIEWED { - return false - } else if msg.Event == model.WEBSOCKET_EVENT_PREFERENCE_CHANGED { - return false - } else if msg.Event == model.WEBSOCKET_EVENT_EPHEMERAL_MESSAGE { - // For now, ephemeral messages are sent directly to individual users - return false - } else if msg.Event == model.WEBSOCKET_EVENT_WEBRTC { - // No need to tell anyone that a webrtc event is going on - return false - } + // If the event is destined to a specific user + if len(msg.Broadcast.UserId) > 0 && webCon.UserId != msg.Broadcast.UserId { + return false + } - // Only report events to users who are in the team for the event - if len(msg.TeamId) > 0 { - allowed := webCon.IsMemberOfTeam(msg.TeamId) + // if the user is omitted don't send the message + if _, ok := msg.Broadcast.OmitUsers[webCon.UserId]; ok { + return false + } - if !allowed { - return false - } - } + // Only report events to users who are in the channel for the event + if len(msg.Broadcast.ChannelId) > 0 { + return webCon.IsMemberOfChannel(msg.Broadcast.ChannelId) + } - // Only report events to users who are in the channel for the event execept deleted events - if len(msg.ChannelId) > 0 && msg.Event != model.WEBSOCKET_EVENT_CHANNEL_DELETED { - allowed := webCon.IsMemberOfChannel(msg.ChannelId) + // Only report events to users who are in the team for the event + if len(msg.Broadcast.TeamId) > 0 { + return webCon.IsMemberOfTeam(msg.Broadcast.TeamId) - if !allowed { - return false - } - } } return true diff --git a/api/webrtc.go b/api/webrtc.go index 4664524f4..ba6054125 100644 --- a/api/webrtc.go +++ b/api/webrtc.go @@ -43,7 +43,7 @@ func webrtcMessage(req *model.WebSocketRequest) (map[string]interface{}, *model. return nil, NewInvalidWebSocketParamError(req.Action, "to_user_id") } - event := model.NewWebSocketEvent("", "", toUserId, model.WEBSOCKET_EVENT_WEBRTC) + event := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_WEBRTC, "", "", toUserId, nil) event.Data = req.Data go Publish(event) diff --git a/api/websocket_test.go b/api/websocket_test.go index b0dc1e955..be762429a 100644 --- a/api/websocket_test.go +++ b/api/websocket_test.go @@ -78,7 +78,10 @@ func TestWebSocketEvent(t *testing.T) { WebSocketClient.Listen() - evt1 := model.NewWebSocketEvent(th.BasicTeam.Id, th.BasicChannel.Id, "somerandomid", model.WEBSOCKET_EVENT_TYPING) + omitUser := make(map[string]bool, 1) + omitUser["somerandomid"] = true + evt1 := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_TYPING, "", th.BasicChannel.Id, "", omitUser) + evt1.Add("user_id", "somerandomid") go Publish(evt1) time.Sleep(300 * time.Millisecond) @@ -89,7 +92,7 @@ func TestWebSocketEvent(t *testing.T) { for { select { case resp := <-WebSocketClient.EventChannel: - if resp.Event == model.WEBSOCKET_EVENT_TYPING && resp.UserId == "somerandomid" { + if resp.Event == model.WEBSOCKET_EVENT_TYPING && resp.Data["user_id"].(string) == "somerandomid" { eventHit = true } case <-stop: @@ -106,7 +109,7 @@ func TestWebSocketEvent(t *testing.T) { t.Fatal("did not receive typing event") } - evt2 := model.NewWebSocketEvent(th.BasicTeam.Id, "somerandomid", "somerandomid", model.WEBSOCKET_EVENT_TYPING) + evt2 := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_TYPING, "", "somerandomid", "", nil) go Publish(evt2) time.Sleep(300 * time.Millisecond) -- cgit v1.2.3-1-g7c22