Add settings modal and option to show calls
This commit is contained in:
@@ -41,6 +41,13 @@ func InitAuthDB(filepath string) error {
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS settings (
|
||||
user_id TEXT PRIMARY KEY,
|
||||
settings_json TEXT NOT NULL DEFAULT '{}',
|
||||
updated_at INTEGER NOT NULL,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_sessions_user_id ON sessions(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_sessions_expires_at ON sessions(expires_at);
|
||||
`
|
||||
|
||||
@@ -191,6 +191,22 @@ func HandleMessages(c echo.Context) error {
|
||||
}
|
||||
}
|
||||
|
||||
// Get user ID from context to fetch settings
|
||||
userID, ok := c.Get("user_id").(string)
|
||||
if !ok {
|
||||
return c.JSON(http.StatusUnauthorized, map[string]string{
|
||||
"error": "User not authenticated",
|
||||
})
|
||||
}
|
||||
|
||||
// Fetch user settings to check if calls should be shown
|
||||
settings, err := GetUserSettings(userID)
|
||||
if err != nil {
|
||||
slog.Error("Error getting user settings", "error", err)
|
||||
// If we can't get settings, default to showing calls
|
||||
settings = GetDefaultSettings()
|
||||
}
|
||||
|
||||
activities, err := GetActivityByAddress(userDB, address, startDate, endDate, limit, offset)
|
||||
if err != nil {
|
||||
slog.Error("Error getting activity", "error", err)
|
||||
@@ -198,6 +214,18 @@ func HandleMessages(c echo.Context) error {
|
||||
"error": "Failed to get activity",
|
||||
})
|
||||
}
|
||||
|
||||
// Filter out calls if show_calls setting is false
|
||||
if !settings.Conversations.ShowCalls {
|
||||
filteredActivities := []ActivityItem{}
|
||||
for _, activity := range activities {
|
||||
if activity.Type != "call" {
|
||||
filteredActivities = append(filteredActivities, activity)
|
||||
}
|
||||
}
|
||||
activities = filteredActivities
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, activities)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// Settings represents user settings stored as JSON
|
||||
type Settings struct {
|
||||
Conversations ConversationSettings `json:"conversations"`
|
||||
}
|
||||
|
||||
// ConversationSettings contains settings for the conversation view
|
||||
type ConversationSettings struct {
|
||||
ShowCalls bool `json:"show_calls"`
|
||||
}
|
||||
|
||||
// GetDefaultSettings returns the default settings
|
||||
func GetDefaultSettings() Settings {
|
||||
return Settings{
|
||||
Conversations: ConversationSettings{
|
||||
ShowCalls: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// GetUserSettings retrieves settings for a user
|
||||
func GetUserSettings(userID string) (Settings, error) {
|
||||
var settingsJSON string
|
||||
var updatedAt int64
|
||||
|
||||
err := authDB.QueryRow(
|
||||
"SELECT settings_json, updated_at FROM settings WHERE user_id = ?",
|
||||
userID,
|
||||
).Scan(&settingsJSON, &updatedAt)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
// Return default settings if no settings exist
|
||||
return GetDefaultSettings(), nil
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return Settings{}, err
|
||||
}
|
||||
|
||||
var settings Settings
|
||||
if err := json.Unmarshal([]byte(settingsJSON), &settings); err != nil {
|
||||
return Settings{}, err
|
||||
}
|
||||
|
||||
return settings, nil
|
||||
}
|
||||
|
||||
// SaveUserSettings saves settings for a user
|
||||
func SaveUserSettings(userID string, settings Settings) error {
|
||||
settingsJSON, err := json.Marshal(settings)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
now := time.Now().Unix()
|
||||
|
||||
_, err = authDB.Exec(`
|
||||
INSERT INTO settings (user_id, settings_json, updated_at)
|
||||
VALUES (?, ?, ?)
|
||||
ON CONFLICT(user_id) DO UPDATE SET
|
||||
settings_json = excluded.settings_json,
|
||||
updated_at = excluded.updated_at
|
||||
`, userID, string(settingsJSON), now)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// HandleGetSettings handles GET /api/settings
|
||||
func HandleGetSettings(c echo.Context) error {
|
||||
userID := c.Get("user_id").(string)
|
||||
|
||||
settings, err := GetUserSettings(userID)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{
|
||||
"error": "Failed to get settings",
|
||||
})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, settings)
|
||||
}
|
||||
|
||||
// HandleUpdateSettings handles PUT /api/settings
|
||||
func HandleUpdateSettings(c echo.Context) error {
|
||||
userID := c.Get("user_id").(string)
|
||||
|
||||
var settings Settings
|
||||
if err := c.Bind(&settings); err != nil {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{
|
||||
"error": "Invalid settings data",
|
||||
})
|
||||
}
|
||||
|
||||
if err := SaveUserSettings(userID, settings); err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{
|
||||
"error": "Failed to save settings",
|
||||
})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, settings)
|
||||
}
|
||||
Reference in New Issue
Block a user