feat: user profiles (#32)

* add user profiles and theme selectors

* lowercase buttons by default

* basic layout

* (wip) init token APIs

* refactor server to support variable options

* fix types

* api refactor / registration tests

* implement UI for url and join

* remove console.logs

* rename repository factory

* fix upload size
This commit is contained in:
Hayden 2022-10-06 18:54:09 -08:00 committed by GitHub
parent 1ca430af21
commit 79f7ad40cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
76 changed files with 5154 additions and 388 deletions

View file

@ -21,6 +21,41 @@ const docTemplate = `{
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/v1/groups/invitations": {
"post": {
"security": [
{
"Bearer": []
}
],
"produces": [
"application/json"
],
"tags": [
"User"
],
"summary": "Get the current user",
"parameters": [
{
"description": "User Data",
"name": "payload",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/v1.GroupInvitationCreate"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/v1.GroupInvitation"
}
}
}
}
},
"/v1/items": {
"get": {
"security": [
@ -1504,6 +1539,9 @@ const docTemplate = `{
},
"password": {
"type": "string"
},
"token": {
"type": "string"
}
}
},
@ -1544,6 +1582,31 @@ const docTemplate = `{
}
}
},
"v1.GroupInvitation": {
"type": "object",
"properties": {
"expiresAt": {
"type": "string"
},
"token": {
"type": "string"
},
"uses": {
"type": "integer"
}
}
},
"v1.GroupInvitationCreate": {
"type": "object",
"properties": {
"expiresAt": {
"type": "string"
},
"uses": {
"type": "integer"
}
}
},
"v1.ItemAttachmentToken": {
"type": "object",
"properties": {

View file

@ -13,6 +13,41 @@
},
"basePath": "/api",
"paths": {
"/v1/groups/invitations": {
"post": {
"security": [
{
"Bearer": []
}
],
"produces": [
"application/json"
],
"tags": [
"User"
],
"summary": "Get the current user",
"parameters": [
{
"description": "User Data",
"name": "payload",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/v1.GroupInvitationCreate"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/v1.GroupInvitation"
}
}
}
}
},
"/v1/items": {
"get": {
"security": [
@ -1496,6 +1531,9 @@
},
"password": {
"type": "string"
},
"token": {
"type": "string"
}
}
},
@ -1536,6 +1574,31 @@
}
}
},
"v1.GroupInvitation": {
"type": "object",
"properties": {
"expiresAt": {
"type": "string"
},
"token": {
"type": "string"
},
"uses": {
"type": "integer"
}
}
},
"v1.GroupInvitationCreate": {
"type": "object",
"properties": {
"expiresAt": {
"type": "string"
},
"uses": {
"type": "integer"
}
}
},
"v1.ItemAttachmentToken": {
"type": "object",
"properties": {

View file

@ -328,6 +328,8 @@ definitions:
type: string
password:
type: string
token:
type: string
type: object
v1.ApiSummary:
properties:
@ -353,6 +355,22 @@ definitions:
version:
type: string
type: object
v1.GroupInvitation:
properties:
expiresAt:
type: string
token:
type: string
uses:
type: integer
type: object
v1.GroupInvitationCreate:
properties:
expiresAt:
type: string
uses:
type: integer
type: object
v1.ItemAttachmentToken:
properties:
token:
@ -376,6 +394,27 @@ info:
title: Go API Templates
version: "1.0"
paths:
/v1/groups/invitations:
post:
parameters:
- description: User Data
in: body
name: payload
required: true
schema:
$ref: '#/definitions/v1.GroupInvitationCreate'
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/v1.GroupInvitation'
security:
- Bearer: []
summary: Get the current user
tags:
- User
/v1/items:
get:
produces:

View file

@ -114,12 +114,16 @@ func run(cfg *config.Config) error {
}
app.db = c
app.repos = repo.EntAllRepos(c, cfg.Storage.Data)
app.repos = repo.New(c, cfg.Storage.Data)
app.services = services.NewServices(app.repos)
// =========================================================================
// Start Server
app.server = server.NewServer(app.conf.Web.Host, app.conf.Web.Port)
app.server = server.NewServer(
server.WithHost(app.conf.Web.Host),
server.WithPort(app.conf.Web.Port),
)
routes := app.newRouter(app.repos)
if app.conf.Mode != config.ModeDevelopment {

View file

@ -66,6 +66,8 @@ func (a *app) newRouter(repos *repo.AllRepos) *chi.Mux {
r.Post(v1Base("/users/logout"), v1Ctrl.HandleAuthLogout())
r.Get(v1Base("/users/refresh"), v1Ctrl.HandleAuthRefresh())
r.Post(v1Base("/groups/invitations"), v1Ctrl.HandleGroupInvitationsCreate())
r.Get(v1Base("/locations"), v1Ctrl.HandleLocationGetAll())
r.Post(v1Base("/locations"), v1Ctrl.HandleLocationCreate())
r.Get(v1Base("/locations/{id}"), v1Ctrl.HandleLocationGet())

View file

@ -0,0 +1,62 @@
package v1
import (
"net/http"
"time"
"github.com/hay-kot/homebox/backend/internal/services"
"github.com/hay-kot/homebox/backend/pkgs/server"
"github.com/rs/zerolog/log"
)
type (
GroupInvitationCreate struct {
Uses int `json:"uses"`
ExpiresAt time.Time `json:"expiresAt"`
}
GroupInvitation struct {
Token string `json:"token"`
ExpiresAt time.Time `json:"expiresAt"`
Uses int `json:"uses"`
}
)
// HandleUserSelf godoc
// @Summary Get the current user
// @Tags User
// @Produce json
// @Param payload body GroupInvitationCreate true "User Data"
// @Success 200 {object} GroupInvitation
// @Router /v1/groups/invitations [Post]
// @Security Bearer
func (ctrl *V1Controller) HandleGroupInvitationsCreate() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
data := GroupInvitationCreate{}
if err := server.Decode(r, &data); err != nil {
log.Err(err).Msg("failed to decode user registration data")
server.RespondError(w, http.StatusInternalServerError, err)
return
}
if data.ExpiresAt.IsZero() {
data.ExpiresAt = time.Now().Add(time.Hour * 24)
}
ctx := services.NewContext(r.Context())
token, err := ctrl.svc.User.NewInvitation(ctx, data.Uses, data.ExpiresAt)
if err != nil {
log.Err(err).Msg("failed to create new token")
server.RespondError(w, http.StatusInternalServerError, err)
return
}
server.Respond(w, http.StatusCreated, GroupInvitation{
Token: token,
ExpiresAt: data.ExpiresAt,
Uses: data.Uses,
})
}
}

View file

@ -56,7 +56,6 @@ func (ctrl *V1Controller) HandleItemsCreate() http.HandlerFunc {
}
server.Respond(w, http.StatusCreated, item)
}
}
@ -154,8 +153,7 @@ func (ctrl *V1Controller) HandleItemUpdate() http.HandlerFunc {
func (ctrl *V1Controller) HandleItemsImport() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Max upload size of 10 MB - TODO: Set via config
err := r.ParseMultipartForm(10 << 20)
err := r.ParseMultipartForm(ctrl.maxUploadSize << 20)
if err != nil {
log.Err(err).Msg("failed to parse multipart form")
server.RespondServerError(w)