-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.go
268 lines (238 loc) · 7.37 KB
/
search.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
package main
import (
"bytes"
"crypto/tls"
"errors"
"fmt"
"log"
"net/url"
"time"
"codeberg.org/FiskFan1999/gemini"
bolt "go.etcd.io/bbolt"
)
func SearchHandler(u *url.URL, c *tls.Conn) gemini.Response {
if u.RawQuery == "" {
return gemini.ResponseFormat{
Status: gemini.Input,
Mime: "keyword or @username search",
Lines: nil,
}
}
searchTerm, err := url.QueryUnescape(u.RawQuery)
if err != nil {
return gemini.ResponseFormat{
Status: gemini.BadRequest,
Mime: err.Error(),
Lines: nil,
}
}
var lines gemini.Lines
if len(searchTerm) >= 2 && searchTerm[0] == '@' {
var header string
var threads []SearchResultThread
var posts []SearchResultPost
header = fmt.Sprintf("Search by user %s", searchTerm[1:])
// username search
threads, posts, err = SearchUser(searchTerm[1:])
if err != nil {
return gemini.ResponseFormat{
Status: gemini.BadRequest,
Mime: err.Error(),
Lines: nil,
}
}
lines = append(lines, fmt.Sprintf("%s%s", gemini.Header, header), "")
lines.Header(2, "Created threads")
for _, t := range threads {
lines.LinkDesc(fmt.Sprintf("/thread/%s/", t.ID), fmt.Sprintf("<%s> %s", t.Author, t.Title))
lines.Line(fmt.Sprintf("ID: %s", t.ID))
lines.Quote(t.FirstPost)
}
lines.Header(2, "Replies")
for _, p := range posts {
lines.LinkDesc(fmt.Sprintf("/thread/%s/", p.ThreadID), fmt.Sprintf("<%s> %s", p.ThreadAuthor, p.ThreadTitle))
lines.Line(fmt.Sprintf("ID: %s thread: %s", p.ID, p.ThreadID))
lines.Quote(p.Text)
}
} else {
// keyword search
foundIDs, err := DoKeywordSearch(searchTerm)
if err != nil {
return gemini.TemporaryFailure.Error(err)
}
/*
Populate information about these posts from database
(accounts for if a post is deleted).
*/
var results []SearchResultPost
if err := db.View(func(tx *bolt.Tx) error {
posts := tx.Bucket(DBALLPOSTS)
threads := tx.Bucket(DBALLTHREADS)
for _, id := range foundIDs {
post := posts.Bucket(id)
if post == nil {
log.Printf("Note: post bucket %s not found", id)
continue
}
var newResult SearchResultPost
newResult.Author = string(post.Get([]byte("user")))
newResult.Text = string(post.Get([]byte("text")))
newResult.ID = make([]byte, 16)
copy(newResult.ID, id)
newResult.ThreadID = make([]byte, 16)
copy(newResult.ThreadID, post.Get([]byte("thread")))
thread := threads.Bucket(newResult.ThreadID)
if thread == nil {
// thread may be deleted
log.Printf("Thread id %s not found", newResult.ThreadID)
continue
}
newResult.ThreadTitle = string(thread.Get([]byte("title")))
newResult.ThreadAuthor = string(thread.Get([]byte("user")))
results = append(results, newResult)
}
return nil
}); err != nil {
return gemini.TemporaryFailure.Error(err)
}
// write replies
lines.Header(1, fmt.Sprintf("Results for keywords %s", searchTerm))
for _, p := range results {
lines.LinkDesc(fmt.Sprintf("/thread/%s/", p.ThreadID), fmt.Sprintf("<%s> %s", p.ThreadAuthor, p.ThreadTitle))
lines.Line(fmt.Sprintf("ID: %s thread: %s", p.ID, p.ThreadID))
lines.Quote(p.Text)
}
}
return gemini.ResponseFormat{
Status: gemini.Success,
Mime: "text/gemini",
Lines: lines,
}
}
type SearchResultThread struct {
Title string
Author string
ID []byte
FirstPost string
}
type SearchResultPost struct {
ThreadID []byte
ThreadTitle string
ThreadAuthor string
ID []byte
Text string
Author string
Time time.Time
}
var SearchUsernameNotFound = errors.New("User by that name not found")
func SearchUser(username string) (threads []SearchResultThread, posts []SearchResultPost, err error) {
/*
Get all threads and posts by this user.
*/
err = db.View(func(tx *bolt.Tx) error {
// Get all threads by this user
/*
+--------------+----------------------------------+
| KEY | VALUE |
+--------------+----------------------------------+
| locked | 0 |
| posts* | |
| title | user1 first thread |
| user | user1 |
| archived | 0 |
| lastmodified | 2022-10-04T20:22:28.548479-04:00 |
+--------------+----------------------------------+
*/
users := tx.Bucket(DBUSERS)
if users.Bucket([]byte(username)) == nil {
return SearchUsernameNotFound
}
allPostsBucket := tx.Bucket(DBALLPOSTS)
allThreads := tx.Bucket(DBALLTHREADS)
userThreads := tx.Bucket(DBUSERTHREADS)
threadsByUser := userThreads.Bucket([]byte(username))
if threadsByUser != nil {
// read curser from back to front
threadsCursor := threadsByUser.Cursor()
for _, v := threadsCursor.Last(); v != nil; _, v = threadsCursor.Prev() {
// v = thread id
threadBucket := allThreads.Bucket(v)
if threadBucket == nil {
return errors.New("thread not found")
}
threadInfo := SearchResultThread{}
threadInfo.Title = string(threadBucket.Get([]byte("title")))
threadInfo.Author = string(threadBucket.Get([]byte("user")))
threadInfo.ID = make([]byte, len(v))
copy(threadInfo.ID, v)
/*
Get the text of the OP body (check if it is deleted)
*/
postsInThisThread := threadBucket.Bucket([]byte("posts"))
idOfFirstPost := postsInThisThread.Get(itob(1))
firstPost := allPostsBucket.Bucket(idOfFirstPost)
if firstPost != nil {
threadInfo.FirstPost = string(firstPost.Get([]byte("text")))
}
threads = append(threads, threadInfo)
}
}
// get all posts by this user
// (not including first posts
// on threads, because already
// in threads menu)
/*
+----------+--------------------------------+
| KEY | VALUE |
+----------+--------------------------------+
| archived | 0 |
| index | 0000000000000001 |
| text | user1 first post in second |
| | thread |
| thread | 0000000000000002 |
| user | user1 |
+----------+--------------------------------+
*/
userPosts := tx.Bucket(DBUSERPOSTS)
postsByUser := userPosts.Bucket([]byte(username))
if postsByUser != nil {
postsCursor := postsByUser.Cursor()
for _, v := postsCursor.Last(); v != nil; _, v = postsCursor.Prev() {
// v = post id
postBucket := allPostsBucket.Bucket(v)
if postBucket == nil {
/*
Was deleted
*/
fmt.Printf("post with id %d not found\n", v)
continue
}
if bytes.Equal(postBucket.Get([]byte("index")), []byte("0000000000000001")) {
// don't include first post in thread
continue
}
/*
TODO: parse time of post
*/
post := SearchResultPost{}
post.ID = make([]byte, 16)
copy(post.ID, v)
post.ThreadID = make([]byte, 16)
copy(post.ThreadID, postBucket.Get([]byte("thread")))
post.Text = string(postBucket.Get([]byte("text")))
/*
Get information about the thread
*/
thisThread := allThreads.Bucket(post.ThreadID)
if thisThread == nil {
fmt.Printf("Thread with id %s not found.\n", post.ThreadID)
}
post.ThreadTitle = string(thisThread.Get([]byte("title")))
post.ThreadAuthor = string(thisThread.Get([]byte("user")))
posts = append(posts, post)
}
}
return nil
})
return
}