Refactor some chat commands

This commit is contained in:
Bohdan Horbeshko 2022-01-17 14:58:16 -05:00
parent e260668f91
commit 0b79d6feb1
2 changed files with 83 additions and 93 deletions

View file

@ -480,12 +480,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
} }
// create new secret chat with current user // create new secret chat with current user
case "secret": case "secret":
_, user, err := c.GetContactByID(chatID, nil) _, err := c.client.CreateNewSecretChat(&client.CreateNewSecretChatRequest{
if err != nil || user == nil {
return "User not found", true
}
_, err = c.client.CreateNewSecretChat(&client.CreateNewSecretChatRequest{
UserID: chatID, UserID: chatID,
}) })
if err != nil { if err != nil {
@ -497,36 +492,30 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
return notEnoughArguments, true return notEnoughArguments, true
} }
if chatID > 0 { _, err := c.client.CreateNewBasicGroupChat(&client.CreateNewBasicGroupChatRequest{
_, err := c.client.CreateNewBasicGroupChat(&client.CreateNewBasicGroupChatRequest{ UserIDs: []int64{chatID},
UserIDs: []int64{chatID}, Title: args[0],
Title: args[0], })
}) if err != nil {
if err != nil { return err.Error(), true
return err.Error(), true
}
} }
// blacklists current user // blacklists current user
case "block": case "block":
if chatID > 0 { _, err := c.client.ToggleMessageSenderIsBlocked(&client.ToggleMessageSenderIsBlockedRequest{
_, err := c.client.ToggleMessageSenderIsBlocked(&client.ToggleMessageSenderIsBlockedRequest{ Sender: &client.MessageSenderUser{UserID: chatID},
Sender: &client.MessageSenderUser{UserID: chatID}, IsBlocked: true,
IsBlocked: true, })
}) if err != nil {
if err != nil { return err.Error(), true
return err.Error(), true
}
} }
// unblacklists current user // unblacklists current user
case "unblock": case "unblock":
if chatID > 0 { _, err := c.client.ToggleMessageSenderIsBlocked(&client.ToggleMessageSenderIsBlockedRequest{
_, err := c.client.ToggleMessageSenderIsBlocked(&client.ToggleMessageSenderIsBlockedRequest{ Sender: &client.MessageSenderUser{UserID: chatID},
Sender: &client.MessageSenderUser{UserID: chatID}, IsBlocked: false,
IsBlocked: false, })
}) if err != nil {
if err != nil { return err.Error(), true
return err.Error(), true
}
} }
// invite @username to current groupchat // invite @username to current groupchat
case "invite": case "invite":
@ -534,19 +523,18 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
return notEnoughArguments, true return notEnoughArguments, true
} }
if chatID < 0 { contact, _, err := c.GetContactByUsername(args[0])
userID, err := c.usernameOrIDToID(args[0]) if err != nil {
if err != nil { return err.Error(), true
return err.Error(), true }
}
_, err = c.client.AddChatMember(&client.AddChatMemberRequest{ _, err = c.client.AddChatMember(&client.AddChatMemberRequest{
ChatID: chatID, ChatID: chatID,
UserID: userID, UserID: contact.ID,
}) ForwardLimit: 100,
if err != nil { })
return err.Error(), true if err != nil {
} return err.Error(), true
} }
// kick @username from current group chat // kick @username from current group chat
case "kick": case "kick":
@ -554,20 +542,18 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
return notEnoughArguments, true return notEnoughArguments, true
} }
if chatID < 0 { contact, _, err := c.GetContactByUsername(args[0])
userID, err := c.usernameOrIDToID(args[0]) if err != nil {
if err != nil { return err.Error(), true
return err.Error(), true }
}
_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ _, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
ChatID: chatID, ChatID: chatID,
UserID: userID, UserID: contact.ID,
Status: &client.ChatMemberStatusLeft{}, Status: c.formatRestrict(false, 0),
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true
}
} }
// ban @username from current chat [for N hours] // ban @username from current chat [for N hours]
case "ban": case "ban":
@ -575,49 +561,37 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
return notEnoughArguments, true return notEnoughArguments, true
} }
if chatID < 0 { contact, _, err := c.GetContactByUsername(args[0])
userID, err := c.usernameOrIDToID(args[0])
if err != nil {
return err.Error(), true
}
var until int32
if len(args) > 1 {
hours, err := strconv.ParseInt(args[1], 10, 32)
if err != nil {
until = int32(time.Now().Unix() + hours*3600)
}
}
_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
ChatID: chatID,
UserID: userID,
Status: &client.ChatMemberStatusBanned{
BannedUntilDate: until,
},
})
if err != nil {
return err.Error(), true
}
}
// leave current chat
case "leave":
chat, _, err := c.GetContactByID(chatID, nil)
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true
} }
chatType := chat.Type.ChatTypeType() var hours int64
if chatType == client.TypeChatTypeBasicGroup || chatType == client.TypeChatTypeSupergroup { if len(args) > 1 {
_, err = c.client.LeaveChat(&client.LeaveChatRequest{ hours, err = strconv.ParseInt(args[1], 10, 32)
ChatID: chatID,
})
if err != nil { if err != nil {
return err.Error(), true return "Invalid number of hours", true
} }
c.unsubscribe(chatID)
} }
_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
ChatID: chatID,
UserID: contact.ID,
Status: c.formatRestrict(true, hours),
})
if err != nil {
return err.Error(), true
}
// leave current chat
case "leave":
_, err := c.client.LeaveChat(&client.LeaveChatRequest{
ChatID: chatID,
})
if err != nil {
return err.Error(), true
}
c.unsubscribe(chatID)
// close secret chat // close secret chat
case "close": case "close":
chat, _, err := c.GetContactByID(chatID, nil) chat, _, err := c.GetContactByID(chatID, nil)
@ -642,6 +616,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
_, err := c.client.DeleteChatHistory(&client.DeleteChatHistoryRequest{ _, err := c.client.DeleteChatHistory(&client.DeleteChatHistoryRequest{
ChatID: chatID, ChatID: chatID,
RemoveFromChatList: true, RemoveFromChatList: true,
Revoke: true,
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true
@ -650,7 +625,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
c.unsubscribe(chatID) c.unsubscribe(chatID)
// message search // message search
case "search": case "search":
var limit int32 = 10 var limit int32 = 100
if len(args) > 1 { if len(args) > 1 {
newLimit, err := strconv.ParseInt(args[1], 10, 32) newLimit, err := strconv.ParseInt(args[1], 10, 32)
if err == nil { if err == nil {
@ -697,7 +672,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
} }
c.sendMessagesReverse(chatID, messages.Messages) c.sendMessagesReverse(chatID, messages.Messages)
// members list (for admins) // chat members
case "members": case "members":
var query string var query string
if len(args) > 0 { if len(args) > 0 {

View file

@ -312,6 +312,21 @@ func (c *Client) formatContent(file *client.File, filename string) string {
) )
} }
func (c *Client) formatRestrict(ban bool, hours int64) client.ChatMemberStatus {
var until int32
if hours != 0 {
until = int32(time.Now().Unix() + hours*3600)
}
if ban {
return &client.ChatMemberStatusBanned{
BannedUntilDate: until,
}
} else {
return &client.ChatMemberStatusLeft{}
}
}
func (c *Client) messageToText(message *client.Message) string { func (c *Client) messageToText(message *client.Message) string {
if message.Content == nil { if message.Content == nil {
log.Warnf("Unknown message (big emoji?): %#v", message) log.Warnf("Unknown message (big emoji?): %#v", message)