PLT-3734 Cleaning up shouldSendEvent function (#4024)

* PLT-3734 Cleaning up shouldSendEvent function

* Fix LHS unread highlight and jewel mentions
Этот коммит содержится в:
enahum
2016-09-27 11:19:50 -03:00
коммит произвёл Christopher Speller
родитель bfca752940
Коммит 60347559c7
17 изменённых файлов: 117 добавлений и 110 удалений

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

@@ -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
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) {
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) {
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) {
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
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 {
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) *
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) {
} }
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) {
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) {
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 *
} }
} }
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) {
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) {
} 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) {
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) {
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) {
} }
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) {
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) {
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) {
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 {
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 {
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) {
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) {
} 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) {
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
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) {
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) {
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() {
} }
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 // if the user is omitted don't send the message
// post about users in channels they are not in trigger warnings. if _, ok := msg.Broadcast.OmitUsers[webCon.UserId]; ok {
if len(msg.ChannelId) > 0 { return false
allowed := webCon.IsMemberOfChannel(msg.ChannelId) }
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)
}
} 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 // Only report events to users who are in the team for the event
if len(msg.TeamId) > 0 { if len(msg.Broadcast.TeamId) > 0 {
allowed := webCon.IsMemberOfTeam(msg.TeamId) return webCon.IsMemberOfTeam(msg.Broadcast.TeamId)
if !allowed {
return false
}
}
// 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)
if !allowed {
return false
}
}
} }
return true return true

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

@@ -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) {
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) {
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) {
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 {
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 (
) )
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())
json := m.ToJson() json := m.ToJson()
result := WebSocketEventFromJson(strings.NewReader(json)) result := WebSocketEventFromJson(strings.NewReader(json))
@@ -23,7 +23,7 @@ func TestWebSocketEvent(t *testing.T) {
t.Fatal("should be valid") t.Fatal("should be valid")
} }
if m.TeamId != result.TeamId { if m.Broadcast.TeamId != result.Broadcast.TeamId {
t.Fatal("Ids do not match") t.Fatal("Ids do not match")
} }

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

