-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add habitica and todoist webhook handlers
- Loading branch information
Showing
7 changed files
with
317 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package models | ||
|
||
const HabiticaHabitType = "habit" | ||
const HabiticaDailyType = "daily" | ||
const HabiticaTodoType = "todo" | ||
|
||
type HabiticaWebhook struct { | ||
Type string `json:"type"` | ||
Direction string `json:"direction"` | ||
Task HabiticaWebhookTask `json:"task"` | ||
} | ||
|
||
type HabiticaWebhookTask struct { | ||
Id string `json:"id"` | ||
Up int `json:"counterUp"` | ||
Down int `json:"counterDown"` | ||
|
||
Type string `json:"type"` | ||
Text string `json:"text"` | ||
Notes string `json:"notes"` | ||
} | ||
|
||
type HabiticaHabitRule struct { | ||
HabitId string | ||
DailyId string | ||
MinScore int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package models | ||
|
||
type TodoistWebhook struct { | ||
EventName string `json:"event_name"` | ||
EventData TodoistEvent `json:"event_data"` | ||
} | ||
|
||
type TodoistEvent struct { | ||
Content string `json:"content"` | ||
Description string `json:"description"` | ||
ProjectId string `json:"project_id"` | ||
Id string `json:"id"` | ||
} | ||
|
||
type TodoistHabiticaTextRule struct { | ||
Name string | ||
Rule string | ||
HabitId string | ||
} | ||
|
||
type TodoistHabiticaProjectRule struct { | ||
Name string | ||
ProjectId string | ||
HabitId string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,13 +9,17 @@ import ( | |
|
||
_ "github.com/joho/godotenv/autoload" | ||
|
||
"misc/clients" | ||
Check failure on line 12 in internal/server/server.go GitHub Actions / build
|
||
"misc/internal/database" | ||
"misc/internal/services" | ||
) | ||
|
||
type Server struct { | ||
port int | ||
|
||
db database.Service | ||
db database.Service | ||
habService services.HabiticaMinHabitService | ||
todoHabService services.TodoistHabiticaService | ||
} | ||
|
||
func NewServer() *http.Server { | ||
|
@@ -25,6 +29,20 @@ func NewServer() *http.Server { | |
|
||
db: database.New(), | ||
} | ||
habClient := clients.NewHabiticaClient( | ||
os.Getenv("HABITICA_API_USER"), | ||
os.Getenv("HABITICA_API_KEY"), | ||
) | ||
|
||
NewServer.habService = *services.NewHabitcaMinHabitService( | ||
NewServer.db, | ||
&habClient, | ||
) | ||
|
||
NewServer.todoHabService = services.NewTodoistHabiticaService( | ||
NewServer.db, | ||
&habClient, | ||
) | ||
|
||
// Declare Server config | ||
server := &http.Server{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package services | ||
|
||
import ( | ||
"fmt" | ||
"misc/internal/models" | ||
) | ||
|
||
type HabitRuleStore interface { | ||
GetHabitRule(string) (*models.HabiticaHabitRule, error) | ||
} | ||
|
||
type DailyUpdater interface { | ||
ScoreDaily(string) error | ||
} | ||
|
||
type HabiticaMinHabitService struct { | ||
db HabitRuleStore | ||
updater DailyUpdater | ||
} | ||
|
||
func NewHabitcaMinHabitService(db HabitRuleStore, updater DailyUpdater) *HabiticaMinHabitService { | ||
return &HabiticaMinHabitService{db: db, updater: updater} | ||
} | ||
|
||
func (h *HabiticaMinHabitService) CheckMinHabit(habitId string, currScore int) error { | ||
rule, err := h.db.GetHabitRule(habitId) | ||
if err != nil { | ||
return fmt.Errorf("error getting habit rule: %w", err) | ||
} | ||
|
||
if currScore == rule.MinScore { | ||
if err := h.updater.ScoreDaily(rule.DailyId); err != nil { | ||
return fmt.Errorf("error scoring daily: %w", err) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package services | ||
|
||
import ( | ||
"fmt" | ||
"log/slog" | ||
"misc/internal/models" | ||
"strings" | ||
) | ||
|
||
type TodoistHabiticaRuleStore interface { | ||
GetTodoistHabiticaTextRules() ([]models.TodoistHabiticaTextRule, error) | ||
GetTodoistHabiticaProjectRule(string) (models.TodoistHabiticaProjectRule, error) | ||
} | ||
|
||
type TodoistHabiticaService struct { | ||
db TodoistHabiticaRuleStore | ||
updater DailyUpdater | ||
} | ||
|
||
func NewTodoistHabiticaService(db TodoistHabiticaRuleStore, updater DailyUpdater) TodoistHabiticaService { | ||
return TodoistHabiticaService{ | ||
db: db, | ||
updater: updater, | ||
} | ||
} | ||
|
||
func (s *TodoistHabiticaService) ScoreTask(taskStr, projectId string) error { | ||
// check text rules first, if we hit one score the task and return | ||
rules, err := s.db.GetTodoistHabiticaTextRules() | ||
|
||
if err != nil { | ||
return fmt.Errorf("error getting text rules: %w", err) | ||
} | ||
|
||
slog.Info("got text rules", "rules", rules, "taskStr", taskStr) | ||
for _, rule := range rules { | ||
if strings.HasPrefix(strings.ToLower(taskStr), rule.Rule) { | ||
if err := s.updater.ScoreDaily(rule.HabitId); err != nil { | ||
return fmt.Errorf("error scoring habit: %w", err) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
// check project rule | ||
rule, err := s.db.GetTodoistHabiticaProjectRule(projectId) | ||
if err != nil { | ||
return fmt.Errorf("error getting project rule: %w", err) | ||
} | ||
err = s.updater.ScoreDaily(rule.HabitId) | ||
if err != nil { | ||
return fmt.Errorf("error scoring habit: %w", err) | ||
} | ||
return nil | ||
} |