Skip to content

Commit

Permalink
Merge pull request #3 from AllanCapistrano/feat/message-by-message-id
Browse files Browse the repository at this point in the history
Feat/message by message
  • Loading branch information
AllanCapistrano authored Sep 28, 2023
2 parents 01ea23a + 9c84645 commit a603a93
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 67 deletions.
16 changes: 14 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,25 @@ func main() {
// messages.SubmitMessage(nodeURL, "LB_REPLY", "{asdfghjkl}", 15)

// Reading some messages by an index.
messages, err := messages.GetAllMessagesByIndex(nodeURL, "LB_ENTRY_REPLY")
messagesByIndex, err := messages.GetAllMessagesByIndex(nodeURL, "LB_ENTRY_REPLY")
if err != nil {
log.Fatal(err)
}

for _, v := range messages {
for _, v := range messagesByIndex {
fmt.Println([]byte(strings.Trim(v.Content, "\t")))
fmt.Printf("Index: %s | Content: %s\n", v.Index, v.Content)
}

messageID := "9597556533c5e91112c0b02244799a4d308ca007486e8e844d5d78b0f298b667"

message, err := messages.GetMessageFormattedByMessageID(
nodeURL,
messageID,
)
if err != nil {
log.Fatal(err)
}
fmt.Println(message.Index)
fmt.Println(message.Content)
}
79 changes: 16 additions & 63 deletions messages/getAllMessagesByIndex.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"context"
"errors"
"log"
"strings"

"github.com/allancapistrano/tangle-client-go/utils"
iotago "github.com/iotaledger/iota.go/v2"
)

Expand All @@ -28,82 +26,37 @@ func GetAllMessagesByIndex(nodeUrl string, index string) ([]Message, error) {

if msgIdsResponse.Count > 0 {
for i = 0; i < msgIdsResponse.Count; i++ {
messageId, err := iotago.MessageIDFromHexString(msgIdsResponse.MessageIDs[i])
if err != nil {
return nil, errors.New("unable to convert message ID from hex to message ID representation")
}
var message Message

messageReturned, err := node.MessageByMessageID(context.Background(), messageId)
if err != nil {
return nil, errors.New("unable to get message by given message ID")
}
messageReturned, err := getMessageByMessageID(nodeUrl, msgIdsResponse.MessageIDs[i])

message, err := formatMessagePayload(*messageReturned, index)
if err != nil {
log.Println(err)

message = Message{
Index: "Error",
Content: err.Error(),
}
}
} else {
message, err = formatMessagePayload(*messageReturned, index)

if err != nil {
log.Println(err)

SanitizeMessage(&message)
message = Message{
Index: "Error",
Content: err.Error(),
}
} else {
sanitizeMessage(&message)

messages = append(messages, message)
messages = append(messages, message)
}
}
}
} else {
log.Println("No messages with this index were found.")
}

return messages, nil
}

// 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
}
55 changes: 55 additions & 0 deletions messages/getMessageByMessageId.go
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
}
104 changes: 102 additions & 2 deletions messages/message.go
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
}

0 comments on commit a603a93

Please sign in to comment.