added profile getter and setter

This commit is contained in:
Roland Osborne 2022-01-19 00:03:46 -08:00
parent 6412bd4df1
commit 6a377a7fc0
8 changed files with 133 additions and 16 deletions

View File

@ -359,7 +359,6 @@ paths:
application/json: application/json:
schema: schema:
type: string type: string
'401':
'401': '401':
description: permission denied description: permission denied
'500': '500':
@ -726,6 +725,8 @@ paths:
description: account disabled description: account disabled
'500': '500':
description: internal server error description: internal server error
/profile/data:
put: put:
tags: tags:
- profile - profile
@ -772,6 +773,30 @@ paths:
description: account disabled description: account disabled
'500': '500':
description: internal server error description: internal server error
put:
tags:
- profile
description: Set base64 encode image data for profile. Access granted to app tokens of account holder.
operationId: set-profile-image
security:
- bearerAuth: []
responses:
'200':
description: success
'401':
description: permission denied
'405':
description: invalid image
'410':
description: account disabled
'500':
description: internal server error
requestBody:
content:
application/octet-stream:
schema:
type: string
format: binary
/profile/message: /profile/message:
get: get:
@ -3992,16 +4017,12 @@ components:
ProfileData: ProfileData:
type: object type: object
properties: properties:
handle:
type: string
name: name:
type: string type: string
description: description:
type: string type: string
location: location:
type: string type: string
image:
type: string
Account: Account:
type: object type: object
@ -4477,7 +4498,7 @@ components:
basicCredentials: basicCredentials:
type: http type: http
schema scheme: basic
bearerAuth: bearerAuth:
type: http type: http

View File

@ -27,7 +27,7 @@ func Authorize(w http.ResponseWriter, r *http.Request) {
var token string var token string
err := ParseRequest(r, w, &token) err := ParseRequest(r, w, &token)
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest); w.WriteHeader(http.StatusBadRequest)
return return
} }

View File

@ -0,0 +1,34 @@
package databag
import (
"net/http"
)
func GetProfile(w http.ResponseWriter, r *http.Request) {
account, res := BearerAppToken(r, true);
if res != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
if account.Disabled {
w.WriteHeader(http.StatusGone);
return
}
detail := account.AccountDetail
// send profile data
profile := Profile {
Guid: account.Guid,
Handle: account.Username,
Name: detail.Name,
Description: detail.Description,
Location: detail.Location,
Image: detail.Image,
Revision: account.ProfileRevision,
Version: APP_VERSION,
Node: "https://" + getStrConfigValue(CONFIG_DOMAIN, "") + "/",
}
WriteResponse(w, profile)
}

View File

@ -13,11 +13,6 @@ import (
"net/http" "net/http"
) )
func GetProfile(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}
func GetProfileImage(w http.ResponseWriter, r *http.Request) { func GetProfileImage(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
@ -28,7 +23,8 @@ func GetProfileMessage(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
func SetProfile(w http.ResponseWriter, r *http.Request) { func SetProfileImage(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }

View File

@ -0,0 +1,41 @@
package databag
import (
"net/http"
"databag/internal/store"
)
func SetProfile(w http.ResponseWriter, r *http.Request) {
account, res := BearerAppToken(r, true);
if res != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
if account.Disabled {
w.WriteHeader(http.StatusGone);
return
}
detail := account.AccountDetail
// extract profile data from body
var profileData ProfileData;
err := ParseRequest(r, w, &profileData)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// update record
detail.Name = profileData.Name
detail.Location = profileData.Location
detail.Description = profileData.Description
if store.DB.Save(&detail).Error != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}

View File

@ -244,11 +244,9 @@ type Profile struct {
} }
type ProfileData struct { type ProfileData struct {
Handle string `json:"handle,omitempty"`
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"` Description string `json:"description,omitempty"`
Location string `json:"location,omitempty"` Location string `json:"location,omitempty"`
Image string `json:"image,omitempty"`
} }
type Revision struct { type Revision struct {

View File

@ -860,10 +860,17 @@ var routes = Routes{
Route{ Route{
"SetProfile", "SetProfile",
strings.ToUpper("Put"), strings.ToUpper("Put"),
"/profile", "/profile/data",
SetProfile, SetProfile,
}, },
Route{
"SetProfileImage",
strings.ToUpper("Put"),
"/profile/image",
SetProfileImage,
},
Route{ Route{
"AddGroup", "AddGroup",
strings.ToUpper("Post"), strings.ToUpper("Post"),
@ -899,3 +906,4 @@ var routes = Routes{
Status, Status,
}, },
} }

View File

@ -62,5 +62,24 @@ func TestAttachAccount(t *testing.T) {
assert.Less(t, cur - 60, auth.Timestamp) assert.Less(t, cur - 60, auth.Timestamp)
// set profile // set profile
profileData := ProfileData{
Name: "Namer",
Location: "San Francisco",
Description: "databaggerr",
};
r, w, _ = NewRequest("PUT", "/profile/data", &profileData)
SetBearerAuth(r, access)
SetProfile(w, r)
assert.NoError(t, ReadResponse(w, nil))
// get profile
r, w, _ = NewRequest("GET", "/profile", nil)
SetBearerAuth(r, access)
GetProfile(w, r)
var profile Profile
assert.NoError(t, ReadResponse(w, &profile))
assert.Equal(t, guid, profile.Guid)
assert.Equal(t, "user", profile.Handle)
assert.Equal(t, "Namer", profile.Name)
} }