feat: start custom preferences on backend
This commit is contained in:
parent
86a1841f4f
commit
7ea5efae93
|
@ -4,9 +4,12 @@ import (
|
|||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"codeberg.org/u1f320/pronouns.cc/backend/common"
|
||||
"codeberg.org/u1f320/pronouns.cc/backend/icons"
|
||||
"emperror.dev/errors"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/georgysavva/scany/v2/pgxscan"
|
||||
|
@ -49,8 +52,47 @@ type User struct {
|
|||
DeletedAt *time.Time
|
||||
SelfDelete *bool
|
||||
DeleteReason *string
|
||||
|
||||
CustomPreferences CustomPreferences
|
||||
}
|
||||
|
||||
type CustomPreferences = map[string]CustomPreference
|
||||
|
||||
type CustomPreference struct {
|
||||
Icon string `json:"icon"`
|
||||
Tooltip string `json:"tooltip"`
|
||||
Size PreferenceSize `json:"size"`
|
||||
Muted bool `json:"muted"`
|
||||
Favourite bool `json:"favourite"`
|
||||
}
|
||||
|
||||
func (c CustomPreference) Validate() string {
|
||||
if !icons.IsValid(c.Icon) {
|
||||
return fmt.Sprintf("custom preference icon %q is invalid", c.Icon)
|
||||
}
|
||||
|
||||
if c.Tooltip == "" {
|
||||
return "custom preference tooltip is empty"
|
||||
}
|
||||
if common.StringLength(&c.Tooltip) > FieldEntryMaxLength {
|
||||
return fmt.Sprintf("custom preference tooltip is too long, max %d characters, is %d characters", FieldEntryMaxLength, common.StringLength(&c.Tooltip))
|
||||
}
|
||||
|
||||
if c.Size != PreferenceSizeLarge && c.Size != PreferenceSizeNormal && c.Size != PreferenceSizeSmall {
|
||||
return fmt.Sprintf("custom preference size %q is invalid", string(c.Size))
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
type PreferenceSize string
|
||||
|
||||
const (
|
||||
PreferenceSizeLarge PreferenceSize = "large"
|
||||
PreferenceSizeNormal PreferenceSize = "normal"
|
||||
PreferenceSizeSmall PreferenceSize = "small"
|
||||
)
|
||||
|
||||
func (u User) NumProviders() (numProviders int) {
|
||||
if u.Discord != nil {
|
||||
numProviders++
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -46,6 +46,7 @@ func dbMemberToMember(u db.User, m db.Member, fields []db.Field, isOwnMember boo
|
|||
Username: u.Username,
|
||||
DisplayName: u.DisplayName,
|
||||
Avatar: u.Avatar,
|
||||
CustomPreferences: u.CustomPreferences,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -61,6 +62,7 @@ type PartialUser struct {
|
|||
Username string `json:"name"`
|
||||
DisplayName *string `json:"display_name"`
|
||||
Avatar *string `json:"avatar"`
|
||||
CustomPreferences db.CustomPreferences `json:"custom_preferences"`
|
||||
}
|
||||
|
||||
func (s *Server) getMember(w http.ResponseWriter, r *http.Request) error {
|
||||
|
|
|
@ -23,6 +23,7 @@ type GetUserResponse struct {
|
|||
Pronouns []db.PronounEntry `json:"pronouns"`
|
||||
Members []PartialMember `json:"members"`
|
||||
Fields []db.Field `json:"fields"`
|
||||
CustomPreferences db.CustomPreferences `json:"custom_preferences"`
|
||||
}
|
||||
|
||||
type GetMeResponse struct {
|
||||
|
@ -69,6 +70,7 @@ func dbUserToResponse(u db.User, fields []db.Field, members []db.Member) GetUser
|
|||
Names: db.NotNull(u.Names),
|
||||
Pronouns: db.NotNull(u.Pronouns),
|
||||
Fields: db.NotNull(fields),
|
||||
CustomPreferences: u.CustomPreferences,
|
||||
}
|
||||
|
||||
resp.Members = make([]PartialMember, len(members))
|
||||
|
|
|
@ -10,6 +10,7 @@ import (
|
|||
"codeberg.org/u1f320/pronouns.cc/backend/server"
|
||||
"emperror.dev/errors"
|
||||
"github.com/go-chi/render"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type PatchUserRequest struct {
|
||||
|
@ -23,6 +24,7 @@ type PatchUserRequest struct {
|
|||
Fields *[]db.Field `json:"fields"`
|
||||
Avatar *string `json:"avatar"`
|
||||
ListPrivate *bool `json:"list_private"`
|
||||
CustomPreferences *db.CustomPreferences `json:"custom_preferences"`
|
||||
}
|
||||
|
||||
// patchUser parses a PatchUserRequest and updates the user with the given ID.
|
||||
|
@ -115,6 +117,19 @@ func (s *Server) patchUser(w http.ResponseWriter, r *http.Request) error {
|
|||
return *err
|
||||
}
|
||||
|
||||
// validate custom preferences
|
||||
if req.CustomPreferences != nil {
|
||||
for k, v := range *req.CustomPreferences {
|
||||
_, err := uuid.Parse(k)
|
||||
if err != nil {
|
||||
return server.APIError{Code: server.ErrBadRequest, Details: "One or more custom preference IDs is not a UUID."}
|
||||
}
|
||||
if s := v.Validate(); s != "" {
|
||||
return server.APIError{Code: server.ErrBadRequest, Details: s}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// update avatar
|
||||
var avatarHash *string = nil
|
||||
if req.Avatar != nil {
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
// This script regenerates the list of icons for the frontend (frontend/src/icons.json)
|
||||
// and the backend (backend/icons/icons.go) from the currently installed version of Bootstrap Icons.
|
||||
// Run with `pnpm node icons.js` in the frontend directory.
|
||||
|
||||
import { writeFileSync } from "fs";
|
||||
import icons from "bootstrap-icons/font/bootstrap-icons.json" assert { type: "json" };
|
||||
|
||||
const keys = Object.keys(icons);
|
||||
|
||||
console.log(`Found ${keys.length} icons`);
|
||||
const output = JSON.stringify(keys);
|
||||
console.log(`Saving file as src/icons.json`);
|
||||
|
||||
writeFileSync("src/icons.json", output);
|
||||
|
||||
const goCode1 = `// Generated code. DO NOT EDIT
|
||||
package icons
|
||||
|
||||
var icons = [...]string{
|
||||
`;
|
||||
|
||||
const goCode2 = `}
|
||||
|
||||
// IsValid returns true if the input is the name of a Bootstrap icon.
|
||||
func IsValid(name string) bool {
|
||||
for i := range icons {
|
||||
if icons[i] == name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
`;
|
||||
|
||||
let goOutput = goCode1;
|
||||
|
||||
keys.forEach((element) => {
|
||||
goOutput += ` "${element}",\n`;
|
||||
});
|
||||
|
||||
goOutput += goCode2;
|
||||
|
||||
console.log("Writing Go code");
|
||||
writeFileSync("../backend/icons/icons.go", goOutput);
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,5 @@
|
|||
-- +migrate Up
|
||||
|
||||
-- 2023-04-19: Add custom preferences
|
||||
|
||||
alter table users add column custom_preferences jsonb not null default '{}';
|
Loading…
Reference in New Issue