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
|
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 {
|
func DbSaveGroupWithoutClients(ctx context.Context, group *Group) error {
|
||||||
err := dbConn.QueryRow(ctx, `
|
err := dbConn.QueryRow(ctx, `
|
||||||
INSERT INTO chat_groups (name, creator_id, owner_id, enable_client_colors, color_red, color_green, color_blue, created_at)
|
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)
|
http.Error(response, "invalid token", http.StatusUnauthorized)
|
||||||
}
|
}
|
||||||
|
|
||||||
subject := request.FormValue("subject")
|
targetStr := request.FormValue("subject")
|
||||||
if subject == "" {
|
if targetStr == "" {
|
||||||
|
http.Error(response, "invalid subject", http.StatusBadRequest)
|
||||||
|
}
|
||||||
|
targetId, err := ConvertStringUint32(targetStr)
|
||||||
|
if err != nil {
|
||||||
http.Error(response, "invalid subject", http.StatusBadRequest)
|
http.Error(response, "invalid subject", http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -313,6 +317,6 @@ func HttpHandleNewMessage(response http.ResponseWriter, request *http.Request) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(response, "invalid token", http.StatusUnauthorized)
|
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 {
|
func WsSendToGroup(ctx context.Context, groupId uint32, senderId uint32, message string) error {
|
||||||
subjectFlt, ok := (*clientMessage)["subject"].(float64)
|
group, err := CacheGetGroup(groupId)
|
||||||
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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
var msg = map[string]any{
|
return errors.New("group invalid")
|
||||||
"from": "server",
|
}
|
||||||
"error": "subject 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 {
|
for groupClientId := range group.Clients {
|
||||||
@@ -119,16 +101,15 @@ func handleAuthenticatedMessage(client *Client, clientMessage *map[string]any) b
|
|||||||
"from": "group",
|
"from": "group",
|
||||||
"group": group.Id,
|
"group": group.Id,
|
||||||
"sender": client.Name,
|
"sender": client.Name,
|
||||||
"content": content,
|
"content": message,
|
||||||
}
|
}
|
||||||
sendMessageCloseIfTimeout(groupClient, &msg)
|
sendMessageCloseIfTimeout(groupClient, &msg)
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
var msg = map[string]any{
|
|
||||||
"from": "server",
|
|
||||||
}
|
}
|
||||||
sendMessageCloseIfTimeout(client)
|
|
||||||
|
|
||||||
|
func handleAuthenticatedMessage(client *Client, clientMessage *map[string]any) bool {
|
||||||
|
sendMessageCloseIfTimeout(client, clientMessage)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user