Send logged messages when joining a channel

This commit is contained in:
Ken-Håvard Lieng 2017-05-20 23:22:56 +02:00
parent a4e9eeefed
commit 62a69dfd44
3 changed files with 39 additions and 29 deletions

View File

@ -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],

View File

@ -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,

View File

@ -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() {