Skip to content

Commit

Permalink
added more reminder types
Browse files Browse the repository at this point in the history
  • Loading branch information
Paz committed Jan 5, 2025
1 parent e21d893 commit d7461c1
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 36 deletions.
2 changes: 1 addition & 1 deletion frontend/src/components/NavigationSidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const props = defineProps<{
}>()
const emit = defineEmits(["refresh", "file-select"])
const emit = defineEmits(["refresh"])
const directoryRepository = new DirectoryContentRepository()
const noteRepository = new NoteRepository()
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/views/HomeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ function showSidebar() {
<div id="sidebar">
<div id="nav-area">
<NavigationSidebar class="sidebar-content" :directory-content="directoryContent"
:current-directory="currentDirectory" />
:current-directory="currentDirectory"
@refresh="updateFromRoutePath()"/>

<template v-if="isSidebarVisible">
<button class="shrinker" @click="hideSidebar()">◀</button>
Expand Down
102 changes: 81 additions & 21 deletions internal/reminder/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import (
)

var interval = time.Hour
var untilRegex *regexp.Regexp
var onyxDirectiveRegex *regexp.Regexp

func init() {
r, err := regexp.Compile(`\(#until:\d\d\d\d-\d{1,2}-\d{1,2}\)`)
r, err := regexp.Compile(`\[\[.*:.*\]\]`)
if err != nil {
log.Fatal(err)
}

untilRegex = r
onyxDirectiveRegex = r
}

type MonitoringService struct {
Expand Down Expand Up @@ -98,7 +98,7 @@ func (ms *MonitoringService) sendMatrixMessages(reminders []Reminder) error {

var builder strings.Builder
for _, reminder := range reminders {
builder.WriteString(fmt.Sprintf("%s\n%s\n(%s)\n\n\n", reminder.Date, reminder.ToDo, reminder.Source))
builder.WriteString(fmt.Sprintf("%s\n%s\n(%s)\n\n\n", reminder.Date, reminder.Content, reminder.Source))
}

err = ms.matrixService.SendMessage(builder.String())
Expand All @@ -118,10 +118,32 @@ func extractRemindersFromNote(note types.Note) []Reminder {
continue
}
reminders = append(reminders, reminder)

logging.Info(fmt.Sprintf("found reminder (%s): %s", reminder.Type, reminder.Content))
}
return reminders
}

func extractOnyxExpression(text string) (OnyxExpression, bool) {
match := onyxDirectiveRegex.FindString(text)
if match == "" {
return OnyxExpression{}, false
}

stripped := strings.Replace(match, "[[", "", 1)
stripped = strings.Replace(stripped, "]]", "", 1)

parts := strings.SplitN(stripped, ":", 2)
if len(parts) != 2 {
return OnyxExpression{}, false
}

return OnyxExpression{
Type: parts[0],
Content: parts[1],
}, true
}

func extractReminderFromLine(line string, source string) (Reminder, bool) {
prefix := "- [ ] "
if !strings.HasPrefix(line, prefix) {
Expand All @@ -130,29 +152,67 @@ func extractReminderFromLine(line string, source string) (Reminder, bool) {

stripped := strings.Replace(line, prefix, "", 1)

match := untilRegex.FindString(line)
if match == "" {
onyxExpr, ok := extractOnyxExpression(stripped)
if !ok {
return Reminder{}, false
}

todo := strings.Replace(stripped, match, "", 1)
if onyxExpr.Type == deadlineType {
date, err := time.Parse("2006-01-02", onyxExpr.Content)
if err != nil {
logging.Error(err.Error())
return Reminder{}, false
}

dateString := strings.Replace(match, "(#until:", "", 1)
dateString = strings.Replace(dateString, ")", "", 1)
if time.Now().Before(date) {
return Reminder{}, false
}

date, err := time.Parse("2006-01-02", dateString)
if err != nil {
logging.Error(err.Error())
return Reminder{}, false
}
return Reminder{
Date: date,
Content: strings.TrimSpace(stripped),
Type: onyxExpr.Type,
Source: source,
}, true
} else if onyxExpr.Type == birthdayType {
date, err := time.Parse("2006-01-02", onyxExpr.Content)
if err != nil {
logging.Error(err.Error())
return Reminder{}, false
}

if time.Now().Before(date) {
return Reminder{}, false
now := time.Now()
if now.Day() != date.Day() || now.Month() != date.Month() {
return Reminder{}, false
}

return Reminder{
Date: date,
Content: strings.TrimSpace(stripped),
Type: onyxExpr.Type,
Source: source,
}, true
} else if onyxExpr.Type == dateType {
date, err := time.Parse("2006-01-02", onyxExpr.Content)
if err != nil {
logging.Error(err.Error())
return Reminder{}, false
}

now := time.Now()
if now.Day() != date.Day() || now.Month() != date.Month() || now.Year() != date.Year() {
return Reminder{}, false
}

return Reminder{
Date: date,
Content: strings.TrimSpace(stripped),
Type: onyxExpr.Type,
Source: source,
}, true
} else {
logging.Warning(fmt.Sprintf("onyx expression '%s' not implemented yet", onyxExpr.Type))
}

return Reminder{
Date: date,
ToDo: strings.TrimSpace(todo),
Source: source,
}, true
return Reminder{}, false
}
27 changes: 23 additions & 4 deletions internal/reminder/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@ import (
"time"
)

func TestExtractOnyxExpression1(t *testing.T) {
line := "- [ ] Learn something [[until:2024-12-24]]"

expr, ok := extractOnyxExpression(line)
if !ok {
t.Error("could not extract onyx expression")
}

wantType := "until"
if expr.Type != wantType {
t.Errorf("%s != %s", expr.Type, wantType)
}

wantContent := "2024-12-24"
if expr.Content != wantContent {
t.Errorf("%s != %s", expr.Content, wantContent)
}
}

func TestExtractReminder1(t *testing.T) {
line := "- [ ] Learn something (#until:2024-12-24)"

Expand All @@ -16,8 +35,8 @@ func TestExtractReminder1(t *testing.T) {
}

wantToDo := "Learn something"
if reminder.ToDo != wantToDo {
t.Errorf("%s != %s", reminder.ToDo, wantToDo)
if reminder.Content != wantToDo {
t.Errorf("%s != %s", reminder.Content, wantToDo)
}

wantDate := time.Date(2024, time.December, 24, 0, 0, 0, 0, time.UTC)
Expand All @@ -42,8 +61,8 @@ func TestExtractReminder2(t *testing.T) {

// TODO: Should this really be the behaviour?
wantToDo := "Learn something and something else"
if reminder.ToDo != wantToDo {
t.Errorf("%s != %s", reminder.ToDo, wantToDo)
if reminder.Content != wantToDo {
t.Errorf("%s != %s", reminder.Content, wantToDo)
}

wantDate := time.Date(2024, time.December, 24, 0, 0, 0, 0, time.UTC)
Expand Down
16 changes: 13 additions & 3 deletions internal/reminder/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@ package reminder

import "time"

const birthdayType = "birthday"
const deadlineType = "deadline"
const dateType = "date"

type Reminder struct {
Date time.Time `json:"date"`
ToDo string `json:"todo"`
Source string `json:"source"`
Date time.Time `json:"date"`
Content string `json:"todo"`
Type string `json:"string"`
Source string `json:"source"`
}

type OnyxExpression struct {
Type string
Content string
}
2 changes: 1 addition & 1 deletion onyx.env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ONYX_VERSION=0.4.0
ONYX_VERSION=0.5.0
16 changes: 11 additions & 5 deletions testdata/Reminders.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

Using a list with checkboxes, a keyword and a date, reminders can be enabled:

## Reminders that will be sent until they are checked
## Reminders that will be sent on and after the date if unchecked

- [x] Started Onyx development (#until:2024-12-24)
- [ ] Fixed all bugs in Onyx (#until:2024-12-31)
- [ ] Saved the world (#until:2022-01-21)
- [ ] Visited Mars with a self built rocket without fuel (#until:2006-11-02)
- [x] Started Onyx development [[deadline:2024-12-24]]
- [ ] Fixed all bugs in Onyx [[deadline:2024-12-31]]

## Birthdays

- [ ] Max Mustermann [[birthday:1990-01-05]]

## Specific dates

- [ ] Some important appointment [[date:2025-01-05]]
Empty file added testdata/Stuff.md
Empty file.
Empty file added testdata/Stufffff.md
Empty file.

0 comments on commit d7461c1

Please sign in to comment.