Open platform to collect and prioritize feedback
Split db entities and the db handler functions
Matt Roberts354b3ad7cbca
diff --git a/app/services/sqlstore/dbEntities/README.md b/app/services/sqlstore/dbEntities/README.mdnew file mode 100644index 000000000..6d9106320--- /dev/null+++ b/app/services/sqlstore/dbEntities/README.md@@ -0,0 +1,60 @@+# dbEntities Package++This package contains database entity mapping structs that convert PostgreSQL query results to domain models.++## Purpose++The `dbEntities` package provides a centralized location for database-to-model conversion logic, separating data mapping concerns from the business logic in the `postgres` package.++## Exported Types++### User (and UserProvider)++The `User` type is **exported** for use in commercial/premium features:++```go+import "github.com/getfider/fider/app/services/sqlstore/dbEntities"++// Use dbEntities.User in commercial code+var user dbEntities.User+err := trx.Get(&user, "SELECT id, name, email... FROM users WHERE id = $1", userID)+if err != nil {+ return err+}++// Convert to entity.User+entityUser := user.ToModel(ctx)+```++## Unexported Types++All other types (comment, post, tag, tenant, etc.) are **unexported** (lowercase) and only used internally by the `postgres` package. They can be exported later if needed by simply capitalizing the type name and its `toModel` method.++## Architecture++**Current State:**+- `postgres` package: Contains all the db* struct definitions and uses them directly+- `dbEntities` package: Contains the same struct definitions with User exported+- Both coexist without conflicts++**Usage Pattern:**+- Regular Fider code continues using `postgres` package as before+- Commercial/premium code can import and use `dbEntities.User` directly+- No changes needed to existing `postgres` code++## Converting Additional Types++To export another type for commercial use:++1. Open the corresponding file (e.g., `comment.go`, `post.go`)+2. Capitalize the struct name: `type comment` → `type Comment`+3. Capitalize the method: `func (c *comment) toModel` → `func (c *Comment) ToModel`+4. Update any internal references if needed++## Testing++The package includes unit tests for the exported User type. Run them with:++```bash+godotenv -f .test.env go test ./app/services/sqlstore/dbEntities/...+```\ No newline at end of filediff --git a/app/services/sqlstore/dbEntities/billing.go b/app/services/sqlstore/dbEntities/billing.gonew file mode 100644index 000000000..8e6941482--- /dev/null+++ b/app/services/sqlstore/dbEntities/billing.go@@ -0,0 +1,35 @@+package dbEntities++import (+ "context"++ "github.com/getfider/fider/app/models/entity"+ "github.com/getfider/fider/app/models/enum"+ "github.com/getfider/fider/app/pkg/dbx"+)++type billingState struct {+ Status int `db:"status"`+ PlanID string `db:"paddle_plan_id"`+ SubscriptionID string `db:"paddle_subscription_id"`+ TrialEndsAt dbx.NullTime `db:"trial_ends_at"`+ SubscriptionEndsAt dbx.NullTime `db:"subscription_ends_at"`+}++func (s *billingState) toModel(ctx context.Context) *entity.BillingState {+ model := &entity.BillingState{+ Status: enum.BillingStatus(s.Status),+ PlanID: s.PlanID,+ SubscriptionID: s.SubscriptionID,+ }++ if s.TrialEndsAt.Valid {+ model.TrialEndsAt = &s.TrialEndsAt.Time+ }++ if s.SubscriptionEndsAt.Valid {+ model.SubscriptionEndsAt = &s.SubscriptionEndsAt.Time+ }++ return model+}diff --git a/app/services/sqlstore/dbEntities/comment.go b/app/services/sqlstore/dbEntities/comment.gonew file mode 100644index 000000000..8f4e6bd02--- /dev/null+++ b/app/services/sqlstore/dbEntities/comment.go@@ -0,0 +1,42 @@+package dbEntities++import (+ "context"+ "encoding/json"+ "time"++ "github.com/getfider/fider/app/models/entity"+ "github.com/getfider/fider/app/pkg/dbx"+)++type comment struct {+ ID int `db:"id"`+ Content string `db:"content"`+ CreatedAt time.Time `db:"created_at"`+ User *User `db:"user"`+ Attachments []string `db:"attachment_bkeys"`+ EditedAt dbx.NullTime `db:"edited_at"`+ EditedBy *User `db:"edited_by"`+ ReactionCounts dbx.NullString `db:"reaction_counts"`+ IsApproved bool `db:"is_approved"`+}++func (c *comment) toModel(ctx context.Context) *entity.Comment {+ comment := &entity.Comment{+ ID: c.ID,+ Content: c.Content,+ CreatedAt: c.CreatedAt,+ User: c.User.ToModel(ctx),+ Attachments: c.Attachments,+ IsApproved: c.IsApproved,+ }+ if c.EditedAt.Valid {+ comment.EditedBy = c.EditedBy.ToModel(ctx)+ comment.EditedAt = &c.EditedAt.Time+ }++ if c.ReactionCounts.Valid {+ _ = json.Unmarshal([]byte(c.ReactionCounts.String), &comment.ReactionCounts)+ }+ return comment+}diff --git a/app/services/sqlstore/dbEntities/oauth.go b/app/services/sqlstore/dbEntities/oauth.gonew file mode 100644index 000000000..c703f4fdb--- /dev/null+++ b/app/services/sqlstore/dbEntities/oauth.go@@ -0,0 +1,43 @@+package dbEntities++import (+ "github.com/getfider/fider/app/models/entity"+)++type oauthConfig struct {+ ID int `db:"id"`+ Provider string `db:"provider"`+ DisplayName string `db:"display_name"`+ LogoBlobKey string `db:"logo_bkey"`+ Status int `db:"status"`+ IsTrusted bool `db:"is_trusted"`+ ClientID string `db:"client_id"`+ ClientSecret string `db:"client_secret"`+ AuthorizeURL string `db:"authorize_url"`+ TokenURL string `db:"token_url"`+ Scope string `db:"scope"`+ ProfileURL string `db:"profile_url"`+ JSONUserIDPath string `db:"json_user_id_path"`+ JSONUserNamePath string `db:"json_user_name_path"`+ JSONUserEmailPath string `db:"json_user_email_path"`+}++func (m *oauthConfig) toModel() *entity.OAuthConfig {+ return &entity.OAuthConfig{+ ID: m.ID,+ Provider: m.Provider,+ DisplayName: m.DisplayName,+ Status: m.Status,+ IsTrusted: m.IsTrusted,+ LogoBlobKey: m.LogoBlobKey,+ ClientID: m.ClientID,+ ClientSecret: m.ClientSecret,+ AuthorizeURL: m.AuthorizeURL,+ TokenURL: m.TokenURL,+ ProfileURL: m.ProfileURL,+ Scope: m.Scope,+ JSONUserIDPath: m.JSONUserIDPath,+ JSONUserNamePath: m.JSONUserNamePath,+ JSONUserEmailPath: m.JSONUserEmailPath,+ }+}diff --git a/app/services/sqlstore/dbEntities/post.go b/app/services/sqlstore/dbEntities/post.gonew file mode 100644index 000000000..e8f4e7901--- /dev/null+++ b/app/services/sqlstore/dbEntities/post.go@@ -0,0 +1,73 @@+package dbEntities++import (+ "context"+ "time"++ "github.com/getfider/fider/app/models/entity"+ "github.com/getfider/fider/app/models/enum"+ "github.com/getfider/fider/app/pkg/dbx"+ "github.com/lib/pq"+)++type post struct {+ ID int `db:"id"`+ Number int `db:"number"`+ Title string `db:"title"`+ Slug string `db:"slug"`+ Description string `db:"description"`+ CreatedAt time.Time `db:"created_at"`+ User *User `db:"user"`+ HasVoted bool `db:"has_voted"`+ VotesCount int `db:"votes_count"`+ CommentsCount int `db:"comments_count"`+ RecentVotes int `db:"recent_votes_count"`+ RecentComments int `db:"recent_comments_count"`+ Status int `db:"status"`+ Response dbx.NullString `db:"response"`+ RespondedAt dbx.NullTime `db:"response_date"`+ ResponseUser *User `db:"response_user"`+ OriginalNumber dbx.NullInt `db:"original_number"`+ OriginalTitle dbx.NullString `db:"original_title"`+ OriginalSlug dbx.NullString `db:"original_slug"`+ OriginalStatus dbx.NullInt `db:"original_status"`+ Tags pq.StringArray `db:"tags"`+ IsApproved bool `db:"is_approved"`+}++func (i *post) toModel(ctx context.Context) *entity.Post {+ post := &entity.Post{+ ID: i.ID,+ Number: i.Number,+ Title: i.Title,+ Slug: i.Slug,+ Description: i.Description,+ CreatedAt: i.CreatedAt,+ User: i.User.ToModel(ctx),+ HasVoted: i.HasVoted,+ VotesCount: i.VotesCount,+ CommentsCount: i.CommentsCount,+ Status: enum.PostStatus(i.Status),+ Tags: i.Tags,+ IsApproved: i.IsApproved,+ }++ if i.Response.Valid {+ post.Response = &entity.PostResponse{+ Text: i.Response.String,+ RespondedAt: i.RespondedAt.Time,+ User: i.ResponseUser.ToModel(ctx),+ }++ if i.OriginalNumber.Valid {+ post.Response.Original = &entity.OriginalPost{+ Number: int(i.OriginalNumber.Int64),+ Title: i.OriginalTitle.String,+ Slug: i.OriginalSlug.String,+ Status: enum.PostStatus(i.OriginalStatus.Int64),+ }+ }+ }++ return post+}diff --git a/app/services/sqlstore/dbEntities/tag.go b/app/services/sqlstore/dbEntities/tag.gonew file mode 100644index 000000000..db85d3ae4--- /dev/null+++ b/app/services/sqlstore/dbEntities/tag.go@@ -0,0 +1,23 @@+package dbEntities++import (+ "github.com/getfider/fider/app/models/entity"+)++type tag struct {+ ID int `db:"id"`+ Name string `db:"name"`+ Slug string `db:"slug"`+ Color string `db:"color"`+ IsPublic bool `db:"is_public"`+}++func (t *tag) toModel() *entity.Tag {+ return &entity.Tag{+ ID: t.ID,+ Name: t.Name,+ Slug: t.Slug,+ Color: t.Color,+ IsPublic: t.IsPublic,+ }+}diff --git a/app/services/sqlstore/dbEntities/tenant.go b/app/services/sqlstore/dbEntities/tenant.gonew file mode 100644index 000000000..3d89d6fe6--- /dev/null+++ b/app/services/sqlstore/dbEntities/tenant.go@@ -0,0 +1,52 @@+package dbEntities++import (+ "github.com/getfider/fider/app/models/entity"+ "github.com/getfider/fider/app/models/enum"+)++type Tenant struct {+ ID int `db:"id"`+ Name string `db:"name"`+ Subdomain string `db:"subdomain"`+ CNAME string `db:"cname"`+ Invitation string `db:"invitation"`+ WelcomeMessage string `db:"welcome_message"`+ Status int `db:"status"`+ Locale string `db:"locale"`+ IsPrivate bool `db:"is_private"`+ LogoBlobKey string `db:"logo_bkey"`+ CustomCSS string `db:"custom_css"`+ AllowedSchemes string `db:"allowed_schemes"`+ IsEmailAuthAllowed bool `db:"is_email_auth_allowed"`+ IsFeedEnabled bool `db:"is_feed_enabled"`+ PreventIndexing bool `db:"prevent_indexing"`+ IsModerationEnabled bool `db:"is_moderation_enabled"`+}++func (t *Tenant) toModel() *entity.Tenant {+ if t == nil {+ return nil+ }++ tenant := &entity.Tenant{+ ID: t.ID,+ Name: t.Name,+ Subdomain: t.Subdomain,+ CNAME: t.CNAME,+ Invitation: t.Invitation,+ WelcomeMessage: t.WelcomeMessage,+ Status: enum.TenantStatus(t.Status),+ Locale: t.Locale,+ IsPrivate: t.IsPrivate,+ LogoBlobKey: t.LogoBlobKey,+ CustomCSS: t.CustomCSS,+ AllowedSchemes: t.AllowedSchemes,+ IsEmailAuthAllowed: t.IsEmailAuthAllowed,+ IsFeedEnabled: t.IsFeedEnabled,+ PreventIndexing: t.PreventIndexing,+ IsModerationEnabled: t.IsModerationEnabled,+ }++ return tenant+}diff --git a/app/services/sqlstore/dbEntities/user.go b/app/services/sqlstore/dbEntities/user.gonew file mode 100644index 000000000..cf0fc6abe--- /dev/null+++ b/app/services/sqlstore/dbEntities/user.go@@ -0,0 +1,89 @@+package dbEntities++import (+ "context"+ "database/sql"+ "net/url"++ "github.com/getfider/fider/app"+ "github.com/getfider/fider/app/models/entity"+ "github.com/getfider/fider/app/models/enum"+ "github.com/getfider/fider/app/pkg/web"+)++// User is the database mapping for users table+type User struct {+ ID sql.NullInt64 `db:"id"`+ Name sql.NullString `db:"name"`+ Email sql.NullString `db:"email"`+ Tenant *Tenant `db:"tenant"`+ Role sql.NullInt64 `db:"role"`+ Status sql.NullInt64 `db:"status"`+ AvatarType sql.NullInt64 `db:"avatar_type"`+ AvatarBlobKey sql.NullString `db:"avatar_bkey"`+ IsVerified sql.NullBool `db:"is_verified"`+ Providers []*UserProvider+}++type UserProvider struct {+ Name sql.NullString `db:"provider"`+ UID sql.NullString `db:"provider_uid"`+}++type userSetting struct {+ Key string `db:"key"`+ Value string `db:"value"`+}++func (u *User) ToModel(ctx context.Context) *entity.User {+ if u == nil {+ return nil+ }++ tenant, ok := ctx.Value(app.TenantCtxKey).(*entity.Tenant)+ if !ok || tenant == nil {+ tenant = u.Tenant.toModel()+ }++ avatarType := enum.AvatarType(u.AvatarType.Int64)+ avatarURL := ""+ if u.AvatarType.Valid {+ avatarURL = buildAvatarURL(ctx, avatarType, int(u.ID.Int64), u.Name.String, u.AvatarBlobKey.String)+ }++ user := &entity.User{+ ID: int(u.ID.Int64),+ Name: u.Name.String,+ Email: u.Email.String,+ Tenant: tenant,+ Role: enum.Role(u.Role.Int64),+ Status: enum.UserStatus(u.Status.Int64),+ AvatarType: avatarType,+ AvatarBlobKey: u.AvatarBlobKey.String,+ AvatarURL: avatarURL,+ IsVerified: u.IsVerified.Bool,+ }++ if u.Providers != nil {+ user.Providers = make([]*entity.UserProvider, len(u.Providers))+ for i, p := range u.Providers {+ user.Providers[i] = &entity.UserProvider{+ Name: p.Name.String,+ UID: p.UID.String,+ }+ }+ }++ return user+}++func buildAvatarURL(ctx context.Context, avatarType enum.AvatarType, id int, name, avatarBlobKey string) string {+ if name == "" {+ name = "-"+ }++ if avatarType == enum.AvatarTypeCustom {+ return web.AssetsURL(ctx, "/static/images/%s", avatarBlobKey)+ }+ return web.AssetsURL(ctx, "/static/avatars/%s/%d/%s", avatarType.String(), id, url.PathEscape(name))+}diff --git a/app/services/sqlstore/dbEntities/user_test.go b/app/services/sqlstore/dbEntities/user_test.gonew file mode 100644index 000000000..e4ca46f7c--- /dev/null+++ b/app/services/sqlstore/dbEntities/user_test.go@@ -0,0 +1,91 @@+package dbEntities_test++import (+ "context"+ "database/sql"+ "net/url"+ "testing"++ "github.com/getfider/fider/app"+ "github.com/getfider/fider/app/models/enum"+ "github.com/getfider/fider/app/pkg/web"+ "github.com/getfider/fider/app/services/sqlstore/dbEntities"+)++func TestUserToModel(t *testing.T) {+ // Create a proper context with web.Request+ u, _ := url.Parse("http://test.fider.io")+ req := web.Request{URL: u}+ ctx := context.WithValue(context.Background(), app.RequestCtxKey, req)++ // Create a test dbEntities.User+ dbUser := &dbEntities.User{+ ID: sql.NullInt64{Int64: 1, Valid: true},+ Name: sql.NullString{String: "John Doe", Valid: true},+ Email: sql.NullString{String: "[email protected]", Valid: true},+ Role: sql.NullInt64{Int64: int64(enum.RoleAdministrator), Valid: true},+ Status: sql.NullInt64{Int64: int64(enum.UserActive), Valid: true},+ AvatarType: sql.NullInt64{Int64: int64(enum.AvatarTypeGravatar), Valid: true},+ AvatarBlobKey: sql.NullString{String: "", Valid: true},+ IsVerified: sql.NullBool{Bool: true, Valid: true},+ Providers: []*dbEntities.UserProvider{+ {+ Name: sql.NullString{String: "google", Valid: true},+ UID: sql.NullString{String: "123456", Valid: true},+ },+ },+ }++ // Convert to entity.User+ entityUser := dbUser.ToModel(ctx)++ // Verify conversion+ if entityUser == nil {+ t.Fatal("ToModel returned nil")+ }++ if entityUser.ID != 1 {+ t.Errorf("Expected ID 1, got %d", entityUser.ID)+ }++ if entityUser.Name != "John Doe" {+ t.Errorf("Expected Name 'John Doe', got '%s'", entityUser.Name)+ }++ if entityUser.Email != "[email protected]" {+ t.Errorf("Expected Email '[email protected]', got '%s'", entityUser.Email)+ }++ if entityUser.Role != enum.RoleAdministrator {+ t.Errorf("Expected Role Administrator, got %v", entityUser.Role)+ }++ if entityUser.Status != enum.UserActive {+ t.Errorf("Expected Status Active, got %v", entityUser.Status)+ }++ if !entityUser.IsVerified {+ t.Error("Expected IsVerified to be true")+ }++ if len(entityUser.Providers) != 1 {+ t.Fatalf("Expected 1 provider, got %d", len(entityUser.Providers))+ }++ if entityUser.Providers[0].Name != "google" {+ t.Errorf("Expected provider name 'google', got '%s'", entityUser.Providers[0].Name)+ }++ if entityUser.Providers[0].UID != "123456" {+ t.Errorf("Expected provider UID '123456', got '%s'", entityUser.Providers[0].UID)+ }+}++func TestUserToModel_Nil(t *testing.T) {+ var dbUser *dbEntities.User+ entityUser := dbUser.ToModel(context.Background())++ if entityUser != nil {+ t.Error("Expected ToModel on nil to return nil")+ }+}diff --git a/app/services/sqlstore/dbEntities/verification.go b/app/services/sqlstore/dbEntities/verification.gonew file mode 100644index 000000000..4fdacbe82--- /dev/null+++ b/app/services/sqlstore/dbEntities/verification.go@@ -0,0 +1,43 @@+package dbEntities++import (+ "time"++ "github.com/getfider/fider/app/models/entity"+ "github.com/getfider/fider/app/models/enum"+ "github.com/getfider/fider/app/pkg/dbx"+)++type emailVerification struct {+ ID int `db:"id"`+ Name string `db:"name"`+ Email string `db:"email"`+ Key string `db:"key"`+ Kind enum.EmailVerificationKind `db:"kind"`+ UserID dbx.NullInt `db:"user_id"`+ CreatedAt time.Time `db:"created_at"`+ ExpiresAt time.Time `db:"expires_at"`+ VerifiedAt dbx.NullTime `db:"verified_at"`+}++func (t *emailVerification) toModel() *entity.EmailVerification {+ model := &entity.EmailVerification{+ Name: t.Name,+ Email: t.Email,+ Key: t.Key,+ Kind: t.Kind,+ CreatedAt: t.CreatedAt,+ ExpiresAt: t.ExpiresAt,+ VerifiedAt: nil,+ }++ if t.VerifiedAt.Valid {+ model.VerifiedAt = &t.VerifiedAt.Time+ }++ if t.UserID.Valid {+ model.UserID = int(t.UserID.Int64)+ }++ return model+}diff --git a/app/services/sqlstore/dbEntities/vote.go b/app/services/sqlstore/dbEntities/vote.gonew file mode 100644index 000000000..82946df53--- /dev/null+++ b/app/services/sqlstore/dbEntities/vote.go@@ -0,0 +1,33 @@+package dbEntities++import (+ "context"+ "time"++ "github.com/getfider/fider/app/models/entity"+ "github.com/getfider/fider/app/models/enum"+)++type vote struct {+ User *struct {+ ID int `db:"id"`+ Name string `db:"name"`+ Email string `db:"email"`+ AvatarType int64 `db:"avatar_type"`+ AvatarBlobKey string `db:"avatar_bkey"`+ } `db:"user"`+ CreatedAt time.Time `db:"created_at"`+}++func (v *vote) toModel(ctx context.Context) *entity.Vote {+ vote := &entity.Vote{+ CreatedAt: v.CreatedAt,+ User: &entity.VoteUser{+ ID: v.User.ID,+ Name: v.User.Name,+ Email: v.User.Email,+ AvatarURL: buildAvatarURL(ctx, enum.AvatarType(v.User.AvatarType), v.User.ID, v.User.Name, v.User.AvatarBlobKey),+ },+ }+ return vote+}diff --git a/commercial/services/sqlstore/postgres/moderation.go b/commercial/services/sqlstore/postgres/moderation.goindex 907ccbe0f..924a3df2f 100644--- a/commercial/services/sqlstore/postgres/moderation.go+++ b/commercial/services/sqlstore/postgres/moderation.go@@ -13,6 +13,7 @@ import ("github.com/getfider/fider/app/pkg/bus""github.com/getfider/fider/app/pkg/dbx""github.com/getfider/fider/app/pkg/errors"+ "github.com/getfider/fider/app/services/sqlstore/dbEntities")func ApprovePost(ctx context.Context, c *cmd.ApprovePost) error {@@ -143,46 +144,24 @@ func BulkDeclineItems(ctx context.Context, c *cmd.BulkDeclineItems) error {}type dbModerationPost struct {- ID int `db:"id"`- Number int `db:"number"`- Title string `db:"title"`- Slug string `db:"slug"`- Description string `db:"description"`- CreatedAt time.Time `db:"created_at"`- User *dbUser `db:"user"`+ ID int `db:"id"`+ Number int `db:"number"`+ Title string `db:"title"`+ Slug string `db:"slug"`+ Description string `db:"description"`+ CreatedAt time.Time `db:"created_at"`+ User *dbEntities.User `db:"user"`}type dbModerationComment struct {- ID int `db:"id"`- PostID int `db:"post_id"`- PostNumber int `db:"post_number"`- PostSlug string `db:"post_slug"`- Content string `db:"content"`- CreatedAt time.Time `db:"created_at"`- User *dbUser `db:"user"`- PostTitle string `db:"post_title"`-}--type dbUser struct {- ID int `db:"user_id"`- Name string `db:"user_name"`- Email string `db:"user_email"`- Role enum.Role `db:"user_role"`- Status enum.UserStatus `db:"user_status"`- AvatarType enum.AvatarType `db:"user_avatar_type"`- AvatarBkey string `db:"user_avatar_bkey"`-}--func (u *dbUser) toModel(ctx context.Context) *entity.User {- return &entity.User{- ID: u.ID,- Name: u.Name,- Email: u.Email,- Role: u.Role,- Status: u.Status,- AvatarType: u.AvatarType,- AvatarBlobKey: u.AvatarBkey,- }+ ID int `db:"id"`+ PostID int `db:"post_id"`+ PostNumber int `db:"post_number"`+ PostSlug string `db:"post_slug"`+ Content string `db:"content"`+ CreatedAt time.Time `db:"created_at"`+ User *dbEntities.User `db:"user"`+ PostTitle string `db:"post_title"`}func GetModerationItems(ctx context.Context, q *query.GetModerationItems) error {@@ -211,7 +190,7 @@ func GetModerationItems(ctx context.Context, q *query.GetModerationItems) errorfor _, post := range posts {userWithEmail := &entity.UserWithEmail{- User: post.User.toModel(ctx),+ User: post.User.ToModel(ctx),}q.Result = append(q.Result, &query.ModerationItem{@@ -252,7 +231,7 @@ func GetModerationItems(ctx context.Context, q *query.GetModerationItems) errorfor _, comment := range comments {userWithEmail := &entity.UserWithEmail{- User: comment.User.toModel(ctx),+ User: comment.User.ToModel(ctx),}q.Result = append(q.Result, &query.ModerationItem{