Blame


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