2026-03-26 12:48:47 +00:00
|
|
|
package models
|
|
|
|
|
|
2026-03-29 23:16:44 +02:00
|
|
|
import (
|
2026-04-02 01:58:48 +02:00
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
2026-04-01 00:39:20 +02:00
|
|
|
"slices"
|
2026-03-29 23:16:44 +02:00
|
|
|
"strings"
|
2026-04-02 01:58:48 +02:00
|
|
|
"time"
|
2026-03-29 23:16:44 +02:00
|
|
|
)
|
2026-03-26 12:48:47 +00:00
|
|
|
|
2026-04-02 01:58:48 +02:00
|
|
|
// Date is a date-only time value that marshals as "YYYY-MM-DD" in JSON.
|
|
|
|
|
type Date struct{ time.Time }
|
|
|
|
|
|
|
|
|
|
// ParseDate parses a date string in "YYYY-MM-DD" or RFC3339 format.
|
|
|
|
|
func ParseDate(s string) (Date, error) {
|
|
|
|
|
for _, layout := range []string{"2006-01-02", time.RFC3339} {
|
|
|
|
|
if t, err := time.Parse(layout, s); err == nil {
|
|
|
|
|
return Date{t.UTC()}, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Date{}, fmt.Errorf("cannot parse date %q: expected YYYY-MM-DD", s)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d Date) MarshalJSON() ([]byte, error) {
|
|
|
|
|
return json.Marshal(d.Format("2006-01-02"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *Date) UnmarshalJSON(b []byte) error {
|
|
|
|
|
var s string
|
|
|
|
|
if err := json.Unmarshal(b, &s); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
parsed, err := ParseDate(s)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
*d = parsed
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 12:48:47 +00:00
|
|
|
type Node struct {
|
2026-04-01 00:13:43 +02:00
|
|
|
ID string `json:"id"`
|
|
|
|
|
Title string `json:"title"`
|
|
|
|
|
Content string `json:"content,omitempty"`
|
2026-04-02 01:58:48 +02:00
|
|
|
DueDate *Date `json:"due_date,omitempty"`
|
2026-04-01 00:13:43 +02:00
|
|
|
CreatedAt string `json:"created_at"`
|
|
|
|
|
UpdatedAt string `json:"updated_at"`
|
|
|
|
|
Tags []string `json:"tags,omitempty"`
|
|
|
|
|
Relations map[string][]string `json:"relations,omitempty"`
|
2026-03-29 23:16:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewNode() *Node {
|
|
|
|
|
return &Node{
|
2026-04-01 00:13:43 +02:00
|
|
|
Relations: make(map[string][]string),
|
2026-03-29 23:16:44 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n *Node) AddTag(tag string) {
|
|
|
|
|
if tag == "" {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-04-01 12:50:45 +02:00
|
|
|
// If it's a property (name::value format), replace any existing tag with the same prefix.
|
|
|
|
|
if idx := strings.Index(tag, "::"); idx >= 0 {
|
|
|
|
|
prefix := tag[:idx+2]
|
|
|
|
|
var newTags []string
|
|
|
|
|
for _, t := range n.Tags {
|
|
|
|
|
if !strings.HasPrefix(t, prefix) {
|
|
|
|
|
newTags = append(newTags, t)
|
2026-03-29 23:16:44 +02:00
|
|
|
}
|
|
|
|
|
}
|
2026-04-01 12:50:45 +02:00
|
|
|
n.Tags = newTags
|
2026-03-29 23:16:44 +02:00
|
|
|
}
|
2026-04-01 00:39:20 +02:00
|
|
|
if !slices.Contains(n.Tags, tag) {
|
2026-04-01 00:13:43 +02:00
|
|
|
n.Tags = append(n.Tags, tag)
|
2026-03-29 23:16:44 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 00:13:43 +02:00
|
|
|
func (n *Node) RemoveTag(tag string) {
|
2026-03-29 23:16:44 +02:00
|
|
|
var newTags []string
|
2026-04-01 00:13:43 +02:00
|
|
|
for _, t := range n.Tags {
|
2026-03-29 23:16:44 +02:00
|
|
|
if t != tag {
|
|
|
|
|
newTags = append(newTags, t)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-01 00:13:43 +02:00
|
|
|
n.Tags = newTags
|
2026-03-29 23:16:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (n *Node) AddRelation(relType RelType, target string) {
|
2026-04-01 00:13:43 +02:00
|
|
|
if n.Relations == nil {
|
|
|
|
|
n.Relations = make(map[string][]string)
|
2026-03-29 23:16:44 +02:00
|
|
|
}
|
2026-04-02 13:20:03 +02:00
|
|
|
if relType == RelAssignee || relType == RelCreated {
|
2026-04-01 00:13:43 +02:00
|
|
|
n.Relations[string(relType)] = []string{target}
|
2026-03-29 23:16:44 +02:00
|
|
|
return
|
|
|
|
|
}
|
2026-04-01 00:39:20 +02:00
|
|
|
if !slices.Contains(n.Relations[string(relType)], target) {
|
2026-04-01 00:13:43 +02:00
|
|
|
n.Relations[string(relType)] = append(n.Relations[string(relType)], target)
|
2026-03-29 23:16:44 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 00:13:43 +02:00
|
|
|
func (n *Node) RemoveRelation(relType RelType, target string) {
|
|
|
|
|
if n.Relations == nil {
|
|
|
|
|
return
|
2026-03-29 23:16:44 +02:00
|
|
|
}
|
|
|
|
|
var newTgts []string
|
2026-04-01 00:13:43 +02:00
|
|
|
for _, tgt := range n.Relations[string(relType)] {
|
2026-03-29 23:16:44 +02:00
|
|
|
if tgt != target {
|
|
|
|
|
newTgts = append(newTgts, tgt)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if len(newTgts) == 0 {
|
2026-04-01 00:13:43 +02:00
|
|
|
delete(n.Relations, string(relType))
|
2026-03-29 23:16:44 +02:00
|
|
|
} else {
|
2026-04-01 00:13:43 +02:00
|
|
|
n.Relations[string(relType)] = newTgts
|
2026-03-29 23:16:44 +02:00
|
|
|
}
|
2026-03-26 12:48:47 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-27 02:11:46 +01:00
|
|
|
func (n *Node) GetProperty(k string) string {
|
2026-04-01 12:50:45 +02:00
|
|
|
prefix := "_" + k + "::"
|
2026-04-01 00:13:43 +02:00
|
|
|
for _, t := range n.Tags {
|
2026-04-02 01:58:48 +02:00
|
|
|
if strings.HasPrefix(t, prefix) {
|
|
|
|
|
return strings.TrimPrefix(t, prefix)
|
|
|
|
|
}
|
2026-03-26 12:48:47 +00:00
|
|
|
}
|
2026-03-27 02:11:46 +01:00
|
|
|
return ""
|
2026-03-26 12:48:47 +00:00
|
|
|
}
|
2026-03-28 04:15:36 +01:00
|
|
|
|
|
|
|
|
func (n *Node) GetDisplayTags() []string {
|
|
|
|
|
var tags []string
|
2026-04-01 00:13:43 +02:00
|
|
|
for _, t := range n.Tags {
|
2026-03-28 04:15:36 +01:00
|
|
|
if !strings.HasPrefix(t, "_") {
|
|
|
|
|
tags = append(tags, t)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return tags
|
|
|
|
|
}
|