fix db function naming, connections query, and nil map init
This commit is contained in:
@@ -31,13 +31,13 @@ func getUser(ctx context.Context, token string) (*User, error) {
|
||||
user, err := CacheGetUserById(userId)
|
||||
if err != nil {
|
||||
user = &User{Id: userId}
|
||||
if err = DbGetUserById(ctx, user); err != nil {
|
||||
if err = DbUserGetById(ctx, user); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = DbGetUserGroups(ctx, user); err != nil {
|
||||
if err = DbUserGetGroups(ctx, user); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = DbGetUserConnections(ctx, user); err != nil {
|
||||
if err = DbUserGetConnections(ctx, user); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
CacheSaveUser(user)
|
||||
@@ -50,10 +50,10 @@ func getGroup(ctx context.Context, groupId uint32) (*Group, error) {
|
||||
group, err := CacheGetGroup(groupId)
|
||||
if err != nil {
|
||||
group = &Group{Id: groupId}
|
||||
if err = DbGetGroupById(ctx, group); err != nil {
|
||||
if err = DbGroupGetById(ctx, group); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = DbGetGroupMembers(ctx, group); err != nil {
|
||||
if err = DbGroupGetMembers(ctx, group); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
CacheSaveGroup(group)
|
||||
@@ -214,6 +214,33 @@ func HttpHandleUserMessage(response http.ResponseWriter, request *http.Request)
|
||||
return
|
||||
}
|
||||
|
||||
targetId, err := ConvertStringUint32(request.FormValue("recipientid"))
|
||||
if err != nil {
|
||||
http.Error(response, "invalid recipient id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
target, err := CacheGetUserById(targetId)
|
||||
if err != nil {
|
||||
target = &User{Id: targetId}
|
||||
err = DbUserGetById(ctx, target)
|
||||
if err != nil {
|
||||
http.Error(response, "invalid recipient id", http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
if user.Connections[target.Id] == nil {
|
||||
http.Error(response, "invalid recipient id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
message := request.FormValue("message")
|
||||
if message == "" {
|
||||
http.Error(response, "empty message", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
WsSendToUser(user, target, message)
|
||||
}
|
||||
|
||||
func HttpHandleNewToken(response http.ResponseWriter, request *http.Request) {
|
||||
@@ -243,15 +270,15 @@ func HttpHandleNewToken(response http.ResponseWriter, request *http.Request) {
|
||||
user, err = CacheGetUserByName(username)
|
||||
if err != nil {
|
||||
user = &User{Name: username}
|
||||
if err = DbGetUserByName(ctx, user); err != nil {
|
||||
if err = DbUserGetByName(ctx, user); err != nil {
|
||||
http.Error(response, "bad login1", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
if err = DbGetUserGroups(ctx, user); err != nil {
|
||||
if err = DbUserGetGroups(ctx, user); err != nil {
|
||||
http.Error(response, "bad login1", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
if err = DbGetUserConnections(ctx, user); err != nil {
|
||||
if err = DbUserGetConnections(ctx, user); err != nil {
|
||||
http.Error(response, "bad login1", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
@@ -309,7 +336,7 @@ func HttpHandeGroupCreate(response http.ResponseWriter, request *http.Request) {
|
||||
group.EnableUserColors = true
|
||||
}
|
||||
|
||||
err = DbGroupSaveWithoutUsers(ctx, &group)
|
||||
err = DbGroupSave(ctx, &group)
|
||||
if err != nil {
|
||||
http.Error(response, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
@@ -486,7 +513,7 @@ func HttpHandleGroupChangeOwner(response http.ResponseWriter, request *http.Requ
|
||||
newOwner, err := CacheGetUserByName(newOwnerName)
|
||||
if err != nil {
|
||||
newOwner = &User{Name: newOwnerName}
|
||||
err = DbGetUserByName(ctx, newOwner)
|
||||
err = DbUserGetByName(ctx, newOwner)
|
||||
if err != nil {
|
||||
http.Error(response, "user not in group", http.StatusBadRequest)
|
||||
return
|
||||
@@ -517,30 +544,33 @@ func HttpHandleGroupMessage(response http.ResponseWriter, request *http.Request)
|
||||
}
|
||||
|
||||
ctx := request.Context()
|
||||
|
||||
user, err := getUser(ctx, request.FormValue("token"))
|
||||
if err != nil {
|
||||
http.Error(response, "invalid token", http.StatusUnauthorized)
|
||||
http.Error(response, "invalid token", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
groupIdStr := request.FormValue("groupid")
|
||||
groupId, err := ConvertStringUint32(groupIdStr)
|
||||
if err != nil {
|
||||
http.Error(response, "no such group", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
targetStr := request.FormValue("subject")
|
||||
if targetStr == "" {
|
||||
http.Error(response, "invalid subject", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
targetId, err := ConvertStringUint32(targetStr)
|
||||
if err != nil {
|
||||
http.Error(response, "invalid subject", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
group, err := getGroup(ctx, groupId)
|
||||
|
||||
content := request.FormValue("content")
|
||||
if content == "" {
|
||||
http.Error(response, "invalid content", http.StatusBadRequest)
|
||||
http.Error(response, "empty message", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = WsSendToGroup(ctx, targetId, user.Id, content)
|
||||
_, ok := group.Users[user.Id]
|
||||
if !ok {
|
||||
http.Error(response, "no such group", http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
err = WsSendToGroup(group, user, content)
|
||||
if err != nil {
|
||||
http.Error(response, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user