Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improved sessionName + NewClient + InMemorySessionName #30

Merged
merged 4 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 80 additions & 11 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,72 @@ func NewClient(appId int, apiHash string, cType ClientType, opts *ClientOpts) (*
}
}

var sessionStorage telegram.SessionStorage
if opts.Session == nil || opts.Session.GetName() == sessionMaker.InMemorySessionName {
sessionStorage = &session.StorageMemory{}
storage.Load("", true)
} else {
sessionStorage = &sessionMaker.SessionStorage{
Session: opts.Session,
}
}

d := dispatcher.NewNativeDispatcher(opts.AutoFetchReply)

// client := telegram.NewClient(appId, apiHash, telegram.Options{
// DCList: opts.DCList,
// UpdateHandler: d,
// SessionStorage: sessionStorage,
// Logger: opts.Logger,
// Device: telegram.DeviceConfig{
// DeviceModel: "GoTGProto",
// SystemVersion: runtime.GOOS,
// AppVersion: VERSION,
// SystemLangCode: opts.SystemLangCode,
// LangCode: opts.ClientLangCode,
// },
// })

ctx, cancel := context.WithCancel(context.Background())

c := Client{
Resolver: opts.Resolver,
PublicKeys: opts.PublicKeys,
DC: opts.DC,
DCList: opts.DCList,
DisableCopyright: opts.DisableCopyright,
Logger: opts.Logger,
SystemLangCode: opts.SystemLangCode,
ClientLangCode: opts.ClientLangCode,
Dispatcher: d,
sessionStorage: sessionStorage,
clientType: cType,
ctx: ctx,
autoFetchReply: opts.AutoFetchReply,
cancel: cancel,
appId: appId,
apiHash: apiHash,
}

c.printCredit()

return &c, c.Start(nil)
}

