mirror of
https://github.com/ProtonMail/go-proton-api.git
synced 2025-12-23 23:57:50 -05:00
feat(GODT-2554): Implement user settings routes.
This commit is contained in:
49
core_settings.go
Normal file
49
core_settings.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package proton
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/go-resty/resty/v2"
|
||||
)
|
||||
|
||||
func (c *Client) GetUserSettings(ctx context.Context) (UserSettings, error) {
|
||||
var res struct {
|
||||
UserSettings UserSettings
|
||||
}
|
||||
|
||||
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
|
||||
return r.SetResult(&res).Get("/core/v4/settings")
|
||||
}); err != nil {
|
||||
return UserSettings{}, err
|
||||
}
|
||||
|
||||
return res.UserSettings, nil
|
||||
}
|
||||
|
||||
func (c *Client) SetUserSettingsTelemetry(ctx context.Context, req SetTelemetryReq) (UserSettings, error) {
|
||||
var res struct {
|
||||
UserSettings UserSettings
|
||||
}
|
||||
|
||||
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
|
||||
return r.SetBody(req).SetResult(&res).Put("/core/v4/settings/telemetry")
|
||||
}); err != nil {
|
||||
return UserSettings{}, err
|
||||
}
|
||||
|
||||
return res.UserSettings, nil
|
||||
}
|
||||
|
||||
func (c *Client) SetUserSettingsCrashReports(ctx context.Context, req SetCrashReportReq) (UserSettings, error) {
|
||||
var res struct {
|
||||
UserSettings UserSettings
|
||||
}
|
||||
|
||||
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
|
||||
return r.SetBody(req).SetResult(&res).Put("/core/v4/settings/crashreports")
|
||||
}); err != nil {
|
||||
return UserSettings{}, err
|
||||
}
|
||||
|
||||
return res.UserSettings, nil
|
||||
}
|
||||
20
core_settings_type.go
Normal file
20
core_settings_type.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package proton
|
||||
|
||||
type UserSettings struct {
|
||||
Telemetry SettingsBool
|
||||
CrashReports SettingsBool
|
||||
}
|
||||
|
||||
type SetTelemetryReq struct {
|
||||
Telemetry SettingsBool
|
||||
}
|
||||
|
||||
type SetCrashReportReq struct {
|
||||
CrashReports SettingsBool
|
||||
}
|
||||
type SettingsBool int
|
||||
|
||||
const (
|
||||
SettingDisabled SettingsBool = iota
|
||||
SettingEnabled
|
||||
)
|
||||
@@ -14,6 +14,7 @@ type account struct {
|
||||
username string
|
||||
addresses map[string]*address
|
||||
mailSettings *mailSettings
|
||||
userSettings proton.UserSettings
|
||||
|
||||
auth map[string]auth
|
||||
authLock sync.RWMutex
|
||||
@@ -33,6 +34,7 @@ func newAccount(userID, username string, armKey string, salt, verifier []byte) *
|
||||
username: username,
|
||||
addresses: make(map[string]*address),
|
||||
mailSettings: newMailSettings(username),
|
||||
userSettings: newUserSettings(),
|
||||
|
||||
auth: make(map[string]auth),
|
||||
keys: []key{{keyID: uuid.NewString(), key: armKey}},
|
||||
|
||||
@@ -44,6 +44,32 @@ func (b *Backend) SetMailSettingsAttachPublicKey(userID string, attach bool) (pr
|
||||
})
|
||||
}
|
||||
|
||||
func (b *Backend) GetUserSettings(userID string) (proton.UserSettings, error) {
|
||||
return withAcc(b, userID, func(acc *account) (proton.UserSettings, error) {
|
||||
return acc.userSettings, nil
|
||||
})
|
||||
}
|
||||
|
||||
func (b *Backend) SetUserSettingsTelemetry(userID string, telemetry proton.SettingsBool) (proton.UserSettings, error) {
|
||||
return withAcc(b, userID, func(acc *account) (proton.UserSettings, error) {
|
||||
if telemetry != proton.SettingDisabled && telemetry != proton.SettingEnabled {
|
||||
return proton.UserSettings{}, errors.New("bad value")
|
||||
}
|
||||
acc.userSettings.Telemetry = telemetry
|
||||
return acc.userSettings, nil
|
||||
})
|
||||
}
|
||||
|
||||
func (b *Backend) SetUserSettingsCrashReports(userID string, crashReports proton.SettingsBool) (proton.UserSettings, error) {
|
||||
return withAcc(b, userID, func(acc *account) (proton.UserSettings, error) {
|
||||
if crashReports != proton.SettingDisabled && crashReports != proton.SettingEnabled {
|
||||
return proton.UserSettings{}, errors.New("bad value")
|
||||
}
|
||||
acc.userSettings.CrashReports = crashReports
|
||||
return acc.userSettings, nil
|
||||
})
|
||||
}
|
||||
|
||||
func (b *Backend) GetAddressID(email string) (string, error) {
|
||||
return withAccEmail(b, email, func(acc *account) (string, error) {
|
||||
addr, ok := acc.getAddr(email)
|
||||
|
||||
9
server/backend/core_settings.go
Normal file
9
server/backend/core_settings.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"github.com/ProtonMail/go-proton-api"
|
||||
)
|
||||
|
||||
func newUserSettings() proton.UserSettings {
|
||||
return proton.UserSettings{Telemetry: proton.SettingEnabled, CrashReports: proton.SettingEnabled}
|
||||
}
|
||||
64
server/core_settings.go
Normal file
64
server/core_settings.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/ProtonMail/go-proton-api"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func (s *Server) handleGetUserSettings() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
settings, err := s.b.GetUserSettings(c.GetString("UserID"))
|
||||
if err != nil {
|
||||
c.AbortWithStatus(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"UserSettings": settings,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) handlePutUserSettingsTelemetry() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
var req proton.SetTelemetryReq
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.AbortWithStatus(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
settings, err := s.b.SetUserSettingsTelemetry(c.GetString("UserID"), req.Telemetry)
|
||||
if err != nil {
|
||||
c.AbortWithStatus(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"UserSettings": settings,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) handlePutUserSettingsCrashReports() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
var req proton.SetCrashReportReq
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.AbortWithStatus(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
settings, err := s.b.SetUserSettingsCrashReports(c.GetString("UserID"), req.CrashReports)
|
||||
if err != nil {
|
||||
c.AbortWithStatus(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"UserSettings": settings,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -68,6 +68,11 @@ func initRouter(s *Server) {
|
||||
events.GET("/:eventID", s.handleGetEvents())
|
||||
events.GET("/latest", s.handleGetEventsLatest())
|
||||
}
|
||||
if settings := core.Group("/settings"); settings != nil {
|
||||
settings.GET("", s.handleGetUserSettings())
|
||||
settings.PUT("/telemetry", s.handlePutUserSettingsTelemetry())
|
||||
settings.PUT("/crashreports", s.handlePutUserSettingsCrashReports())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1773,6 +1773,39 @@ func TestServer_MailSettings(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestServer_UserSettings(t *testing.T) {
|
||||
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) {
|
||||
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) {
|
||||
settings, err := c.GetUserSettings(context.Background())
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, proton.SettingEnabled, settings.Telemetry)
|
||||
require.Equal(t, proton.SettingEnabled, settings.CrashReports)
|
||||
|
||||
settings, err = c.SetUserSettingsTelemetry(context.Background(), proton.SetTelemetryReq{Telemetry: proton.SettingDisabled})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, proton.SettingDisabled, settings.Telemetry)
|
||||
require.Equal(t, proton.SettingEnabled, settings.CrashReports)
|
||||
|
||||
settings, err = c.SetUserSettingsCrashReports(context.Background(), proton.SetCrashReportReq{CrashReports: proton.SettingDisabled})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, proton.SettingDisabled, settings.Telemetry)
|
||||
require.Equal(t, proton.SettingDisabled, settings.CrashReports)
|
||||
|
||||
settings, err = c.SetUserSettingsTelemetry(context.Background(), proton.SetTelemetryReq{Telemetry: 2})
|
||||
require.Error(t, err)
|
||||
|
||||
settings, err = c.SetUserSettingsCrashReports(context.Background(), proton.SetCrashReportReq{CrashReports: 2})
|
||||
require.Error(t, err)
|
||||
|
||||
settings, err = c.GetUserSettings(context.Background())
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, proton.SettingDisabled, settings.Telemetry)
|
||||
require.Equal(t, proton.SettingDisabled, settings.CrashReports)
|
||||
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestServer_Domains(t *testing.T) {
|
||||
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) {
|
||||
domains, err := m.GetDomains(ctx)
|
||||
|
||||
Reference in New Issue
Block a user