-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add text, command, location, custom message implementation (#12)
* Change the JSONPayload Content field to any * feat(message: users, groups, rooms): Add support text, command, location, custom message
- Loading branch information
Showing
8 changed files
with
445 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
Copyright © 2024 Carlson <[email protected]> | ||
*/ | ||
package agora_chat | ||
|
||
type MessageType string | ||
|
||
const ( | ||
MessageTypeText MessageType = "txt" // Text message | ||
MessageTypeImage MessageType = "img" // Image message | ||
MessageTypeAudio MessageType = "audio" // Voice message | ||
MessageTypeVideo MessageType = "video" // Video message | ||
MessageTypeFile MessageType = "file" // File message | ||
MessageTypeLoc MessageType = "loc" // Location message | ||
MessageTypeCmd MessageType = "cmd" // Command message | ||
MessageTypeCustom MessageType = "custom" // Custom message | ||
) | ||
|
||
// TextMessageBody represents the payload data for a text type message. | ||
type TextMessageBody struct { | ||
Msg string `json:"msg,omitempty"` | ||
} | ||
|
||
// CMDMessageBody represents the payload data of the command type message. | ||
type CMDMessageBody struct { | ||
Action string `json:"action,omitempty"` | ||
} | ||
|
||
// LocationMessageBody represents the payload data of the location type message. | ||
type LocationMessageBody struct { | ||
Lat string `json:"lat,omitempty"` | ||
Lng string `json:"lng,omitempty"` | ||
Addr string `json:"addr,omitempty"` | ||
} | ||
|
||
// CustomMessageBody represents the payload data of the custom type message. | ||
type CustomMessageBody struct { | ||
CustomEvent string `json:"customEvent,omitempty"` | ||
CustomExts map[string]string `json:"customExts,omitempty"` | ||
} | ||
|
||
type Message struct { | ||
From string `json:"from,omitempty"` | ||
To []string `json:"to,omitempty"` | ||
Type MessageType `json:"type,omitempty"` | ||
Body interface{} `json:"body,omitempty"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
Copyright © 2024 Carlson <[email protected]> | ||
*/ | ||
package agora_chat | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func validateMessage(message *Message) error { | ||
if message == nil { | ||
return fmt.Errorf("message must not be nil") | ||
} | ||
|
||
if message.To == nil || len(message.To) <= 0 { | ||
return fmt.Errorf("to must be specified") | ||
} | ||
|
||
if message.Type == "" { | ||
return fmt.Errorf("type must be specified") | ||
} | ||
|
||
if message.Body == nil { | ||
return fmt.Errorf("body must be specified") | ||
} | ||
|
||
if message.Type == MessageTypeText { | ||
textBody, ok := message.Body.(TextMessageBody) | ||
if !ok { | ||
return fmt.Errorf("invalid type: expected TextMessageBody") | ||
} | ||
return validateTextMessageBody(textBody) | ||
} | ||
|
||
if message.Type == MessageTypeCmd { | ||
cmdBody, ok := message.Body.(CMDMessageBody) | ||
if !ok { | ||
return fmt.Errorf("invalid type: expected CMDMessageBody") | ||
} | ||
return validateCMDMessageBody(cmdBody) | ||
} | ||
|
||
if message.Type == MessageTypeLoc { | ||
locBody, ok := message.Body.(LocationMessageBody) | ||
if !ok { | ||
return fmt.Errorf("invalid type: expected CMDMessageBody") | ||
} | ||
return validateLocationMessageBody(locBody) | ||
} | ||
|
||
return nil | ||
|
||
} | ||
|
||
func validateTextMessageBody(body TextMessageBody) error { | ||
if body.Msg == "" { | ||
return fmt.Errorf("msg is required when specifying TextMessageBody") | ||
} | ||
return nil | ||
} | ||
|
||
func validateCMDMessageBody(body CMDMessageBody) error { | ||
if body.Action == "" { | ||
return fmt.Errorf("action is required when specifying CMDMessageBody") | ||
} | ||
return nil | ||
} | ||
|
||
func validateLocationMessageBody(body LocationMessageBody) error { | ||
if body.Lat == "" { | ||
return fmt.Errorf("lat is required when specifying LocationMessageBody") | ||
} | ||
|
||
if body.Lng == "" { | ||
return fmt.Errorf("lng is required when specifying LocationMessageBody") | ||
} | ||
|
||
if body.Addr == "" { | ||
return fmt.Errorf("addr is required when specifying LocationMessageBody") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.