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]
98 func (n *ThreadedNewsYAML) GetCategories(paths []string) []hotline.NewsCategoryListData15 {
102 var categories []hotline.NewsCategoryListData15
103 for _, c := range n.getCatByPath(paths) {
104 categories = append(categories, c)
107 slices.SortFunc(categories, func(a, b hotline.NewsCategoryListData15) int {
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
126 func (n *ThreadedNewsYAML) PostArticle(newsPath []string, parentArticleID uint32, article hotline.NewsArtData) error {
130 binary.BigEndian.PutUint32(article.ParentArt[:], parentArticleID)
132 if len(newsPath) == 0 {
133 return fmt.Errorf("invalid news path")
136 cats := n.getCatByPath(newsPath[:len(newsPath)-1])
138 catName := newsPath[len(newsPath)-1]
142 for k := range cat.Articles {
143 keys = append(keys, int(k))
149 prevID := uint32(keys[len(keys)-1])
152 binary.BigEndian.PutUint32(article.PrevArt[:], prevID)
154 // Set next article Type
155 binary.BigEndian.PutUint32(cat.Articles[prevID].NextArt[:], nextID)
158 // Update parent article with first child reply
159 parentID := parentArticleID
161 parentArt := cat.Articles[parentID]
163 if parentArt.FirstChildArt == [4]byte{0, 0, 0, 0} {
164 binary.BigEndian.PutUint32(parentArt.FirstChildArt[:], nextID)
168 cat.Articles[nextID] = &article
175 func (n *ThreadedNewsYAML) DeleteArticle(newsPath []string, articleID uint32, _ bool) error {
180 // // TODO: Handle delete recursive
183 if len(newsPath) == 0 {
184 return fmt.Errorf("invalid news path")
187 cats := n.getCatByPath(newsPath[:len(newsPath)-1])
189 catName := newsPath[len(newsPath)-1]
192 delete(cat.Articles, articleID)
198 func (n *ThreadedNewsYAML) ListArticles(newsPath []string) hotline.NewsArtListData {
202 var cat hotline.NewsCategoryListData15
203 cats := n.ThreadedNews.Categories
205 for _, fp := range newsPath {
207 cats = cats[fp].SubCats
210 return cat.GetNewsArtListData()
213 func (n *ThreadedNewsYAML) Load() error {
217 fh, err := os.Open(n.filePath)
223 n.ThreadedNews = hotline.ThreadedNews{}
225 return yaml.NewDecoder(fh).Decode(&n.ThreadedNews)
228 func (n *ThreadedNewsYAML) writeFile() error {
229 out, err := yaml.Marshal(&n.ThreadedNews)
234 // Define a temporary file path in the same directory.
235 tempFilePath := n.filePath + ".tmp"
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)
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)