inbox.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package sqlite
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "github.com/pkg/errors"
  7. "google.golang.org/protobuf/encoding/protojson"
  8. storepb "github.com/usememos/memos/proto/gen/store"
  9. "github.com/usememos/memos/store"
  10. )
  11. func (d *DB) CreateInbox(ctx context.Context, create *store.Inbox) (*store.Inbox, error) {
  12. messageString := "{}"
  13. if create.Message != nil {
  14. bytes, err := protojson.Marshal(create.Message)
  15. if err != nil {
  16. return nil, errors.Wrap(err, "failed to marshal inbox message")
  17. }
  18. messageString = string(bytes)
  19. }
  20. fields := []string{"`sender_id`", "`receiver_id`", "`status`", "`message`"}
  21. placeholder := []string{"?", "?", "?", "?"}
  22. args := []any{create.SenderID, create.ReceiverID, create.Status, messageString}
  23. stmt := "INSERT INTO `inbox` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ") RETURNING `id`, `created_ts`"
  24. if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
  25. &create.ID,
  26. &create.CreatedTs,
  27. ); err != nil {
  28. return nil, err
  29. }
  30. return create, nil
  31. }
  32. func (d *DB) ListInboxes(ctx context.Context, find *store.FindInbox) ([]*store.Inbox, error) {
  33. where, args := []string{"1 = 1"}, []any{}
  34. if find.ID != nil {
  35. where, args = append(where, "`id` = ?"), append(args, *find.ID)
  36. }
  37. if find.SenderID != nil {
  38. where, args = append(where, "`sender_id` = ?"), append(args, *find.SenderID)
  39. }
  40. if find.ReceiverID != nil {
  41. where, args = append(where, "`receiver_id` = ?"), append(args, *find.ReceiverID)
  42. }
  43. if find.Status != nil {
  44. where, args = append(where, "`status` = ?"), append(args, *find.Status)
  45. }
  46. query := "SELECT `id`, `created_ts`, `sender_id`, `receiver_id`, `status`, `message` FROM `inbox` WHERE " + strings.Join(where, " AND ") + " ORDER BY `created_ts` DESC"
  47. if find.Limit != nil {
  48. query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit)
  49. if find.Offset != nil {
  50. query = fmt.Sprintf("%s OFFSET %d", query, *find.Offset)
  51. }
  52. }
  53. rows, err := d.db.QueryContext(ctx, query, args...)
  54. if err != nil {
  55. return nil, err
  56. }
  57. defer rows.Close()
  58. list := []*store.Inbox{}
  59. for rows.Next() {
  60. inbox := &store.Inbox{}
  61. var messageBytes []byte
  62. if err := rows.Scan(
  63. &inbox.ID,
  64. &inbox.CreatedTs,
  65. &inbox.SenderID,
  66. &inbox.ReceiverID,
  67. &inbox.Status,
  68. &messageBytes,
  69. ); err != nil {
  70. return nil, err
  71. }
  72. message := &storepb.InboxMessage{}
  73. if err := protojsonUnmarshaler.Unmarshal(messageBytes, message); err != nil {
  74. return nil, err
  75. }
  76. inbox.Message = message
  77. list = append(list, inbox)
  78. }
  79. if err := rows.Err(); err != nil {
  80. return nil, err
  81. }
  82. return list, nil
  83. }
  84. func (d *DB) UpdateInbox(ctx context.Context, update *store.UpdateInbox) (*store.Inbox, error) {
  85. set, args := []string{"`status` = ?"}, []any{update.Status.String()}
  86. args = append(args, update.ID)
  87. query := "UPDATE `inbox` SET " + strings.Join(set, ", ") + " WHERE `id` = ? RETURNING `id`, `created_ts`, `sender_id`, `receiver_id`, `status`, `message`"
  88. inbox := &store.Inbox{}
  89. var messageBytes []byte
  90. if err := d.db.QueryRowContext(ctx, query, args...).Scan(
  91. &inbox.ID,
  92. &inbox.CreatedTs,
  93. &inbox.SenderID,
  94. &inbox.ReceiverID,
  95. &inbox.Status,
  96. &messageBytes,
  97. ); err != nil {
  98. return nil, err
  99. }
  100. message := &storepb.InboxMessage{}
  101. if err := protojsonUnmarshaler.Unmarshal(messageBytes, message); err != nil {
  102. return nil, err
  103. }
  104. inbox.Message = message
  105. return inbox, nil
  106. }
  107. func (d *DB) DeleteInbox(ctx context.Context, delete *store.DeleteInbox) error {
  108. result, err := d.db.ExecContext(ctx, "DELETE FROM `inbox` WHERE `id` = ?", delete.ID)
  109. if err != nil {
  110. return err
  111. }
  112. if _, err := result.RowsAffected(); err != nil {
  113. return err
  114. }
  115. return nil
  116. }