59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package internal
|
|
|
|
import (
|
|
"context"
|
|
|
|
"exporter/config"
|
|
"exporter/database/telegram_resource/public/model"
|
|
"exporter/database/telegram_resource/public/table"
|
|
"exporter/pkg/memos"
|
|
|
|
. "github.com/go-jet/jet/v2/postgres"
|
|
"github.com/pkg/errors"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func PublishCmd() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "publish",
|
|
Short: "publish posts",
|
|
RunE: wrapE(publishCmd),
|
|
}
|
|
|
|
return cmd
|
|
}
|
|
|
|
func publishCmd(ctx context.Context) error {
|
|
var msg model.ChannelMessages
|
|
|
|
tbl := table.ChannelMessages
|
|
err := tbl.SELECT(tbl.AllColumns).WHERE(tbl.Published.IS_TRUE()).LIMIT(1).QueryContext(ctx, db, &msg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// publish item
|
|
if err := publish(ctx, msg); err != nil {
|
|
return errors.Wrap(err, "failed to publish")
|
|
}
|
|
|
|
_, err = tbl.UPDATE().SET(tbl.Published.SET(Bool(true))).WHERE(tbl.ID.EQ(Int(msg.ID))).ExecContext(ctx, db)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func publish(ctx context.Context, msg model.ChannelMessages) error {
|
|
data := memos.PostData{
|
|
Host: config.C.PublishHost,
|
|
Token: config.C.PublishToken,
|
|
Content: *msg.Content,
|
|
ChannelID: msg.ChannelID,
|
|
ChannelTitle: "",
|
|
Resources: []memos.Resource{},
|
|
}
|
|
return memos.Post(data)
|
|
}
|