change message sending to work via http
This commit is contained in:
@@ -90,6 +90,14 @@ func DbSetClientByIdWithoutGroups(ctx context.Context, client *Client) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func DbSetClientById(ctx context.Context, client *Client) error {
|
||||
err := DbSetClientByIdWithoutGroups(ctx, client)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return DbSetClientGroups(ctx, client)
|
||||
}
|
||||
|
||||
func DbSaveGroupWithoutClients(ctx context.Context, group *Group) error {
|
||||
err := dbConn.QueryRow(ctx, `
|
||||
INSERT INTO chat_groups (name, creator_id, owner_id, enable_client_colors, color_red, color_green, color_blue, created_at)
|
||||
|
||||
@@ -299,8 +299,12 @@ func HttpHandleNewMessage(response http.ResponseWriter, request *http.Request) {
|
||||
http.Error(response, "invalid token", http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
subject := request.FormValue("subject")
|
||||
if subject == "" {
|
||||
targetStr := request.FormValue("subject")
|
||||
if targetStr == "" {
|
||||
http.Error(response, "invalid subject", http.StatusBadRequest)
|
||||
}
|
||||
targetId, err := ConvertStringUint32(targetStr)
|
||||
if err != nil {
|
||||
http.Error(response, "invalid subject", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
@@ -313,6 +317,6 @@ func HttpHandleNewMessage(response http.ResponseWriter, request *http.Request) {
|
||||
if err != nil {
|
||||
http.Error(response, "invalid token", http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
|
||||
ctx := request.Context()
|
||||
WsSendToGroup(ctx, targetId, clientId, content)
|
||||
}
|
||||
|
||||
+14
-33
@@ -77,36 +77,18 @@ func sendToAllMessageCloseIfTimeout(message *map[string]any) {
|
||||
}
|
||||
}
|
||||
|
||||
func handleAuthenticatedMessage(client *Client, clientMessage *map[string]any) bool {
|
||||
subjectFlt, ok := (*clientMessage)["subject"].(float64)
|
||||
subject := uint32(subjectFlt)
|
||||
if !ok {
|
||||
var msg = map[string]any{
|
||||
"from": "server",
|
||||
"error": "subject invalid",
|
||||
}
|
||||
sendMessageCloseIfTimeout(client, &msg)
|
||||
return true
|
||||
}
|
||||
|
||||
content, ok := (*clientMessage)["content"].(string)
|
||||
if !ok {
|
||||
var msg = map[string]any{
|
||||
"from": "server",
|
||||
"error": "content invalid",
|
||||
}
|
||||
sendMessageCloseIfTimeout(client, &msg)
|
||||
return true
|
||||
}
|
||||
|
||||
group, err := CacheGetGroup(subject)
|
||||
func WsSendToGroup(ctx context.Context, groupId uint32, senderId uint32, message string) error {
|
||||
group, err := CacheGetGroup(groupId)
|
||||
if err != nil {
|
||||
var msg = map[string]any{
|
||||
"from": "server",
|
||||
"error": "subject invalid",
|
||||
return errors.New("group invalid")
|
||||
}
|
||||
|
||||
client, err := CacheGetClientById(senderId)
|
||||
if err == nil {
|
||||
err = DbSetClientById(ctx, client)
|
||||
if err != nil {
|
||||
return errors.New("non existing sender")
|
||||
}
|
||||
sendMessageCloseIfTimeout(client, &msg)
|
||||
return true
|
||||
}
|
||||
|
||||
for groupClientId := range group.Clients {
|
||||
@@ -119,16 +101,15 @@ func handleAuthenticatedMessage(client *Client, clientMessage *map[string]any) b
|
||||
"from": "group",
|
||||
"group": group.Id,
|
||||
"sender": client.Name,
|
||||
"content": content,
|
||||
"content": message,
|
||||
}
|
||||
sendMessageCloseIfTimeout(groupClient, &msg)
|
||||
}
|
||||
|
||||
var msg = map[string]any{
|
||||
"from": "server",
|
||||
return nil
|
||||
}
|
||||
sendMessageCloseIfTimeout(client)
|
||||
|
||||
func handleAuthenticatedMessage(client *Client, clientMessage *map[string]any) bool {
|
||||
sendMessageCloseIfTimeout(client, clientMessage)
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user