From 55e0fbf24e2177095f487d38b4dbcb870990616d Mon Sep 17 00:00:00 2001
From: Athurg Gooth <athurg@gooth.org>
Date: Sun, 8 Oct 2023 18:29:22 +0800
Subject: [PATCH] fix: create activity without some attributes  (#2356)

---
 store/db/mysql/activity.go  | 26 +++++++++++++++++++-------
 store/db/sqlite/activity.go | 30 +++++++++++++++++++-----------
 2 files changed, 38 insertions(+), 18 deletions(-)

diff --git a/store/db/mysql/activity.go b/store/db/mysql/activity.go
index ab55232e..d81364cf 100644
--- a/store/db/mysql/activity.go
+++ b/store/db/mysql/activity.go
@@ -2,6 +2,7 @@ package mysql
 
 import (
 	"context"
+	"strings"
 
 	"github.com/pkg/errors"
 
@@ -9,13 +10,24 @@ import (
 )
 
 func (d *DB) CreateActivity(ctx context.Context, create *store.Activity) (*store.Activity, error) {
-	stmt := "INSERT INTO activity (`creator_id`, `type`, `level`, `payload`) VALUES (?, ?, ?, ?)"
-	result, err := d.db.ExecContext(ctx, stmt,
-		create.CreatorID,
-		create.Type,
-		create.Level,
-		create.Payload,
-	)
+	fields := []string{"`creator_id`", "`type`", "`level`", "`payload`"}
+	placeholder := []string{"?", "?", "?", "?"}
+	args := []any{create.CreatorID, create.Type, create.Level, create.Payload}
+
+	if create.ID != 0 {
+		fields = append(fields, "`id`")
+		placeholder = append(placeholder, "?")
+		args = append(args, create.ID)
+	}
+
+	if create.CreatedTs != 0 {
+		fields = append(fields, "`created_ts`")
+		placeholder = append(placeholder, "FROM_UNIXTIME(?)")
+		args = append(args, create.CreatedTs)
+	}
+
+	stmt := "INSERT INTO activity (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ")"
+	result, err := d.db.ExecContext(ctx, stmt, args...)
 	if err != nil {
 		return nil, errors.Wrap(err, "failed to execute statement")
 	}
diff --git a/store/db/sqlite/activity.go b/store/db/sqlite/activity.go
index 7f030681..ccb7d68c 100644
--- a/store/db/sqlite/activity.go
+++ b/store/db/sqlite/activity.go
@@ -2,22 +2,30 @@ package sqlite
 
 import (
 	"context"
+	"strings"
 
 	"github.com/usememos/memos/store"
 )
 
 func (d *DB) CreateActivity(ctx context.Context, create *store.Activity) (*store.Activity, error) {
-	stmt := `
-		INSERT INTO activity (
-			creator_id,
-			type,
-			level,
-			payload
-		)
-		VALUES (?, ?, ?, ?)
-		RETURNING id, created_ts
-	`
-	if err := d.db.QueryRowContext(ctx, stmt, create.CreatorID, create.Type, create.Level, create.Payload).Scan(
+	fields := []string{"`creator_id`", "`type`", "`level`", "`payload`"}
+	placeholder := []string{"?", "?", "?", "?"}
+	args := []any{create.CreatorID, create.Type, create.Level, create.Payload}
+
+	if create.ID != 0 {
+		fields = append(fields, "`id`")
+		placeholder = append(placeholder, "?")
+		args = append(args, create.ID)
+	}
+
+	if create.CreatedTs != 0 {
+		fields = append(fields, "`created_ts`")
+		placeholder = append(placeholder, "?")
+		args = append(args, create.CreatedTs)
+	}
+
+	stmt := "INSERT INTO activity (" + strings.Join(fields, ", ") + ") VALUES (" + strings.Join(placeholder, ", ") + ") RETURNING `id`, `created_ts`"
+	if err := d.db.QueryRowContext(ctx, stmt, args...).Scan(
 		&create.ID,
 		&create.CreatedTs,
 	); err != nil {