Add websocket event and cache invalidation for deleting channels (#2807)

Этот коммит содержится в:
Joram Wilander
2016-04-27 16:05:39 -04:00
коммит произвёл Harrison Healey
родитель ada513a014
Коммит 7695cbd1b4
8 изменённых файлов: 68 добавлений и 35 удалений

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

@@ -687,14 +687,20 @@ func deleteChannel(c *Context, w http.ResponseWriter, r *http.Request) {
c.LogAudit("name=" + channel.Name) c.LogAudit("name=" + channel.Name)
post := &model.Post{ChannelId: channel.Id, Message: fmt.Sprintf( go func() {
c.T("api.channel.delete_channel.archived"), InvalidateCacheForChannel(channel.Id)
user.Username)} message := model.NewMessage(c.TeamId, channel.Id, c.Session.UserId, model.ACTION_CHANNEL_DELETED)
if _, err := CreatePost(c, post, false); err != nil { PublishAndForget(message)
l4g.Error(utils.T("api.channel.delete_channel.failed_post.error"), err)
c.Err = model.NewLocAppError("deleteChannel", "api.channel.delete_channel.failed_send.app_error", nil, "") post := &model.Post{
return ChannelId: channel.Id,
} Message: fmt.Sprintf(c.T("api.channel.delete_channel.archived"), user.Username),
Type: model.POST_CHANNEL_DELETED,
}
if _, err := CreatePost(c, post, false); err != nil {
l4g.Error(utils.T("api.channel.delete_channel.failed_post.error"), err)
}
}()
result := make(map[string]string) result := make(map[string]string)
result["id"] = channel.Id result["id"] = channel.Id

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

@@ -118,6 +118,10 @@ func (c *WebConn) InvalidateCache() {
c.hasPermissionsToTeam = make(map[string]bool) c.hasPermissionsToTeam = make(map[string]bool)
} }
func (c *WebConn) InvalidateCacheForChannel(channelId string) {
delete(c.hasPermissionsToChannel, channelId)
}
func (c *WebConn) HasPermissionsToTeam(teamId string) bool { func (c *WebConn) HasPermissionsToTeam(teamId string) bool {
perm, ok := c.hasPermissionsToTeam[teamId] perm, ok := c.hasPermissionsToTeam[teamId]
if !ok { if !ok {

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

@@ -10,21 +10,23 @@ import (
) )
type Hub struct { type Hub struct {
connections map[*WebConn]bool connections map[*WebConn]bool
register chan *WebConn register chan *WebConn
unregister chan *WebConn unregister chan *WebConn
broadcast chan *model.Message broadcast chan *model.Message
stop chan string stop chan string
invalidateUser chan string invalidateUser chan string
invalidateChannel chan string
} }
var hub = &Hub{ var hub = &Hub{
register: make(chan *WebConn), register: make(chan *WebConn),
unregister: make(chan *WebConn), unregister: make(chan *WebConn),
connections: make(map[*WebConn]bool), connections: make(map[*WebConn]bool),
broadcast: make(chan *model.Message), broadcast: make(chan *model.Message),
stop: make(chan string), stop: make(chan string),
invalidateUser: make(chan string), invalidateUser: make(chan string),
invalidateChannel: make(chan string),
} }
func PublishAndForget(message *model.Message) { func PublishAndForget(message *model.Message) {
@@ -37,6 +39,10 @@ func InvalidateCacheForUser(userId string) {
hub.invalidateUser <- userId hub.invalidateUser <- userId
} }
func InvalidateCacheForChannel(channelId string) {
hub.invalidateChannel <- channelId
}
func (h *Hub) Register(webConn *WebConn) { func (h *Hub) Register(webConn *WebConn) {
h.register <- webConn h.register <- webConn
} }
@@ -74,6 +80,11 @@ func (h *Hub) Start() {
} }
} }
case channelId := <-h.invalidateChannel:
for webCon := range h.connections {
webCon.InvalidateCacheForChannel(channelId)
}
case msg := <-h.broadcast: case msg := <-h.broadcast:
for webCon := range h.connections { for webCon := range h.connections {
if shouldSendEvent(webCon, msg) { if shouldSendEvent(webCon, msg) {
@@ -136,8 +147,8 @@ func shouldSendEvent(webCon *WebConn, msg *model.Message) bool {
} }
} }
// Only report events to users who are in the channel for the event // Only report events to users who are in the channel for the event execept deleted events
if len(msg.ChannelId) > 0 { if len(msg.ChannelId) > 0 && msg.Action != model.ACTION_CHANNEL_DELETED {
allowed := webCon.HasPermissionsToChannel(msg.ChannelId) allowed := webCon.HasPermissionsToChannel(msg.ChannelId)
if !allowed { if !allowed {

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

@@ -13,6 +13,7 @@ const (
ACTION_POSTED = "posted" ACTION_POSTED = "posted"
ACTION_POST_EDITED = "post_edited" ACTION_POST_EDITED = "post_edited"
ACTION_POST_DELETED = "post_deleted" ACTION_POST_DELETED = "post_deleted"
ACTION_CHANNEL_DELETED = "channel_deleted"
ACTION_CHANNEL_VIEWED = "channel_viewed" ACTION_CHANNEL_VIEWED = "channel_viewed"
ACTION_NEW_USER = "new_user" ACTION_NEW_USER = "new_user"
ACTION_USER_ADDED = "user_added" ACTION_USER_ADDED = "user_added"

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

@@ -16,6 +16,7 @@ const (
POST_SYSTEM_GENERIC = "system_generic" POST_SYSTEM_GENERIC = "system_generic"
POST_JOIN_LEAVE = "system_join_leave" POST_JOIN_LEAVE = "system_join_leave"
POST_HEADER_CHANGE = "system_header_change" POST_HEADER_CHANGE = "system_header_change"
POST_CHANNEL_DELETED = "system_channel_deleted"
POST_EPHEMERAL = "system_ephemeral" POST_EPHEMERAL = "system_ephemeral"
) )

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

@@ -18,6 +18,8 @@ import * as GlobalActions from 'action_creators/global_actions.jsx';
import Constants from 'utils/constants.jsx'; import Constants from 'utils/constants.jsx';
const SocketEvents = Constants.SocketEvents; const SocketEvents = Constants.SocketEvents;
import {browserHistory} from 'react-router';
const MAX_WEBSOCKET_FAILS = 7; const MAX_WEBSOCKET_FAILS = 7;
const WEBSOCKET_RETRY_TIME = 3000; const WEBSOCKET_RETRY_TIME = 3000;
@@ -135,6 +137,10 @@ function handleMessage(msg) {
handleChannelViewedEvent(msg); handleChannelViewedEvent(msg);
break; break;
case SocketEvents.CHANNEL_DELETED:
handleChannelDeletedEvent(msg);
break;
case SocketEvents.PREFERENCE_CHANGED: case SocketEvents.PREFERENCE_CHANGED:
handlePreferenceChangedEvent(msg); handlePreferenceChangedEvent(msg);
break; break;
@@ -234,6 +240,15 @@ function handleChannelViewedEvent(msg) {
} }
} }
function handleChannelDeletedEvent(msg) {
if (ChannelStore.getCurrentId() === msg.channel_id) {
const teamUrl = TeamStore.getCurrentTeamRelativeUrl();
browserHistory.push(teamUrl + '/channels/' + Constants.DEFAULT_CHANNEL);
} else {
AsyncClient.getChannels();
}
}
function handlePreferenceChangedEvent(msg) { function handlePreferenceChangedEvent(msg) {
const preference = JSON.parse(msg.props.preference); const preference = JSON.parse(msg.props.preference);
GlobalActions.emitPreferenceChangedEvent(preference); GlobalActions.emitPreferenceChangedEvent(preference);

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

@@ -20,19 +20,6 @@ function getWindowLocationOrigin() {
class TeamStoreClass extends EventEmitter { class TeamStoreClass extends EventEmitter {
constructor() { constructor() {
super(); super();
this.emitChange = this.emitChange.bind(this);
this.addChangeListener = this.addChangeListener.bind(this);
this.removeChangeListener = this.removeChangeListener.bind(this);
this.get = this.get.bind(this);
this.getByName = this.getByName.bind(this);
this.getAll = this.getAll.bind(this);
this.getCurrentId = this.getCurrentId.bind(this);
this.getCurrent = this.getCurrent.bind(this);
this.getCurrentTeamUrl = this.getCurrentTeamUrl.bind(this);
this.getCurrentInviteLink = this.getCurrentInviteLink.bind(this);
this.saveTeam = this.saveTeam.bind(this);
this.clear(); this.clear();
} }
@@ -104,6 +91,13 @@ class TeamStoreClass extends EventEmitter {
return null; return null;
} }
getCurrentTeamRelativeUrl() {
if (this.getCurrent()) {
return '/' + this.getCurrent().name;
}
return null;
}
getCurrentInviteLink() { getCurrentInviteLink() {
const current = this.getCurrent(); const current = this.getCurrent();

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

@@ -147,6 +147,7 @@ export default {
POSTED: 'posted', POSTED: 'posted',
POST_EDITED: 'post_edited', POST_EDITED: 'post_edited',
POST_DELETED: 'post_deleted', POST_DELETED: 'post_deleted',
CHANNEL_DELETED: 'channel_deleted',
CHANNEL_VIEWED: 'channel_viewed', CHANNEL_VIEWED: 'channel_viewed',
NEW_USER: 'new_user', NEW_USER: 'new_user',
USER_ADDED: 'user_added', USER_ADDED: 'user_added',