Commit 60347559 authored by enahum's avatar enahum Committed by Christopher Speller

PLT-3734 Cleaning up shouldSendEvent function (#4024)

* PLT-3734 Cleaning up shouldSendEvent function

* Fix LHS unread highlight and jewel mentions
parent bfca7529
...@@ -167,7 +167,7 @@ func CreateDirectChannel(userId string, otherUserId string) (*model.Channel, *mo ...@@ -167,7 +167,7 @@ func CreateDirectChannel(userId string, otherUserId string) (*model.Channel, *mo
return nil, result.Err return nil, result.Err
} }
} else { } 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) message.Add("teammate_id", otherUserId)
go Publish(message) go Publish(message)
...@@ -579,7 +579,9 @@ func AddUserToChannel(user *model.User, channel *model.Channel) (*model.ChannelM ...@@ -579,7 +579,9 @@ func AddUserToChannel(user *model.User, channel *model.Channel) (*model.ChannelM
go func() { go func() {
InvalidateCacheForUser(user.Id) 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) go Publish(message)
}() }()
...@@ -789,7 +791,8 @@ func deleteChannel(c *Context, w http.ResponseWriter, r *http.Request) { ...@@ -789,7 +791,8 @@ func deleteChannel(c *Context, w http.ResponseWriter, r *http.Request) {
go func() { go func() {
InvalidateCacheForChannel(channel.Id) 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) go Publish(message)
post := &model.Post{ post := &model.Post{
...@@ -827,7 +830,7 @@ func setLastViewedAt(c *Context, w http.ResponseWriter, r *http.Request) { ...@@ -827,7 +830,7 @@ func setLastViewedAt(c *Context, w http.ResponseWriter, r *http.Request) {
Srv.Store.Preference().Save(&model.Preferences{preference}) 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) message.Add("channel_id", id)
go Publish(message) go Publish(message)
...@@ -882,7 +885,7 @@ func updateLastViewedAt(c *Context, w http.ResponseWriter, r *http.Request) { ...@@ -882,7 +885,7 @@ func updateLastViewedAt(c *Context, w http.ResponseWriter, r *http.Request) {
Srv.Store.Preference().Save(&model.Preferences{preference}) 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) message.Add("channel_id", id)
go Publish(message) go Publish(message)
...@@ -1111,10 +1114,17 @@ func RemoveUserFromChannel(userIdToRemove string, removerUserId string, channel ...@@ -1111,10 +1114,17 @@ func RemoveUserFromChannel(userIdToRemove string, removerUserId string, channel
InvalidateCacheForUser(userIdToRemove) 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) message.Add("remover_id", removerUserId)
go Publish(message) 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 return nil
} }
......
...@@ -69,7 +69,7 @@ func setCollapsePreference(c *Context, value string) *model.CommandResponse { ...@@ -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} 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()) socketMessage.Add("preference", pref.ToJson())
go Publish(socketMessage) go Publish(socketMessage)
......
...@@ -79,7 +79,7 @@ func (me *msgProvider) DoCommand(c *Context, channelId string, message string) * ...@@ -79,7 +79,7 @@ func (me *msgProvider) DoCommand(c *Context, channelId string, message string) *
targetChannelId = channel.Data.(*model.Channel).Id targetChannelId = channel.Data.(*model.Channel).Id
} }
makeDirectChannelVisible(c.TeamId, targetChannelId) makeDirectChannelVisible(targetChannelId)
if len(parsedMessage) > 0 { if len(parsedMessage) > 0 {
post := &model.Post{} post := &model.Post{}
post.Message = parsedMessage post.Message = parsedMessage
......
...@@ -286,11 +286,11 @@ func handlePostEvents(c *Context, post *model.Post, triggerWebhooks bool) { ...@@ -286,11 +286,11 @@ func handlePostEvents(c *Context, post *model.Post, triggerWebhooks bool) {
} }
if channel.Type == model.CHANNEL_DIRECT { 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 var members []model.ChannelMember
if result := <-Srv.Store.Channel().GetMembers(channelId); result.Err != nil { 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) 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) { ...@@ -320,7 +320,7 @@ func makeDirectChannelVisible(teamId string, channelId string) {
if saveResult := <-Srv.Store.Preference().Save(&model.Preferences{*preference}); saveResult.Err != nil { 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) l4g.Error(utils.T("api.post.make_direct_channel_visible.save_pref.error"), member.UserId, otherUserId, saveResult.Err.Message)
} else { } 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()) message.Add("preference", preference.ToJson())
go Publish(message) go Publish(message)
...@@ -335,7 +335,7 @@ func makeDirectChannelVisible(teamId string, channelId string) { ...@@ -335,7 +335,7 @@ func makeDirectChannelVisible(teamId string, channelId string) {
if updateResult := <-Srv.Store.Preference().Save(&model.Preferences{preference}); updateResult.Err != nil { 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) l4g.Error(utils.T("api.post.make_direct_channel_visible.update_pref.error"), member.UserId, otherUserId, updateResult.Err.Message)
} else { } 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()) message.Add("preference", preference.ToJson())
go Publish(message) go Publish(message)
...@@ -778,7 +778,7 @@ func sendNotifications(c *Context, post *model.Post, team *model.Team, channel * ...@@ -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("post", post.ToJson())
message.Add("channel_type", channel.Type) message.Add("channel_type", channel.Type)
message.Add("channel_display_name", channel.DisplayName) message.Add("channel_display_name", channel.DisplayName)
...@@ -1103,7 +1103,7 @@ func SendEphemeralPost(teamId, userId string, post *model.Post) { ...@@ -1103,7 +1103,7 @@ func SendEphemeralPost(teamId, userId string, post *model.Post) {
post.Filenames = []string{} 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()) message.Add("post", post.ToJson())
go Publish(message) go Publish(message)
...@@ -1164,7 +1164,7 @@ func updatePost(c *Context, w http.ResponseWriter, r *http.Request) { ...@@ -1164,7 +1164,7 @@ func updatePost(c *Context, w http.ResponseWriter, r *http.Request) {
} else { } else {
rpost := result.Data.(*model.Post) 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()) message.Add("post", rpost.ToJson())
go Publish(message) go Publish(message)
...@@ -1445,7 +1445,7 @@ func deletePost(c *Context, w http.ResponseWriter, r *http.Request) { ...@@ -1445,7 +1445,7 @@ func deletePost(c *Context, w http.ResponseWriter, r *http.Request) {
return 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()) message.Add("post", post.ToJson())
go Publish(message) go Publish(message)
......
...@@ -837,7 +837,7 @@ func TestMakeDirectChannelVisible(t *testing.T) { ...@@ -837,7 +837,7 @@ func TestMakeDirectChannelVisible(t *testing.T) {
channel := Client.Must(Client.CreateDirectChannel(user2.Id)).Data.(*model.Channel) 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 { 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") t.Fatal("Errored trying to set direct channel to be visible for user1")
......
...@@ -110,8 +110,9 @@ func SetStatusOnline(userId string, sessionId string, manual bool) { ...@@ -110,8 +110,9 @@ func SetStatusOnline(userId string, sessionId string, manual bool) {
} }
if broadcast { 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("status", model.STATUS_ONLINE)
event.Add("user_id", status.UserId)
go Publish(event) go Publish(event)
} }
} }
...@@ -130,8 +131,9 @@ func SetStatusOffline(userId string, manual bool) { ...@@ -130,8 +131,9 @@ func SetStatusOffline(userId string, manual bool) {
l4g.Error(utils.T("api.status.save_status.error"), userId, result.Err) 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("status", model.STATUS_OFFLINE)
event.Add("user_id", status.UserId)
go Publish(event) go Publish(event)
} }
...@@ -166,8 +168,9 @@ func SetStatusAwayIfNeeded(userId string, manual bool) { ...@@ -166,8 +168,9 @@ func SetStatusAwayIfNeeded(userId string, manual bool) {
l4g.Error(utils.T("api.status.save_status.error"), userId, result.Err) 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("status", model.STATUS_AWAY)
event.Add("user_id", status.UserId)
go Publish(event) go Publish(event)
} }
......
...@@ -121,7 +121,7 @@ func TestStatuses(t *testing.T) { ...@@ -121,7 +121,7 @@ func TestStatuses(t *testing.T) {
for { for {
select { select {
case resp := <-WebSocketClient.EventChannel: 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) status := resp.Data["status"].(string)
if status == model.STATUS_ONLINE { if status == model.STATUS_ONLINE {
onlineHit = true onlineHit = true
......
...@@ -304,8 +304,8 @@ func JoinUserToTeam(team *model.Team, user *model.User) *model.AppError { ...@@ -304,8 +304,8 @@ func JoinUserToTeam(team *model.Team, user *model.User) *model.AppError {
RemoveAllSessionsForUserId(user.Id) RemoveAllSessionsForUserId(user.Id)
InvalidateCacheForUser(user.Id) InvalidateCacheForUser(user.Id)
// This message goes to every channel, so the channelId is irrelevant // This message goes to everyone, so the teamId, channelId and userId are irrelevant
go Publish(model.NewWebSocketEvent("", "", user.Id, model.WEBSOCKET_EVENT_NEW_USER)) go Publish(model.NewWebSocketEvent(model.WEBSOCKET_EVENT_NEW_USER, "", "", "", nil))
return nil return nil
} }
...@@ -357,7 +357,9 @@ func LeaveTeam(team *model.Team, user *model.User) *model.AppError { ...@@ -357,7 +357,9 @@ func LeaveTeam(team *model.Team, user *model.User) *model.AppError {
RemoveAllSessionsForUserId(user.Id) RemoveAllSessionsForUserId(user.Id)
InvalidateCacheForUser(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 return nil
} }
......
...@@ -269,8 +269,8 @@ func CreateUser(user *model.User) (*model.User, *model.AppError) { ...@@ -269,8 +269,8 @@ func CreateUser(user *model.User) (*model.User, *model.AppError) {
ruser.Sanitize(map[string]bool{}) ruser.Sanitize(map[string]bool{})
// This message goes to every channel, so the channelId is irrelevant // This message goes to everyone, so the teamId, channelId and userId are irrelevant
go Publish(model.NewWebSocketEvent("", "", ruser.Id, model.WEBSOCKET_EVENT_NEW_USER)) go Publish(model.NewWebSocketEvent(model.WEBSOCKET_EVENT_NEW_USER, "", "", "", nil))
return ruser, nil return ruser, nil
} }
...@@ -1289,8 +1289,11 @@ func uploadProfileImage(c *Context, w http.ResponseWriter, r *http.Request) { ...@@ -1289,8 +1289,11 @@ func uploadProfileImage(c *Context, w http.ResponseWriter, r *http.Request) {
} else { } else {
user := result.Data.(*model.User) user := result.Data.(*model.User)
user = sanitizeProfile(c, 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) message.Add("user", user)
go Publish(message) go Publish(message)
} }
...@@ -1340,7 +1343,9 @@ func updateUser(c *Context, w http.ResponseWriter, r *http.Request) { ...@@ -1340,7 +1343,9 @@ func updateUser(c *Context, w http.ResponseWriter, r *http.Request) {
updatedUser := rusers[0] updatedUser := rusers[0]
updatedUser = sanitizeProfile(c, updatedUser) 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) message.Add("user", updatedUser)
go Publish(message) go Publish(message)
...@@ -2496,8 +2501,12 @@ func userTyping(req *model.WebSocketRequest) (map[string]interface{}, *model.App ...@@ -2496,8 +2501,12 @@ func userTyping(req *model.WebSocketRequest) (map[string]interface{}, *model.App
parentId = "" 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("parent_id", parentId)
event.Add("user_id", req.Session.UserId)
go Publish(event) go Publish(event)
return nil, nil return nil, nil
......
...@@ -1793,7 +1793,7 @@ func TestUserTyping(t *testing.T) { ...@@ -1793,7 +1793,7 @@ func TestUserTyping(t *testing.T) {
for { for {
select { select {
case resp := <-WebSocketClient2.EventChannel: 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 eventHit = true
} }
case <-stop: case <-stop:
......
...@@ -64,7 +64,7 @@ func InvalidateCacheForChannel(channelId string) { ...@@ -64,7 +64,7 @@ func InvalidateCacheForChannel(channelId string) {
func (h *Hub) Register(webConn *WebConn) { func (h *Hub) Register(webConn *WebConn) {
h.register <- 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)) msg.Add("server_version", fmt.Sprintf("%v.%v.%v", model.CurrentVersion, model.BuildNumber, utils.CfgHash))
go Publish(msg) go Publish(msg)
} }
...@@ -146,52 +146,25 @@ func (h *Hub) Start() { ...@@ -146,52 +146,25 @@ func (h *Hub) Start() {
} }
func shouldSendEvent(webCon *WebConn, msg *model.WebSocketEvent) bool { func shouldSendEvent(webCon *WebConn, msg *model.WebSocketEvent) bool {
if webCon.UserId == msg.UserId { // If the event is destined to a specific user
// Don't need to tell the user they are typing if len(msg.Broadcast.UserId) > 0 && webCon.UserId != msg.Broadcast.UserId {
if msg.Event == model.WEBSOCKET_EVENT_TYPING { return false
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
}
// Only report events to users who are in the team for the event // if the user is omitted don't send the message
if len(msg.TeamId) > 0 { if _, ok := msg.Broadcast.OmitUsers[webCon.UserId]; ok {
allowed := webCon.IsMemberOfTeam(msg.TeamId) return false
}
if !allowed { // Only report events to users who are in the channel for the event
return false 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 // Only report events to users who are in the team for the event
if len(msg.ChannelId) > 0 && msg.Event != model.WEBSOCKET_EVENT_CHANNEL_DELETED { if len(msg.Broadcast.TeamId) > 0 {
allowed := webCon.IsMemberOfChannel(msg.ChannelId) return webCon.IsMemberOfTeam(msg.Broadcast.TeamId)
if !allowed {
return false
}
}
} }
return true return true
......
...@@ -43,7 +43,7 @@ func webrtcMessage(req *model.WebSocketRequest) (map[string]interface{}, *model. ...@@ -43,7 +43,7 @@ func webrtcMessage(req *model.WebSocketRequest) (map[string]interface{}, *model.
return nil, NewInvalidWebSocketParamError(req.Action, "to_user_id") 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 event.Data = req.Data
go Publish(event) go Publish(event)
......
...@@ -78,7 +78,10 @@ func TestWebSocketEvent(t *testing.T) { ...@@ -78,7 +78,10 @@ func TestWebSocketEvent(t *testing.T) {
WebSocketClient.Listen() 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) go Publish(evt1)
time.Sleep(300 * time.Millisecond) time.Sleep(300 * time.Millisecond)
...@@ -89,7 +92,7 @@ func TestWebSocketEvent(t *testing.T) { ...@@ -89,7 +92,7 @@ func TestWebSocketEvent(t *testing.T) {
for { for {
select { select {
case resp := <-WebSocketClient.EventChannel: 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 eventHit = true
} }
case <-stop: case <-stop:
...@@ -106,7 +109,7 @@ func TestWebSocketEvent(t *testing.T) { ...@@ -106,7 +109,7 @@ func TestWebSocketEvent(t *testing.T) {
t.Fatal("did not receive typing event") 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) go Publish(evt2)
time.Sleep(300 * time.Millisecond) time.Sleep(300 * time.Millisecond)
......
...@@ -33,20 +33,26 @@ type WebSocketMessage interface { ...@@ -33,20 +33,26 @@ type WebSocketMessage interface {
IsValid() bool IsValid() bool
} }
type WebsocketBroadcast struct {
OmitUsers map[string]bool `json:"-"` // broadcast is omitted for users listed here
UserId string `json:"user_id"` // broadcast only occurs for this user
ChannelId string `json:"channel_id"` // broadcast only occurs for users in this channel
TeamId string `json:"team_id"` // broadcast only occurs for users in this team
}
type WebSocketEvent struct { type WebSocketEvent struct {
TeamId string `json:"team_id"`
ChannelId string `json:"channel_id"`
UserId string `json:"user_id"`
Event string `json:"event"` Event string `json:"event"`
Data map[string]interface{} `json:"data"` Data map[string]interface{} `json:"data"`
Broadcast *WebsocketBroadcast `json:"broadcast"`
} }
func (m *WebSocketEvent) Add(key string, value interface{}) { func (m *WebSocketEvent) Add(key string, value interface{}) {
m.Data[key] = value m.Data[key] = value
} }
func NewWebSocketEvent(teamId string, channelId string, userId string, event string) *WebSocketEvent { func NewWebSocketEvent(event, teamId, channelId, userId string, omitUsers map[string]bool) *WebSocketEvent {
return &WebSocketEvent{TeamId: teamId, ChannelId: channelId, UserId: userId, Event: event, Data: make(map[string]interface{})} return &WebSocketEvent{Event: event, Data: make(map[string]interface{}),
Broadcast: &WebsocketBroadcast{TeamId: teamId, ChannelId: channelId, UserId: userId, OmitUsers: omitUsers}}
} }
func (o *WebSocketEvent) IsValid() bool { func (o *WebSocketEvent) IsValid() bool {
......
...@@ -9,7 +9,7 @@ import ( ...@@ -9,7 +9,7 @@ import (
) )
func TestWebSocketEvent(t *testing.T) { func TestWebSocketEvent(t *testing.T) {
m := NewWebSocketEvent(NewId(), NewId(), NewId(), "some_event") m := NewWebSocketEvent("some_event", NewId(), NewId(), NewId(), nil)
m.Add("RootId", NewId()) m.Add("RootId", NewId())