diff --git a/server/irc_handler.go b/server/irc_handler.go index e33ee7a7..e67acde7 100644 --- a/server/irc_handler.go +++ b/server/irc_handler.go @@ -81,6 +81,8 @@ func (i *ircHandler) join(msg *irc.Message) { channelStore.AddUser(msg.Nick, i.client.Host, msg.Params[0]) if msg.Nick == i.client.GetNick() { + i.session.sendLastMessages(i.client.Host, msg.Params[0], 50) + go i.session.user.AddChannel(storage.Channel{ Server: i.client.Host, Name: msg.Params[0], diff --git a/server/session.go b/server/session.go index f6893cbe..98ba7dd7 100644 --- a/server/session.go +++ b/server/session.go @@ -127,6 +127,41 @@ func (s *Session) sendError(err error, server string) { }) } +func (s *Session) sendLastMessages(server, channel string, count int) { + messages, hasMore, err := s.user.GetLastMessages(server, channel, count) + if err == nil && len(messages) > 0 { + res := Messages{ + Server: server, + To: channel, + Messages: messages, + } + + if hasMore { + res.Next = messages[0].ID + } + + s.sendJSON("messages", res) + } +} + +func (s *Session) sendMessages(server, channel string, count int, fromID string) { + messages, hasMore, err := s.user.GetMessages(server, channel, count, fromID) + if err == nil && len(messages) > 0 { + res := Messages{ + Server: server, + To: channel, + Messages: messages, + Prepend: true, + } + + if hasMore { + res.Next = messages[0].ID + } + + s.sendJSON("messages", res) + } +} + func (s *Session) print(server string, a ...interface{}) { s.sendJSON("print", Message{ Server: server, diff --git a/server/websocket_handler.go b/server/websocket_handler.go index c10844ce..88236dd8 100644 --- a/server/websocket_handler.go +++ b/server/websocket_handler.go @@ -79,20 +79,7 @@ func (h *wsHandler) init(r *http.Request) { Users: channelStore.GetUsers(channel.Server, channel.Name), }) - messages, hasMore, err := h.session.user.GetLastMessages(channel.Server, channel.Name, 50) - if err == nil && len(messages) > 0 { - res := Messages{ - Server: channel.Server, - To: channel.Name, - Messages: messages, - } - - if hasMore { - res.Next = messages[0].ID - } - - h.session.sendJSON("messages", res) - } + h.session.sendLastMessages(channel.Server, channel.Name, 50) } } @@ -281,21 +268,7 @@ func (h *wsHandler) fetchMessages(b []byte) { var data FetchMessages json.Unmarshal(b, &data) - messages, hasMore, err := h.session.user.GetMessages(data.Server, data.Channel, 200, data.Next) - if err == nil && len(messages) > 0 { - res := Messages{ - Server: data.Server, - To: data.Channel, - Messages: messages, - Prepend: true, - } - - if hasMore { - res.Next = messages[0].ID - } - - h.session.sendJSON("messages", res) - } + h.session.sendMessages(data.Server, data.Channel, 200, data.Next) } func (h *wsHandler) initHandlers() {