-
Notifications
You must be signed in to change notification settings - Fork 0
/
webui.go
698 lines (607 loc) · 21.8 KB
/
webui.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/*
*/
package webui
import (
"backend"
"database/sql"
"encoding/json"
"html/template"
"io/ioutil"
"os"
"net/http"
"reflect"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/laktek/Stack-on-Go/stackongo"
"golang.org/x/net/context"
"google.golang.org/appengine"
"google.golang.org/appengine/log"
)
// Functions for sorting
type byCreationDate []stackongo.Question
type ByDisplayName []userData
func (a byCreationDate) Len() int { return len(a) }
func (a byCreationDate) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byCreationDate) Less(i, j int) bool { return a[i].Creation_date > a[j].Creation_date }
func (a ByDisplayName) Len() int { return len(a) }
func (a ByDisplayName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByDisplayName) Less(i, j int) bool {
return a[i].User_info.Display_name < a[j].User_info.Display_name
}
// Reply to send to main template
type genReply struct {
Wrapper *stackongo.Questions // Information about the query
Caches []cacheInfo // Slice of the 4 caches (Unanswered, Answered, Pending, Updating)
User stackongo.User // Information on the current user
Qns map[int]stackongo.User // Map of users by question ids
UpdateTime int64
Query []string // String array holding query and query type (tag vs user)
}
// Generic reply to send to other templates
type queryReply struct {
User stackongo.User
UpdateTime int64
Page int
LastPage int
Data interface{}
}
// Info on the various caches
type cacheInfo struct {
CacheType string // "unanswered"/"answered"/"pending"/"updating"
Questions []stackongo.Question // list of questions
Info string // blurb about the cache
}
// Data struct with SO information, caches, user information
type webData struct {
Wrapper *stackongo.Questions // Request information
Caches map[string][]stackongo.Question // Caches by question states
Qns map[int]stackongo.User // Map of users by question ids
Users map[int]userData // Map of users by user ids
CacheLock sync.Mutex // For multithreading, will use to avoid updating cache and serving cache at the same time
}
// User information and the user's caches
type userData struct {
User_info stackongo.User // SE user info
Caches map[string][]stackongo.Question // Questions modified by user sorted into cacheTypes
}
// Information on tags
type tagData struct {
Tag string //The actual tag, hyphenated string
Count int //The number of questions with that tag in the db
}
// Simplified user struct
type userInfo struct {
ID int
Name string
Pic string
Link string
}
// Creates an initialised webData struct
func newWebData() webData {
return webData{
Caches: map[string][]stackongo.Question{
"unanswered": []stackongo.Question{},
"answered": []stackongo.Question{},
"pending": []stackongo.Question{},
"updating": []stackongo.Question{},
},
Qns: make(map[int]stackongo.User),
Users: make(map[int]userData),
}
}
const timeout = 6 * time.Hour // Time to wait between querying new SE questions
// Standard guest user
var guest = stackongo.User{
Display_name: "Guest",
}
// Pointer to database connection to communicate with Cloud SQL
var db *sql.DB
var DB_STRING = ""
//Stores the last time the database was read into the cache
//This is then checked against the update time of the database and determine whether the cache should be updated
var lastPull = time.Now().Add(-1 * time.Hour * 24 * 7).Unix()
var recentChangedQns = []string{} // Array of the most recently changed questions
var mostRecentUpdate int64 // Time of most recent update
/* --------- Template functions ------------ */
// Returns timeUnix as a formatted string
func (r genReply) Timestamp(timeUnix int64) string {
est, _ := time.LoadLocation("Australia/Sydney")
timeFormat := "Jan 2 at 15:04 2006"
return time.Unix(timeUnix, 0).In(est).Format(timeFormat)
}
// Returns current page + num
func (r queryReply) PagePlus(num int) int {
return r.Page + num
}
//The app engine will run its own main function and imports this code as a package
//So no main needs to be defined
//All routes go in to init
func init() {
recentChangedQns = []string{}
lastPull = time.Now().Add(-1 * time.Hour * 24 * 7).Unix()
// Initialising stackongo session
backend.NewSession()
// Handlers for pages
http.HandleFunc("/login", authHandler)
http.HandleFunc("/", handler)
http.HandleFunc("/tag", handler)
http.HandleFunc("/user", handler)
http.HandleFunc("/viewTags", handler)
http.HandleFunc("/viewUsers", handler)
http.HandleFunc("/dbUpdated", updateHandler)
http.HandleFunc("/search", handler)
http.HandleFunc("/addQuestion", handler)
http.HandleFunc("/pullNewQn", handler)
}
func checkForDBConnection() bool {
return !(DB_STRING == "")
}
func connectToDB(ctx context.Context) {
version := appengine.VersionID(ctx)
versionID := strings.Split(version, ".")
log.Infof(ctx, "Current serving app version: %s", versionID[0])
if versionID[0] == "live" {
DB_STRING = os.Getenv("LIVE_DB")
} else {
DB_STRING = os.Getenv("TEST_DB")
}
log.Infof(ctx, "connecting to DB with string %s", DB_STRING)
db = backend.SqlInit(DB_STRING)
}
// Handler for authorizing user
// Redirects user to a url for authentication
// Once authenticated, returns to the home page with a code which we use to get the current user
func authHandler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
log.Infof(ctx, "Redirecting to SO login")
auth_url := backend.AuthURL(r.Header["Referer"][0])
header := w.Header()
header.Add("Location", auth_url)
w.WriteHeader(302)
}
// Handler for checking if the database has been updated
// Writes a JSON object to the page
// ie. {"Updated": true, "Questions: ["Title1", "Title2"]}
func updateHandler(w http.ResponseWriter, r *http.Request) {
time, _ := strconv.ParseInt(r.FormValue("time"), 10, 64)
// Writing the page to JSON format
pageText := "{\"Updated\": " + strconv.FormatBool(mostRecentUpdate > time) + ","
pageText += "\"Questions\": ["
for _, question := range recentChangedQns {
pageText += "\"" + question + "\","
}
pageText = strings.TrimSuffix(pageText, ",")
pageText += "]}"
// Write text into response
w.Write([]byte(pageText))
}
// Handler for main information to be read and written from.
// Does following functions in order:
// Refreshes sql db for changes to questions in SE API.
// Updates local cache if there's been changes to the db.
// Gets the current user to send in response.
// If a form has been submitted, the local cache and db gets updated with new values
// Finds the current subpage and redirects to the relevant handler
func handler(w http.ResponseWriter, r *http.Request) {
// Set context for logging
ctx := appengine.NewContext(r)
if (checkForDBConnection() == false) {
connectToDB(ctx)
}
backend.SetTransport(ctx)
if strings.HasPrefix(r.URL.Path, "/pullNewQn") {
newQnHandler(w, r, ctx)
return
}
// Pull any new questions added to StackOverflow
lastPull = updateDB(db, ctx, lastPull)
// Get the current user
user := getUser(w, r, ctx)
// Collect page number
pageNum, _ := strconv.Atoi(r.FormValue("page"))
if pageNum == 0 {
pageNum = 1
}
// Update the new cache on submit if submitting cookie is set
cookie, _ := r.Cookie("submitting")
if cookie != nil && cookie.Value == "true" {
// Update the cache based on the form values sent in the request
updateTime, err := updatingCache_User(ctx, r, user)
if err != nil {
log.Errorf(ctx, "Error updating cache: %v", err.Error())
} else {
mostRecentUpdate = updateTime
}
// Removing the cookie
http.SetCookie(w, &http.Cookie{Name: "submitting", Value: ""})
}
// Send to valid subpages
// else errorHandler
if strings.HasPrefix(r.URL.Path, "/?") || strings.HasPrefix(r.URL.Path, "/home") || r.URL.Path == "/" {
// Get data to send to page
data, updateTime, err := readFromDb(ctx, "")
if err != nil {
log.Errorf(ctx, "Error reading from db: %v", err.Error())
} else {
mostRecentUpdate = updateTime
}
// Parse the html template to serve to the page
page := template.Must(template.ParseFiles("public/template.html"))
pageQuery := []string{
"",
"",
}
// WriteResponse creates a new response with the various caches
if err := page.Execute(w, writeResponse(user, data, pageNum, pageQuery)); err != nil {
log.Errorf(ctx, "%v", err.Error())
}
} else if strings.HasPrefix(r.URL.Path, "/tag") && r.FormValue("tagSearch") != "" {
tagHandler(w, r, ctx, pageNum, user)
} else if strings.HasPrefix(r.URL.Path, "/user") {
userHandler(w, r, ctx, pageNum, user)
} else if strings.HasPrefix(r.URL.Path, "/viewTags") {
viewTagsHandler(w, r, ctx, pageNum, user)
} else if strings.HasPrefix(r.URL.Path, "/viewUsers") {
viewUsersHandler(w, r, ctx, pageNum, user)
} else if strings.HasPrefix(r.URL.Path, "/search") {
searchHandler(w, r, ctx, pageNum, user)
} else if strings.HasPrefix(r.URL.Path, "/addQuestion") {
addQuestionHandler(w, r, ctx, pageNum, user)
} else if strings.HasPrefix(r.URL.Path, "/addNewQuestion") {
addNewQuestionToDatabaseHandler(w, r, ctx)
} else {
errorHandler(w, r, ctx, http.StatusNotFound, "")
}
}
// Handler for adding new question page
func addQuestionHandler(w http.ResponseWriter, r *http.Request, ctx context.Context,
pageNum int, user stackongo.User) {
page := template.Must(template.ParseFiles("public/addQuestion.html"))
if err := page.Execute(w, queryReply{user, mostRecentUpdate, pageNum, 0, nil}); err != nil {
log.Warningf(ctx, "%v", err.Error())
}
}
// Handler for pulling questions from Stack Overflow manually, based on a given ID
// Request is parsed to find the supplied ID
// A check is completed to see if the question is already in the system
// If so, it retrieves that question, and returns it to be viewed, along with a message
// Makes a new backend request to retrieve new questions
// Parses the returned data into a new page, which can be inserted into the template.
func newQnHandler(w http.ResponseWriter, r *http.Request, ctx context.Context) {
id, _ := strconv.Atoi(r.FormValue("id"))
res, err := backend.CheckForExistingQuestion(db, id)
if err != nil {
log.Infof(ctx, "QUERY FAILED, %v", err)
}
if res == 1 {
existingQn := backend.PullQnByID(db, ctx, id)
if err != nil {
log.Warningf(ctx, err.Error())
}
w.Write(existingQn)
} else {
intArray := []int{id}
questions, err := backend.GetQuestions(ctx, intArray)
if err != nil {
log.Warningf(ctx, err.Error())
} else {
questions.Items[0].Body = backend.StripTags(questions.Items[0].Body)
qnJson, err := json.Marshal(questions.Items[0])
if err != nil {
log.Warningf(ctx, err.Error())
}
w.Write(qnJson)
}
}
}
// Handler for adding a new question to the database upon submission
// It is returned as a stringified JSON object in the request body
// The string is unmarshalled into a stackongo.Question type, and added to an array
// to be added into the database using the AddQuestions function in backend/databasing.go
func addNewQuestionToDatabaseHandler(w http.ResponseWriter, r *http.Request, ctx context.Context) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Infof(ctx, "%v", err)
}
var f interface{}
err = json.Unmarshal(body, &f)
if err != nil {
log.Infof(ctx, "%v", err)
}
m := f.(map[string]interface{})
question := m["Question"]
state := m["State"]
if err != nil {
log.Infof(ctx, "%v", err)
}
var qn stackongo.Question
json.Unmarshal([]byte(question.(string)), &qn)
log.Infof(ctx, "%v", qn)
user := getUser(w, r, ctx)
log.Infof(ctx, "%v", user.User_id)
if err := backend.AddSingleQuestion(db, qn, state.(string), user.User_id); err != nil {
log.Warningf(ctx, "Error adding new question to db:\t", err)
}
backend.UpdateTableTimes(db, ctx, "question")
}
// Handler for keywords, tags, users in the search box
// Checks input against fields in the question/user caches and returns any matches
func searchHandler(w http.ResponseWriter, r *http.Request, ctx context.Context, pageNum int, user stackongo.User) {
search := r.FormValue("search")
query := "questions.question_id LIKE '" + search + "'" + // By question id
" OR questions.question_url LIKE '" + search + "'" + // By url
" OR questions.question_title LIKE '%" + search + "%'" + // By part of title
" OR questions.body LIKE '%" + search + "%'" + // By part of body
" OR (questions.user LIKE'" + search + "' AND questions.state!='unanswered')" + // By Owner id
" OR (user.name LIKE '%" + search + "%' AND questions.state!='unanswered')" + // By Owner display name
" OR questions.question_id IN (SELECT question_id FROM question_tag WHERE tag like '" + search + "')" // By tags
tempData, updateTime, err := readFromDb(ctx, query)
if err != nil {
log.Errorf(ctx, "Error reading from db: %v", err.Error())
} else {
mostRecentUpdate = updateTime
}
page := template.Must(template.ParseFiles("public/template.html"))
var pageQuery = []string{
"search",
search,
}
if err := page.Execute(w, writeResponse(user, tempData, pageNum, pageQuery)); err != nil {
log.Errorf(ctx, "%v", err.Error())
}
}
// Handler to find all questions with specific tags
func tagHandler(w http.ResponseWriter, r *http.Request, ctx context.Context, pageNum int, user stackongo.User) {
// Collect query
tag := r.FormValue("tagSearch")
query := "questions.question_id IN (SELECT question_id FROM question_tag WHERE tag like '" + tag + "')" // By tags
tempData, updateTime, err := readFromDb(ctx, query)
if err != nil {
log.Errorf(ctx, "Error reading from db: %v", err.Error())
} else {
mostRecentUpdate = updateTime
}
page := template.Must(template.ParseFiles("public/template.html"))
var tagQuery = []string{
"tag",
tag,
}
if err := page.Execute(w, writeResponse(user, tempData, pageNum, tagQuery)); err != nil {
log.Warningf(ctx, "%v", err.Error())
}
}
// Handler to find all questions answered/being answered by the user in URL
func userHandler(w http.ResponseWriter, r *http.Request, ctx context.Context, pageNum int, user stackongo.User) {
userID_string := r.FormValue("id")
log.Infof(ctx, "current user id=%s", userID_string)
// Create a new webData struct
tempData, updateTime, err := readFromDb(ctx, "state='unanswered' OR questions.user="+userID_string)
if err != nil {
log.Errorf(ctx, "Error reading from db: %v", err.Error())
} else {
mostRecentUpdate = updateTime
}
page := template.Must(template.ParseFiles("public/template.html"))
userID_int, _ := strconv.Atoi(userID_string)
var Query = []string{
"user",
tempData.Users[userID_int].User_info.Display_name,
}
log.Infof(ctx, "Query = %v", Query)
if err := page.Execute(w, writeResponse(user, tempData, pageNum, Query)); err != nil {
log.Warningf(ctx, "%v", err.Error())
}
}
//Display a list of tags that are logged in the database
//User can either click on a tag to view any questions containing that tag
//Format array of tags into another array, to be easier formatted on the page into a table in the template
//An array of tagData arrays of size 4
func viewTagsHandler(w http.ResponseWriter, r *http.Request, ctx context.Context, pageNum int, user stackongo.User) {
query := readTagsFromDb(ctx)
var tagArray [][]tagData
var tempTagArray []tagData
i := 0
for _, t := range query {
tempTagArray = append(tempTagArray, t)
i++
if i == 4 {
tagArray = append(tagArray, tempTagArray)
i = 0
//clear the temp array.
tempTagArray = nil
}
}
tagArray = append(tagArray, tempTagArray)
page := template.Must(template.ParseFiles("public/viewTags.html"))
first := (pageNum - 1) * 5
last := pageNum * 5
lastPage := len(tagArray) / 5
if len(tagArray)%5 != 0 {
lastPage++
}
if last > len(tagArray) {
last = len(tagArray)
}
if err := page.Execute(w, queryReply{user, mostRecentUpdate, pageNum, lastPage, tagArray[first:last]}); err != nil {
log.Warningf(ctx, "%v", err.Error())
}
}
// Handler for viewing all users in the database
// Formats the response into an array of userData maps, for easier formatting onto the page.
// User data is stored as a map, which gives no guarantee as to the order of iteration
// It is first read into an array, and that array sorted lexicographically by the users Display name.
func viewUsersHandler(w http.ResponseWriter, r *http.Request, ctx context.Context, pageNum int, user stackongo.User) {
query := readUsersFromDb(ctx, "")
var querySorted []userData
for id, i := range query {
if id != user.User_id {
querySorted = append(querySorted, i)
}
}
sort.Sort(ByDisplayName(querySorted))
var queryArray [][]userData
var tempQueryArray []userData
for i, u := range querySorted {
tempQueryArray = append(tempQueryArray, u)
if (i != 0 && i%4 == 0) || i+1 == len(querySorted) {
queryArray = append(queryArray, tempQueryArray)
//clear temp array
tempQueryArray = nil
}
}
final := struct {
User userData
Others [][]userData
}{
query[user.User_id],
queryArray,
}
page := template.Must(template.ParseFiles("public/viewUsers.html"))
if err := page.Execute(w, queryReply{user, mostRecentUpdate, pageNum, 0, final}); err != nil {
log.Errorf(ctx, "%v", err.Error())
}
}
// Returns the current user requesting the page
func getUser(w http.ResponseWriter, r *http.Request, ctx context.Context) stackongo.User {
// Collect userId from browser cookie
username, err := r.Cookie("user_name")
if err == nil && username.Value != "" && username.Value != "Guest" {
return readUserFromDb(ctx, username.Value)
}
// If user_id cookie is not set, look for code in url request to collect access token.
// If code is not available, return guest user
code := r.FormValue("code")
if code == "" {
log.Infof(ctx, "Returning guest user")
return guest
}
queries := r.URL.Query()
queries.Del("code")
r.URL.RawQuery = queries.Encode()
// Collect access token using the recieved code
access_tokens, err := backend.ObtainAccessToken(code, r.URL.String())
if err != nil {
log.Warningf(ctx, "Access token not obtained: %v", err.Error())
return guest
}
// Get the authenticated user with the collected access token
user, err := backend.AuthenticatedUser(map[string]string{}, access_tokens["access_token"])
if err != nil {
log.Warningf(ctx, err.Error())
return guest
}
// Add user to db if not already in
addUserToDB(ctx, user)
//zhu li do the thing
//updateLoginTime(ctx, user)
return user
}
// Update the database if the lastPullTime is more than 6 hours before the current time
func updateDB(db *sql.DB, ctx context.Context, lastPullTime int64) int64 {
// If the last pull was more than 6 hours ago
if lastPull < time.Now().Add(-1*timeout).Unix() {
log.Infof(ctx, "Updating database")
// Remove deleted questions from the database
log.Infof(ctx, "Removing deleted questions from db")
if err := backend.RemoveDeletedQuestions(db, ctx); err != nil {
log.Warningf(ctx, "Error removing deleted questions: %v", err.Error())
return lastPullTime
}
// Setting time frame to get new questions.
toDate := time.Now()
fromDate := time.Unix(lastPull, 0)
// Collect new questions from SO
questions, err := backend.GetNewQns(fromDate, toDate)
if err != nil {
log.Warningf(ctx, "Error getting new questions: %v", err.Error())
return lastPullTime
}
// Add new questions to database
log.Infof(ctx, "Adding new questions to db")
if err := backend.AddQuestions(db, ctx, questions); err != nil {
log.Warningf(ctx, "Error adding new questions: %v", err.Error())
return lastPullTime
}
lastPullTime = time.Now().Unix()
log.Infof(ctx, "New questions added")
}
return lastPullTime
}
// Write a genReply struct with the inputted Question slices
// This can call readFromDb() now as a method, most of this is redundant.
func writeResponse(user stackongo.User, writeData webData, pageNum int, query []string) genReply {
return genReply{
Wrapper: writeData.Wrapper, // The global wrapper
Caches: []cacheInfo{ // Slices caches and their relevant info
cacheInfo{
CacheType: "unanswered",
Questions: writeData.Caches["unanswered"],
Info: "These are questions that have not yet been answered by the Places API team",
},
cacheInfo{
CacheType: "answered",
Questions: writeData.Caches["answered"],
Info: "These are questions that have been answered by the Places API team",
},
cacheInfo{
CacheType: "pending",
Questions: writeData.Caches["pending"],
Info: "These are questions that are being answered by the Places API team",
},
cacheInfo{
CacheType: "updating",
Questions: writeData.Caches["updating"],
Info: "These are questions that will be answered in the next release",
},
},
User: user, // Current user information
Qns: writeData.Qns, // Map users by questions answered
UpdateTime: mostRecentUpdate, // Time of last update
Query: query, // Current query value
}
}
// Handler for errors
func errorHandler(w http.ResponseWriter, r *http.Request, ctx context.Context, status int, err string) {
w.WriteHeader(status)
switch status {
case http.StatusNotFound:
page := template.Must(template.ParseFiles("public/404.html"))
if err := page.Execute(w, nil); err != nil {
errorHandler(w, r, ctx, http.StatusInternalServerError, err.Error())
return
}
case http.StatusInternalServerError:
w.Write([]byte("Internal error: " + err))
}
}
// Returns true if toFind is an element of slice
func contains(slice []string, toFind string) bool {
for _, tag := range slice {
if reflect.DeepEqual(tag, toFind) {
return true
}
}
return false
}
// Initializes userData struct
func newUser(u stackongo.User) userData {
return userData{
User_info: u,
Caches: map[string][]stackongo.Question{
"answered": []stackongo.Question{},
"pending": []stackongo.Question{},
"updating": []stackongo.Question{},
},
}
}
// Returns the smaller value
func Min(x int, y int) int {
if x < y {
return x
} else {
return y
}
}