aboutsummaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
Diffstat (limited to 'service')
-rw-r--r--service/auth.go8
-rw-r--r--service/logging.go8
-rw-r--r--service/service.go46
-rw-r--r--service/transport.go25
4 files changed, 87 insertions, 0 deletions
diff --git a/service/auth.go b/service/auth.go
index 0c2e7f6..431e093 100644
--- a/service/auth.go
+++ b/service/auth.go
@@ -149,6 +149,14 @@ func (s *authService) ServeRetweetedByPage(ctx context.Context, client io.Writer
return s.Service.ServeRetweetedByPage(ctx, client, c, id)
}
+func (s *authService) ServeSearchPage(ctx context.Context, client io.Writer, c *model.Client, q string, qType string, offset int) (err error) {
+ c, err = s.getClient(ctx)
+ if err != nil {
+ return
+ }
+ return s.Service.ServeSearchPage(ctx, client, c, q, qType, offset)
+}
+
func (s *authService) Like(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {
c, err = s.getClient(ctx)
if err != nil {
diff --git a/service/logging.go b/service/logging.go
index f5e6d66..a5ffd57 100644
--- a/service/logging.go
+++ b/service/logging.go
@@ -125,6 +125,14 @@ func (s *loggingService) ServeRetweetedByPage(ctx context.Context, client io.Wri
return s.Service.ServeRetweetedByPage(ctx, client, c, id)
}
+func (s *loggingService) ServeSearchPage(ctx context.Context, client io.Writer, c *model.Client, q string, qType string, offset int) (err error) {
+ defer func(begin time.Time) {
+ s.logger.Printf("method=%v, q=%v, type=%v, offset=%v, took=%v, err=%v\n",
+ "ServeSearchPage", q, qType, offset, time.Since(begin), err)
+ }(time.Now())
+ return s.Service.ServeSearchPage(ctx, client, c, q, qType, offset)
+}
+
func (s *loggingService) Like(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n",
diff --git a/service/service.go b/service/service.go
index 2787079..157ddf8 100644
--- a/service/service.go
+++ b/service/service.go
@@ -39,6 +39,7 @@ type Service interface {
ServeEmojiPage(ctx context.Context, client io.Writer, c *model.Client) (err error)
ServeLikedByPage(ctx context.Context, client io.Writer, c *model.Client, id string) (err error)
ServeRetweetedByPage(ctx context.Context, client io.Writer, c *model.Client, id string) (err error)
+ ServeSearchPage(ctx context.Context, client io.Writer, c *model.Client, q string, qType string, offset int) (err error)
Like(ctx context.Context, client io.Writer, c *model.Client, id string) (err error)
UnLike(ctx context.Context, client io.Writer, c *model.Client, id string) (err error)
Retweet(ctx context.Context, client io.Writer, c *model.Client, id string) (err error)
@@ -593,6 +594,51 @@ func (svc *service) ServeRetweetedByPage(ctx context.Context, client io.Writer,
return
}
+
+func (svc *service) ServeSearchPage(ctx context.Context, client io.Writer, c *model.Client, q string, qType string, offset int) (err error) {
+ var hasNext bool
+ var nextLink string
+
+ results, err := c.Search(ctx, q, qType, 20, true, offset)
+ if err != nil {
+ return
+ }
+
+ switch qType {
+ case "accounts":
+ hasNext = len(results.Accounts) == 20
+ case "statuses":
+ hasNext = len(results.Statuses) == 20
+ }
+
+ if hasNext {
+ offset += 20
+ nextLink = fmt.Sprintf("/search?q=%s&type=%s&offset=%d", q, qType, offset)
+ }
+
+ commonData, err := svc.getCommonData(ctx, client, c)
+ if err != nil {
+ return
+ }
+
+ data := &renderer.SearchData{
+ CommonData: commonData,
+ Q: q,
+ Type: qType,
+ Users: results.Accounts,
+ Statuses: results.Statuses,
+ HasNext: hasNext,
+ NextLink: nextLink,
+ }
+
+ err = svc.renderer.RenderSearchPage(ctx, client, data)
+ if err != nil {
+ return
+ }
+
+ return
+}
+
func (svc *service) getCommonData(ctx context.Context, client io.Writer, c *model.Client) (data *renderer.CommonData, err error) {
data = new(renderer.CommonData)
diff --git a/service/transport.go b/service/transport.go
index 6541772..c42462f 100644
--- a/service/transport.go
+++ b/service/transport.go
@@ -6,6 +6,7 @@ import (
"mime/multipart"
"net/http"
"path"
+ "strconv"
"github.com/gorilla/mux"
)
@@ -280,6 +281,30 @@ func NewHandler(s Service, staticDir string) http.Handler {
}
}).Methods(http.MethodGet)
+ r.HandleFunc("/search", func(w http.ResponseWriter, req *http.Request) {
+ ctx := getContextWithSession(context.Background(), req)
+
+ q := req.URL.Query().Get("q")
+ qType := req.URL.Query().Get("type")
+ offsetStr := req.URL.Query().Get("offset")
+
+ var offset int
+ var err error
+ if len(offsetStr) > 1 {
+ offset, err = strconv.Atoi(offsetStr)
+ if err != nil {
+ s.ServeErrorPage(ctx, w, err)
+ return
+ }
+ }
+
+ err = s.ServeSearchPage(ctx, w, nil, q, qType, offset)
+ if err != nil {
+ s.ServeErrorPage(ctx, w, err)
+ return
+ }
+ }).Methods(http.MethodGet)
+
r.HandleFunc("/signout", func(w http.ResponseWriter, req *http.Request) {
// TODO remove session from database
w.Header().Add("Set-Cookie", fmt.Sprintf("session_id=;max-age=0"))