activity.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package mysql
  2. import (
  3. "context"
  4. "strings"
  5. "github.com/pkg/errors"
  6. "google.golang.org/protobuf/encoding/protojson"
  7. storepb "github.com/usememos/memos/proto/gen/store"
  8. "github.com/usememos/memos/store"
  9. )
  10. func (d *DB) CreateActivity(ctx context.Context, create *store.Activity) (*store.Activity, error) {
  11. payloadString := "{}"
  12. if create.Payload != nil {
  13. bytes, err := protojson.Marshal(create.Payload)
  14. if err != nil {
  15. return nil, errors.Wrap(err, "failed to marshal activity payload")
  16. }
  17. payloadString = string(bytes)
  18. }
  19. fields := []string{"`creator_id`", "`type`", "`level`", "`payload`"}
  20. placeholder := []string{"?", "?", "?", "?"}
  21. args := []any{create.CreatorID, create.Type.String(), create.Level.String(), payloadString}
  22. stmt := "INSERT INTO `activity` (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ")"
  23. result, err := d.db.ExecContext(ctx, stmt, args...)
  24. if err != nil {
  25. return nil, errors.Wrap(err, "failed to execute statement")
  26. }
  27. id, err := result.LastInsertId()
  28. if err != nil {
  29. return nil, errors.Wrap(err, "failed to get last insert id")
  30. }
  31. id32 := int32(id)
  32. list, err := d.ListActivities(ctx, &store.FindActivity{ID: &id32})
  33. if err != nil || len(list) == 0 {
  34. return nil, errors.Wrap(err, "failed to find activity")
  35. }
  36. return list[0], nil
  37. }
  38. func (d *DB) ListActivities(ctx context.Context, find *store.FindActivity) ([]*store.Activity, error) {
  39. where, args := []string{"1 = 1"}, []any{}
  40. if find.ID != nil {
  41. where, args = append(where, "`id` = ?"), append(args, *find.ID)
  42. }
  43. if find.Type != nil {
  44. where, args = append(where, "`type` = ?"), append(args, find.Type.String())
  45. }
  46. query := "SELECT `id`, `creator_id`, `type`, `level`, `payload`, UNIX_TIMESTAMP(`created_ts`) FROM `activity` WHERE " + strings.Join(where, " AND ") + " ORDER BY `created_ts` DESC"
  47. rows, err := d.db.QueryContext(ctx, query, args...)
  48. if err != nil {
  49. return nil, err
  50. }
  51. defer rows.Close()
  52. list := []*store.Activity{}
  53. for rows.Next() {
  54. activity := &store.Activity{}
  55. var payloadBytes []byte
  56. if err := rows.Scan(
  57. &activity.ID,
  58. &activity.CreatorID,
  59. &activity.Type,
  60. &activity.Level,
  61. &payloadBytes,
  62. &activity.CreatedTs,
  63. ); err != nil {
  64. return nil, err
  65. }
  66. payload := &storepb.ActivityPayload{}
  67. if err := protojsonUnmarshaler.Unmarshal(payloadBytes, payload); err != nil {
  68. return nil, err
  69. }
  70. activity.Payload = payload
  71. list = append(list, activity)
  72. }
  73. if err := rows.Err(); err != nil {
  74. return nil, err
  75. }
  76. return list, nil
  77. }