2022-04-14 07:22:00 -05:00
package pref
import (
"bytes"
2022-08-17 21:07:20 -05:00
"database/sql/driver"
2022-04-14 07:22:00 -05:00
"encoding/json"
"errors"
2022-08-17 21:07:20 -05:00
"fmt"
2022-04-14 07:22:00 -05:00
"time"
2023-02-21 04:19:07 -06:00
"github.com/grafana/grafana/pkg/util/errutil"
2022-04-14 07:22:00 -05:00
)
var ErrPrefNotFound = errors . New ( "preference not found" )
2023-02-21 04:19:07 -06:00
var ErrUnknownCookieType = errutil . NewBase (
errutil . StatusBadRequest ,
"preferences.unknownCookieType" ,
errutil . WithPublicMessage ( "Got an unknown cookie preference type. Expected a set containing one or more of 'functional', 'performance', or 'analytics'}" ) ,
)
2022-04-14 07:22:00 -05:00
type Preference struct {
2022-08-17 21:07:20 -05:00
ID int64 ` xorm:"pk autoincr 'id'" db:"id" `
OrgID int64 ` xorm:"org_id" db:"org_id" `
UserID int64 ` xorm:"user_id" db:"user_id" `
TeamID int64 ` xorm:"team_id" db:"team_id" `
Teams [ ] int64 ` xorm:"extends" `
Version int ` db:"version" `
HomeDashboardID int64 ` xorm:"home_dashboard_id" db:"home_dashboard_id" `
Timezone string ` db:"timezone" `
2022-11-21 08:53:56 -06:00
WeekStart * string ` db:"week_start" `
2022-08-17 21:07:20 -05:00
Theme string ` db:"theme" `
Created time . Time ` db:"created" `
Updated time . Time ` db:"updated" `
JSONData * PreferenceJSONData ` xorm:"json_data" db:"json_data" `
2022-04-14 07:22:00 -05:00
}
2023-02-21 04:19:07 -06:00
func ( p Preference ) Cookies ( typ string ) bool {
if p . JSONData == nil || p . JSONData . CookiePreferences == nil {
return false
}
_ , ok := p . JSONData . CookiePreferences [ typ ]
return ok
}
2022-04-14 07:22:00 -05:00
type GetPreferenceWithDefaultsQuery struct {
Teams [ ] int64
OrgID int64
UserID int64
}
type GetPreferenceQuery struct {
OrgID int64
UserID int64
TeamID int64
}
type SavePreferenceCommand struct {
UserID int64
OrgID int64
TeamID int64
2023-02-21 04:19:07 -06:00
HomeDashboardID int64 ` json:"homeDashboardId,omitempty" `
HomeDashboardUID * string ` json:"homeDashboardUID,omitempty" `
Timezone string ` json:"timezone,omitempty" `
WeekStart string ` json:"weekStart,omitempty" `
Theme string ` json:"theme,omitempty" `
Language string ` json:"language,omitempty" `
QueryHistory * QueryHistoryPreference ` json:"queryHistory,omitempty" `
CookiePreferences [ ] CookieType ` json:"cookiePreferences,omitempty" `
2022-04-14 07:22:00 -05:00
}
type PatchPreferenceCommand struct {
UserID int64
OrgID int64
TeamID int64
2023-02-21 04:19:07 -06:00
HomeDashboardID * int64 ` json:"homeDashboardId,omitempty" `
HomeDashboardUID * string ` json:"homeDashboardUID,omitempty" `
Timezone * string ` json:"timezone,omitempty" `
WeekStart * string ` json:"weekStart,omitempty" `
Theme * string ` json:"theme,omitempty" `
Language * string ` json:"language,omitempty" `
QueryHistory * QueryHistoryPreference ` json:"queryHistory,omitempty" `
CookiePreferences [ ] CookieType ` json:"cookiePreferences,omitempty" `
2022-04-14 07:22:00 -05:00
}
type PreferenceJSONData struct {
2023-02-21 04:19:07 -06:00
Language string ` json:"language" `
QueryHistory QueryHistoryPreference ` json:"queryHistory" `
CookiePreferences map [ string ] struct { } ` json:"cookiePreferences" `
2022-04-14 07:22:00 -05:00
}
type QueryHistoryPreference struct {
HomeTab string ` json:"homeTab" `
}
func ( j * PreferenceJSONData ) FromDB ( data [ ] byte ) error {
dec := json . NewDecoder ( bytes . NewBuffer ( data ) )
dec . UseNumber ( )
return dec . Decode ( j )
}
2022-08-17 21:07:20 -05:00
func ( j * PreferenceJSONData ) Scan ( val interface { } ) error {
switch v := val . ( type ) {
case [ ] byte :
if len ( v ) == 0 {
return nil
}
return json . Unmarshal ( v , & j )
case string :
if len ( v ) == 0 {
return nil
}
return json . Unmarshal ( [ ] byte ( v ) , & j )
default :
return fmt . Errorf ( "unsupported type: %T" , v )
}
}
func ( j * PreferenceJSONData ) Value ( ) ( driver . Value , error ) {
return j . ToDB ( )
}
2022-04-14 07:22:00 -05:00
func ( j * PreferenceJSONData ) ToDB ( ) ( [ ] byte , error ) {
if j == nil {
return nil , nil
}
return json . Marshal ( j )
}
func ( p Preference ) TableName ( ) string { return "preferences" }
2023-02-21 04:19:07 -06:00
// swagger:model
// Enum: analytics,performance,functional
type CookieType string