github.com/bitcubate/cryptojournal@v1.2.5-0.20171102134152-f578b3d788ab/src/stories/actions/upvoted.go (about) 1 package storyactions 2 3 import ( 4 "net/http" 5 "strings" 6 7 "github.com/fragmenta/mux" 8 "github.com/fragmenta/query" 9 "github.com/fragmenta/server" 10 "github.com/fragmenta/server/config" 11 "github.com/fragmenta/view" 12 13 "github.com/bitcubate/cryptojournal/src/lib/session" 14 "github.com/bitcubate/cryptojournal/src/lib/stats" 15 "github.com/bitcubate/cryptojournal/src/stories" 16 ) 17 18 // HandleListUpvoted displays a list of stories the user has upvoted in the past 19 func HandleListUpvoted(w http.ResponseWriter, r *http.Request) error { 20 stats.RegisterHit(r) 21 22 // Build a query 23 q := stories.Query().Limit(listLimit) 24 25 // Select only above 0 points, Order by rank, then points, then name 26 q.Where("points > 0").Order("rank desc, points desc, id desc") 27 28 // Select only stories which the user has upvoted 29 user := session.CurrentUser(w, r) 30 if !user.Anon() { 31 // Can we use a join instead? 32 v := query.New("votes", "story_id").Select("select story_id as id from votes").Where("user_id=? AND story_id IS NOT NULL AND points > 0", user.ID) 33 34 storyIDs := v.ResultIDs() 35 if len(storyIDs) > 0 { 36 q.WhereIn("id", storyIDs) 37 } 38 } 39 40 // Fetch the params 41 params, err := mux.Params(r) 42 if err != nil { 43 return server.InternalError(err) 44 } 45 46 // Set the offset in pages if we have one 47 page := int(params.GetInt("page")) 48 if page > 0 { 49 q.Offset(listLimit * page) 50 } 51 52 // Fetch the stories 53 results, err := stories.FindAll(q) 54 if err != nil { 55 return server.InternalError(err) 56 } 57 58 // Render the template 59 view := view.NewRenderer(w, r) 60 61 view.AddKey("page", page) 62 view.AddKey("stories", results) 63 view.AddKey("pubdate", storiesModTime(results)) 64 view.AddKey("meta_title", "Stories you have upvoted") 65 view.AddKey("meta_desc", config.Get("meta_desc")) 66 view.AddKey("meta_keywords", config.Get("meta_keywords")) 67 view.AddKey("meta_rss", storiesXMLPath(w, r)) 68 view.Template("stories/views/index.html.got") 69 view.AddKey("currentUser", session.CurrentUser(w, r)) 70 71 if strings.HasSuffix(r.URL.Path, ".xml") { 72 view.Layout("") 73 view.Template("stories/views/index.xml.got") 74 } 75 76 return view.Render() 77 78 }