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,7 +492,6 @@ 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],
@ -505,10 +499,8 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
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,
@ -516,10 +508,8 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
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,
@ -527,89 +517,74 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
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":
if len(args) < 1 { if len(args) < 1 {
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 { if err != nil {
return err.Error(), true return err.Error(), true
} }
}
// kick @username from current group chat // kick @username from current group chat
case "kick": case "kick":
if len(args) < 1 { if len(args) < 1 {
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":
if len(args) < 1 { if len(args) < 1 {
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
} }
var until int32 var hours int64
if len(args) > 1 { if len(args) > 1 {
hours, err := strconv.ParseInt(args[1], 10, 32) hours, err = strconv.ParseInt(args[1], 10, 32)
if err != nil { if err != nil {
until = int32(time.Now().Unix() + hours*3600) return "Invalid number of hours", true
} }
} }
_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ _, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
ChatID: chatID, ChatID: chatID,
UserID: userID, UserID: contact.ID,
Status: &client.ChatMemberStatusBanned{ Status: c.formatRestrict(true, hours),
BannedUntilDate: until,
},
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true
} }
}
// leave current chat // leave current chat
case "leave": case "leave":
chat, _, err := c.GetContactByID(chatID, nil) _, err := c.client.LeaveChat(&client.LeaveChatRequest{
if err != nil {
return err.Error(), true
}
chatType := chat.Type.ChatTypeType()
if chatType == client.TypeChatTypeBasicGroup || chatType == client.TypeChatTypeSupergroup {
_, err = c.client.LeaveChat(&client.LeaveChatRequest{
ChatID: chatID, ChatID: chatID,
}) })
if err != nil { if err != nil {
@ -617,7 +592,6 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
} }
c.unsubscribe(chatID) 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)