Skip to content

Commit

Permalink
allow using any gorm dialector insteat of forcing sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
celestix committed Mar 10, 2024
1 parent dc0e38a commit c24da3b
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 42 deletions.
6 changes: 4 additions & 2 deletions examples/auth-using-api-base/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package main

import (
"log"

"github.com/celestix/gotgproto"
"github.com/celestix/gotgproto/examples/auth-using-api-base/web"
"github.com/celestix/gotgproto/sessionMaker"
"log"
"github.com/glebarez/sqlite"
)

func main() {
Expand All @@ -29,7 +31,7 @@ func main() {

// custom authenticator using web api
AuthConversator: web.GetWebAuth(),
Session: sessionMaker.SqliteSession("webbot"),
Session: sessionMaker.SqlSession(sqlite.Open("webbot")),
},
)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion examples/authorizing-as-user/sqlite_session/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/celestix/gotgproto"
"github.com/celestix/gotgproto/sessionMaker"
"gorm.io/driver/sqlite"
)

func main() {
Expand All @@ -25,7 +26,7 @@ func main() {
clientType,
// Optional parameters of client
&gotgproto.ClientOpts{
Session: sessionMaker.SqliteSession("echobot"),
Session: sessionMaker.SqlSession(sqlite.Open("echobot")),
},
)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion examples/echo-bot/sqlite_session/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/celestix/gotgproto/dispatcher/handlers/filters"
"github.com/celestix/gotgproto/ext"
"github.com/celestix/gotgproto/sessionMaker"
"github.com/glebarez/sqlite"
"github.com/gotd/td/tg"
)

Expand All @@ -30,7 +31,7 @@ func main() {
clientType,
// Optional parameters of client
&gotgproto.ClientOpts{
Session: sessionMaker.SqliteSession("echobot"),
Session: sessionMaker.SqlSession(sqlite.Open("echobot")),
},
)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/pkg/errors v0.9.1
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.27.0
gorm.io/gorm v1.25.5
gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde
)

