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»"}, "channel": command{"title description", "create new channel «title» with «description»"},
"secret": command{"", "create secretchat with current user"}, "secret": command{"", "create secretchat with current user"},
"search": command{"string [limit]", "search <string> in current chat"}, "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"}, "block": command{"", "blacklist current user"},
"unblock": command{"", "unblacklist current user"}, "unblock": command{"", "unblacklist current user"},
"invite": command{"id or @username", "add user to current chat"}, "invite": command{"id or @username", "add user to current chat"},
"kick": command{"id or @username", "remove 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"}, "ban": command{"id or @username [hours]", "restrict @username from current chat for [hours] or forever"},
"leave": command{"", "leave current chat"}, "leave": command{"", "leave current chat"},
"close": command{"", "close current secret chat"}, "close": command{"", "close current secret chat"},
"delete": command{"", "delete current chat from chat list"}, "delete": command{"", "delete current chat from chat list"},
//"members": command{"[query]", "search members [by optional query] in current chat (requires admin rights)"}, //"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) { func (c *Client) usernameOrIdToId(username string) (int32, error) {
userID, err := strconv.ParseInt(username, 10, 32) userID, err := strconv.ParseInt(username, 10, 32)
// couldn't parse the id, try to lookup as a username // 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{ messages, err := c.client.SearchChatMessages(&client.SearchChatMessagesRequest{
ChatId: chatID, ChatId: chatID,
Query: query, Query: query,
Limit: limit, Limit: limit,
SenderUserId: c.me.Id, Filter: &client.SearchMessagesFilterEmpty{},
Filter: &client.SearchMessagesFilterEmpty{},
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true
} }
for i := len(messages.Messages) - 1; i >= 0; i-- { c.sendMessagesReverse(chatID, messages.Messages)
gateway.SendMessage( // get latest entries from history
c.jid, case "history":
strconv.FormatInt(chatID, 10), var limit int32 = 10
c.formatMessage(0, 0, false, messages.Messages[i]), if len(args) > 0 {
c.xmpp, 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": case "help":
return helpString(helpTypeChat), true return helpString(helpTypeChat), true
default: default: