Skip to content

Commit

Permalink
fix: make the command argument independent
Browse files Browse the repository at this point in the history
  • Loading branch information
monotykamary committed Aug 30, 2024
1 parent 48ff9c1 commit 085e273
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions pkg/discord/command/sum/sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/dwarvesf/fortress-discord/pkg/discord/view"
"github.com/dwarvesf/fortress-discord/pkg/logger"
"github.com/dwarvesf/fortress-discord/pkg/model"
"net/url"
)

type Sum struct {
Expand All @@ -24,36 +25,35 @@ func New(l logger.Logger, svc service.Servicer, view view.Viewer) SumCommander {

func (e *Sum) Sum(message *model.DiscordMessage) error {
var template string
var url string
var articleURL string
var data *model.Sum
var err error

switch len(message.ContentArgs) {
case 2:
// Original behavior: ?sum <url>
url = message.ContentArgs[1]
data, err = e.svc.Sum().SummarizeArticle(string(model.TemplateSummary), url)
case 3:
// New behavior: ?sum <template> <url>
template = message.ContentArgs[1]
url = message.ContentArgs[2]
if !model.IsValidTemplateType(template) {
errorSummary := &model.Sum{
Title: "Error",
Summary: fmt.Sprintf("Invalid template type. Supported types are: %s, %s, %s, %s",
model.TemplateSummary, model.TemplateKeyPoints, model.TemplateNuggets, model.TemplateFacts),
}
return e.view.Sum().Sum(message, errorSummary)
// Parse arguments
for _, arg := range message.ContentArgs[1:] {
if isValidURL(arg) {
articleURL = arg
} else if model.IsValidTemplateType(arg) {
template = arg
}
data, err = e.svc.Sum().SummarizeArticle(template, url)
default:
}

// Validate parsed arguments
if articleURL == "" {
errorSummary := &model.Sum{
Title: "Error",
Summary: "Invalid command format. Use: ?sum <url> or ?sum <template> <url>",
Summary: "No valid URL provided. Use: ?sum <url> or ?sum <template> <url> or ?sum <url> <template>",
}
return e.view.Sum().Sum(message, errorSummary)
}

// If no template is provided, use the default summary template
if template == "" {
template = string(model.TemplateSummary)
}

// Summarize the article
data, err = e.svc.Sum().SummarizeArticle(template, articleURL)
if err != nil {
e.L.Error(err, "failed to summarize the given article")
errorSummary := &model.Sum{
Expand All @@ -63,6 +63,12 @@ func (e *Sum) Sum(message *model.DiscordMessage) error {
return e.view.Sum().Sum(message, errorSummary)
}

// render
// Render the summary
return e.view.Sum().Sum(message, data)
}

// isValidURL checks if a string is a valid URL
func isValidURL(str string) bool {
u, err := url.Parse(str)
return err == nil && u.Scheme != "" && u.Host != ""
}

0 comments on commit 085e273

Please sign in to comment.