]> git.r.bdr.sh - rbdr/mobius/blob - internal/mobius/threaded_news.go
Fix panic on empty news path
[rbdr/mobius] / internal / mobius / threaded_news.go
1 package mobius
2
3 import (
4 "cmp"
5 "encoding/binary"
6 "fmt"
7 "github.com/jhalter/mobius/hotline"
8 "gopkg.in/yaml.v3"
9 "os"
10 "slices"
11 "sort"
12 "sync"
13 )
14
15 type ThreadedNewsYAML struct {
16 ThreadedNews hotline.ThreadedNews
17
18 filePath string
19
20 mu sync.Mutex
21 }
22
23 func NewThreadedNewsYAML(filePath string) (*ThreadedNewsYAML, error) {
24 tn := &ThreadedNewsYAML{filePath: filePath}
25
26 err := tn.Load()
27
28 return tn, err
29 }
30
31 func (n *ThreadedNewsYAML) CreateGrouping(newsPath []string, name string, t [2]byte) error {
32 n.mu.Lock()
33 defer n.mu.Unlock()
34
35 cats := n.getCatByPath(newsPath)
36 cats[name] = hotline.NewsCategoryListData15{
37 Name: name,
38 Type: t,
39 Articles: map[uint32]*hotline.NewsArtData{},
40 SubCats: make(map[string]hotline.NewsCategoryListData15),
41 }
42
43 return n.writeFile()
44 }
45
46 func (n *ThreadedNewsYAML) NewsItem(newsPath []string) hotline.NewsCategoryListData15 {
47 n.mu.Lock()
48 defer n.mu.Unlock()
49
50 cats := n.ThreadedNews.Categories
51 delName := newsPath[len(newsPath)-1]
52 if len(newsPath) > 1 {
53 for _, fp := range newsPath[0 : len(newsPath)-1] {
54 cats = cats[fp].SubCats
55 }
56 }
57
58 return cats[delName]
59 }
60
61 func (n *ThreadedNewsYAML) DeleteNewsItem(newsPath []string) error {
62 n.mu.Lock()
63 defer n.mu.Unlock()
64
65 cats := n.ThreadedNews.Categories
66 delName := newsPath[len(newsPath)-1]
67 if len(newsPath) > 1 {
68 for _, fp := range newsPath[0 : len(newsPath)-1] {
69 cats = cats[fp].SubCats
70 }
71 }
72
73 delete(cats, delName)
74
75 return n.writeFile()
76 }
77
78 func (n *ThreadedNewsYAML) GetArticle(newsPath []string, articleID uint32) *hotline.NewsArtData {
79 n.mu.Lock()
80 defer n.mu.Unlock()
81
82 var cat hotline.NewsCategoryListData15
83 cats := n.ThreadedNews.Categories
84
85 for _, fp := range newsPath {
86 cat = cats[fp]
87 cats = cats[fp].SubCats
88 }
89
90 art := cat.Articles[articleID]
91 if art == nil {
92 return nil
93 }
94
95 return art
96 }
97
98 func (n *ThreadedNewsYAML) GetCategories(paths []string) []hotline.NewsCategoryListData15 {
99 n.mu.Lock()
100 defer n.mu.Unlock()
101
102 var categories []hotline.NewsCategoryListData15
103 for _, c := range n.getCatByPath(paths) {
104 categories = append(categories, c)
105 }
106
107 slices.SortFunc(categories, func(a, b hotline.NewsCategoryListData15) int {
108 return cmp.Compare(
109 a.Name,
110 b.Name,
111 )
112 })
113
114 return categories
115 }
116
117 func (n *ThreadedNewsYAML) getCatByPath(paths []string) map[string]hotline.NewsCategoryListData15 {
118 cats := n.ThreadedNews.Categories
119 for _, path := range paths {
120 cats = cats[path].SubCats
121 }
122
123 return cats
124 }
125
126 func (n *ThreadedNewsYAML) PostArticle(newsPath []string, parentArticleID uint32, article hotline.NewsArtData) error {
127 n.mu.Lock()
128 defer n.mu.Unlock()
129
130 binary.BigEndian.PutUint32(article.ParentArt[:], parentArticleID)
131
132 if len(newsPath) == 0 {
133 return fmt.Errorf("invalid news path")
134 }
135
136 cats := n.getCatByPath(newsPath[:len(newsPath)-1])
137
138 catName := newsPath[len(newsPath)-1]
139 cat := cats[catName]
140
141 var keys []int
142 for k := range cat.Articles {
143 keys = append(keys, int(k))
144 }
145
146 nextID := uint32(1)
147 if len(keys) > 0 {
148 sort.Ints(keys)
149 prevID := uint32(keys[len(keys)-1])
150 nextID = prevID + 1
151
152 binary.BigEndian.PutUint32(article.PrevArt[:], prevID)
153
154 // Set next article Type
155 binary.BigEndian.PutUint32(cat.Articles[prevID].NextArt[:], nextID)
156 }
157
158 // Update parent article with first child reply
159 parentID := parentArticleID
160 if parentID != 0 {
161 parentArt := cat.Articles[parentID]
162
163 if parentArt.FirstChildArt == [4]byte{0, 0, 0, 0} {
164 binary.BigEndian.PutUint32(parentArt.FirstChildArt[:], nextID)
165 }
166 }
167
168 cat.Articles[nextID] = &article
169
170 cats[catName] = cat
171
172 return n.writeFile()
173 }
174
175 func (n *ThreadedNewsYAML) DeleteArticle(newsPath []string, articleID uint32, _ bool) error {
176 n.mu.Lock()
177 defer n.mu.Unlock()
178
179 //if recursive {
180 // // TODO: Handle delete recursive
181 //}
182
183 if len(newsPath) == 0 {
184 return fmt.Errorf("invalid news path")
185 }
186
187 cats := n.getCatByPath(newsPath[:len(newsPath)-1])
188
189 catName := newsPath[len(newsPath)-1]
190
191 cat := cats[catName]
192 delete(cat.Articles, articleID)
193 cats[catName] = cat
194
195 return n.writeFile()
196 }
197
198 func (n *ThreadedNewsYAML) ListArticles(newsPath []string) hotline.NewsArtListData {
199 n.mu.Lock()
200 defer n.mu.Unlock()
201
202 var cat hotline.NewsCategoryListData15
203 cats := n.ThreadedNews.Categories
204
205 for _, fp := range newsPath {
206 cat = cats[fp]
207 cats = cats[fp].SubCats
208 }
209
210 return cat.GetNewsArtListData()
211 }
212
213 func (n *ThreadedNewsYAML) Load() error {
214 n.mu.Lock()
215 defer n.mu.Unlock()
216
217 fh, err := os.Open(n.filePath)
218 if err != nil {
219 return err
220 }
221 defer fh.Close()
222
223 n.ThreadedNews = hotline.ThreadedNews{}
224
225 return yaml.NewDecoder(fh).Decode(&n.ThreadedNews)
226 }
227
228 func (n *ThreadedNewsYAML) writeFile() error {
229 out, err := yaml.Marshal(&n.ThreadedNews)
230 if err != nil {
231 return err
232 }
233
234 // Define a temporary file path in the same directory.
235 tempFilePath := n.filePath + ".tmp"
236
237 // Write the marshaled YAML to the temporary file.
238 if err := os.WriteFile(tempFilePath, out, 0644); err != nil {
239 return fmt.Errorf("write to temporary file: %v", err)
240 }
241
242 // Atomically rename the temporary file to the final file path.
243 if err := os.Rename(tempFilePath, n.filePath); err != nil {
244 return fmt.Errorf("rename temporary file to final file: %v", err)
245 }
246
247 return nil
248 }