diff options
Diffstat (limited to 'pkg/idec/message.go')
-rw-r--r-- | pkg/idec/message.go | 89 |
1 files changed, 82 insertions, 7 deletions
diff --git a/pkg/idec/message.go b/pkg/idec/message.go index d018df2..48236e4 100644 --- a/pkg/idec/message.go +++ b/pkg/idec/message.go @@ -3,16 +3,17 @@ package idec import ( "encoding/base64" "fmt" + "log" "strings" "gitrepo.ru/neonxp/idecnode/pkg/model" "go.etcd.io/bbolt" ) -func (i *IDEC) GetMessagesByEcho(echoID string, offset, limit int) ([]string, error) { - messages := make([]string, 0) - - return messages, i.db.View(func(tx *bbolt.Tx) error { +func (i *IDEC) GetMessageIDsByEcho(echoID string, offset, limit int) ([]string, int, error) { + messages := make([]string, 0, limit) + count := 0 + return messages, count, i.db.View(func(tx *bbolt.Tx) error { if _, ok := i.config.Echos[echoID]; !ok { return nil } @@ -22,14 +23,15 @@ func (i *IDEC) GetMessagesByEcho(echoID string, offset, limit int) ([]string, er return nil } + count = bEcho.Stats().KeyN + cur := bEcho.Cursor() cur.First() - all := bEcho.Stats().KeyN if limit == 0 { - limit = all + limit = count } if offset < 0 { - offset = max(0, all+offset-1) + offset = max(0, count+offset-1) } for i := 0; i < offset; i++ { // skip offset entries @@ -47,6 +49,57 @@ func (i *IDEC) GetMessagesByEcho(echoID string, offset, limit int) ([]string, er }) } +func (i *IDEC) GetMessagesByEcho(echoID string, parent string, offset, limit int) ([]*model.Message, error) { + messages := make([]*model.Message, 0, limit) + return messages, i.db.View(func(tx *bbolt.Tx) error { + if _, ok := i.config.Echos[echoID]; !ok { + return nil + } + + bEcho := tx.Bucket([]byte(echoID)) + if bEcho == nil { + return nil + } + bMessages := tx.Bucket([]byte(msgBucket)) + if bMessages == nil { + return nil + } + + count := bEcho.Stats().KeyN + + cur := bEcho.Cursor() + cur.First() + if limit == 0 { + limit = count + } + if offset < 0 { + offset = max(0, count+offset-1) + } + for i := 0; i < offset; i++ { + // skip offset entries + cur.Next() + } + for i := 0; len(messages) < limit; i++ { + _, v := cur.Next() + if v == nil { + break + } + + msgText := bMessages.Get(v) + + msg, err := model.MessageFromBundle(string(v), string(msgText)) + if err != nil { + return err + } + if msg.RepTo == parent { + messages = append(messages, msg) + } + } + + return nil + }) +} + func (i *IDEC) GetMessage(messageID string) (*model.Message, error) { var msg *model.Message return msg, i.db.View(func(tx *bbolt.Tx) error { @@ -62,6 +115,28 @@ func (i *IDEC) GetMessage(messageID string) (*model.Message, error) { }) } +func (i *IDEC) GetMessages(messageIDs []string) ([]*model.Message, error) { + msgs := make([]*model.Message, 0, len(messageIDs)) + return msgs, i.db.View(func(tx *bbolt.Tx) error { + bucket := tx.Bucket([]byte(msgBucket)) + if bucket == nil { + return ErrMessageNotFound + } + for _, messageID := range messageIDs { + b := bucket.Get([]byte(messageID)) + msg, err := model.MessageFromBundle(messageID, string(b)) + if err != nil { + log.Println(err) + continue + } + + msgs = append(msgs, msg) + } + + return nil + }) +} + func (i *IDEC) SavePointMessage(point string, rawMessage string) error { rawMessage = strings.NewReplacer("-", "+", "_", "/").Replace(rawMessage) |