mirror of
https://github.com/usememos/memos.git
synced 2025-02-12 01:10:38 +01:00
fix: user api
This commit is contained in:
parent
3fa918169e
commit
825bea59f0
@ -25,6 +25,7 @@ type MemoPatch struct {
|
||||
type MemoFind struct {
|
||||
Id *int
|
||||
CreatorId *int
|
||||
RowStatus *string
|
||||
}
|
||||
|
||||
type MemoDelete struct {
|
||||
|
@ -29,8 +29,9 @@ type UserPatch struct {
|
||||
type UserFind struct {
|
||||
Id *int `json:"id"`
|
||||
|
||||
Name *string `json:"name"`
|
||||
OpenId *string
|
||||
Name *string `json:"name"`
|
||||
Password *string
|
||||
OpenId *string
|
||||
}
|
||||
|
||||
type UserRenameCheck struct {
|
||||
|
@ -63,6 +63,17 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
|
||||
memoFind := &api.MemoFind{
|
||||
CreatorId: &userId,
|
||||
}
|
||||
showHiddenMemo, err := strconv.ParseBool(c.QueryParam("hidden"))
|
||||
if err != nil {
|
||||
showHiddenMemo = false
|
||||
}
|
||||
|
||||
rowStatus := "NORMAL"
|
||||
if showHiddenMemo {
|
||||
rowStatus = "HIDDEN"
|
||||
}
|
||||
memoFind.RowStatus = &rowStatus
|
||||
|
||||
list, err := s.MemoService.FindMemoList(memoFind)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch memo list").SetInternal(err)
|
||||
|
@ -51,14 +51,51 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err)
|
||||
}
|
||||
|
||||
isUsable := true
|
||||
if user != nil {
|
||||
isUsable = false
|
||||
}
|
||||
|
||||
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
||||
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(user)); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal user response").SetInternal(err)
|
||||
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(isUsable)); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal rename check response").SetInternal(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
g.PATCH("user/me", func(c echo.Context) error {
|
||||
g.POST("/user/password_check", func(c echo.Context) error {
|
||||
userId := c.Get(getUserIdContextKey()).(int)
|
||||
userPasswordCheck := &api.UserPasswordCheck{}
|
||||
if err := json.NewDecoder(c.Request().Body).Decode(userPasswordCheck); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post user password check request").SetInternal(err)
|
||||
}
|
||||
|
||||
if userPasswordCheck.Password == "" {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post user password check request")
|
||||
}
|
||||
|
||||
userFind := &api.UserFind{
|
||||
Id: &userId,
|
||||
Password: &userPasswordCheck.Password,
|
||||
}
|
||||
user, err := s.UserService.FindUser(userFind)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err)
|
||||
}
|
||||
|
||||
isValid := false
|
||||
if user != nil {
|
||||
isValid = true
|
||||
}
|
||||
|
||||
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
||||
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(isValid)); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal password check response").SetInternal(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
g.PATCH("/user/me", func(c echo.Context) error {
|
||||
userId := c.Get(getUserIdContextKey()).(int)
|
||||
userPatch := &api.UserPatch{
|
||||
Id: userId,
|
||||
@ -67,7 +104,7 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch user request").SetInternal(err)
|
||||
}
|
||||
|
||||
if *userPatch.ResetOpenId {
|
||||
if userPatch.ResetOpenId != nil && *userPatch.ResetOpenId {
|
||||
openId := common.GenUUID()
|
||||
userPatch.OpenId = &openId
|
||||
}
|
||||
|
@ -104,10 +104,10 @@ func patchMemo(db *DB, patch *api.MemoPatch) (*api.Memo, error) {
|
||||
set, args := []string{}, []interface{}{}
|
||||
|
||||
if v := patch.Content; v != nil {
|
||||
set, args = append(set, "content = ?"), append(args, v)
|
||||
set, args = append(set, "content = ?"), append(args, *v)
|
||||
}
|
||||
if v := patch.RowStatus; v != nil {
|
||||
set, args = append(set, "row_status = ?"), append(args, v)
|
||||
set, args = append(set, "row_status = ?"), append(args, *v)
|
||||
}
|
||||
|
||||
args = append(args, patch.Id)
|
||||
@ -150,6 +150,9 @@ func findMemoList(db *DB, find *api.MemoFind) ([]*api.Memo, error) {
|
||||
if v := find.CreatorId; v != nil {
|
||||
where, args = append(where, "creator_id = ?"), append(args, *v)
|
||||
}
|
||||
if v := find.RowStatus; v != nil {
|
||||
where, args = append(where, "row_status = ?"), append(args, *v)
|
||||
}
|
||||
|
||||
rows, err := db.Db.Query(`
|
||||
SELECT
|
||||
|
@ -69,7 +69,7 @@ func createShortcut(db *DB, create *api.ShortcutCreate) (*api.Shortcut, error) {
|
||||
INSERT INTO shortcut (
|
||||
title,
|
||||
payload,
|
||||
creator_id,
|
||||
creator_id
|
||||
)
|
||||
VALUES (?, ?, ?)
|
||||
RETURNING id, title, payload, creator_id, created_ts, updated_ts, row_status
|
||||
@ -103,6 +103,7 @@ func createShortcut(db *DB, create *api.ShortcutCreate) (*api.Shortcut, error) {
|
||||
|
||||
func patchShortcut(db *DB, patch *api.ShortcutPatch) (*api.Shortcut, error) {
|
||||
set, args := []string{}, []interface{}{}
|
||||
|
||||
if v := patch.Title; v != nil {
|
||||
set, args = append(set, "title = ?"), append(args, *v)
|
||||
}
|
||||
|
@ -57,7 +57,11 @@ func createUser(db *DB, create *api.UserCreate) (*api.User, error) {
|
||||
)
|
||||
VALUES (?, ?, ?)
|
||||
RETURNING id, name, password, open_id, created_ts, updated_ts
|
||||
`)
|
||||
`,
|
||||
create.Name,
|
||||
create.Password,
|
||||
create.OpenId,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, FormatError(err)
|
||||
}
|
||||
@ -83,14 +87,15 @@ func patchUser(db *DB, patch *api.UserPatch) (*api.User, error) {
|
||||
set, args := []string{}, []interface{}{}
|
||||
|
||||
if v := patch.Name; v != nil {
|
||||
set, args = append(set, "name = ?"), append(args, *v)
|
||||
set, args = append(set, "name = ?"), append(args, v)
|
||||
}
|
||||
if v := patch.Password; v != nil {
|
||||
set, args = append(set, "password = ?"), append(args, *v)
|
||||
set, args = append(set, "password = ?"), append(args, v)
|
||||
}
|
||||
if v := patch.OpenId; v != nil {
|
||||
set, args = append(set, "open_id = ?"), append(args, *v)
|
||||
set, args = append(set, "open_id = ?"), append(args, v)
|
||||
}
|
||||
|
||||
args = append(args, patch.Id)
|
||||
|
||||
row, err := db.Db.Query(`
|
||||
|
@ -78,7 +78,7 @@ namespace api {
|
||||
export function checkUsernameUsable(name: string) {
|
||||
return request<boolean>({
|
||||
method: "POST",
|
||||
url: "/api/user/checkusername",
|
||||
url: "/api/user/rename_check",
|
||||
data: {
|
||||
name,
|
||||
},
|
||||
@ -88,15 +88,15 @@ namespace api {
|
||||
export function checkPasswordValid(password: string) {
|
||||
return request<boolean>({
|
||||
method: "POST",
|
||||
url: "/api/user/validpassword",
|
||||
url: "/api/user/password_check",
|
||||
data: {
|
||||
password,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function updateUserinfo(userinfo: Partial<{ name: string; password: string }>) {
|
||||
return request({
|
||||
export function updateUserinfo(userinfo: Partial<{ name: string; password: string; resetOpenId: boolean }>) {
|
||||
return request<Model.User>({
|
||||
method: "PATCH",
|
||||
url: "/api/user/me",
|
||||
data: userinfo,
|
||||
@ -120,7 +120,7 @@ namespace api {
|
||||
export function getMyDeletedMemos() {
|
||||
return request<Model.Memo[]>({
|
||||
method: "GET",
|
||||
url: "/api/memo/?hidden=true",
|
||||
url: "/api/memo?hidden=true",
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -55,12 +55,14 @@ class UserService {
|
||||
}
|
||||
|
||||
public async resetOpenId(): Promise<string> {
|
||||
const openId = await api.resetOpenId();
|
||||
const user = await api.updateUserinfo({
|
||||
resetOpenId: true,
|
||||
});
|
||||
appStore.dispatch({
|
||||
type: "RESET_OPENID",
|
||||
payload: openId,
|
||||
payload: user.openId,
|
||||
});
|
||||
return openId;
|
||||
return user.openId;
|
||||
}
|
||||
|
||||
private convertResponseModelUser(user: Model.User): Model.User {
|
||||
|
Loading…
x
Reference in New Issue
Block a user