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() state.GetSite()
} }
m, _ := url.ParseQuery(r.URL.RawQuery) m, _ := url.ParseQuery(r.URL.RawQuery)
state.SearchType = "Posts" state.SearchType = "All"
if len(m["searchtype"]) > 0 { if len(m["searchtype"]) > 0 {
switch m["searchtype"][0] { switch m["searchtype"][0] {
case "Posts":
state.SearchType = "Posts"
case "Comments": case "Comments":
state.SearchType = "Comments" state.SearchType = "Comments"
case "Communities": case "Communities":
state.SearchType = "Communities" state.SearchType = "Communities"
case "Url":
state.SearchType = "Url"
} }
} }
state.Search(state.SearchType) state.Search(state.SearchType)
Render(w, "index.html", state) Render(w, "index.html", state)

View file

@ -157,14 +157,14 @@ func (s State) Unknown() string {
func (p State) SortBy(v string) string { func (p State) SortBy(v string) string {
var q string var q string
if p.Query != "" || p.SearchType == "Communities" { 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 return "?" + q + "sort=" + v + "&listingType=" + p.Listing
} }
func (p State) ListBy(v string) string { func (p State) ListBy(v string) string {
var q string var q string
if p.Query != "" || p.SearchType == "Communities" { 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 return "?" + q + "sort=" + p.Sort + "&listingType=" + v
} }
@ -625,10 +625,14 @@ func (state *State) Search(searchtype string) {
return return
} else { } else {
for i, p := range resp.Posts { for i, p := range resp.Posts {
state.Posts = append(state.Posts, Post{ post := Post{
PostView: p, PostView: p,
Rank: (state.Page-1)*25 + i + 1, Rank: (state.Page-1)*25 + i + 1,
State: state, State: state,
}
state.Activities = append(state.Activities, Activity{
Timestamp: p.Post.Published,
Post: &post,
}) })
} }
for _, c := range resp.Comments { for _, c := range resp.Comments {
@ -641,6 +645,9 @@ func (state *State) Search(searchtype string) {
Comment: &comment, 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 state.Communities = resp.Communities
} }
} }

View file

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