This repository was archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsiteLemmy.go
307 lines (279 loc) · 6.7 KB
/
siteLemmy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package main
import (
"context"
"errors"
"net/http"
"strconv"
"strings"
"time"
rl "github.com/gen2brain/raylib-go/raylib"
lemmy "go.elara.ws/go-lemmy"
)
type LemmySite struct {
*lemmy.Client
base string
}
func NewLemmyClient(site string, user string, pass string) LemmySite {
if site == "" {
return LemmySite{}
}
cl, err := lemmy.New("https://" + site)
if err != nil {
panic(err)
}
if user != "" {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
err = cl.ClientLogin(ctx, lemmy.Login{UsernameOrEmail: user, Password: pass})
if err != nil {
return LemmySite{}
}
}
return LemmySite{cl, site}
}
func (LemmySite) Destroy() {}
func (l LemmySite) GetListingInfo() []ListingInfo {
return []ListingInfo{
{
name: "Community: New",
args: []ListingArgument{
{
name: "Community",
},
},
},
{
name: "Community: Hot",
args: []ListingArgument{
{
name: "Community",
},
},
},
{
name: "Community: Active",
args: []ListingArgument{
{
name: "Community",
},
},
},
{
name: "Community: Top Week",
args: []ListingArgument{
{
name: "Community",
},
},
},
{
name: "Community: Top Month",
args: []ListingArgument{
{
name: "Community",
},
},
},
{
name: "Community: Top All Time",
args: []ListingArgument{
{
name: "Community",
},
},
},
}
}
type LemmyPostListing struct {
lemmy.GetPosts
args []interface{}
page int64
kind int
persistent int64
}
func (p *LemmyPostListing) GetInfo() (int, []interface{}) {
return p.kind, p.args
}
func (p *LemmyPostListing) GetPersistent() interface{} {
return float64(p.persistent)
}
func (lem LemmySite) GetListing(kind int, args []interface{}, persist interface{}) (ImageListing, []ImageEntry) {
if lem.Client == nil {
return ErrorListing{errors.New("not signed in to Lemmy")}, nil
}
var posts lemmy.GetPosts
var err error
if kind < 6 {
var sub *lemmy.GetCommunityResponse
sub, err = lem.Community(context.Background(), lemmy.GetCommunity{Name: lemmy.NewOptional[string](args[0].(string))})
if err == nil && sub.Error.IsValid() {
err = errors.New(sub.Error.String())
}
if err == nil {
posts.CommunityID = lemmy.NewOptional[int64](sub.CommunityView.Counts.CommunityID)
posts.Sort = lemmy.NewOptional[lemmy.SortType]([]lemmy.SortType{
lemmy.SortTypeNew, lemmy.SortTypeHot, lemmy.SortTypeActive, lemmy.SortTypeTopWeek, lemmy.SortTypeTopMonth, lemmy.SortTypeTopAll,
}[kind])
}
} else {
err = errors.New("unknown kind")
}
if err != nil {
return ErrorListing{err}, nil
}
posts.Limit = lemmy.NewOptional[int64](20)
ls := &LemmyPostListing{GetPosts: posts, kind: kind, args: args}
if persist != nil {
temp := persist.(float64)
ls.persistent = int64(temp)
}
out, err := lem.ExtendListing(ls)
if err != nil {
return ErrorListing{err}, nil
}
return ls, out
}
func (lem LemmySite) ExtendListing(cont ImageListing) ([]ImageEntry, error) {
posts, ok := cont.(*LemmyPostListing)
if !ok {
return nil, errors.New("unable to cast listing")
}
if posts.page == -1 {
return nil, nil
}
posts.page += 1
posts.Page = lemmy.NewOptional[int64](posts.page)
resp, err := lem.Posts(context.Background(), posts.GetPosts)
if err != nil {
return nil, err
}
if resp.Error.IsValid() {
return nil, errors.New(resp.Error.String())
}
ls := make([]ImageEntry, 0, len(resp.Posts))
for _, v := range resp.Posts {
if v.Post.ID < posts.persistent {
continue
}
ls = append(ls, LemmyImageEntry{lem.base, v.Post})
}
if len(ls) == 0 {
posts.page = -1
return nil, nil
}
return ls, nil
}
func (lem LemmySite) GetRequest(url string) (*http.Response, error) {
return http.Get(url)
}
func (lem LemmySite) GetResolvableDomains() []string {
// TODO: ???????????????????
// The problem with the fediverse...
return []string{"feddit.nl", "burggit.moe"}
}
func (lem LemmySite) ResolveURL(u string) (string, ImageEntry) {
ind := strings.LastIndexByte(u, '/')
if ind == -1 {
return "", nil
}
ind2 := strings.LastIndexByte(u[:ind], '/')
if u[ind2+1:ind] != "post" {
return "", nil
}
id, err := strconv.ParseInt(u[ind+1:], 10, 64)
if err != nil {
return "", nil
}
ps, err := lem.Post(context.Background(), lemmy.GetPost{
ID: lemmy.NewOptional[int64](id),
})
if err != nil || ps.Error.IsValid() {
return "", nil
}
return "", LemmyImageEntry{lem.base, ps.PostView.Post}
}
type LemmyImageEntry struct {
site string
lemmy.Post
}
func (l LemmyImageEntry) GetName() string { return l.Name }
func (l LemmyImageEntry) GetText() string { return l.Body.ValueOr("") }
func (l LemmyImageEntry) GetURL() string { return l.URL.ValueOr("") }
func (l LemmyImageEntry) GetGalleryInfo(bool) []ImageEntry { return nil }
func (l LemmyImageEntry) GetType() ImageEntryType {
if l.URL.IsValid() {
return IETYPE_REGULAR
}
return IETYPE_TEXT
}
func (l LemmyImageEntry) GetPostURL() string {
return "https://" + l.site + "/post/" + strconv.FormatInt(l.ID, 10)
}
func (l LemmyImageEntry) GetSaveName() string {
if !l.URL.IsValid() {
return ""
}
u := l.URL.ValueOrZero()
ind := strings.LastIndexByte(u, '/')
if ind == -1 {
return ""
}
s := u[ind+1:]
if s == "" {
return s
}
ind = strings.IndexByte(s, '?')
if ind != -1 {
s = s[:ind]
}
return s
}
// TODO
func (l LemmyImageEntry) GetInfo() string { return "" }
// TODO: Support imgur maybe
func (l LemmyImageEntry) Combine(ie ImageEntry) {
if ie.GetText() != "" {
if l.Body.IsValid() {
l.Body.Set(l.Body.ValueOrZero() + "\n" + ie.GetText())
} else {
l.Body.Set(ie.GetText())
}
}
l.URL.Set(ie.GetURL())
}
type LemmyProducer struct {
*BufferedImageProducer
site LemmySite
}
func NewLemmyProducer(site LemmySite, kind int, args []interface{}, persistent interface{}) LemmyProducer {
return LemmyProducer{NewBufferedImageProducer(site, kind, args, persistent), site}
}
func (p LemmyProducer) ActionHandler(key int32, sel int, call int) ActionRet {
useful, ok := p.items[sel].(LemmyImageEntry)
if !ok {
return p.BufferedImageProducer.ActionHandler(key, sel, call)
}
switch key {
case rl.KeyL:
if p.listing.(*LemmyPostListing).kind != 1 {
p.listing.(*LemmyPostListing).persistent = useful.ID
p.items = p.items[:sel+1]
for i := BIP_BUFBEFORE + 1; i < BIP_BUFBEFORE+1+BIP_BUFAFTER; i++ {
p.buffer[i] = BufferObject{}
}
p.listing.(*LemmyPostListing).page = -1
return ARET_MOVEUP
}
case rl.KeyEnter:
ret := p.BufferedImageProducer.ActionHandler(key, sel, call)
rl.SetWindowTitle(p.GetTitle())
return ret
}
return p.BufferedImageProducer.ActionHandler(key, sel, call)
}
func (p LemmyProducer) GetTitle() string {
if p.listing == nil {
return p.BufferedImageProducer.GetTitle()
}
return "multiSav - Lemmy"
}