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