Blob


1 // Package lemmy provides a client interface to the Lemmy HTTP API version 3.
2 package lemmy
4 import (
5 "fmt"
6 "io/fs"
7 "strconv"
8 "strings"
9 "time"
10 )
12 type Community struct {
13 ID int `json:"id"`
14 FName string `json:"name"`
15 Title string `json:"title"`
16 Local bool
17 ActorID string `json:"actor_id"`
18 Published time.Time
19 }
21 func (c *Community) Name() string { return c.String() }
22 func (c *Community) Size() int64 { return 0 }
23 func (c *Community) Mode() fs.FileMode { return fs.ModeDir | 0o0555 }
24 func (c *Community) ModTime() time.Time { return c.Published }
25 func (c *Community) IsDir() bool { return c.Mode().IsDir() }
26 func (c *Community) Sys() interface{} { return nil }
28 func (c Community) String() string {
29 if c.Local {
30 return c.FName
31 }
32 noscheme := strings.TrimPrefix(c.ActorID, "https://")
33 instance, _, _ := strings.Cut(noscheme, "/")
34 return fmt.Sprintf("%s@%s", c.FName, instance)
35 }
37 type Post struct {
38 ID int
39 Title string `json:"name"`
40 Body string
41 CreatorID int `json:"creator_id"`
42 URL string
43 Published time.Time
44 Updated time.Time
45 Creator Person `json:"-"`
46 }
48 func (p *Post) Name() string { return strconv.Itoa(p.ID) }
50 func (p *Post) Size() int64 {
51 return int64(len(p.Body))
52 }
54 func (p *Post) Mode() fs.FileMode { return fs.ModeDir | 0o0555 }
55 func (p *Post) IsDir() bool { return p.Mode().IsDir() }
56 func (p *Post) Sys() interface{} { return nil }
57 func (p *Post) ModTime() time.Time {
58 if p.Updated.IsZero() {
59 return p.Published
60 }
61 return p.Updated
62 }
64 type Comment struct {
65 ID int
66 PostID int `json:"post_id"`
67 // Holds ordered comment IDs referenced by this comment
68 // for threading.
69 Path string
70 Content string
71 CreatorID int `json:"creator_id"`
72 Published time.Time
73 Updated time.Time
74 ActivityURL string `json:"ap_id"`
75 Creator Person `json:"-"`
76 }
78 func (c *Comment) Name() string { return strconv.Itoa(c.ID) }
80 func (c *Comment) Size() int64 { return 0 }
81 func (c *Comment) Mode() fs.FileMode { return 0444 }
82 func (c *Comment) ModTime() time.Time {
83 if c.Updated.IsZero() {
84 return c.Published
85 }
86 return c.Updated
87 }
88 func (c *Comment) IsDir() bool { return c.Mode().IsDir() }
89 func (c *Comment) Sys() interface{} { return nil }
91 // ParseCommentPath returns the comment IDs referenced by a Comment.
92 func ParseCommentPath(s string) []int {
93 elems := strings.Split(s, ".")
94 if len(elems) == 1 {
95 return []int{}
96 }
97 if elems[0] != "0" {
98 return []int{}
99 }
100 refs := make([]int, len(elems))
101 for i, ele := range elems {
102 id, err := strconv.Atoi(ele)
103 if err != nil {
104 return refs
106 refs[i] = id
108 return refs
111 type Person struct {
112 ID int `json:"id"`
113 Name string `json:"name"`
114 ActorID string `json:"actor_id"`
115 Local bool `json:"local"`
118 func (p Person) String() string {
119 if p.Local {
120 return p.Name
122 noscheme := strings.TrimPrefix(p.ActorID, "https://")
123 instance, _, _ := strings.Cut(noscheme, "/")
124 return fmt.Sprintf("%s@%s", p.Name, instance)