You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.7 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package matrix
  2. import (
  3. "encoding/json"
  4. "github.com/matrix-org/go-neb/api"
  5. "github.com/matrix-org/go-neb/database"
  6. "github.com/matrix-org/gomatrix"
  7. log "github.com/sirupsen/logrus"
  8. )
  9. // NEBStore implements the gomatrix.Storer interface.
  10. //
  11. // It persists the next batch token in the database, and includes a ClientConfig for the client.
  12. type NEBStore struct {
  13. gomatrix.InMemoryStore
  14. Database database.Storer
  15. ClientConfig api.ClientConfig
  16. }
  17. // SaveNextBatch saves to the database.
  18. func (s *NEBStore) SaveNextBatch(userID, nextBatch string) {
  19. if err := s.Database.UpdateNextBatch(userID, nextBatch); err != nil {
  20. log.WithFields(log.Fields{
  21. log.ErrorKey: err,
  22. "user_id": userID,
  23. "next_batch": nextBatch,
  24. }).Error("Failed to persist next_batch token")
  25. }
  26. }
  27. // LoadNextBatch loads from the database.
  28. func (s *NEBStore) LoadNextBatch(userID string) string {
  29. token, err := s.Database.LoadNextBatch(userID)
  30. if err != nil {
  31. log.WithFields(log.Fields{
  32. log.ErrorKey: err,
  33. "user_id": userID,
  34. }).Error("Failed to load next_batch token")
  35. return ""
  36. }
  37. return token
  38. }
  39. // StarterLinkMessage represents a message with a starter_link custom data.
  40. type StarterLinkMessage struct {
  41. Body string
  42. Link string
  43. }
  44. // MarshalJSON converts this message into actual event content JSON.
  45. func (m StarterLinkMessage) MarshalJSON() ([]byte, error) {
  46. var data map[string]string
  47. if m.Link != "" {
  48. data = map[string]string{
  49. "org.matrix.neb.starter_link": m.Link,
  50. }
  51. }
  52. msg := struct {
  53. MsgType string `json:"msgtype"`
  54. Body string `json:"body"`
  55. Data map[string]string `json:"data,omitempty"`
  56. }{
  57. "m.notice", m.Body, data,
  58. }
  59. return json.Marshal(msg)
  60. }