aboutsummaryrefslogtreecommitdiff
path: root/telegram/utils.go
diff options
context:
space:
mode:
Diffstat (limited to 'telegram/utils.go')
-rw-r--r--telegram/utils.go46
1 files changed, 41 insertions, 5 deletions
diff --git a/telegram/utils.go b/telegram/utils.go
index 7ab5765..2c8b00d 100644
--- a/telegram/utils.go
+++ b/telegram/utils.go
@@ -53,6 +53,18 @@ var replyRegex = regexp.MustCompile("\\A>>? ?([0-9]+)\\n")
const newlineChar string = "\n"
const messageHeaderSeparator string = " | " // no hrunicode allowed here yet
+// ChatType is an enum of chat types, roughly corresponding to TDLib's one but better
+type ChatType int
+
+const (
+ ChatTypeUnknown ChatType = iota
+ ChatTypePrivate
+ ChatTypeBasicGroup
+ ChatTypeSupergroup
+ ChatTypeSecret
+ ChatTypeChannel
+)
+
// GetContactByUsername resolves username to user id retrieves user and chat information
func (c *Client) GetContactByUsername(username string) (*client.Chat, *client.User, error) {
if !c.Online() {
@@ -130,10 +142,10 @@ func (c *Client) GetContactByID(id int64, chat *client.Chat) (*client.Chat, *cli
return chat, user, nil
}
-// IsPM checks if a chat is PM
-func (c *Client) IsPM(id int64) (bool, error) {
+// GetChatType obtains chat type from its information
+func (c *Client) GetChatType(id int64) (ChatType, error) {
if !c.Online() || id == 0 {
- return false, errOffline
+ return ChatTypeUnknown, errOffline
}
var err error
@@ -144,14 +156,38 @@ func (c *Client) IsPM(id int64) (bool, error) {
ChatId: id,
})
if err != nil {
- return false, err
+ return ChatTypeUnknown, err
}
c.cache.SetChat(id, chat)
}
chatType := chat.Type.ChatTypeType()
- if chatType == client.TypeChatTypePrivate || chatType == client.TypeChatTypeSecret {
+ if chatType == client.TypeChatTypePrivate {
+ return ChatTypePrivate, nil
+ } else if chatType == client.TypeChatTypeBasicGroup {
+ return ChatTypeBasicGroup, nil
+ } else if chatType == client.TypeChatTypeSupergroup {
+ supergroup, _ := chat.Type.(*client.ChatTypeSupergroup)
+ if supergroup.IsChannel {
+ return ChatTypeChannel, nil
+ }
+ return ChatTypeSupergroup, nil
+ } else if chatType == client.TypeChatTypeSecret {
+ return ChatTypeSecret, nil
+ }
+
+ return ChatTypeUnknown, errors.New("Unknown chat type")
+}
+
+// IsPM checks if a chat is PM
+func (c *Client) IsPM(id int64) (bool, error) {
+ typ, err := c.GetChatType(id)
+ if err != nil {
+ return false, err
+ }
+
+ if typ == ChatTypePrivate || typ == ChatTypeSecret {
return true, nil
}
return false, nil