@@ -23,7 +23,7 @@ export function handleNewPost(post, msg) {
} else { } else {
AsyncClient.getChannel(post.channel_id); AsyncClient.getChannel(post.channel_id);
} }
} else if (msg && (TeamStore.getCurrentId() === msg.team_id || msg.data.channel_type === Constants.DM_CHANNEL)) { } else if (msg && (TeamStore.getCurrentId() === msg.data.team_id || msg.data.channel_type === Constants.DM_CHANNEL)) {
if (Client.teamId) { if (Client.teamId) {
AsyncClient.getChannel(post.channel_id); AsyncClient.getChannel(post.channel_id);
} }

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

@@ -179,7 +179,7 @@ function handlePostEditEvent(msg) {
PostStore.emitChange(); PostStore.emitChange();
// Update channel state // Update channel state
if (ChannelStore.getCurrentId() === msg.channel_id) { if (ChannelStore.getCurrentId() === msg.broadcast.channel_id) {
if (window.isActive) { if (window.isActive) {
AsyncClient.updateLastViewedAt(null, false); AsyncClient.updateLastViewedAt(null, false);
} }
@@ -204,41 +204,41 @@ function handleNewUserEvent() {
} }
function handleLeaveTeamEvent(msg) { function handleLeaveTeamEvent(msg) {
if (UserStore.getCurrentId() === msg.user_id) { if (UserStore.getCurrentId() === msg.data.user_id) {
TeamStore.removeTeamMember(msg.team_id); TeamStore.removeTeamMember(msg.broadcast.team_id);
// if the are on the team begin removed redirect them to the root // if the are on the team begin removed redirect them to the root
if (TeamStore.getCurrentId() === msg.team_id) { if (TeamStore.getCurrentId() === msg.broadcast.team_id) {
TeamStore.setCurrentId(''); TeamStore.setCurrentId('');
Client.setTeamId(''); Client.setTeamId('');
browserHistory.push('/'); browserHistory.push('/');
} }
} else if (TeamStore.getCurrentId() === msg.team_id) { } else if (TeamStore.getCurrentId() === msg.broadcast.team_id) {
UserActions.getMoreDmList(); UserActions.getMoreDmList();
} }
} }
function handleDirectAddedEvent(msg) { function handleDirectAddedEvent(msg) {
AsyncClient.getChannel(msg.channel_id); AsyncClient.getChannel(msg.broadcast.channel_id);
AsyncClient.getDirectProfiles(); AsyncClient.getDirectProfiles();
} }
function handleUserAddedEvent(msg) { function handleUserAddedEvent(msg) {
if (ChannelStore.getCurrentId() === msg.channel_id) { if (ChannelStore.getCurrentId() === msg.broadcast.channel_id) {
AsyncClient.getChannelExtraInfo(); AsyncClient.getChannelExtraInfo();
} }
if (TeamStore.getCurrentId() === msg.team_id && UserStore.getCurrentId() === msg.user_id) { if (TeamStore.getCurrentId() === msg.data.team_id && UserStore.getCurrentId() === msg.data.user_id) {
AsyncClient.getChannel(msg.channel_id); AsyncClient.getChannel(msg.broadcast.channel_id);
} }
} }
function handleUserRemovedEvent(msg) { function handleUserRemovedEvent(msg) {
if (UserStore.getCurrentId() === msg.user_id) { if (UserStore.getCurrentId() === msg.broadcast.user_id) {
AsyncClient.getChannels(); AsyncClient.getChannels();
if (msg.data.remover_id !== msg.user_id && if (msg.data.remover_id !== msg.broadcast.user_id &&
msg.channel_id === ChannelStore.getCurrentId() && msg.data.channel_id === ChannelStore.getCurrentId() &&
$('#removed_from_channel').length > 0) { $('#removed_from_channel').length > 0) {
var sentState = {}; var sentState = {};
sentState.channelName = ChannelStore.getCurrent().display_name; sentState.channelName = ChannelStore.getCurrent().display_name;
@@ -247,32 +247,33 @@ function handleUserRemovedEvent(msg) {
BrowserStore.setItem('channel-removed-state', sentState); BrowserStore.setItem('channel-removed-state', sentState);
$('#removed_from_channel').modal('show'); $('#removed_from_channel').modal('show');
} }
} else if (ChannelStore.getCurrentId() === msg.channel_id) { } else if (ChannelStore.getCurrentId() === msg.broadcast.channel_id) {
AsyncClient.getChannelExtraInfo(); AsyncClient.getChannelExtraInfo();
} }
} }
function handleUserUpdatedEvent(msg) { function handleUserUpdatedEvent(msg) {
if (UserStore.getCurrentId() !== msg.user_id) { const user = msg.data.user;
UserStore.saveProfile(msg.data.user); if (UserStore.getCurrentId() !== user.id) {
if (UserStore.hasDirectProfile(msg.user_id)) { UserStore.saveProfile(user);
UserStore.saveDirectProfile(msg.data.user); if (UserStore.hasDirectProfile(user.id)) {
UserStore.saveDirectProfile(user);
} }
UserStore.emitChange(msg.user_id); UserStore.emitChange(user.id);
} }
} }
function handleChannelViewedEvent(msg) { function handleChannelViewedEvent(msg) {
// Useful for when multiple devices have the app open to different channels // Useful for when multiple devices have the app open to different channels
if (TeamStore.getCurrentId() === msg.team_id && if (TeamStore.getCurrentId() === msg.broadcast.team_id &&
ChannelStore.getCurrentId() !== msg.channel_id && ChannelStore.getCurrentId() !== msg.data.channel_id &&
UserStore.getCurrentId() === msg.user_id) { UserStore.getCurrentId() === msg.broadcast.user_id) {
AsyncClient.getChannel(msg.channel_id); AsyncClient.getChannel(msg.data.channel_id);
} }
} }
function handleChannelDeletedEvent(msg) { function handleChannelDeletedEvent(msg) {
if (ChannelStore.getCurrentId() === msg.channel_id) { if (ChannelStore.getCurrentId() === msg.data.channel_id) {
const teamUrl = TeamStore.getCurrentTeamRelativeUrl(); const teamUrl = TeamStore.getCurrentTeamRelativeUrl();
browserHistory.push(teamUrl + '/channels/' + Constants.DEFAULT_CHANNEL); browserHistory.push(teamUrl + '/channels/' + Constants.DEFAULT_CHANNEL);
} }
@@ -285,11 +286,11 @@ function handlePreferenceChangedEvent(msg) {
} }
function handleUserTypingEvent(msg) { function handleUserTypingEvent(msg) {
GlobalActions.emitRemoteUserTypingEvent(msg.channel_id, msg.user_id, msg.data.parent_id); GlobalActions.emitRemoteUserTypingEvent(msg.broadcast.channel_id, msg.data.user_id, msg.data.parent_id);
} }
function handleStatusChangedEvent(msg) { function handleStatusChangedEvent(msg) {
UserStore.setStatus(msg.user_id, msg.data.status); UserStore.setStatus(msg.data.user_id, msg.data.status);
} }
function handleHelloEvent(msg) { function handleHelloEvent(msg) {