-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharticle.go
184 lines (155 loc) · 5.31 KB
/
article.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"fmt"
"net/http"
"strconv"
"time"
)
// JSON-parsed poll options - all the data that defines a poll.
type PollOptionData struct {
Options []string //`db:"options"`
AnyoneCanAddOptions bool //`db:"bAnyoneCanAddOptions"`
CanSelectMultipleOptions bool //`db:"bCanSelectMultipleOptions"`
RankedChoiceVoting bool //`db:"bRankedChoiceVoting"`
}
// JSON-parsed format of an article.
type Article struct {
// News parameters:
Author string
Title string
Description string
Url string
UrlToImage string
PublishedAt string
// Custom parameters:
Id int64
UserId int64
UrlToThumbnail string
NewsSourceId string
Host string
Category string
Language string
Country string
PublishedAtUnix time.Time
TimeSince string
Size int // 0=normal, 1=large (headline), 2=full page (article or viewPollResults)
AuthorIconUrl string
Bucket string // "" by default, but can override Category as a way to categorize articles
Upvoted int
VoteTally int
NumComments int
NumLines int
ThumbnailStatus int
IsThumbnail bool
// Poll parameters:
IsPoll bool
WeVoted bool
ShowNewOption bool // Prompt for new option creation when voting on poll
PollOptionData PollOptionData
PollTallyInfo PollTallyInfo
VoteOptionIds []int64
VoteData []bool
LongestItem int
Ellipsify func(text string, maxLength int) string
}
//////////////////////////////////////////////////////////////////////////////
//
// display article
//
//////////////////////////////////////////////////////////////////////////////
func articleHandler(w http.ResponseWriter, r *http.Request) {
RefreshSession(w, r)
pr("articleHandler")
prVal("r.URL.Query()", r.URL.Query())
prVal("r.URL", r.URL)
prVal("r.URL.Path", r.URL.Path)
reqPostId := parseUrlParam(r, "postId")
postId, err := strconv.ParseInt(reqPostId, 10, 64)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) // TODO: prettify error displaying - use dinosaurs.
return
}
userId := GetSession(w, r)
userData := GetUserData(userId)
// TODO_REFACTOR: unify articles and posts in database.
article, err := fetchArticle(postId, userId)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) // TODO: prettify error displaying - use dinosaurs.
return
}
articleGroups := make([]ArticleGroup, len(newsCategoryInfo.CategoryOrder))
for c, category := range newsCategoryInfo.CategoryOrder {
articleGroups[c].Category = category
articleGroups[c].HeaderColor = newsCategoryInfo.HeaderColors[category]
}
moreArticles := []Article{}
if article.IsPoll {
// Check if user has already voted in this poll, and if so, take them to view the poll results.
reqChangeVote := parseUrlParam(r, "changeVote")
prVal("reqChangeVote", reqChangeVote)
if reqChangeVote == "" { // But don't redirect if this is a request to change their vote.
if DbExists("SELECT * FROM $$PollVote WHERE UserId=$1 AND PollId=$2", userId, postId) {
http.Redirect(w, r, fmt.Sprintf("/viewPollResults/?postId=%d", postId), http.StatusSeeOther)
return
}
}
reqAddOption := parseUrlParam(r, "addOption")
if reqAddOption != "" {
article.ShowNewOption = true
}
// Suggested articles for further reading - on the sidebar.
moreArticles = fetchSuggestedPolls(userId, postId)
} else {
moreArticles = fetchArticlesFromThisNewsSource(article.NewsSourceId, userId, postId, 10)
}
//prVal("len(moreArticles)", len(moreArticles))
//prVal("len(concated articles)", len(append(moreArticles, article)))
upvotes, downvotes := deduceVotingArrows(append(moreArticles, article))
headComment, upcommentvotes, downcommentvotes := ReadCommentsFromDB(article.Id, userId)
//prVal("upvotes", upvotes)
//prVal("downvotes", downvotes)
//prVal("upcommentvotes", upcommentvotes)
//prVal("downcommentvotes", downcommentvotes)
// Render the news articles.
fa := makeFrameArgs2(r, article.Title, "", "news", userId, userData.Username, upvotes, downvotes)
fa.Metadata["og:image"] = article.UrlToImage
fa.Metadata["og:description"] = article.Description
prVal("fa.Metadata", fa.Metadata)
articleArgs := struct {
FrameArgs
Article Article
UpCommentVotes []int64
DownCommentVotes []int64
HeadComment Comment
ArticleGroups []ArticleGroup
MoreArticlesFromThisSource []Article
CommentPrompt string
FocusOnTopComment bool
ShowAdminCommands bool
Admin bool
}{
FrameArgs: fa,
Article: article,
UpCommentVotes: upcommentvotes,
DownCommentVotes: downcommentvotes,
HeadComment: headComment,
ArticleGroups: articleGroups,
MoreArticlesFromThisSource: moreArticles,
CommentPrompt: "Add a comment to start a conversation!",
FocusOnTopComment: true,
Admin: userData.Admin,
}
executeTemplate(w, kArticle, articleArgs)
}
//////////////////////////////////////////////////////////////////////////////
//
// fix urls in article to be absolute
//
//////////////////////////////////////////////////////////////////////////////
func makeUrlsAbsolute(article *Article) {
assert(flags.domain != "")
article.Url = "http://" + flags.domain + article.Url
article.UrlToImage = "http://" + flags.domain + article.UrlToImage
article.UrlToThumbnail = "http://" + flags.domain + article.UrlToThumbnail
article.AuthorIconUrl = "http://" + flags.domain + article.AuthorIconUrl
}