mirror of
https://github.com/usememos/memos.git
synced 2025-01-30 00:51:14 +08:00
chore: update activity store definition
This commit is contained in:
parent
9d2b785be6
commit
1b34119e60
5 changed files with 106 additions and 7 deletions
|
@ -2,8 +2,30 @@ package store
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
storepb "github.com/usememos/memos/proto/gen/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ActivityType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
ActivityTypeMemoComment ActivityType = "MEMO_COMMENT"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (t ActivityType) String() string {
|
||||||
|
return string(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
type ActivityLevel string
|
||||||
|
|
||||||
|
const (
|
||||||
|
ActivityLevelInfo ActivityLevel = "INFO"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (l ActivityLevel) String() string {
|
||||||
|
return string(l)
|
||||||
|
}
|
||||||
|
|
||||||
type Activity struct {
|
type Activity struct {
|
||||||
ID int32
|
ID int32
|
||||||
|
|
||||||
|
@ -12,9 +34,9 @@ type Activity struct {
|
||||||
CreatedTs int64
|
CreatedTs int64
|
||||||
|
|
||||||
// Domain specific fields
|
// Domain specific fields
|
||||||
Type string
|
Type ActivityType
|
||||||
Level string
|
Level ActivityLevel
|
||||||
Payload string
|
Payload *storepb.ActivityPayload
|
||||||
}
|
}
|
||||||
|
|
||||||
type FindActivity struct {
|
type FindActivity struct {
|
||||||
|
|
|
@ -5,14 +5,24 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"google.golang.org/protobuf/encoding/protojson"
|
||||||
|
|
||||||
|
storepb "github.com/usememos/memos/proto/gen/store"
|
||||||
"github.com/usememos/memos/store"
|
"github.com/usememos/memos/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (d *DB) CreateActivity(ctx context.Context, create *store.Activity) (*store.Activity, error) {
|
func (d *DB) CreateActivity(ctx context.Context, create *store.Activity) (*store.Activity, error) {
|
||||||
|
payloadString := "{}"
|
||||||
|
if create.Payload != nil {
|
||||||
|
bytes, err := protojson.Marshal(create.Payload)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "failed to marshal activity payload")
|
||||||
|
}
|
||||||
|
payloadString = string(bytes)
|
||||||
|
}
|
||||||
fields := []string{"`creator_id`", "`type`", "`level`", "`payload`"}
|
fields := []string{"`creator_id`", "`type`", "`level`", "`payload`"}
|
||||||
placeholder := []string{"?", "?", "?", "?"}
|
placeholder := []string{"?", "?", "?", "?"}
|
||||||
args := []any{create.CreatorID, create.Type, create.Level, create.Payload}
|
args := []any{create.CreatorID, create.Type.String(), create.Level.String(), payloadString}
|
||||||
|
|
||||||
if create.ID != 0 {
|
if create.ID != 0 {
|
||||||
fields = append(fields, "`id`")
|
fields = append(fields, "`id`")
|
||||||
|
@ -64,17 +74,23 @@ func (d *DB) ListActivities(ctx context.Context, find *store.FindActivity) ([]*s
|
||||||
list := []*store.Activity{}
|
list := []*store.Activity{}
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
activity := &store.Activity{}
|
activity := &store.Activity{}
|
||||||
|
var payloadBytes []byte
|
||||||
if err := rows.Scan(
|
if err := rows.Scan(
|
||||||
&activity.ID,
|
&activity.ID,
|
||||||
&activity.CreatorID,
|
&activity.CreatorID,
|
||||||
&activity.Type,
|
&activity.Type,
|
||||||
&activity.Level,
|
&activity.Level,
|
||||||
&activity.Payload,
|
&payloadBytes,
|
||||||
&activity.CreatedTs,
|
&activity.CreatedTs,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
payload := &storepb.ActivityPayload{}
|
||||||
|
if err := protojsonUnmarshaler.Unmarshal(payloadBytes, payload); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
activity.Payload = payload
|
||||||
list = append(list, activity)
|
list = append(list, activity)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
9
store/db/mysql/common.go
Normal file
9
store/db/mysql/common.go
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
import "google.golang.org/protobuf/encoding/protojson"
|
||||||
|
|
||||||
|
var (
|
||||||
|
protojsonUnmarshaler = protojson.UnmarshalOptions{
|
||||||
|
DiscardUnknown: true,
|
||||||
|
}
|
||||||
|
)
|
|
@ -4,13 +4,26 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"google.golang.org/protobuf/encoding/protojson"
|
||||||
|
|
||||||
|
storepb "github.com/usememos/memos/proto/gen/store"
|
||||||
"github.com/usememos/memos/store"
|
"github.com/usememos/memos/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (d *DB) CreateActivity(ctx context.Context, create *store.Activity) (*store.Activity, error) {
|
func (d *DB) CreateActivity(ctx context.Context, create *store.Activity) (*store.Activity, error) {
|
||||||
|
payloadString := "{}"
|
||||||
|
if create.Payload != nil {
|
||||||
|
bytes, err := protojson.Marshal(create.Payload)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "failed to marshal activity payload")
|
||||||
|
}
|
||||||
|
payloadString = string(bytes)
|
||||||
|
}
|
||||||
|
|
||||||
fields := []string{"`creator_id`", "`type`", "`level`", "`payload`"}
|
fields := []string{"`creator_id`", "`type`", "`level`", "`payload`"}
|
||||||
placeholder := []string{"?", "?", "?", "?"}
|
placeholder := []string{"?", "?", "?", "?"}
|
||||||
args := []any{create.CreatorID, create.Type, create.Level, create.Payload}
|
args := []any{create.CreatorID, create.Type.String(), create.Level.String(), payloadString}
|
||||||
|
|
||||||
if create.ID != 0 {
|
if create.ID != 0 {
|
||||||
fields = append(fields, "`id`")
|
fields = append(fields, "`id`")
|
||||||
|
@ -52,17 +65,23 @@ func (d *DB) ListActivities(ctx context.Context, find *store.FindActivity) ([]*s
|
||||||
list := []*store.Activity{}
|
list := []*store.Activity{}
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
activity := &store.Activity{}
|
activity := &store.Activity{}
|
||||||
|
var payloadBytes []byte
|
||||||
if err := rows.Scan(
|
if err := rows.Scan(
|
||||||
&activity.ID,
|
&activity.ID,
|
||||||
&activity.CreatorID,
|
&activity.CreatorID,
|
||||||
&activity.Type,
|
&activity.Type,
|
||||||
&activity.Level,
|
&activity.Level,
|
||||||
&activity.Payload,
|
&payloadBytes,
|
||||||
&activity.CreatedTs,
|
&activity.CreatedTs,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
payload := &storepb.ActivityPayload{}
|
||||||
|
if err := protojsonUnmarshaler.Unmarshal(payloadBytes, payload); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
activity.Payload = payload
|
||||||
list = append(list, activity)
|
list = append(list, activity)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
33
test/store/activity_test.go
Normal file
33
test/store/activity_test.go
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
package teststore
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
storepb "github.com/usememos/memos/proto/gen/store"
|
||||||
|
"github.com/usememos/memos/store"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestActivityStore(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
ts := NewTestingStore(ctx, t)
|
||||||
|
user, err := createTestingHostUser(ctx, ts)
|
||||||
|
require.NoError(t, err)
|
||||||
|
create := &store.Activity{
|
||||||
|
CreatorID: user.ID,
|
||||||
|
Type: store.ActivityTypeMemoComment,
|
||||||
|
Level: store.ActivityLevelInfo,
|
||||||
|
Payload: &storepb.ActivityPayload{},
|
||||||
|
}
|
||||||
|
activity, err := ts.CreateActivity(ctx, create)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, activity)
|
||||||
|
activities, err := ts.ListActivities(ctx, &store.FindActivity{
|
||||||
|
ID: &activity.ID,
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, 1, len(activities))
|
||||||
|
require.Equal(t, activity, activities[0])
|
||||||
|
}
|
Loading…
Reference in a new issue