forked from jtremback/Social-RTC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
319 lines (268 loc) · 12.2 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Social</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/styles.css">
<link href="http://cdnjs.cloudflare.com/ajax/libs/x-editable/1.5.0/bootstrap3-editable/css/bootstrap-editable.css" rel="stylesheet"/>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/peerjs/0.3.10/peer.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/x-editable/1.5.0/bootstrap3-editable/js/bootstrap-editable.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/cryptico/0.0.1343522940/cryptico.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/localforage/0.9.2/localforage.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.3/moment.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/handlebars.js/2.0.0/handlebars.min.js"></script>
<script src="js/routie.min.js"></script>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<a href="index.html" class="navbar-brand">Social</a>
<button class="navbar-toggle" type="button" data-toggle="collapse" data-target="#navbar-main">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<form class="navbar-form navbar-right search" role="search">
<div class="form-group">
<input type="text" id="inputSearch" class="form-control" placeholder="User identifier">
</div>
<button type="submit" class="btn btn-link">Go</button>
</form>
</div>
</div>
<div class="content container">
<div class="login-page row">
<div class="col-lg-12">
<h3>Login</h3>
</div>
<div class="col-lg-12">
<form class="form-horizontal login-form">
<fieldset>
<div class="form-group">
<label for="inputEmail" class="col-lg-2 control-label">Email address</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="inputEmail" placeholder="Email address">
</div>
<label for="inputPassphrase" class="col-lg-2 control-label">Passphrase</label>
<div class="col-lg-10">
<input type="password" class="form-control" id="inputPassphrase" placeholder="Passphrase">
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<span class="help-block"><strong>Hint:</strong> You don't have to register to login - enter *any* email and passphrase - just be sure to use the same credentials next time to get the same public identifer!</span>
<button type="submit" class="btn btn-success">LOGIN</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
<!-- Main content area, where stuff happens -->
<div class="feed-page row">
</div>
</div>
<script id="profile" type="text/x-handlebars-template">
<div class="col-lg-4">
<div class="row">
<div class="col-lg-4">
<img id="avatar" src="{{#if avatar}}{{avatar}}{{else}}http://placehold.it/250x250&text=Avatar+Image{{/if}}"/>
</div>
<div class="col-lg-8">
<h5 id="username">{{#if username}}{{username}}{{else}}New User{{/if}}</h5>
<span id="bio" class="text-muted">{{#if bio}}{{bio}}{{else}}Add a short bio{{/if}}</span>
<span class="text-primary">{{id}}</span>
</div>
</div>
{{#if self }}
<div class="row vert-offset-top-2">
<div class="col-lg-12">
<form class="post-form" role="form">
<div class="form-group">
<textarea class="form-control" rows="3" id="textAreaPost" placeholder="Say something..."></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary pull-right">POST</button>
</div>
</form>
</div>
</div>
{{/if}}
</div>
<div class="col-lg-8 feed">
<h5>Posts</h5>
<hr>
{{#posts}}
<blockquote>
<p>{{content}}</p>
<small><span class="time" data-time="{{time}}"></span></small>
</blockquote>
{{/posts}}
</div>
</script>
<script>
var peer;
var user = {};
var profile = {};
function refreshTimestamps() {
$('.time').each(function(index) {
var time = moment.unix($(this).data('time')).fromNow();
$(this).text(time);
});
}
function clearLocal() {
localforage.clear(function() {
console.log('Local storage is now empty.');
});
}
function viewProfile(id) {
// Looking at own profile
if (id == user.id) {
localforage.getItem(id, function(data) {
// Load profile template
var source = $("#profile").html();
var template = Handlebars.compile(source);
// Notify that it's a self profile and load into template
data.self = true;
data.posts.reverse();
$('.feed-page').html(template(data));
$('#username').editable({
mode: 'inline',
unsavedclass: null,
showbuttons: false,
highlight: '#dff0d8',
success: function(response, username) {
data.username = username;
localforage.setItem(user.id, data, function() {
console.log('Username saved as: ' + username);
});
}
});
$('#bio').editable({
mode: 'inline',
unsavedclass: null,
showbuttons: false,
highlight: '#dff0d8',
success: function(response, bio) {
data.bio = bio;
localforage.setItem(user.id, data, function() {
console.log('Bio saved as: ' + bio);
});
}
});
$('#avatar').editable({
mode: 'popup',
unsavedclass: null,
showbuttons: false,
highlight: '#dff0d8',
success: function(response, avatar) {
data.avatar = avatar;
localforage.setItem(user.id, data, function() {
$('#avatar').attr('src', avatar);
console.log('Avatar path saved as: ' + avatar);
});
}
});
// Update the time stamps using moment.js
refreshTimestamps();
});
} else {
if (peer) {
var conn = peer.connect(id);
conn.on('open', function() {
conn.send(user.pubkey);
});
// Receive payload back
conn.on('data', function(payload) {
decrypted = cryptico.decrypt(payload.cipher, user.privkey);
if (decrypted.signature == 'verified' && cryptico.publicKeyID(decrypted.publicKeyString) == id) {
console.log('Message verified: ' + decrypted.plaintext);
// Load template
var source = $("#profile").html();
var template = Handlebars.compile(source);
var data = JSON.parse(decrypted.plaintext);
data.posts.reverse();
$('.feed-page').html(template(data));
// Update the time stamps using moment.js
refreshTimestamps();
} else {
console.log('There was an error');
}
conn.close();
});
conn.on('error', function() {
alert('Error connecting to peer');
});
}
}
}
function addPost(text,time) {
// Grab the user's JSON profile and add the latest item
localforage.getItem(user.id, function(data) {
data['posts'].push({"time":time,"content":text});
localforage.setItem(user.id, data, function() {
viewProfile(user.id);
});
});
}
function doLogin(email,passphrase) {
// Generate RSA keypair
user.privkey = cryptico.generateRSAKey(email + passphrase, 1024);
// Extract public key & ID
user.pubkey = cryptico.publicKeyString(user.privkey);
// TBD: Potentially find a solution better than MD5 for identifiers, this was quick and dirty
user.id = cryptico.publicKeyID(user.pubkey);
// Create a new PeerJS connection
peer = new Peer(user.id, {
host: 'dev.estate',
port: 9000,
debug: 3,
config: {
'iceServers': [{
url: 'stun:stun.l.google.com:19302'
} // Pass in optional STUN and TURN server for maximum network compatibility
]
}
});
// Log that connection to PeerServer was successful, show ID
peer.on('open', function(id) {
localforage.getItem(user.id, function(data) {
profile = data;
if ($.isEmptyObject(profile)) {
console.log('New local profile created for: ' + user.id);
profile = {};
profile.id = user.id;
profile.pubkey = user.pubkey;
profile.posts = [];
localforage.setItem(user.id, profile, function() {
$('.search').fadeIn();
$('.login-page').slideUp('fast');
$('.feed-page').show();
viewProfile(user.id);
});
}
else {
$('.search').fadeIn();
$('.login-page').slideUp('fast');
$('.feed-page').show();
viewProfile(user.id);
}
});
});
// Incoming connection
peer.on('connection', function(conn) {
// Receive messages
conn.on('data', function(pubkey) {
console.log('Public Key Received: ' + pubkey);
// Send users's DB across
localforage.getItem(user.id, function(data) {
conn.send(cryptico.encrypt(JSON.stringify(data), pubkey, user.privkey));
});
});
});
}
// Post form is sub