forked from danjac/photoshare
/
feeds.go
85 lines (67 loc) · 1.92 KB
/
feeds.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
package photoshare
import (
"fmt"
"github.com/gorilla/feeds"
"net/http"
"strconv"
"time"
)
func photoFeed(w http.ResponseWriter,
r *http.Request,
title string,
description string,
link string,
photos *photoList) error {
baseURL := getBaseURL(r)
feed := &feeds.Feed{
Title: title,
Link: &feeds.Link{Href: baseURL + link},
Description: description,
Created: time.Now(),
}
for _, photo := range photos.Items {
item := &feeds.Item{
Id: strconv.FormatInt(photo.ID, 10),
Title: photo.Title,
Link: &feeds.Link{Href: fmt.Sprintf("%s/#/detail/%d", baseURL, photo.ID)},
Description: fmt.Sprintf("<img src=\"%s/uploads/thumbnails/%s\">", baseURL, photo.Filename),
Created: photo.CreatedAt,
}
feed.Add(item)
}
atom, err := feed.ToAtom()
if err != nil {
return err
}
writeBody(w, []byte(atom), http.StatusOK, "application/atom+xml")
return nil
}
func latestFeed(ctx *context, w http.ResponseWriter, r *http.Request) error {
photos, err := ctx.datamapper.getPhotos(newPage(1), "")
if err != nil {
return err
}
return photoFeed(w, r, "Latest photos", "Most recent photos", "/latest", photos)
}
func popularFeed(ctx *context, w http.ResponseWriter, r *http.Request) error {
photos, err := ctx.datamapper.getPhotos(newPage(1), "votes")
if err != nil {
return err
}
return photoFeed(w, r, "Popular photos", "Most upvoted photos", "/popular", photos)
}
func ownerFeed(ctx *context, w http.ResponseWriter, r *http.Request) error {
ownerID := ctx.params.getInt("owner")
owner, err := ctx.datamapper.getActiveUser(ownerID)
if err != nil {
return err
}
title := "Feeds for " + owner.Name
description := "List of feeds for " + owner.Name
link := fmt.Sprintf("/owner/%d/%s", ownerID, owner.Name)
photos, err := ctx.datamapper.getPhotosByOwnerID(newPage(1), ownerID)
if err != nil {
return err
}
return photoFeed(w, r, title, description, link, photos)
}