search all, url

This commit is contained in:
Ryan Stafford 2024-04-27 15:22:47 -04:00
parent 8dcbb2665e
commit 4a862b4e03
3 changed files with 18 additions and 4 deletions

View file

@ -947,14 +947,19 @@ func Search(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
state.GetSite()
}
m, _ := url.ParseQuery(r.URL.RawQuery)
state.SearchType = "Posts"
state.SearchType = "All"
if len(m["searchtype"]) > 0 {
switch m["searchtype"][0] {
case "Posts":
state.SearchType = "Posts"
case "Comments":
state.SearchType = "Comments"
case "Communities":
state.SearchType = "Communities"
case "Url":
state.SearchType = "Url"
}
}
state.Search(state.SearchType)
Render(w, "index.html", state)

View file

@ -157,14 +157,14 @@ func (s State) Unknown() string {
func (p State) SortBy(v string) string {
var q string
if p.Query != "" || p.SearchType == "Communities" {
q = "q=" + p.Query + "&communityname=" + p.CommunityName + "&username=" + p.UserName + "&searchtype=" + p.SearchType + "&"
q = "q=" + url.QueryEscape(p.Query) + "&communityname=" + p.CommunityName + "&username=" + p.UserName + "&searchtype=" + p.SearchType + "&"
}
return "?" + q + "sort=" + v + "&listingType=" + p.Listing
}
func (p State) ListBy(v string) string {
var q string
if p.Query != "" || p.SearchType == "Communities" {
q = "q=" + p.Query + "&communityname=" + p.CommunityName + "&username=" + p.UserName + "&searchtype=" + p.SearchType + "&"
q = "q=" + url.QueryEscape(p.Query) + "&communityname=" + p.CommunityName + "&username=" + p.UserName + "&searchtype=" + p.SearchType + "&"
}
return "?" + q + "sort=" + p.Sort + "&listingType=" + v
}
@ -625,10 +625,14 @@ func (state *State) Search(searchtype string) {
return
} else {
for i, p := range resp.Posts {
state.Posts = append(state.Posts, Post{
post := Post{
PostView: p,
Rank: (state.Page-1)*25 + i + 1,
State: state,
}
state.Activities = append(state.Activities, Activity{
Timestamp: p.Post.Published,
Post: &post,
})
}
for _, c := range resp.Comments {
@ -641,6 +645,9 @@ func (state *State) Search(searchtype string) {
Comment: &comment,
})
}
sort.Slice(state.Activities, func(i, j int) bool {
return state.Activities[i].Timestamp.After(state.Activities[j].Timestamp.Time)
})
state.Communities = resp.Communities
}
}

View file

@ -34,8 +34,10 @@
{{ if ne .SearchType "Communities" }}
<div>
<select name="searchtype">
<option value="All"{{ if eq .SearchType "All" }} selected{{ end }}>all</option>
<option value="Posts"{{ if eq .SearchType "Posts" }} selected{{ end }}>posts</option>
<option value="Comments"{{ if eq .SearchType "Comments" }} selected{{ end }}>comments</option>
<option value="Url"{{ if eq .SearchType "Url" }} selected{{ end }}>url</option>
</select>
<label>from
<input type="text" placeholder="everywhere" name="communityname" value="{{.CommunityName}}"></label>