add cache for groups and clients, http sending message remains to be add
This commit is contained in:
@@ -68,7 +68,7 @@ func LoginHandler(response http.ResponseWriter, request *http.Request) {
|
||||
password := request.FormValue("password")
|
||||
|
||||
respondBadLogin := func() {
|
||||
http.Error(response, "bad login", http.StatusConflict)
|
||||
http.Error(response, "bad login", http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
if len(username) < 2 {
|
||||
@@ -101,46 +101,11 @@ func CreateGroupHandler(response http.ResponseWriter, request *http.Request) {
|
||||
if !isMethodAllowed(&response, request) {
|
||||
return
|
||||
}
|
||||
var anyAuthDone bool = false
|
||||
var user User
|
||||
ctx := request.Context()
|
||||
username := request.FormValue("username")
|
||||
password := request.FormValue("password")
|
||||
|
||||
respondBadLogin := func() {
|
||||
http.Error(response, "bad login", http.StatusConflict)
|
||||
}
|
||||
|
||||
if len(password) > 0 {
|
||||
if len(username) < 2 {
|
||||
http.Error(response, "no or too short nick", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
tmp, err := GetUserDataByName(ctx, username)
|
||||
if err != nil {
|
||||
respondBadLogin()
|
||||
return
|
||||
}
|
||||
|
||||
if bcrypt.CompareHashAndPassword([]byte(tmp.Password), []byte(password)) != nil {
|
||||
respondBadLogin()
|
||||
return
|
||||
}
|
||||
user = tmp
|
||||
anyAuthDone = true
|
||||
} else if token := request.FormValue("token"); len(token) > 0 {
|
||||
tmp, err := GetUserFromToken(token)
|
||||
if err != nil {
|
||||
respondBadLogin()
|
||||
return
|
||||
}
|
||||
user = tmp
|
||||
anyAuthDone = true
|
||||
}
|
||||
|
||||
if !anyAuthDone {
|
||||
http.Error(response, "no login or token", http.StatusBadRequest)
|
||||
user, err := GetUserFromToken(request.FormValue("token"))
|
||||
if err != nil {
|
||||
http.Error(response, "invalid token", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -150,7 +115,7 @@ func CreateGroupHandler(response http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
_, err := CreateChatGroupWithoutMembers(ctx, &ChatGroup{
|
||||
_, err = CreateChatGroupWithoutMembers(ctx, &ChatGroup{
|
||||
Name: groupName,
|
||||
CreatorId: user.Id,
|
||||
OwnerId: user.Id,
|
||||
@@ -163,3 +128,33 @@ func CreateGroupHandler(response http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
response.WriteHeader(http.StatusCreated)
|
||||
}
|
||||
|
||||
func SendMessageHandler(response http.ResponseWriter, request *http.Request) {
|
||||
groupId := request.PathValue("groupid")
|
||||
if groupId == "" {
|
||||
http.Error(response, "no group id", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
var user User
|
||||
var err error
|
||||
token := request.FormValue("token")
|
||||
if token == "" {
|
||||
http.Error(response, "no token", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if user, err = GetUserFromToken(token); err != nil || user == nil {
|
||||
http.Error(response, "invalid token", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
content := request.FormValue("content")
|
||||
if content == "" {
|
||||
http.Error(response, "no content", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var isInGroup bool
|
||||
for _, groupId := range user.MemberGroupsId {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user