Skip to content

Commit

Permalink
GoTGProto v1.0.0-beta06
Browse files Browse the repository at this point in the history
- Made `ctx.ResolveUsername` more handy
- Moved `EffectiveChat` interface to `types` package
  • Loading branch information
celestix committed Jul 30, 2022
1 parent e918377 commit 191c68c
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 127 deletions.
2 changes: 1 addition & 1 deletion clientHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var (
Sender *message.Sender
)

const VERSION = "v1.0.0-beta05"
const VERSION = "v1.0.0-beta06"

type ClientHelper struct {
// Unique Telegram Application ID, get it from https://my.telegram.org/apps.
Expand Down
34 changes: 26 additions & 8 deletions ext/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package ext

import (
"context"
"strings"
"time"

"github.com/anonyindian/gotgproto/functions"
"github.com/anonyindian/gotgproto/storage"
"github.com/anonyindian/gotgproto/types"
"github.com/gotd/td/telegram/message"
"github.com/gotd/td/telegram/message/entity"
"github.com/gotd/td/telegram/message/styling"
Expand Down Expand Up @@ -219,10 +221,12 @@ func (ctx *Context) GetUser(userId int64) (*tg.UserFull, error) {
}
}

// GetMessages is used to fetch messages from a PM (Private Chat).
func (ctx *Context) GetMessages(messageIds []tg.InputMessageClass) ([]tg.MessageClass, error) {
return functions.GetMessages(ctx, ctx.Client, messageIds)
}

// BanChatMember is used to ban a user from a chat.
func (ctx *Context) BanChatMember(chatId, userId int64, untilDate int) (tg.UpdatesClass, error) {
peerChatStorage := storage.GetPeerById(chatId)
if peerChatStorage.ID == 0 {
Expand Down Expand Up @@ -250,6 +254,7 @@ func (ctx *Context) BanChatMember(chatId, userId int64, untilDate int) (tg.Updat
}, untilDate)
}

// UnbanChatMember is used to unban a user from a chat.
func (ctx *Context) UnbanChatMember(chatId, userId int64, _ int) (bool, error) {
peerChatStorage := storage.GetPeerById(chatId)
if peerChatStorage.ID == 0 {
Expand All @@ -275,6 +280,7 @@ func (ctx *Context) UnbanChatMember(chatId, userId int64, _ int) (bool, error) {
})
}

// AddChatMembers is used to add members to a chat
func (ctx *Context) AddChatMembers(chatId int64, userIds []int64, forwardLimit int) (bool, error) {
peerChatStorage := storage.GetPeerById(chatId)
if peerChatStorage.ID == 0 {
Expand Down Expand Up @@ -311,6 +317,11 @@ func (ctx *Context) AddChatMembers(chatId int64, userIds []int64, forwardLimit i
return functions.AddChatMembers(ctx, ctx.Client, chatPeer, userPeers, forwardLimit)
}

// ArchiveChats invokes method folders.editPeerFolders#6847d0ab returning error if any.
// Edit peers in peer folder¹
//
// Links:
// 1) https://core.telegram.org/api/folders#peer-folders
func (ctx *Context) ArchiveChats(chatIds []int64) (bool, error) {
chatPeers := make([]tg.InputPeerClass, len(chatIds))
for i, chatId := range chatIds {
Expand Down Expand Up @@ -338,6 +349,11 @@ func (ctx *Context) ArchiveChats(chatIds []int64) (bool, error) {
return functions.ArchiveChats(ctx, ctx.Client, chatPeers)
}

// UnarchiveChats invokes method folders.editPeerFolders#6847d0ab returning error if any.
// Edit peers in peer folder¹
//
// Links:
// 1) https://core.telegram.org/api/folders#peer-folders
func (ctx *Context) UnarchiveChats(chatIds []int64) (bool, error) {
chatPeers := make([]tg.InputPeerClass, len(chatIds))
for i, chatId := range chatIds {
Expand Down Expand Up @@ -365,10 +381,16 @@ func (ctx *Context) UnarchiveChats(chatIds []int64) (bool, error) {
return functions.UnarchiveChats(ctx, ctx.Client, chatPeers)
}

// CreateChannel invokes method channels.createChannel#3d5fb10f returning error if any.
// Create a supergroup/channel¹.
//
// Links:
// 1) https://core.telegram.org/api/channel
func (ctx *Context) CreateChannel(title, about string, broadcast bool) (tg.UpdatesClass, error) {
return functions.CreateChannel(ctx, ctx.Client, title, about, broadcast)
}

// CreateChat invokes method messages.createChat#9cb126e returning error if any. Creates a new chat.
func (ctx *Context) CreateChat(title string, userIds []int64) (tg.UpdatesClass, error) {
userPeers := make([]tg.InputUserClass, len(userIds))
for i, uId := range userIds {
Expand All @@ -387,14 +409,10 @@ func (ctx *Context) CreateChat(title string, userIds []int64) (tg.UpdatesClass,
return functions.CreateChat(ctx, ctx.Client, title, userPeers)
}

// TODO: Add documentation

func (ctx *Context) ResolveUsername(username string) (tg.PeerClass, error) {
peer, err := ctx.Client.ContactsResolveUsername(ctx, username)
if err != nil {
return nil, err
}
return peer.Peer, nil
// ResolveUsername invokes method contacts.resolveUsername#f93ccba3 returning error if any.
// Resolve a @username to get peer info
func (ctx *Context) ResolveUsername(username string) (types.EffectiveChat, error) {
return functions.ExtractContactResolvedPeer(ctx.Client.ContactsResolveUsername(ctx, strings.TrimPrefix(username, "@")))
}

// ExportSessionString returns session of authorized account in the form of string.
Expand Down
164 changes: 46 additions & 118 deletions ext/update.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package ext

import (
"fmt"

"github.com/anonyindian/gotgproto/types"
"github.com/gotd/td/telegram/message"
"github.com/gotd/td/tg"
)
Expand Down Expand Up @@ -32,6 +35,11 @@ func GetNewUpdate(e *tg.Entities, update tg.UpdateClass) *Update {
Entities: e,
}
switch update := update.(type) {
case *tg.UpdateNewMessage:
m, ok := update.GetMessage().(*tg.Message)
if ok {
u.EffectiveMessage = m
}
case message.AnswerableMessageUpdate:
m, ok := update.GetMessage().(*tg.Message)
if ok {
Expand Down Expand Up @@ -137,133 +145,53 @@ func (u *Update) GetChannel() *tg.Channel {
return u.Entities.Channels[c.ChannelID]
}

// EffectiveChat returns the responsible tg.ChatClass for the current update.
func (u *Update) EffectiveChat() tg.ChatClass {
if c := u.GetChannel(); c != nil {
return c
} else if c := u.GetChat(); c != nil {
return c
// GetUserChat returns the responsible tg.User for the current update.
func (u *Update) GetUserChat() *tg.User {
if u.Entities == nil {
return nil
}
return &tg.ChatEmpty{}
var (
peer tg.PeerClass
)
switch {
case u.EffectiveMessage != nil:
peer = u.EffectiveMessage.PeerID
case u.CallbackQuery != nil:
peer = u.CallbackQuery.Peer
case u.ChatJoinRequest != nil:
peer = u.ChatJoinRequest.Peer
case u.ChatParticipant != nil:
peer = &tg.PeerChat{ChatID: u.ChatParticipant.ChatID}
}
if peer == nil {
return nil
}
c, ok := peer.(*tg.PeerUser)
if !ok {
return nil
}
return u.Entities.Users[c.UserID]
}

func (u *Update) GetUnitedChat() UnitedChat {
// EffectiveChat returns the responsible EffectiveChat for the current update.
func (u *Update) EffectiveChat() types.EffectiveChat {
if c := u.GetChannel(); c != nil {
cn := Channel(*c)
cn := types.Channel(*c)
return &cn
} else if c := u.GetChat(); c != nil {
cn := Chat(*c)
cn := types.Chat(*c)
return &cn
} else if c := u.EffectiveUser(); c != nil {
cn := User(*c)
} else if c := u.GetUserChat(); c != nil {
cn := types.User(*c)
return &cn
}
return &EmptyUC{}
}

type UnitedChat interface {
GetID() int64
GetAccessHash() int64
IsAChannel() bool
IsAChat() bool
IsAUser() bool
}

type EmptyUC struct{}

func (*EmptyUC) GetID() int64 {
return 0
}
func (*EmptyUC) GetAccessHash() int64 {
return 0
}
func (*EmptyUC) IsAChannel() bool {
return false
}
func (*EmptyUC) IsAChat() bool {
return false
}
func (*EmptyUC) IsAUser() bool {
return false
}

type User tg.User

func (u *User) GetID() int64 {
return u.ID
}

func (u *User) GetAccessHash() int64 {
return u.AccessHash
}

func (u *User) IsAChannel() bool {
return false
}

func (u *User) IsAChat() bool {
return false
}

func (u *User) IsAUser() bool {
return true
}

func (u *User) Raw() *tg.User {
us := tg.User(*u)
return &us
}

type Channel tg.Channel

func (u *Channel) GetID() int64 {
return u.ID
}

func (u *Channel) GetAccessHash() int64 {
return u.AccessHash
}

func (u *Channel) IsAChannel() bool {
return true
}

func (u *Channel) IsAChat() bool {
return false
}

func (u *Channel) IsAUser() bool {
return false
}

func (u *Channel) Raw() *tg.Channel {
us := tg.Channel(*u)
return &us
}

type Chat tg.Chat

func (u *Chat) GetID() int64 {
return u.ID
}

func (u *Chat) GetAccessHash() int64 {
return 0
}

func (u *Chat) IsAChannel() bool {
return true
}

func (u *Chat) IsAChat() bool {
return false
}

func (u *Chat) IsAUser() bool {
return false
return &types.EmptyUC{}
}

func (u *Chat) Raw() *tg.Chat {
us := tg.Chat(*u)
return &us
// GetUnitedChat returns EffectiveChat interface fot the current update.
//
// Note: This method is deprecated, please use u.EffectiveChat instead.
func (u *Update) GetUnitedChat() types.EffectiveChat {
fmt.Println("[GOTGPROTO][WARNING]: GetUnitedChat method is deprecated, please use EffectiveChat instead.")
return u.EffectiveChat()
}
1 change: 1 addition & 0 deletions functions/messageHelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package functions

import (
"context"

"github.com/gotd/td/tg"
)

Expand Down
47 changes: 47 additions & 0 deletions functions/peerHelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,57 @@ package functions

import (
"context"
"errors"

"github.com/anonyindian/gotgproto/storage"
"github.com/anonyindian/gotgproto/types"
"github.com/gotd/td/tg"
)

func ExtractContactResolvedPeer(p *tg.ContactsResolvedPeer, err error) (types.EffectiveChat, error) {
if err != nil {
return &types.EmptyUC{}, err
}
go func() {
for _, chat := range p.Chats {
switch chat := chat.(type) {
case *tg.Channel:
storage.AddPeer(chat.ID, chat.AccessHash, storage.TypeChannel, chat.Username)
}
}
for _, user := range p.Users {
user, ok := user.(*tg.User)
if !ok {
continue
}
storage.AddPeer(user.ID, user.AccessHash, storage.TypeUser, user.Username)
}
}()
switch p.Peer.(type) {
case *tg.PeerChannel:
if p.Chats == nil || len(p.Chats) == 0 {
return &types.EmptyUC{}, errors.New("peer info not found in the resolved Chats")
}
switch chat := p.Chats[0].(type) {
case *tg.Channel:
var c = types.Channel(*chat)
return &c, nil
case *tg.ChannelForbidden:
return &types.EmptyUC{}, errors.New("peer could not be resolved because Channel Forbidden")
}
case *tg.PeerUser:
if p.Users == nil || len(p.Users) == 0 {
return &types.EmptyUC{}, errors.New("peer info not found in the resolved Chats")
}
switch user := p.Users[0].(type) {
case *tg.User:
var c = types.User(*user)
return &c, nil
}
}
return &types.EmptyUC{}, errors.New("contact not found")
}

// GetChatIdFromPeer returns the chat/user id from the provided tg.PeerClass.
func GetChatIdFromPeer(peer tg.PeerClass) int64 {
switch peer := peer.(type) {
Expand Down
Loading

0 comments on commit 191c68c

Please sign in to comment.