-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from AllanCapistrano/feat/message-by-message-id
Feat/message by message
- Loading branch information
Showing
4 changed files
with
187 additions
and
67 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,55 @@ | ||
package messages | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"log" | ||
|
||
iotago "github.com/iotaledger/iota.go/v2" | ||
) | ||
|
||
// Get a message on the node by a given message ID. | ||
func getMessageByMessageID(nodeUrl string, messageIdHex string) (*iotago.Message, error) { | ||
node := iotago.NewNodeHTTPAPIClient(nodeUrl) | ||
|
||
messageId, err := iotago.MessageIDFromHexString(messageIdHex) | ||
if err != nil { | ||
return &iotago.Message{}, errors.New("unable to convert message ID from hex to message ID representation") | ||
} | ||
|
||
messageReturned, err := node.MessageByMessageID(context.Background(), messageId) | ||
if err != nil { | ||
return &iotago.Message{}, errors.New("unable to get message by given message ID") | ||
} | ||
|
||
return messageReturned, nil | ||
} | ||
|
||
// Get a message on the node by a given message ID, into a custom message type. | ||
func GetMessageFormattedByMessageID(nodeUrl string, messageIdHex string) (Message, error) { | ||
var message Message | ||
|
||
messageReturned, err := getMessageByMessageID(nodeUrl, messageIdHex) | ||
if err != nil { | ||
log.Println(err) | ||
|
||
message = Message{ | ||
Index: "Error", | ||
Content: err.Error(), | ||
} | ||
} else { | ||
message, err = formatMessagePayloadWithoutIndex(messageReturned) | ||
if err != nil { | ||
log.Println(err) | ||
|
||
message = Message{ | ||
Index: "Error", | ||
Content: err.Error(), | ||
} | ||
} | ||
|
||
sanitizeMessage(&message) | ||
} | ||
|
||
return message, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,114 @@ | ||
package messages | ||
|
||
import "github.com/allancapistrano/tangle-client-go/utils" | ||
import ( | ||
"errors" | ||
"strings" | ||
|
||
iotago "github.com/iotaledger/iota.go/v2" | ||
|
||
"github.com/allancapistrano/tangle-client-go/utils" | ||
) | ||
|
||
type Message struct { | ||
Index string `json:"index"` | ||
Content string `json:"content"` | ||
} | ||
|
||
// Sanitizes a message. | ||
func SanitizeMessage(message *Message) { | ||
func sanitizeMessage(message *Message) { | ||
message.Content = utils.SanitizeString(message.Content) | ||
message.Index = utils.SanitizeString(message.Index) | ||
} | ||
|
||
// Formats the message payload into a custom message type. | ||
func formatMessagePayload(message iotago.Message, messageIndex string) (Message, error) { | ||
payloadInString, err := utils.SerializeMessagePayload(message.Payload, true) | ||
if err != nil { | ||
return Message{}, err | ||
} | ||
|
||
index := "" | ||
content := "" | ||
|
||
if strings.Contains(payloadInString, "/") { | ||
payloadTemp := strings.Split(payloadInString, "/") | ||
|
||
index = payloadTemp[0] | ||
content = payloadTemp[1] | ||
} else if strings.Contains(payloadInString, "\v") { | ||
payloadTemp := strings.Split(payloadInString, "\v") | ||
|
||
if len(payloadTemp) == 2 { | ||
index = payloadTemp[0] | ||
content = payloadTemp[1] | ||
} else if len(payloadTemp) == 3 { | ||
index = payloadTemp[1] | ||
content = payloadTemp[2] | ||
} else { | ||
return Message{}, errors.New("unexpected array length") | ||
} | ||
} else if strings.Contains(payloadInString, "\t") { | ||
payloadTemp := strings.Split(payloadInString, "\t") | ||
|
||
index = payloadTemp[0] | ||
content = payloadTemp[1] | ||
} else if strings.Contains(payloadInString, messageIndex) { | ||
payloadTemp := strings.Split(payloadInString, messageIndex) | ||
|
||
index = messageIndex | ||
content = payloadTemp[1] | ||
} else { | ||
return Message{}, errors.New("malformed payload") | ||
} | ||
|
||
formattedMessage := Message{ | ||
Index: strings.Trim(index, "\f"), | ||
Content: strings.Trim(content, "\f"), | ||
} | ||
|
||
return formattedMessage, nil | ||
} | ||
|
||
// Formats the message payload into a custom message type. | ||
func formatMessagePayloadWithoutIndex(message *iotago.Message) (Message, error) { | ||
payloadInString, err := utils.SerializeMessagePayload(message.Payload, true) | ||
if err != nil { | ||
return Message{}, err | ||
} | ||
|
||
index := "" | ||
content := "" | ||
|
||
if strings.Contains(payloadInString, "/") { | ||
payloadTemp := strings.Split(payloadInString, "/") | ||
|
||
index = payloadTemp[0] | ||
content = payloadTemp[1] | ||
} else if strings.Contains(payloadInString, "\v") { | ||
payloadTemp := strings.Split(payloadInString, "\v") | ||
|
||
if len(payloadTemp) == 2 { | ||
index = payloadTemp[0] | ||
content = payloadTemp[1] | ||
} else if len(payloadTemp) == 3 { | ||
index = payloadTemp[1] | ||
content = payloadTemp[2] | ||
} else { | ||
return Message{}, errors.New("unexpected array length") | ||
} | ||
} else if strings.Contains(payloadInString, "\t") { | ||
payloadTemp := strings.Split(payloadInString, "\t") | ||
|
||
index = payloadTemp[0] | ||
content = payloadTemp[1] | ||
} else { | ||
return Message{}, errors.New("malformed payload") | ||
} | ||
|
||
formattedMessage := Message{ | ||
Index: strings.Trim(index, "\f"), | ||
Content: strings.Trim(content, "\f"), | ||
} | ||
|
||
return formattedMessage, nil | ||
} |