require (
Expand All @@ -17,6 +17,7 @@ require (
github.com/go-faster/errors v0.7.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
modernc.org/libc v1.22.5 // indirect
modernc.org/mathutil v1.5.0 // indirect
Expand All @@ -42,6 +43,7 @@ require (
golang.org/x/net v0.21.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.17.0 // indirect
gorm.io/driver/sqlite v1.5.5
nhooyr.io/websocket v1.8.10 // indirect
rsc.io/qr v0.2.0 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ github.com/klauspost/compress v1.17.7 h1:ehO88t2UGzQK66LMdE8tibEd1ErmzZjNEqWkjLA
github.com/klauspost/compress v1.17.7/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down Expand Up @@ -84,8 +86,12 @@ golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/sqlite v1.5.5 h1:7MDMtUZhV065SilG62E0MquljeArQZNfJnjd9i9gx3E=
gorm.io/driver/sqlite v1.5.5/go.mod h1:6NgQ7sQWAIFsPrJJl1lSNSu2TABh0ZZ/zm5fosATavE=
gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls=
gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde h1:9DShaph9qhkIYw7QF91I/ynrr4cOO2PZra2PFD7Mfeg=
gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
modernc.org/libc v1.22.5 h1:91BNch/e5B0uPbJFgqbxXuOnxBQjlS//icfQEGmvyjE=
modernc.org/libc v1.22.5/go.mod h1:jj+Z7dTNX8fBScMVNRAYZ/jF91K8fdT2hYMThc3YjBY=
modernc.org/mathutil v1.5.0 h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ=
Expand Down
65 changes: 37 additions & 28 deletions sessionMaker/sessionConstructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,30 @@ package sessionMaker
import (
"context"
"encoding/json"
"errors"

"github.com/celestix/gotgproto/functions"
"github.com/celestix/gotgproto/storage"
"github.com/gotd/td/session"
"github.com/gotd/td/session/tdesktop"
"gorm.io/gorm"
)

type sessionName interface {
getType() string
}

type sessionNameString string

func (sessionNameString) getType() string { return "str" }

type sessionNameDialector struct {
dialector gorm.Dialector
}

func (sessionNameDialector) getType() string { return "dialector" }

type SessionConstructor interface {
loadSession() (string, []byte, error)
loadSession() (sessionName, []byte, error)
}

type SimpleSessionConstructor int8
Expand All @@ -22,25 +36,20 @@ func SimpleSession() *SimpleSessionConstructor {
return &s
}

func (*SimpleSessionConstructor) loadSession() (string, []byte, error) {
return "gotgproto_simple", nil, nil
func (*SimpleSessionConstructor) loadSession() (sessionName, []byte, error) {
return sessionNameString("gotgproto_simple"), nil, nil
}

type SqliteSessionConstructor struct {
name string
type SqlSessionConstructor struct {
dialector gorm.Dialector
}

func SqliteSession(name string) *SqliteSessionConstructor {
return &SqliteSessionConstructor{name: name}
func SqlSession(dialector gorm.Dialector) *SqlSessionConstructor {
return &SqlSessionConstructor{dialector: dialector}
}

var errSqliteSession error = errors.New("sqlite session")

func (s *SqliteSessionConstructor) loadSession() (string, []byte, error) {
if s.name == "" {
s.name = "new"
}
return s.name, nil, errSqliteSession
func (s *SqlSessionConstructor) loadSession() (sessionName, []byte, error) {
return &sessionNameDialector{s.dialector}, nil, nil
}

type PyrogramSessionConstructor struct {
Expand All @@ -56,16 +65,16 @@ func (s *PyrogramSessionConstructor) Name(name string) *PyrogramSessionConstruct
return s
}

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

type TelethonSessionConstructor struct {
Expand All @@ -81,16 +90,16 @@ func (s *TelethonSessionConstructor) Name(name string) *TelethonSessionConstruct
return s
}

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

type StringSessionConstructor struct {
Expand All @@ -106,12 +115,12 @@ func (s *StringSessionConstructor) Name(name string) *StringSessionConstructor {
return s
}

func (s *StringSessionConstructor) loadSession() (string, []byte, error) {
func (s *StringSessionConstructor) loadSession() (sessionName, []byte, error) {
sd, err := functions.DecodeStringToSession(s.value)
if err != nil {
return s.name, nil, err
return sessionNameString(s.name), nil, err
}
return s.name, sd.Data, err
return sessionNameString(s.name), sd.Data, err
}

type TdataSessionConstructor struct {
Expand All @@ -128,10 +137,10 @@ func (s *TdataSessionConstructor) Name(name string) *TdataSessionConstructor {
return s
}

func (s *TdataSessionConstructor) loadSession() (string, []byte, error) {
func (s *TdataSessionConstructor) loadSession() (sessionName, []byte, error) {
sd, err := session.TDesktopSession(s.Account)
if err != nil {
return s.name, nil, err
return sessionNameString(s.name), nil, err
}
ctx := context.Background()
var (
Expand All @@ -140,11 +149,11 @@ func (s *TdataSessionConstructor) loadSession() (string, []byte, error) {
)
// Save decoded Telegram Desktop session as gotd session.
if err := loader.Save(ctx, sd); err != nil {
return s.name, nil, err
return sessionNameString(s.name), nil, err
}
data, err := json.Marshal(jsonData{
Version: storage.LatestVersion,
Data: *sd,
})
return s.name, data, err
return sessionNameString(s.name), data, err
}
12 changes: 6 additions & 6 deletions sessionMaker/sessionMaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ import (
"fmt"

"github.com/celestix/gotgproto/storage"
"github.com/glebarez/sqlite"
"github.com/gotd/td/session"
"github.com/gotd/td/telegram"
)

func NewSessionStorage(ctx context.Context, sessionType SessionConstructor, inMemory bool) (*storage.PeerStorage, telegram.SessionStorage, error) {
name, data, err := sessionType.loadSession()
if err != nil {
if err != errSqliteSession {
return nil, nil, err
}
peerStorage := storage.NewPeerStorage(
fmt.Sprintf("%s.session", name), false)
return nil, nil, err
}
if sessDialect, ok := name.(*sessionNameDialector); ok {
peerStorage := storage.NewPeerStorage(sessDialect.dialector, false)
return peerStorage, &SessionStorage{
data: peerStorage.GetSession().Data,
peerStorage: peerStorage,
}, nil
}
peerStorage := storage.NewPeerStorage(fmt.Sprintf("%s.session", name), inMemory)
peerStorage := storage.NewPeerStorage(sqlite.Open(fmt.Sprintf("%s.session", name)), inMemory)
if inMemory {
s := session.StorageMemory{}
err := s.StoreSession(ctx, data)
Expand Down
5 changes: 2 additions & 3 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/AnimeKaizoku/cacher"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
Expand All @@ -18,7 +17,7 @@ type PeerStorage struct {
SqlSession *gorm.DB
}

func NewPeerStorage(sessionName string, inMemory bool) *PeerStorage {
func NewPeerStorage(dialector gorm.Dialector, inMemory bool) *PeerStorage {
p := PeerStorage{
inMemory: inMemory,
peerLock: new(sync.RWMutex),
Expand All @@ -32,7 +31,7 @@ func NewPeerStorage(sessionName string, inMemory bool) *PeerStorage {
CleanInterval: 24 * time.Hour,
Revaluate: true,
}
db, err := gorm.Open(sqlite.Open(sessionName), &gorm.Config{
db, err := gorm.Open(dialector, &gorm.Config{
SkipDefaultTransaction: true,
Logger: logger.Default.LogMode(logger.Silent),
})
Expand Down

0 comments on commit c24da3b

Please sign in to comment.