This repository has been archived by the owner on Dec 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
146 lines (127 loc) · 2.86 KB
/
utils.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
/*
utils.go
file that holds various utility functions used in slaj
*/
package main
import (
"fmt"
"log"
"net/http"
"time"
)
// function for changing ugly gay sql time to stuff people can read
func humanTiming(timestamp time.Time) string {
now := time.Now()
if now.Day() == timestamp.Day() && now.Month() == timestamp.Month() && now.Year() == timestamp.Year() {
return timestamp.Format("Today at 3:04 PM")
} else if now.Day()-1 == timestamp.Day() && now.Month() == timestamp.Month() && now.Year() == timestamp.Year() {
return timestamp.Format("Yesterday at 3:04 PM")
} else if now.Day()-2 == timestamp.Day() && now.Month() == timestamp.Month() && now.Year() == timestamp.Year() {
return timestamp.Format("Last Monday at 3:04 PM")
} else {
return timestamp.Format("01/02/2006 3:04 PM")
}
}
// function that checks for an error and logs it. it returns either a true or false value
func checkErr(w http.ResponseWriter, r *http.Request, err error) bool {
// check for an error
if err != nil {
// log the error
log.Printf("[err]: %s%s", r.Host, r.URL.Path)
log.Printf(" %v\n", err)
// return an error message to the client
http.Error(w, fmt.Sprintf("unable to handle request. sorry :(\nerror: %v", err), http.StatusInternalServerError)
return false
}
return true
}
// Find a user by username.
func QueryUser(username string) user {
var users = user{}
err = db.QueryRow(`
SELECT id,
username,
nickname,
avatar,
email,
password,
ip,
level,
role,
online,
last_seen,
color,
yeah_notifications
FROM users WHERE username=?
`, username).
Scan(
&users.ID,
&users.Username,
&users.Nickname,
&users.Avatar,
&users.Email,
&users.Password,
&users.IP,
&users.Level,
&users.Role,
&users.Online,
&users.LastSeen,
&users.Color,
&users.YeahNotifications,
)
return users
}
// Find a profile by user ID.
func QueryProfile(id int) profile {
var profiles = profile{}
err = db.QueryRow(`
SELECT user,
created_at,
nnid,
gender,
region,
comment,
nnid_visibility,
yeah_visibility,
reply_visibility
FROM profiles WHERE user=?
`, id).
Scan(&profiles.User,
&profiles.CreatedAt,
&profiles.NNID,
&profiles.Gender,
&profiles.Region,
&profiles.Comment,
&profiles.NNIDVisibility,
&profiles.YeahVisibility,
&profiles.ReplyVisibility)
return profiles
}
// Find a community by ID.
func QueryCommunity(id string) community {
var communities = community{}
err = db.QueryRow(`
SELECT id,
title,
description,
icon,
banner,
is_featured,
developer_only,
staff_only,
rm
FROM communities WHERE id = ?
`, id).
Scan(
&communities.ID,
&communities.Title,
&communities.Description,
&communities.Icon,
&communities.Banner,
&communities.IsFeatured,
&communities.DeveloperOnly,
&communities.StaffOnly,
&communities.IsRm,
)
return communities
}