Restructure the monorepo into clear top-level directories: - backend/: Go module root (cmd, internal, pkg, configs, migrations, docs/swagger, scripts, tests, go.mod, Makefile, .air.toml) - deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd - docs/: project documentation + reports/ (moved from repo root) - AGENTS.md: new AI coding-agent guide at repo root Update all references to the new layout: - Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root) - docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile, env_file ../../.env, volume mounts ../../backend:/app - deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile - CI: working-directory: backend for go commands, file deploy/docker/Dockerfile, coverage path backend/coverage.out, health_check backend/scripts/ - backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../ - README: architecture tree, quickstart, config paths updated Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh, gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to preserve history. Build, vet, SQLite tests, and docker compose config verified.
96 lines
4.0 KiB
Go
96 lines
4.0 KiB
Go
package model
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// DashboardApp represents a dashboard application configuration.
|
|
// Reference: Chatwoot app/models/dashboard_app.rb — P2B M12 spec
|
|
//
|
|
// A DashboardApp is a custom dashboard widget configuration that can be
|
|
// embedded as an iframe in the GoChat dashboard. Each app belongs to an
|
|
// account and optionally to a specific user (personal dashboards).
|
|
//
|
|
// Content format: JSON array of iframe configurations, e.g.:
|
|
//
|
|
// [{"type": "frame", "url": "https://example.com/widget"}]
|
|
//
|
|
// Validation rules (per Chatwoot):
|
|
// - content must be a JSON array with at least one widget
|
|
// - each element must have type="frame" and url (http/https URI)
|
|
type DashboardApp struct {
|
|
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
|
|
AccountID uint `gorm:"not null;index" json:"account_id"`
|
|
UserID *uint `gorm:"index" json:"user_id,omitempty"` // optional: per-user dashboard
|
|
Title string `gorm:"size:255;not null" json:"title"`
|
|
Description string `gorm:"type:text" json:"description,omitempty"`
|
|
Icon string `gorm:"size:255" json:"icon,omitempty"` // icon URL or icon name
|
|
URL string `gorm:"size:512" json:"url,omitempty"` // primary iframe URL
|
|
Kind string `gorm:"size:100;default:'frame'" json:"kind"` // frame, link
|
|
Content json.RawMessage `gorm:"type:json;serializer:json;default:'[]'" json:"content"` // iframe config array
|
|
Active *bool `gorm:"default:true;not null" json:"active"`
|
|
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
|
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
|
|
|
|
// Relations
|
|
Account *Account `gorm:"foreignKey:AccountID" json:"account,omitempty"`
|
|
User *User `gorm:"foreignKey:UserID" json:"user,omitempty"`
|
|
}
|
|
|
|
func (DashboardApp) TableName() string { return "dashboard_apps" }
|
|
|
|
// BoolPtr returns a pointer to the given bool value.
|
|
// Useful for setting *bool fields like Active where false must be
|
|
// distinguishable from "not set" (nil) to avoid GORM zero-value skipping.
|
|
func BoolPtr(b bool) *bool { return &b }
|
|
|
|
// DashboardWidget represents a single widget/iframe within a DashboardApp's content.
|
|
// This is an in-memory structure used for widget CRUD — not a separate DB table.
|
|
// Widgets are stored as elements in the DashboardApp.Content jsonb array.
|
|
type DashboardWidget struct {
|
|
Type string `json:"type"` // must be "frame"
|
|
URL string `json:"url"` // must be http/https URI
|
|
ID string `json:"id,omitempty"` // optional client-assigned widget ID
|
|
Name string `json:"name,omitempty"` // optional widget display name
|
|
}
|
|
|
|
// ValidateContent checks that content is a valid JSON array of iframe configs.
|
|
// Returns nil if valid, or an error describing what's wrong.
|
|
// Per Chatwoot M12 spec:
|
|
// - content must be a JSON array
|
|
// - each element must have type="frame" and url (http/https URI)
|
|
func ValidateContent(content json.RawMessage) error {
|
|
if len(content) == 0 || string(content) == "" {
|
|
return fmt.Errorf("content must be a JSON array")
|
|
}
|
|
|
|
var widgets []DashboardWidget
|
|
if err := json.Unmarshal(content, &widgets); err != nil {
|
|
return fmt.Errorf("content must be a JSON array: %w", err)
|
|
}
|
|
if len(widgets) == 0 {
|
|
return fmt.Errorf("content must contain at least one widget")
|
|
}
|
|
|
|
for i, w := range widgets {
|
|
if w.Type != "frame" {
|
|
return fmt.Errorf("widget[%d].type must be 'frame', got '%s'", i, w.Type)
|
|
}
|
|
if strings.TrimSpace(w.URL) == "" || !IsValidHTTPURL(w.URL) {
|
|
return fmt.Errorf("widget[%d].url must be http/https URI, got '%s'", i, w.URL)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// IsValidHTTPURL checks if a URL uses http or https scheme.
|
|
func IsValidHTTPURL(u string) bool {
|
|
return (len(u) >= 7 && u[:7] == "http://") || (len(u) >= 8 && u[:8] == "https://")
|
|
}
|