Add /history command

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

View file

@ -40,15 +40,15 @@ 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"},
"block": command{"", "blacklist current user"},
"unblock": command{"", "unblacklist current user"},
"invite": command{"id or @username", "add user to current chat"},
"kick": command{"id or @username", "remove user to current chat"},
"ban": command{"id or @username [hours]", "restrict @username from current chat for [hours] or forever"},
"leave": command{"", "leave current chat"},
"close": command{"", "close current secret chat"},
"delete": command{"", "delete current chat from chat list"},
"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"},
"kick": command{"id or @username", "remove user to current chat"},
"ban": command{"id or @username [hours]", "restrict @username from current chat for [hours] or forever"},
"leave": command{"", "leave current chat"},
"close": command{"", "close current secret chat"},
"delete": command{"", "delete current chat from chat list"},
//"members": command{"[query]", "search members [by optional query] in current chat (requires admin rights)"},
}
@ -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
@ -599,24 +610,35 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
}
messages, err := c.client.SearchChatMessages(&client.SearchChatMessagesRequest{
ChatId: chatID,
Query: query,
Limit: limit,
SenderUserId: c.me.Id,
Filter: &client.SearchMessagesFilterEmpty{},
ChatId: chatID,
Query: query,
Limit: limit,
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: