inbox.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package mysql
  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, ", ") + ")"
  24. result, err := d.db.ExecContext(ctx, stmt, args...)
  25. if err != nil {
  26. return nil, err
  27. }
  28. id, err := result.LastInsertId()
  29. if err != nil {
  30. return nil, err
  31. }
  32. id32 := int32(id)
  33. inbox, err := d.GetInbox(ctx, &store.FindInbox{ID: &id32})
  34. if err != nil {
  35. return nil, err
  36. }
  37. return inbox, nil
  38. }
  39. func (d *DB) ListInboxes(ctx context.Context, find *store.FindInbox) ([]*store.Inbox, error) {
  40. where, args := []string{"1 = 1"}, []any{}
  41. if find.ID != nil {
  42. where, args = append(where, "`id` = ?"), append(args, *find.ID)
  43. }
  44. if find.SenderID != nil {
  45. where, args = append(where, "`sender_id` = ?"), append(args, *find.SenderID)
  46. }
  47. if find.ReceiverID != nil {
  48. where, args = append(where, "`receiver_id` = ?"), append(args, *find.ReceiverID)
  49. }
  50. if find.Status != nil {
  51. where, args = append(where, "`status` = ?"), append(args, *find.Status)
  52. }
  53. query := "SELECT `id`, UNIX_TIMESTAMP(`created_ts`), `sender_id`, `receiver_id`, `status`, `message` FROM `inbox` WHERE " + strings.Join(where, " AND ") + " ORDER BY `created_ts` DESC"
  54. if find.Limit != nil {
  55. query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit)
  56. if find.Offset != nil {
  57. query = fmt.Sprintf("%s OFFSET %d", query, *find.Offset)
  58. }
  59. }
  60. rows, err := d.db.QueryContext(ctx, query, args...)
  61. if err != nil {
  62. return nil, err
  63. }
  64. defer rows.Close()
  65. list := []*store.Inbox{}
  66. for rows.Next() {
  67. inbox := &store.Inbox{}
  68. var messageBytes []byte
  69. if err := rows.Scan(
  70. &inbox.ID,
  71. &inbox.CreatedTs,
  72. &inbox.SenderID,
  73. &inbox.ReceiverID,
  74. &inbox.Status,
  75. &messageBytes,
  76. ); err != nil {
  77. return nil, err
  78. }
  79. message := &storepb.InboxMessage{}
  80. if err := protojsonUnmarshaler.Unmarshal(messageBytes, message); err != nil {
  81. return nil, err
  82. }
  83. inbox.Message = message
  84. list = append(list, inbox)
  85. }
  86. if err := rows.Err(); err != nil {
  87. return nil, err
  88. }
  89. return list, nil
  90. }
  91. func (d *DB) GetInbox(ctx context.Context, find *store.FindInbox) (*store.Inbox, error) {
  92. list, err := d.ListInboxes(ctx, find)
  93. if err != nil {
  94. return nil, errors.Wrap(err, "failed to get inbox")
  95. }
  96. if len(list) != 1 {
  97. return nil, errors.Wrapf(nil, "unexpected inbox count: %d", len(list))
  98. }
  99. return list[0], nil
  100. }
  101. func (d *DB) UpdateInbox(ctx context.Context, update *store.UpdateInbox) (*store.Inbox, error) {
  102. set, args := []string{"`status` = ?"}, []any{update.Status.String()}
  103. args = append(args, update.ID)
  104. query := "UPDATE `inbox` SET " + strings.Join(set, ", ") + " WHERE `id` = ?"
  105. if _, err := d.db.ExecContext(ctx, query, args...); err != nil {
  106. return nil, errors.Wrap(err, "failed to update inbox")
  107. }
  108. inbox, err := d.GetInbox(ctx, &store.FindInbox{ID: &update.ID})
  109. if err != nil {
  110. return nil, err
  111. }
  112. return inbox, nil
  113. }
  114. func (d *DB) DeleteInbox(ctx context.Context, delete *store.DeleteInbox) error {
  115. result, err := d.db.ExecContext(ctx, "DELETE FROM `inbox` WHERE `id` = ?", delete.ID)
  116. if err != nil {
  117. return errors.Wrap(err, "failed to delete inbox")
  118. }
  119. if _, err := result.RowsAffected(); err != nil {
  120. return err
  121. }
  122. return nil
  123. }