add hub function
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package httpRequest
|
||||
|
||||
import (
|
||||
json2 "encoding/json"
|
||||
"encoding/json"
|
||||
"maps"
|
||||
"net/http"
|
||||
"slices"
|
||||
@@ -22,7 +22,7 @@ import (
|
||||
)
|
||||
|
||||
func HandleDm(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
if !validCheckWithResponseOnFail(response, request, normal) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ func HandleDm(response http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
conn, ok := getConnectionWithResponseOnFail(&response, request, user)
|
||||
conn, ok := getConnectionWithResponseOnFail(response, request.FormValue("connectionid"), user)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
@@ -87,7 +87,7 @@ func HandleDm(response http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
|
||||
func HandleUserGetConnectionsUnreadMessages(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
if !validCheckWithResponseOnFail(response, request, normal) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -115,22 +115,22 @@ func HandleUserGetConnectionsUnreadMessages(response http.ResponseWriter, reques
|
||||
|
||||
for _, connId := range connectionIds {
|
||||
count, _ := cache.GetConnectionsUnreadMessages(user.Id, connId)
|
||||
cache.DeallocateConnectionsUnreadMessages(user.Id, connId)
|
||||
cache.DeleteConnectionsUnreadMessages(user.Id, connId)
|
||||
result = append(result, count)
|
||||
}
|
||||
|
||||
json, err := json2.Marshal(result)
|
||||
counts, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
response.Write(json)
|
||||
response.Write(counts)
|
||||
}
|
||||
|
||||
func HandleUserGetConnectionMessages(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
if !validCheckWithResponseOnFail(response, request, normal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -140,7 +140,7 @@ func HandleUserGetConnectionMessages(response http.ResponseWriter, request *http
|
||||
return
|
||||
}
|
||||
|
||||
conn, ok := getConnectionWithResponseOnFail(&response, request, user)
|
||||
conn, ok := getConnectionWithResponseOnFail(response, request.FormValue("connectionid"), user)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
@@ -188,18 +188,18 @@ func HandleUserGetConnectionMessages(response http.ResponseWriter, request *http
|
||||
}
|
||||
}
|
||||
|
||||
json, err := json2.Marshal(messages)
|
||||
data, err := json.Marshal(messages)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteHeader(http.StatusOK)
|
||||
response.Write(json)
|
||||
response.Write(data)
|
||||
}
|
||||
|
||||
func HandleUserNewConnection(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
if !validCheckWithResponseOnFail(response, request, normal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -210,12 +210,12 @@ func HandleUserNewConnection(response http.ResponseWriter, request *http.Request
|
||||
}
|
||||
recipientId, err := convertions.StringToUuid(request.FormValue("recipient"))
|
||||
if err != nil {
|
||||
http.Error(response, "no such user", http.StatusUnauthorized)
|
||||
http.Error(response, "invalid recipient", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
recipient, err := getUserById(ctx, recipientId)
|
||||
if err != nil {
|
||||
http.Error(response, "no such user", http.StatusUnauthorized)
|
||||
http.Error(response, "no such user", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -257,7 +257,7 @@ func HandleUserNewConnection(response http.ResponseWriter, request *http.Request
|
||||
}
|
||||
|
||||
func HandleUserDeleteConnection(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
if !validCheckWithResponseOnFail(response, request, normal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -268,7 +268,7 @@ func HandleUserDeleteConnection(response http.ResponseWriter, request *http.Requ
|
||||
return
|
||||
}
|
||||
|
||||
conn, ok := getConnectionWithResponseOnFail(&response, request, user)
|
||||
conn, ok := getConnectionWithResponseOnFail(response, request.FormValue("connectionid"), user)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
@@ -296,7 +296,7 @@ func HandleUserDeleteConnection(response http.ResponseWriter, request *http.Requ
|
||||
}
|
||||
|
||||
func HandleUserElevateConnection(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
if !validCheckWithResponseOnFail(response, request, normal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -305,7 +305,7 @@ func HandleUserElevateConnection(response http.ResponseWriter, request *http.Req
|
||||
http.Error(response, "invalid token", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
conn, ok := getConnectionWithResponseOnFail(&response, request, user)
|
||||
conn, ok := getConnectionWithResponseOnFail(response, request.FormValue("connectionid"), user)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
@@ -363,7 +363,7 @@ func HandleUserElevateConnection(response http.ResponseWriter, request *http.Req
|
||||
}
|
||||
|
||||
func HandleUserDeElevateConnection(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
if !validCheckWithResponseOnFail(response, request, normal) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -374,7 +374,7 @@ func HandleUserDeElevateConnection(response http.ResponseWriter, request *http.R
|
||||
return
|
||||
}
|
||||
|
||||
conn, ok := getConnectionWithResponseOnFail(&response, request, user)
|
||||
conn, ok := getConnectionWithResponseOnFail(response, request.FormValue("connectionid"), user)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
@@ -409,7 +409,7 @@ func HandleUserDeElevateConnection(response http.ResponseWriter, request *http.R
|
||||
}
|
||||
|
||||
func HandleUserGetConnections(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
if !validCheckWithResponseOnFail(response, request, normal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -423,12 +423,12 @@ func HandleUserGetConnections(response http.ResponseWriter, request *http.Reques
|
||||
user.Mu.RLock()
|
||||
connections := slices.Collect(maps.Values(user.Connections))
|
||||
user.Mu.RUnlock()
|
||||
json, err := json2.Marshal(connections)
|
||||
data, err := json.Marshal(connections)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteHeader(http.StatusOK)
|
||||
response.Write(json)
|
||||
response.Write(data)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user