Store the last tab in a cookie so the server can use it for embedding state
This commit is contained in:
parent
4cad5a0b33
commit
8684e2dea2
File diff suppressed because one or more lines are too long
@ -44,6 +44,7 @@
|
||||
"fontfaceobserver": "^2.0.9",
|
||||
"history": "4.5.1",
|
||||
"immutable": "^3.8.1",
|
||||
"js-cookie": "^2.1.4",
|
||||
"lodash": "^4.17.4",
|
||||
"react": "^15.4.2",
|
||||
"react-dom": "^15.4.2",
|
||||
|
@ -1,3 +1,4 @@
|
||||
import Cookie from 'js-cookie';
|
||||
import { setEnvironment } from '../actions/environment';
|
||||
import { addMessages } from '../actions/message';
|
||||
import { select, updateSelection } from '../actions/tab';
|
||||
@ -17,12 +18,12 @@ export default function initialState({ store }) {
|
||||
});
|
||||
|
||||
if (!store.getState().router.route) {
|
||||
let tab = localStorage.tab;
|
||||
const tab = Cookie.get('tab');
|
||||
if (tab) {
|
||||
tab = JSON.parse(tab);
|
||||
const [server, name = null] = tab.split(':');
|
||||
|
||||
if (find(env.servers, server => server.host === tab.server)) {
|
||||
store.dispatch(select(tab.server, tab.name, true));
|
||||
if (find(env.servers, srv => srv.host === server)) {
|
||||
store.dispatch(select(server, name, true));
|
||||
} else {
|
||||
store.dispatch(updateSelection());
|
||||
}
|
||||
|
@ -1,10 +1,11 @@
|
||||
import Cookie from 'js-cookie';
|
||||
import debounce from 'lodash/debounce';
|
||||
import observe from '../util/observe';
|
||||
import { getSelectedTab } from '../reducers/tab';
|
||||
|
||||
const saveTab = debounce(tab => {
|
||||
localStorage.tab = JSON.stringify(tab);
|
||||
}, 3000);
|
||||
const saveTab = debounce(tab =>
|
||||
Cookie.set('tab', tab.toString(), { expires: 30 })
|
||||
, 3000);
|
||||
|
||||
export default function storage({ store }) {
|
||||
observe(store, getSelectedTab, tab => {
|
||||
|
@ -12,6 +12,14 @@ class Tab extends TabRecord {
|
||||
isChannel() {
|
||||
return this.name && this.name.charAt(0) === '#';
|
||||
}
|
||||
|
||||
toString() {
|
||||
let str = this.server;
|
||||
if (this.name) {
|
||||
str += `:${this.name}`;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
const State = Record({
|
||||
|
@ -3462,6 +3462,10 @@ js-base64@^2.1.9:
|
||||
version "2.1.9"
|
||||
resolved "https://registry.yarnpkg.com/js-base64/-/js-base64-2.1.9.tgz#f0e80ae039a4bd654b5f281fc93f04a914a7fcce"
|
||||
|
||||
js-cookie@^2.1.4:
|
||||
version "2.1.4"
|
||||
resolved "https://registry.yarnpkg.com/js-cookie/-/js-cookie-2.1.4.tgz#da4ec503866f149d164cf25f579ef31015025d8d"
|
||||
|
||||
js-tokens@^3.0.0:
|
||||
version "3.0.1"
|
||||
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.1.tgz#08e9f132484a2c45a30907e9dc4d5567b7f114d7"
|
||||
|
@ -29,6 +29,32 @@ type indexData struct {
|
||||
Messages *Messages `json:"messages,omitempty"`
|
||||
}
|
||||
|
||||
func (d *indexData) addUsersAndMessages(server, channel string, session *Session) {
|
||||
users := channelStore.GetUsers(server, channel)
|
||||
if len(users) > 0 {
|
||||
d.Users = &Userlist{
|
||||
Server: server,
|
||||
Channel: channel,
|
||||
Users: users,
|
||||
}
|
||||
}
|
||||
|
||||
messages, hasMore, err := session.user.GetLastMessages(server, channel, 50)
|
||||
if err == nil && len(messages) > 0 {
|
||||
m := Messages{
|
||||
Server: server,
|
||||
To: channel,
|
||||
Messages: messages,
|
||||
}
|
||||
|
||||
if hasMore {
|
||||
m.Next = messages[0].ID
|
||||
}
|
||||
|
||||
d.Messages = &m
|
||||
}
|
||||
}
|
||||
|
||||
func getIndexData(r *http.Request, session *Session) *indexData {
|
||||
servers := session.user.GetServers()
|
||||
connections := session.getConnectionStates()
|
||||
@ -60,30 +86,32 @@ func getIndexData(r *http.Request, session *Session) *indexData {
|
||||
|
||||
params := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
|
||||
if len(params) == 2 && isChannel(params[1]) {
|
||||
users := channelStore.GetUsers(params[0], params[1])
|
||||
if len(users) > 0 {
|
||||
data.Users = &Userlist{
|
||||
Server: params[0],
|
||||
Channel: params[1],
|
||||
Users: users,
|
||||
data.addUsersAndMessages(params[0], params[1], session)
|
||||
} else {
|
||||
server, channel := parseTabCookie(r, r.URL.Path)
|
||||
if channel != "" {
|
||||
for _, ch := range channels {
|
||||
if server == ch.Server && channel == ch.Name {
|
||||
data.addUsersAndMessages(server, channel, session)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
messages, hasMore, err := session.user.GetLastMessages(params[0], params[1], 50)
|
||||
if err == nil && len(messages) > 0 {
|
||||
m := Messages{
|
||||
Server: params[0],
|
||||
To: params[1],
|
||||
Messages: messages,
|
||||
}
|
||||
|
||||
if hasMore {
|
||||
m.Next = messages[0].ID
|
||||
}
|
||||
|
||||
data.Messages = &m
|
||||
}
|
||||
}
|
||||
|
||||
return &data
|
||||
}
|
||||
|
||||
func parseTabCookie(r *http.Request, path string) (string, string) {
|
||||
if path == "/" {
|
||||
cookie, err := r.Cookie("tab")
|
||||
if err == nil {
|
||||
tab := strings.Split(cookie.Value, ":")
|
||||
|
||||
if len(tab) == 2 && isChannel(tab[1]) {
|
||||
return tab[0], tab[1]
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", ""
|
||||
}
|
||||
|
@ -66,10 +66,13 @@ func (h *wsHandler) init(r *http.Request) {
|
||||
h.session.numWS(), "WebSocket connections")
|
||||
|
||||
channels := h.session.user.GetChannels()
|
||||
params := strings.Split(strings.Trim(r.URL.Query().Get("path"), "/"), "/")
|
||||
path := r.URL.Query().Get("path")
|
||||
params := strings.Split(strings.Trim(path, "/"), "/")
|
||||
tabServer, tabChannel := parseTabCookie(r, path)
|
||||
|
||||
for _, channel := range channels {
|
||||
if len(params) > 1 && channel.Server == params[0] && channel.Name == params[1] {
|
||||
if (len(params) == 2 && channel.Server == params[0] && channel.Name == params[1]) ||
|
||||
(channel.Server == tabServer && channel.Name == tabChannel) {
|
||||
continue
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user