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
case "secret":
_, user, err := c.GetContactByID(chatID, nil)
if err != nil || user == nil {
return "User not found", true
}
_, err = c.client.CreateNewSecretChat(&client.CreateNewSecretChatRequest{
_, err := c.client.CreateNewSecretChat(&client.CreateNewSecretChatRequest{
UserID: chatID,
})
if err != nil {
@ -497,36 +492,30 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
return notEnoughArguments, true
}
if chatID > 0 {
_, err := c.client.CreateNewBasicGroupChat(&client.CreateNewBasicGroupChatRequest{
UserIDs: []int64{chatID},
Title: args[0],
})
if err != nil {
return err.Error(), true
}
_, err := c.client.CreateNewBasicGroupChat(&client.CreateNewBasicGroupChatRequest{
UserIDs: []int64{chatID},
Title: args[0],
})
if err != nil {
return err.Error(), true
}
// blacklists current user
case "block":
if chatID > 0 {
_, err := c.client.ToggleMessageSenderIsBlocked(&client.ToggleMessageSenderIsBlockedRequest{
Sender: &client.MessageSenderUser{UserID: chatID},
IsBlocked: true,
})
if err != nil {
return err.Error(), true
}
_, err := c.client.ToggleMessageSenderIsBlocked(&client.ToggleMessageSenderIsBlockedRequest{
Sender: &client.MessageSenderUser{UserID: chatID},
IsBlocked: true,
})
if err != nil {
return err.Error(), true
}
// unblacklists current user
case "unblock":
if chatID > 0 {
_, err := c.client.ToggleMessageSenderIsBlocked(&client.ToggleMessageSenderIsBlockedRequest{
Sender: &client.MessageSenderUser{UserID: chatID},
IsBlocked: false,
})
if err != nil {
return err.Error(), true
}
_, err := c.client.ToggleMessageSenderIsBlocked(&client.ToggleMessageSenderIsBlockedRequest{
Sender: &client.MessageSenderUser{UserID: chatID},
IsBlocked: false,
})
if err != nil {
return err.Error(), true
}
// invite @username to current groupchat
case "invite":
@ -534,19 +523,18 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
return notEnoughArguments, true
}
if chatID < 0 {
userID, err := c.usernameOrIDToID(args[0])
if err != nil {
return err.Error(), true
}
contact, _, err := c.GetContactByUsername(args[0])
if err != nil {
return err.Error(), true
}
_, err = c.client.AddChatMember(&client.AddChatMemberRequest{
ChatID: chatID,
UserID: userID,
})
if err != nil {
return err.Error(), true
}
_, err = c.client.AddChatMember(&client.AddChatMemberRequest{
ChatID: chatID,
UserID: contact.ID,
ForwardLimit: 100,
})
if err != nil {
return err.Error(), true
}
// kick @username from current group chat
case "kick":
@ -554,20 +542,18 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
return notEnoughArguments, true
}
if chatID < 0 {
userID, err := c.usernameOrIDToID(args[0])
if err != nil {
return err.Error(), true
}
contact, _, err := c.GetContactByUsername(args[0])
if err != nil {
return err.Error(), true
}
_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
ChatID: chatID,
UserID: userID,
Status: &client.ChatMemberStatusLeft{},
})
if err != nil {
return err.Error(), true
}
_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
ChatID: chatID,
UserID: contact.ID,
Status: c.formatRestrict(false, 0),
})
if err != nil {
return err.Error(), true
}
// ban @username from current chat [for N hours]
case "ban":
@ -575,49 +561,37 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
return notEnoughArguments, true
}
if chatID < 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)
contact, _, err := c.GetContactByUsername(args[0])
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,
})
var hours int64
if len(args) > 1 {
hours, err = strconv.ParseInt(args[1], 10, 32)
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
case "close":
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{
ChatID: chatID,
RemoveFromChatList: true,
Revoke: true,
})
if err != nil {
return err.Error(), true
@ -650,7 +625,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
c.unsubscribe(chatID)
// message search
case "search":
var limit int32 = 10
var limit int32 = 100
if len(args) > 1 {
newLimit, err := strconv.ParseInt(args[1], 10, 32)
if err == nil {
@ -697,7 +672,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
}
c.sendMessagesReverse(chatID, messages.Messages)
// members list (for admins)
// chat members
case "members":
var query string
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 {
if message.Content == nil {
log.Warnf("Unknown message (big emoji?): %#v", message)