Add /history command

This commit is contained in:
bodqhrohro 2019-12-08 18:19:35 +02:00
parent fb36f53f3a
commit ad1beafeb3

View file

@ -40,7 +40,7 @@ var chatCommands = map[string]command{
"channel": command{"title description", "create new channel «title» with «description»"},
"secret": command{"", "create secretchat with current user"},
"search": command{"string [limit]", "search <string> in current chat"},
//"history": command{"[limit]", "get last [limit] messages from current chat"},
"history": command{"[limit]", "get last [limit] messages from current chat"},
"block": command{"", "blacklist current user"},
"unblock": command{"", "unblacklist current user"},
"invite": command{"id or @username", "add user to current chat"},
@ -122,6 +122,17 @@ func (c *Client) unsubscribe(chatID int64) {
)
}
func (c *Client) sendMessagesReverse(chatID int64, messages []*client.Message) {
for i := len(messages) - 1; i >= 0; i-- {
gateway.SendMessage(
c.jid,
strconv.FormatInt(chatID, 10),
c.formatMessage(0, 0, false, messages[i]),
c.xmpp,
)
}
}
func (c *Client) usernameOrIdToId(username string) (int32, error) {
userID, err := strconv.ParseInt(username, 10, 32)
// couldn't parse the id, try to lookup as a username
@ -602,21 +613,32 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
ChatId: chatID,
Query: query,
Limit: limit,
SenderUserId: c.me.Id,
Filter: &client.SearchMessagesFilterEmpty{},
})
if err != nil {
return err.Error(), true
}
for i := len(messages.Messages) - 1; i >= 0; i-- {
gateway.SendMessage(
c.jid,
strconv.FormatInt(chatID, 10),
c.formatMessage(0, 0, false, messages.Messages[i]),
c.xmpp,
)
c.sendMessagesReverse(chatID, messages.Messages)
// get latest entries from history
case "history":
var limit int32 = 10
if len(args) > 0 {
newLimit, err := strconv.ParseInt(args[0], 10, 32)
if err == nil {
limit = int32(newLimit)
}
}
messages, err := c.client.GetChatHistory(&client.GetChatHistoryRequest{
ChatId: chatID,
Limit: limit,
})
if err != nil {
return err.Error(), true
}
c.sendMessagesReverse(chatID, messages.Messages)
case "help":
return helpString(helpTypeChat), true
default: