2025-01-06 21:53:29 +00:00
|
|
|
package ttbp
|
|
|
|
|
|
|
|
import (
|
2025-01-07 00:16:11 +00:00
|
|
|
"bufio"
|
2025-01-06 21:53:29 +00:00
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.tilde.town/nbsp/neofeels/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
PathVar = "/var/global/ttbp"
|
|
|
|
PathVarWWW = path.Join(PathVar, "www")
|
|
|
|
PathLive = "https://tilde.town/~"
|
|
|
|
PathUserFile = path.Join(PathVar, "users.txt")
|
|
|
|
PathGraff = path.Join(PathVar, "graffiti")
|
|
|
|
PathWall = path.Join(PathGraff, "wall.txt")
|
|
|
|
PathWallLock = path.Join(PathGraff, ".lock")
|
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Name string
|
|
|
|
Publishing bool
|
|
|
|
PublishDir string
|
|
|
|
LastPublished time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUsers gets all users with a valid ttbp config, with their name, publishing
|
|
|
|
// settings, and date of last publish, if any.
|
|
|
|
func GetUsers() (users []User) {
|
|
|
|
userDirs, _ := os.ReadDir("/home")
|
|
|
|
for _, user := range userDirs {
|
|
|
|
if !user.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if file, err := os.Open(path.Join("/home", user.Name(), ".ttbp/config/ttbprc")); err == nil {
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
// get user config
|
|
|
|
var config config.Config
|
|
|
|
decoder := json.NewDecoder(file)
|
|
|
|
err = decoder.Decode(&config)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// get last published file
|
|
|
|
entries, err := os.ReadDir(path.Join("/home", user.Name(), ".ttbp/entries"))
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var lastPublished time.Time = *new(time.Time)
|
|
|
|
if len(entries) > 0 {
|
|
|
|
info, err := entries[len(entries)-1].Info()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
lastPublished = info.ModTime()
|
|
|
|
}
|
|
|
|
|
|
|
|
users = append(users, User{
|
|
|
|
Name: user.Name(),
|
|
|
|
Publishing: config.Publishing,
|
|
|
|
PublishDir: config.PublishDir,
|
|
|
|
LastPublished: lastPublished,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return users
|
|
|
|
}
|
|
|
|
|
|
|
|
// SortUsersByRecent sorts users, putting the users with the most recent
|
|
|
|
// published feels at the beginning.
|
|
|
|
func SortUsersByRecent(users []User) []User {
|
|
|
|
sort.Slice(users, func(i, j int) bool {
|
|
|
|
return users[i].LastPublished.After(users[j].LastPublished)
|
|
|
|
})
|
|
|
|
return users
|
|
|
|
}
|
2025-01-07 00:16:11 +00:00
|
|
|
|
|
|
|
type Post struct {
|
|
|
|
Date time.Time
|
|
|
|
Words int
|
|
|
|
Author string
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetPostsForUser(user string) (posts []Post) {
|
|
|
|
postFiles, err := os.ReadDir(path.Join("/home", user, ".ttbp/entries"))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, post := range postFiles {
|
|
|
|
// retrieve date of file
|
|
|
|
// assume file ends in .txt
|
|
|
|
fileDate, err := time.Parse("20060102.txt", post.Name())
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// get number of words in file
|
|
|
|
count := 0
|
|
|
|
if file, err := os.Open(path.Join("/home", user, ".ttbp/entries", post.Name())); err == nil {
|
|
|
|
defer file.Close()
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
scanner.Split(bufio.ScanWords)
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-07 00:20:30 +00:00
|
|
|
posts = append([]Post{Post{
|
2025-01-07 00:16:11 +00:00
|
|
|
Author: user,
|
|
|
|
Date: fileDate,
|
|
|
|
Words: count,
|
2025-01-07 00:20:30 +00:00
|
|
|
}}, posts...)
|
2025-01-07 00:16:11 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|