-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
224 lines (187 loc) · 6.21 KB
/
script.js
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
/* global XMLHttpRequest, moment */
function getUrl (url, cb) {
var req = new XMLHttpRequest()
req.addEventListener('load', cb)
req.open('GET', url)
req.send()
}
// http://stackoverflow.com/a/12034334
var escapes = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'/': '/'
}
// HTML encode a string.
// Aweful name to make some bad lines a tad shorter.
function e (string) {
return String(string).replace(/[&<>"'\/]/g, function (s) {
return escapes[s]
})
}
// Aweful name to make some bad lines a tad shorter.
var u = encodeURI
function parseGithub (events, cb) {
if (events.length === 0) {
return cb([])
}
var activities = []
var siteName = 'Github'
var siteIcon = '<i class="fa fa-4x fa-github-alt"></i>'
var usernameLink = '<a href="' + e(u(events[0].actor.url)) + '">' + e(events[0].actor.login) + '</a>'
for (var i = 0; i < events.length; i++) {
var event = events[i]
var repoLink = '<a href="' + e(u(event.repo.url)) + '">' + e(event.repo.name) + '</a>'
var branchLink = '<a href="' + e(u(event.repo.url + '/tree/' + event.payload.ref)) + '">' + e(event.payload.ref) + '</a>'
var content = usernameLink
if (event.type === 'DeleteEvent') {
content += ' deleted the ' + e(event.payload.ref_type) + ' <code>' + e(event.payload.ref) + '</code> at ' + repoLink
} else if (event.type === 'CreateEvent') {
if (event.payload.ref_type === 'repository') {
content += ' created the repository ' + repoLink
} else {
content += ' created the ' + e(event.payload.ref_type) + ' <code>' + branchLink + '</code> at ' + repoLink
}
} else if (event.type === 'IssueCommentEvent') {
var issueLink = '<a href="' + e(u(event.payload.comment.html_url)) + '">' + e(event.payload.issue.title) + '</a> at ' + repoLink
content = usernameLink + ' commented on the issue ' + issueLink + ' with <i>' + e(event.payload.comment.body) + '</i>'
} else if (event.type === 'PushEvent') {
content = usernameLink + ' pushed to ' + branchLink + ' at ' + repoLink
} else {
console.log('Unknown Github event.')
console.log(event)
content = usernameLink + ' did an unknown thingy on Github.'
}
var activity = {
siteName: siteName,
siteIcon: siteIcon,
date: moment(event.created_at),
content: content
}
activities.push(activity)
}
cb(activities)
}
function getGithub (username, cb) {
var url = 'https://api.github.com/users/' + u(username) + '/events'
getUrl(url, function () {
parseGithub(JSON.parse(this.responseText), cb)
})
}
function parseReddit (events, cb) {
if (events.length === 0) {
return cb([])
}
var activities = []
var siteName = 'reddit'
var siteIcon = '<i class="fa fa-4x fa-reddit-alien"></i>'
var username = events.data.children[0].data.author
var usernameLink = '<a href="https://www.reddit.com/user/' + username + '">' + username + '</a>'
for (var i = 0; i < events.data.children.length; i++) {
var kind = events.data.children[i].kind
var event = events.data.children[i].data
var subredditLink = '<a href=https://www.reddit.com/r/"' + event.subreddit + '/">/r/' + event.subreddit + '</a>'
var content, postTitleLink
if (kind === 't3') {
// Post
postTitleLink = '<h3><a href="' + event.url + '">' + event.title + '</a></h3>'
content = usernameLink + ' posted ' + postTitleLink + ' in ' + subredditLink
} else if (kind === 't1') {
// Comment
postTitleLink = '<h4><a href="' + event.link_url + '">' + event.link_title + '</a></h4>'
var comment = event.body
content = usernameLink + ' commented on ' + postTitleLink + ' with <i>' + comment + '</i> in ' + subredditLink
} else {
console.log('Unknown reddit event.')
console.log(event)
content = usernameLink + ' did an unknown thingy on reddit.'
}
var activity = {
siteName: siteName,
siteIcon: siteIcon,
date: moment.unix(event.created_utc),
content: content
}
activities.push(activity)
}
cb(activities)
}
function getReddit (username, cb) {
var url = 'https://api.reddit.com/user/' + username + '/overview'
getUrl(url, function () {
parseReddit(JSON.parse(this.responseText), cb)
})
}
function sort (activities) {
activities.sort(function (a, b) {
return a.date < b.date
})
}
function parseActivity (activity) {
return '' +
'<div class="col-md-12">' +
' <div class="panel panel-default">' +
' <div class="panel-body">' +
' <div class="col-md-1">' +
' <h2>' + activity.siteName + '</h2>' +
' <span class="hidden-xs">' + activity.siteIcon + '</span>' +
' </div>' +
' <div class="col-md-11">' +
' <p>' + activity.content + ' <span class="text-muted">' + activity.date.fromNow() + '</span></p>' +
' </div>' +
' </div>' +
' </div>' +
'</div>'
}
function display (activities) {
sort(activities)
var html = ''
for (var i = 0; i < activities.length; i++) {
html += parseActivity(activities[i])
}
document.getElementById('content').innerHTML = html
}
var allActivities
function addActivities (activities) {
for (var i = 0; i < activities.length; i++) {
allActivities.push(activities[i])
}
display(allActivities)
}
var services = {
'github': getGithub,
'reddit': getReddit
}
function printError () {
console.log('Could not find requested user.')
}
function getKeybase (username) {
var url = 'https://keybase.io/_/api/1.0/user/lookup.json?username=' + username
getUrl(url, function () {
var data = JSON.parse(this.responseText)
if (data.status.code !== 0) {
printError()
return
}
allActivities = []
var profiles = data.them.proofs_summary.all
for (var i = 0; i < profiles.length; i++) {
var profile = profiles[i]
var username = profile.nametag
var service = profile.proof_type
var getData = services[service] || function () {}
getData(username, addActivities)
}
})
}
function search (e) {
e.preventDefault()
var username = this.username.value
console.log(username)
if (username) {
getKeybase(username)
}
}
document.getElementById('form').addEventListener('submit', search)