Skip to content

Commit

Permalink
feat: refactoring DAL & initialization interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
trakhimenok committed Aug 30, 2024
1 parent c6a68a1 commit 4735127
Show file tree
Hide file tree
Showing 23 changed files with 180 additions and 83 deletions.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions botsfw/botsdal/dal_bot_chat.go → botsdal/dal_bot_chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func NewBotChatKey(platformID, botID, chatID string) *dal.Key {
return dal.NewKeyWithParentAndID(botKey, botChatsCollection, chatID)
}

// GetBotChat returns bot chat
// Deprecated: use
func GetBotChat(
ctx context.Context,
tx dal.ReadSession,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
47 changes: 47 additions & 0 deletions botsdal/facade_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package botsdal

import (
"context"
"github.com/bots-go-framework/bots-fw-store/botsfwmodels"
"github.com/bots-go-framework/bots-fw/botsfsobject"
"github.com/dal-go/dalgo/dal"
"github.com/dal-go/dalgo/record"
)

type AppUserDal interface {
CreateAppUserFromBotUser(ctx context.Context, tx dal.ReadwriteTransaction, platform, botID string, botUser botsfsobject.BotUser) (
appUser record.DataWithID[string, botsfwmodels.AppUserData], err error,
)
GetAppUserByBotUserID(ctx context.Context, tx dal.ReadwriteTransaction, platform, botID, botUserID string) (
appUser record.DataWithID[string, botsfwmodels.AppUserData], err error,
)
//UpdateAppUser(ctx context.Context, tx dal.ReadwriteTransaction, appUser record.DataWithID[string, botsfwmodels.AppUserData]) error
//LinkAppUserToBotUser(ctx context.Context, platform, botID, botUserID, appUserID string) (err error)
}

type appUserDal struct {
}

func DefaultAppUserDal() AppUserDal {
return appUserDal{}
}

func (a appUserDal) CreateAppUserFromBotUser(ctx context.Context, tx dal.ReadwriteTransaction, platform, botID string, botUser botsfsobject.BotUser) (appUser record.DataWithID[string, botsfwmodels.AppUserData], err error) {
//TODO implement me
panic("implement me")
}

func (a appUserDal) GetAppUserByBotUserID(ctx context.Context, tx dal.ReadwriteTransaction, platform, botID, botUserID string) (appUser record.DataWithID[string, botsfwmodels.AppUserData], err error) {
//TODO implement me
panic("implement me")
}

func (a appUserDal) UpdateAppUser(ctx context.Context, tx dal.ReadwriteTransaction, appUser record.DataWithID[string, botsfwmodels.AppUserData]) error {
//TODO implement me
panic("implement me")
}

func (a appUserDal) LinkAppUserToBotUser(ctx context.Context, platform, botID, botUserID, appUserID string) (err error) {
//TODO implement me
panic("implement me")
}
13 changes: 13 additions & 0 deletions botsfsobject/bot_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package botsfsobject

// BotUser provides info about current bot user
type BotUser interface {
// GetBotUserID returns bot user ID
GetBotUserID() string

// GetFirstName returns user's first name
GetFirstName() string

// GetLastName returns user's last name
GetLastName() string
}
18 changes: 8 additions & 10 deletions botsfw/app_context.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package botsfw

import (
"context"
"github.com/bots-go-framework/bots-fw-store/botsfwmodels"
"github.com/dal-go/dalgo/record"
"github.com/bots-go-framework/bots-fw/botsdal"
"github.com/strongo/i18n"
)

// BotAppContext is a context for bot app
type BotAppContext interface {
// AppContext is a context for an app that uses the botsfw
type AppContext interface {
//strongoapp.AppUserSettings // TODO: Do we really need it here?

AppUserCollectionName() string
botsdal.AppUserDal
//AppUserCollectionName() string
//GetAppUserByBotUserID(ctx context.Context, platform, botID, botUserID string) (appUser record.DataWithID[string, botsfwmodels.AppUserData], err error)

i18n.TranslationContext

NewBotAppUserEntity() botsfwmodels.AppUserData
GetBotChatEntityFactory(platform string) func() botsfwmodels.BotChatData

GetAppUserByBotUserID(ctx context.Context, platform, botID, botUserID string) (appUser record.DataWithID[string, botsfwmodels.AppUserData], err error)
//NewBotAppUserEntity() botsfwmodels.AppUserData
//GetBotChatEntityFactory(platform string) func() botsfwmodels.BotChatData
}
60 changes: 60 additions & 0 deletions botsfw/bot_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package botsfw

import (
"context"
"errors"
"github.com/bots-go-framework/bots-fw/botsfwconst"
)

// BotContext binds a bot to a specific hosting environment
type BotContext struct {
AppContext AppContext
BotHost BotHost // describes current bot app host environment
BotSettings *BotSettings // keeps parameters of a bot that are static and are not changed in runtime
}

// BotContextProvider provides BotContext by platformID & botID
type BotContextProvider interface {
// GetBotContext returns BotContext by platformID & botID
GetBotContext(ctx context.Context, platformID botsfwconst.Platform, botID string) (botContext *BotContext, err error)
}

type botContextProvider struct {
botHost BotHost
appContext AppContext
botSettingProvider BotSettingsProvider
}

func NewBotContextProvider(botHost BotHost, appContext AppContext, botSettingProvider BotSettingsProvider) BotContextProvider {
if botHost == nil {
panic("required argument botHost == nil")
}
if appContext == nil {
panic("required argument appContext == nil")
}
if botSettingProvider == nil {
panic("required argument botSettingProvider == nil")
}
return botContextProvider{
appContext: appContext,
botHost: botHost,
botSettingProvider: botSettingProvider,
}
}

var ErrUnknownBot = errors.New("unknown bot")

func (v botContextProvider) GetBotContext(ctx context.Context, platformID botsfwconst.Platform, botID string) (botContext *BotContext, err error) {
botSettingsBy := v.botSettingProvider(ctx)
botSettings, ok := botSettingsBy.ByID[botID]
if !ok {
if botSettings, ok = botSettingsBy.ByCode[botID]; !ok {
return nil, ErrUnknownBot
}
}
return &BotContext{
AppContext: v.appContext,
BotHost: v.botHost,
BotSettings: botSettings,
}, nil
}
File renamed without changes.
2 changes: 1 addition & 1 deletion botsfw/context_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package botsfw
import (
"fmt"
"github.com/bots-go-framework/bots-fw-store/botsfwmodels"
"github.com/bots-go-framework/bots-fw/botsfw/botsdal"
"github.com/bots-go-framework/bots-fw/botsdal"
"time"
)

Expand Down
6 changes: 3 additions & 3 deletions botsfw/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type WebhookHandler interface {
GetBotContextAndInputs(c context.Context, r *http.Request) (botContext *BotContext, entriesWithInputs []EntryInputs, err error)

// CreateBotCoreStores TODO: should be deprecated after migration to dalgo
//CreateBotCoreStores(appContext BotAppContext, r *http.Request) botsfwdal.DataAccess
//CreateBotCoreStores(appContext AppContext, r *http.Request) botsfwdal.DataAccess

// CreateWebhookContext creates WebhookContext for current webhook request
CreateWebhookContext(args CreateWebhookContextArgs) (WebhookContext, error)
Expand All @@ -39,7 +39,7 @@ type WebhookHandler interface {

type CreateWebhookContextArgs struct {
HttpRequest *http.Request // TODO: Can we get rid of it? Needed for botHost.GetHTTPClient()
AppContext BotAppContext
AppContext AppContext
BotContext BotContext
WebhookInput WebhookInput
Tx dal.ReadwriteTransaction
Expand All @@ -49,7 +49,7 @@ type CreateWebhookContextArgs struct {

func NewCreateWebhookContextArgs(
httpRequest *http.Request,
appContext BotAppContext,
appContext AppContext,
botContext BotContext,
webhookInput WebhookInput,
tx dal.ReadwriteTransaction,
Expand Down
6 changes: 0 additions & 6 deletions botsfw/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ type BotHost interface {
GetHTTPClient(c context.Context) *http.Client
}

// BotContext describes a bot on a specific platform
type BotContext struct {
BotHost BotHost // describes current bot app host environment
BotSettings *BotSettings // keeps parameters of a bot that are static and are not changed in runtime
}

// NewBotContext creates current bot host & settings
func NewBotContext(botHost BotHost, botSettings *BotSettings) *BotContext {
if botHost == nil {
Expand Down
13 changes: 7 additions & 6 deletions botsfw/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"github.com/bots-go-framework/bots-fw-store/botsfwmodels"
"github.com/bots-go-framework/bots-fw/botsfwconst"
"github.com/dal-go/dalgo/dal"
"github.com/dal-go/dalgo/record"
"github.com/strongo/i18n"
Expand All @@ -28,7 +29,7 @@ type BotSettings struct {

// Platform is a platform that bot is running on
// E.g.: Telegram, Viber, Facebook Messenger, WhatsApp, etc.
Platform Platform
Platform botsfwconst.Platform

// Env is an environment where bot is running
// E.g.: Production/Live, Local/Dev, Staging, etc.
Expand Down Expand Up @@ -84,7 +85,7 @@ func (v BotSettings) GetAppUserByID(ctx context.Context, tx dal.ReadSession, app

// NewBotSettings configures bot application
func NewBotSettings(
platform Platform,
platform botsfwconst.Platform,
environment string,
profile BotProfile,
code, id, token, gaToken string,
Expand Down Expand Up @@ -130,11 +131,11 @@ func NewBotSettings(
}

// SettingsProvider returns settings per different keys (ID, code, API token, Locale)
type SettingsProvider func(c context.Context) SettingsBy
type BotSettingsProvider func(ctx context.Context) BotSettingsBy

// SettingsBy keeps settings per different keys (ID, code, API token, Locale)
// TODO: Decide if it should have map[string]*BotSettings instead of map[string]BotSettings
type SettingsBy struct {
type BotSettingsBy struct {

// ByCode keeps settings by bot code - it is a human-readable ID of a bot
ByCode map[string]*BotSettings
Expand All @@ -146,12 +147,12 @@ type SettingsBy struct {
}

// NewBotSettingsBy create settings per different keys (ID, code, API token, Locale)
func NewBotSettingsBy(bots ...BotSettings) (settingsBy SettingsBy) {
func NewBotSettingsBy(bots ...BotSettings) (settingsBy BotSettingsBy) {
count := len(bots)
if count == 0 {
panic("NewBotSettingsBy: missing required parameter: bots")
}
settingsBy = SettingsBy{
settingsBy = BotSettingsBy{
ByCode: make(map[string]*BotSettings, count),
ByID: make(map[string]*BotSettings, count),
ByProfile: make(map[string][]*BotSettings),
Expand Down
3 changes: 2 additions & 1 deletion botsfw/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package botsfw
import (
"context"
"github.com/bots-go-framework/bots-fw-store/botsfwmodels"
"github.com/bots-go-framework/bots-fw/botsfwconst"
"github.com/dal-go/dalgo/dal"
"github.com/dal-go/dalgo/record"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -31,7 +32,7 @@ func dummyBotProfile() BotProfile {

func TestNewBotSettings(t *testing.T) {
const (
platform = PlatformTelegram
platform = botsfwconst.PlatformTelegram
code = "TestBot1"
token = "TestToken2"
localeCode5 = "Kode5"
Expand Down
2 changes: 1 addition & 1 deletion botsfw/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ type TextMessageFromBot struct {
}

// BotMessageType returns if we want to send a new message or edit existing one
func (m TextMessageFromBot) BotMessageType() BotMessageType {
func (m *TextMessageFromBot) BotMessageType() BotMessageType {
if m.IsEdit {
return BotMessageTypeEditMessage
}
Expand Down
16 changes: 3 additions & 13 deletions botsfw/webhook_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ type WebhookContext interface { // TODO: Make interface much smaller?
SetContext(c context.Context)

ExecutionContext() ExecutionContext
BotAppContext() BotAppContext

AppContext() AppContext

BotContext() BotContext

MustBotChatID() string
Expand Down Expand Up @@ -125,15 +127,3 @@ type BotInputProvider interface {
// Input returns a webhook input from a specific bot interface (Telegram, FB Messenger, Viber, etc.)
Input() WebhookInput
}

// BotAPIUser provides info about current bot user
type BotAPIUser interface {
// FirstName returns user's first name
FirstName() string

// LastName returns user's last name
LastName() string

//IdAsString() string
//IdAsInt64() int64
}
Loading

0 comments on commit 4735127

Please sign in to comment.