func NewClientWithCustomDevice(
celestix marked this conversation as resolved.
Show resolved Hide resolved
appId int,
apiHash string,
cType ClientType,
opts *ClientOpts,
device *telegram.DeviceConfig,
) (*Client, error) {
if opts == nil {
opts = &ClientOpts{
SystemLangCode: "en",
ClientLangCode: "en",
}
}

var sessionStorage telegram.SessionStorage
if opts.Session == nil || opts.Session.GetName() == ":memory:" {
sessionStorage = &session.StorageMemory{}
Expand Down Expand Up @@ -165,22 +231,25 @@ func NewClient(appId int, apiHash string, cType ClientType, opts *ClientOpts) (*

c.printCredit()

return &c, c.Start()
return &c, c.Start(device)
}

func (c *Client) initTelegramClient() {
c.Client = telegram.NewClient(c.appId, c.apiHash, telegram.Options{
DCList: c.DCList,
UpdateHandler: c.Dispatcher,
SessionStorage: c.sessionStorage,
Logger: c.Logger,
Device: telegram.DeviceConfig{
func (c *Client) initTelegramClient(device *telegram.DeviceConfig) {
if device == nil {
device = &telegram.DeviceConfig{
DeviceModel: "GoTGProto",
SystemVersion: runtime.GOOS,
AppVersion: VERSION,
SystemLangCode: c.SystemLangCode,
LangCode: c.ClientLangCode,
},
}
}
c.Client = telegram.NewClient(c.appId, c.apiHash, telegram.Options{
DCList: c.DCList,
UpdateHandler: c.Dispatcher,
SessionStorage: c.sessionStorage,
Logger: c.Logger,
Device: *device,
})
}

Expand Down Expand Up @@ -291,14 +360,14 @@ func (c *Client) Stop() {

// Start connects the client to telegram servers and logins.
// It will return error if the client is already running.
func (c *Client) Start() error {
func (c *Client) Start(device *telegram.DeviceConfig) error {
if c.running {
return intErrors.ErrClientAlreadyRunning
}
if c.ctx.Err() == context.Canceled {
c.ctx, c.cancel = context.WithCancel(context.Background())
}
c.initTelegramClient()
c.initTelegramClient(device)
wg := sync.WaitGroup{}
wg.Add(1)
go func(c *Client) {
Expand Down
108 changes: 74 additions & 34 deletions sessionMaker/sessionName.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const (
TelethonSession
// PyrogramSession is used as SessionType when you want to log in through the string session made by pyrogram - a Python MTProto library.
PyrogramSession
// InMemorySessionName is used when it is necessary to indicate that this session is in memory
InMemorySessionName = ":memory:"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to name this constant as "InMemorySession" instead of "InMemorySessionName", following the convention of library.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done 0973db5

)

// NewSession creates a new session with provided name string and SessionType.
Expand All @@ -42,50 +44,88 @@ func NewSession(sessionName string, sessionType SessionType) *SessionName {
return &s
}

func NewSessionWithoutFiles(sessionValue string, sessionType SessionType) *SessionName {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better if you rename this function to NewInMemorySession() to avoid confusions and consider adding function documentations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done 0973db5

s := SessionName{
name: InMemorySessionName,
sessionType: sessionType,
}
s.data, s.err = s.loadInMemory(sessionValue)
return &s
}

func (s *SessionName) load() ([]byte, error) {
switch s.sessionType {
case PyrogramSession:
storage.Load("pyrogram.session", false)
sd, err := DecodePyrogramSession(s.name)
if err != nil {
return nil, err
}
data, err := json.Marshal(jsonData{
Version: storage.LatestVersion,
Data: *sd,
})
return data, err
return loadByPyrogramSession(s.name)
case TelethonSession:
storage.Load("telethon.session", false)
sd, err := session.TelethonSession(s.name)
if err != nil {
return nil, err
}
data, err := json.Marshal(jsonData{
Version: storage.LatestVersion,
Data: *sd,
})
return data, err
return loadByTelethonSession(s.name)
case StringSession:
storage.Load("gotgproto.session", false)
sd, err := functions.DecodeStringToSession(s.name)
if err != nil {
return nil, err
}

// data, err := json.Marshal(jsonData{
// Version: latestVersion,
// Data: *sd,
// })
return sd.Data, err
return loadByStringSession(s.name)
default:
return loadByDefault(s.name)
}
}

func (s *SessionName) loadInMemory(sessionValue string) ([]byte, error) {
switch s.sessionType {
case PyrogramSession:
return loadByPyrogramSession(sessionValue)
case TelethonSession:
return loadByTelethonSession(sessionValue)
case StringSession:
return loadByStringSession(sessionValue)
default:
if s.name == "" {
s.name = "new"
}
storage.Load(fmt.Sprintf("%s.session", s.name), false)
sFD := storage.GetSession()
return sFD.Data, nil
return loadByDefault(sessionValue)
}
}

func loadByPyrogramSession(value string) ([]byte, error) {
sd, err := DecodePyrogramSession(value)
if err != nil {
return nil, err
}
data, err := json.Marshal(jsonData{
Version: storage.LatestVersion,
Data: *sd,
})
return data, err
}

func loadByTelethonSession(value string) ([]byte, error) {
sd, err := session.TelethonSession(value)
if err != nil {
return nil, err
}
data, err := json.Marshal(jsonData{
Version: storage.LatestVersion,
Data: *sd,
})
return data, err
}

func loadByStringSession(value string) ([]byte, error) {
sd, err := functions.DecodeStringToSession(value)
if err != nil {
return nil, err
}

// data, err := json.Marshal(jsonData{
// Version: latestVersion,
// Data: *sd,
// })
return sd.Data, err
}

func loadByDefault(value string) ([]byte, error) {
if value == "" {
value = "new"
}
storage.Load(fmt.Sprintf("%s.session", value), false)
sFD := storage.GetSession()
return sFD.Data, nil
}

// GetName is used for retrieving name of the session.
Expand Down