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.

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