-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhistory.go
245 lines (191 loc) · 6.43 KB
/
history.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"net/http"
)
var (
historyCategoryInfo = CategoryInfo {
CategoryOrder : []string{
kCommentedPosts,
kSubmittedPosts,
kVotedPosts,
kSubmittedPolls,
kVotedPolls,
},
HeaderColors : map[string]string{
kCommentedPosts: "#f90",
kSubmittedPosts : "#aaf",
kVotedPosts : "#ccc",
kSubmittedPolls : "#da8",
kVotedPolls : "#fa9",
},
}
)
///////////////////////////////////////////////////////////////////////////////
//
// History handler - of user posts, up/down votes,
// TODO: comments
//
///////////////////////////////////////////////////////////////////////////////
func historyHandler(w http.ResponseWriter, r *http.Request) {
RefreshSession(w, r)
pr("historyHandler")
//prVal("r.URL.Query()", r.URL.Query())
reqAlert := parseUrlParam(r, "alert")
// User is current (logged in) user by default.
userId, username := GetSessionInfo(w, r)
isMe := true
viewUserId := userId
viewUsername := "" // "" if viewing ourselves, and if we are viewing someone else it is their username.
// If we request a different user, query the userId we are viewing
reqUsername := parseUrlParam(r, "username")
prVal("reqUsername", reqUsername)
newsSource := ""
if reqUsername != "" && reqUsername != username {
viewUsername = reqUsername
prVal("viewUsername", viewUsername)
// Check if it's a news source.
_, found := newsSourceList[viewUsername]
if found {
isMe = false
newsSource = viewUsername
} else { // Else, check if it's a username. (TODO: disallow usernames to match news sources during registration.)
pr("Valid requested username")
viewUserId = UsernameToUserId(viewUsername)
prVal("viewUserId", viewUserId)
if viewUserId == -1 {
serveErrorMsg(w, "User not found")
return
}
isMe = false // We're viewing another user's history.
}
}
prVal("isMe", isMe)
/*
var userId int64
var username string
var isMe bool
{
// User is current (logged in) user by default.
userId, username := GetSessionInfo(w, r)
isMe = true
// If we request a different user, update the user info to match.
reqUserId := parseUrlParam(r, "userId")
if reqUserId != "" {
newUserId, err := str_to_int64(reqUserId)
check(err)
if newUserId != userId {
username = GetUsername(userId)
if username != "" { // If user is found...
isMe = false
userId = userId
}
}
}
}
*/
// Do something appropriate if user is trying to view their own history and is not logged in
if isMe && userId < 0 {
http.Redirect(w, r, "/login", http.StatusSeeOther)
}
articleGroups := []ArticleGroup{}
allArticles := []Article{}
if newsSource != "" {
allArticles = fetchArticlesFromThisNewsSource(newsSource, userId, -1, 24)
prVal("len(allArticles)", len(allArticles))
//for a := range allArticles {
// allArticles[a].Bucket = newsSource
//}
// We need to determine the news category, otherwise only a single row of news gets displayed due to
// formatArticleGroups hackiness.
// All articles from each news source just get labeled with a single cat, so we can
// tell what the category is by just examining a single article.
newsCat := ""
if len(allArticles) > 0 {
newsCat = allArticles[0].Category
}
prVal("newsCat", newsCat)
articleGroups = formatArticleGroups(allArticles, newsCategoryInfo, newsCat, kAlternateHeadlines)
} else {
/* dupIds := map[int64]bool{}
removeDupIds := func(articles []Article) (filteredArticles []Article) {
numAddedArticles := 0
for _, article := range articles {
// If duplicate id exists, purge the article.
//_, found := dupIds[article.Id]
//if !found {
filteredArticles = append(filteredArticles, article)
dupIds[article.Id] = true
numAddedArticles++
// if numAddedArticles >= 6 {
// break
// }
//}
}
return
}
*/
/*
pr("Get polls posted by user") // << Removed because it's a subset of "articles posted by user".
{
articles := fetchPollsPostedByUser(viewUserId, userId, kNumCols * kRowsPerCategory)
//articles = removeDupIds(articles)
allArticles = append(allArticles, articles...)
for a := range articles {
articles[a].Bucket = kSubmittedPolls
}
articleGroups = append(articleGroups,
formatArticleGroups(articles, historyCategoryInfo, kSubmittedPolls, kNoHeadlines)...)
}
*/
{
pr("Get articles posted by user")
articles := fetchArticlesPostedByUser(viewUserId, userId, kNumCols * kRowsPerCategory)
//articles = removeDupIds(articles)
allArticles = append(allArticles, articles...)
for a := range articles {
articles[a].Bucket = kSubmittedPosts
}
articleGroups = append(articleGroups,
formatArticleGroups(articles, historyCategoryInfo, kSubmittedPosts, kNoHeadlines)...)
}
if isMe {
pr("Get polls voted on by user")
articles := fetchPollsVotedOnByUser(viewUserId, userId, kNumCols * kRowsPerCategory)
//articles = removeDupIds(articles)
allArticles = append(allArticles, articles...)
for a := range articles {
articles[a].Bucket = kVotedPolls
}
articleGroups = append(articleGroups,
formatArticleGroups(articles, historyCategoryInfo, kVotedPolls, kNoHeadlines)...)
}
{
pr("Get articles commented on by user")
articles := fetchArticlesCommentedOnByUser(viewUserId, userId, kNumCols * kRowsPerCategory)
//articles = removeDupIds(articles)
allArticles = append(allArticles, articles...)
for a := range articles {
articles[a].Bucket = kCommentedPosts
}
articleGroups = append(articleGroups,
formatArticleGroups(articles, historyCategoryInfo, kCommentedPosts, kNoHeadlines)...)
}
if isMe {
pr("Get articles voted on by user, and set their bucket accordingly.")
articles := fetchArticlesUpDownVotedOnByUser(viewUserId, userId, kNumCols * kRowsPerCategory)
//articles = removeDupIds(articles)
allArticles = append(allArticles, articles...)
for a := range articles {
articles[a].Bucket = kVotedPosts
}
articleGroups = append(articleGroups,
formatArticleGroups(articles, historyCategoryInfo, kVotedPosts, kAlternateHeadlines)...)
}
for g, _ := range articleGroups {
articleGroups[g].More = "" // Clear any "More ___..." links, since they don't lead anywhere yet.
}
}
upvotes, downvotes := deduceVotingArrows(allArticles)
// Render the history just like we render the news.
renderNews(w, r, "History", username, userId, viewUsername, articleGroups, "history", kNews, upvotes, downvotes, "", reqAlert)
}