/d / /s / /search commands refactoring

This commit is contained in:
Bohdan Horbeshko 2022-01-06 07:13:57 -05:00
parent 570601d1b8
commit ee6653c0c6
2 changed files with 16 additions and 21 deletions

View file

@ -328,7 +328,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
cmd, args := parseCommand(cmdline) cmd, args := parseCommand(cmdline)
switch cmd { switch cmd {
// delete last message(s) // delete message
case "d": case "d":
if c.me == nil { if c.me == nil {
return "@me is not initialized", true return "@me is not initialized", true
@ -345,12 +345,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
limit = 1 limit = 1
} }
messages, err := c.client.SearchChatMessages(&client.SearchChatMessagesRequest{ messages, err := c.getLastMessages(chatID, "", c.me.ID, limit)
ChatID: chatID,
Limit: limit,
Sender: &client.MessageSenderUser{UserID: c.me.ID},
Filter: &client.SearchMessagesFilterEmpty{},
})
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true
} }
@ -371,7 +366,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true
} }
// edit last message // edit message
case "s": case "s":
if c.me == nil { if c.me == nil {
return "@me is not initialized", true return "@me is not initialized", true
@ -384,12 +379,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
return err.Error(), true return err.Error(), true
} }
messages, err := c.client.SearchChatMessages(&client.SearchChatMessagesRequest{ messages, err := c.getLastMessages(chatID, "", c.me.ID, 1)
ChatID: chatID,
Limit: 1,
Sender: &client.MessageSenderUser{UserID: c.me.ID},
Filter: &client.SearchMessagesFilterEmpty{},
})
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true
} }
@ -640,7 +630,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
} }
c.unsubscribe(chatID) c.unsubscribe(chatID)
// search messages within current chat // message search
case "search": case "search":
var limit int32 = 10 var limit int32 = 10
if len(args) > 1 { if len(args) > 1 {
@ -655,12 +645,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
query = args[0] query = args[0]
} }
messages, err := c.client.SearchChatMessages(&client.SearchChatMessagesRequest{ messages, err := c.getLastMessages(chatID, query, 0, limit)
ChatID: chatID,
Query: query,
Limit: limit,
Filter: &client.SearchMessagesFilterEmpty{},
})
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true
} }

View file

@ -616,3 +616,13 @@ func (c *Client) roster(resource string) {
c.addResource(resource) 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,
})
}