diff options
author | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2022-01-06 15:13:57 +0300 |
---|---|---|
committer | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2022-01-06 15:13:57 +0300 |
commit | ee6653c0c6c09a5a38d1c1ef2be5318edfbe2e14 (patch) | |
tree | db8b5d9b0a0642b0e930e7a1caa8b34a3df0a551 | |
parent | 570601d1b8aaaa82b4e618d84a9a95095ed5791a (diff) |
/d / /s / /search commands refactoring
-rw-r--r-- | telegram/commands.go | 27 | ||||
-rw-r--r-- | telegram/utils.go | 10 |
2 files changed, 16 insertions, 21 deletions
diff --git a/telegram/commands.go b/telegram/commands.go index 56945bc..cf4ebc8 100644 --- a/telegram/commands.go +++ b/telegram/commands.go @@ -328,7 +328,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) cmd, args := parseCommand(cmdline) switch cmd { - // delete last message(s) + // delete message case "d": if c.me == nil { return "@me is not initialized", true @@ -345,12 +345,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) limit = 1 } - messages, err := c.client.SearchChatMessages(&client.SearchChatMessagesRequest{ - ChatID: chatID, - Limit: limit, - Sender: &client.MessageSenderUser{UserID: c.me.ID}, - Filter: &client.SearchMessagesFilterEmpty{}, - }) + messages, err := c.getLastMessages(chatID, "", c.me.ID, limit) if err != nil { return err.Error(), true } @@ -371,7 +366,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) if err != nil { return err.Error(), true } - // edit last message + // edit message case "s": if c.me == nil { return "@me is not initialized", true @@ -384,12 +379,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) return err.Error(), true } - messages, err := c.client.SearchChatMessages(&client.SearchChatMessagesRequest{ - ChatID: chatID, - Limit: 1, - Sender: &client.MessageSenderUser{UserID: c.me.ID}, - Filter: &client.SearchMessagesFilterEmpty{}, - }) + messages, err := c.getLastMessages(chatID, "", c.me.ID, 1) if err != nil { return err.Error(), true } @@ -640,7 +630,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) } c.unsubscribe(chatID) - // search messages within current chat + // message search case "search": var limit int32 = 10 if len(args) > 1 { @@ -655,12 +645,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) query = args[0] } - messages, err := c.client.SearchChatMessages(&client.SearchChatMessagesRequest{ - ChatID: chatID, - Query: query, - Limit: limit, - Filter: &client.SearchMessagesFilterEmpty{}, - }) + messages, err := c.getLastMessages(chatID, query, 0, limit) if err != nil { return err.Error(), true } diff --git a/telegram/utils.go b/telegram/utils.go index 4c73f9e..bddffc8 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -616,3 +616,13 @@ func (c *Client) roster(resource string) { c.addResource(resource) } + +func (c *Client) getLastMessages(id int64, query string, from int64, count int32) (*client.Messages, error) { + return c.client.SearchChatMessages(&client.SearchChatMessagesRequest{ + ChatID: id, + Query: query, + Sender: &client.MessageSenderUser{UserID: from}, + Filter: &client.SearchMessagesFilterEmpty{}, + Limit: count, + }) +} |