7 "github.com/jhalter/mobius/hotline"
15 type ThreadedNewsYAML struct {
16 ThreadedNews hotline.ThreadedNews
23 func NewThreadedNewsYAML(filePath string) (*ThreadedNewsYAML, error) {
24 tn := &ThreadedNewsYAML{filePath: filePath}
31 func (n *ThreadedNewsYAML) CreateGrouping(newsPath []string, name string, t [2]byte) error {
35 cats := n.getCatByPath(newsPath)
36 cats[name] = hotline.NewsCategoryListData15{
39 Articles: map[uint32]*hotline.NewsArtData{},
40 SubCats: make(map[string]hotline.NewsCategoryListData15),
46 func (n *ThreadedNewsYAML) NewsItem(newsPath []string) hotline.NewsCategoryListData15 {
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
61 func (n *ThreadedNewsYAML) DeleteNewsItem(newsPath []string) error {
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
78 func (n *ThreadedNewsYAML) GetArticle(newsPath []string, articleID uint32) *hotline.NewsArtData {
82 var cat hotline.NewsCategoryListData15
83 cats := n.ThreadedNews.Categories
85 for _, fp := range newsPath {
87 cats = cats[fp].SubCats
90 art := cat.Articles[articleID]
99 //func (n *ThreadedNewsYAML) GetNewsCatByPath(paths []string) map[string]hotline.NewsCategoryListData15 {
101 // defer n.mu.Unlock()
103 // cats := n.getCatByPath(paths)
108 func (n *ThreadedNewsYAML) GetCategories(paths []string) []hotline.NewsCategoryListData15 {
112 var categories []hotline.NewsCategoryListData15
113 for _, c := range n.getCatByPath(paths) {
114 categories = append(categories, c)
117 slices.SortFunc(categories, func(a, b hotline.NewsCategoryListData15) int {
127 func (n *ThreadedNewsYAML) getCatByPath(paths []string) map[string]hotline.NewsCategoryListData15 {
128 cats := n.ThreadedNews.Categories
129 for _, path := range paths {
130 cats = cats[path].SubCats
136 func (n *ThreadedNewsYAML) PostArticle(newsPath []string, parentArticleID uint32, article hotline.NewsArtData) error {
140 binary.BigEndian.PutUint32(article.ParentArt[:], parentArticleID)
142 cats := n.getCatByPath(newsPath[:len(newsPath)-1])
144 catName := newsPath[len(newsPath)-1]
148 for k := range cat.Articles {
149 keys = append(keys, int(k))
155 prevID := uint32(keys[len(keys)-1])
158 binary.BigEndian.PutUint32(article.PrevArt[:], prevID)
160 // Set next article Type
161 binary.BigEndian.PutUint32(cat.Articles[prevID].NextArt[:], nextID)
164 // Update parent article with first child reply
165 parentID := parentArticleID
167 parentArt := cat.Articles[parentID]
169 if parentArt.FirstChildArt == [4]byte{0, 0, 0, 0} {
170 binary.BigEndian.PutUint32(parentArt.FirstChildArt[:], nextID)
174 cat.Articles[nextID] = &article
181 func (n *ThreadedNewsYAML) DeleteArticle(newsPath []string, articleID uint32, recursive bool) error {
186 // TODO: Handle delete recursive
189 cats := n.getCatByPath(newsPath[:len(newsPath)-1])
191 catName := newsPath[len(newsPath)-1]
194 delete(cat.Articles, articleID)
200 func (n *ThreadedNewsYAML) ListArticles(newsPath []string) hotline.NewsArtListData {
204 var cat hotline.NewsCategoryListData15
205 cats := n.ThreadedNews.Categories
207 for _, fp := range newsPath {
209 cats = cats[fp].SubCats
212 return cat.GetNewsArtListData()
215 func (n *ThreadedNewsYAML) Load() error {
219 fh, err := os.Open(n.filePath)
225 n.ThreadedNews = hotline.ThreadedNews{}
227 decoder := yaml.NewDecoder(fh)
228 return decoder.Decode(&n.ThreadedNews)
231 func (n *ThreadedNewsYAML) writeFile() error {
232 out, err := yaml.Marshal(&n.ThreadedNews)
237 // Define a temporary file path in the same directory.
238 tempFilePath := n.filePath + ".tmp"
240 // Write the marshaled YAML to the temporary file.
241 if err := os.WriteFile(tempFilePath, out, 0644); err != nil {
242 return fmt.Errorf("write to temporary file: %v", err)
245 // Atomically rename the temporary file to the final file path.
246 if err := os.Rename(tempFilePath, n.filePath); err != nil {
247 return fmt.Errorf("rename temporary file to final file: %v", err)