-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
index.html
330 lines (316 loc) · 9.94 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
320
321
322
323
324
325
326
327
328
329
330
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="../style.css">
</head>
<body>
<div id="sign" class="hue page">
<style>
input, button {
margin: 1em 0;
padding: 1em;
}
.or {
display: inline-block;
width: 15%;
}
</style>
<form id="inup" class="sign pad center">
<div class="loud">Welcome,</div>
<div class="mid row col">
<input name="alias" class="huet jot sap" placeholder="alias">
Enter your public username.
</div>
<div class="mid row col">
<input name="pass" class="huet jot sap" type="password" placeholder="passphrase">
And a long private passphrase.
</div>
<div class="mid row col go">
<button class="huet sap act symbol">#</button>
<div class="or">or</div>
<button class="huet sap act symbol">#</button>
</div>
<a href="info">more info</a>
</form>
</div id="sign">
<div id="people" class="hue2 page">
<ul name="users" class="pad">
<h3>Here is a list of people:</h3>
<li name="#">
<!-- <h3 name="."></h3> -->
<ul>
<a href="person" name="#" class="whitet act">
<li>
<span name="who">
<b name="name"></b>
</span>
<i>~</i><i name="alias" class="sort"></i>
<input name="pub">
</li>
</a>
</ul>
</li>
</ul>
</div id="people">
<div id="converse" class="page">
<ul>
</ul>
<div class="model">
<li class="msg">
<span name="fro">Alias</span>: <span name="what"></span>
</li>
</div>
<form>
<input placeholder="to" name="to">
<textarea name="what"></textarea>
<input type="submit" value="submit">
<div class="status"></div>
</form>
</div id="converse">
<div id="person" class="hue3 page">
<style>
#person .back {
position: absolute;
top: 0.3em;
left: 0.5em;
width: 2em;
height: 2em;
z-index: 99999;
}
</style>
<a href="people" class="whitet act back">←</a>
<div class="pad">
<div name="who">
<!-- img src="" -->
<h2 name="name" contenteditable="true">Name</h2>
<i name="born"></i>
<span name="bio" contenteditable="true">bio</span>
<form id="say" class="center none">
<input class="jot" placeholder="Say something!" style="width: 80%;">
<input type="submit" id="speak" class="huet2 sap act symbol" style="min-width: 5em; width: 10%;" value="speak">
</form>
<div>
<ul name="said">
<li name="#" class="sit sort shade rim"><div name="what" class="rim"></div></li>
</ul>
</div>
</div>
<input name="pub">
</div>
</div id="person">
<div id="info" class="page">
<p>A mysterious new example app has appeared! It is not finished/ready yet.</p>
</div id="info">
<div id="tell" class="center">
<style>
#tell {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 0;
overflow: visible;
}
#tell p {
top: -2em;
width: 50%;
border: solid #222 0.2em;
border-top: none;
padding: 1%;
opacity: 0;
visibility: hidden;
transition: visibility 2s, all 1s ease-in;
}
#tell .notify {
top: 0em;
opacity: 0.8;
visibility: visible;
transition: visibility 0s, all 0.25s ease-out;
}
</style>
<p class="mid black">Hello world!</p>
</div>
<!-- script src="/Users/mark/Dropbox/Public/gun/db/examples/jquery.js"></script>
<script src="/Users/mark/Dropbox/Public/gun/db/gun.js"></script>
<script src="/Users/mark/Dropbox/Public/gun/db/lib/cryptomodules.js"></script>
<script src="/Users/mark/Dropbox/Public/gun/db/sea.js"></script>
<script src="/Users/mark/Dropbox/Public/gun/db/as.js"></script -->
<script src="/jquery.js"></script>
<script src="/gun.js"></script>
<script src="/gun/sea.js"></script>
<script src="/gun/as.js"></script>
<script src="/gun/nts.js"></script>
<script>
// Check out the interactive tutorial
// for how to build a simplified version
// of this example: https://scrimba.com/c/c2gBgt4
var gun = Gun(location.origin+'/gun');
var app = gun.get('example/contacts/7');
var user = gun.user();
var c = window.c = {};
(function(){
c.hash = location.hash.slice(1);
gun.on('auth', function(at){
if('sign' === c.hash){ c.hash = '' }
as.route(c.hash || 'people');
});
gun.on('secure', function(at){
/* enforce some rules about shared app level data */
if(!at.put || !at.put.users){ return }
var no;
Gun.node.is(at.put.users, function(val, key){
Gun.SEA.verify(val, false, function(val){
//Gun.SEA.read(val, false, function(val){
if('~@'+key === Gun.val.link.is(val)){ return }
no = true;
})
if(no){ return no }
});
if(no){ return }
this.to.next(at);
});
$(document).on('keyup', "form.#put:first", as.wait(function(){
/*
var form = $(this).closest('form'), data = {alias: $(this).val()};
if(!data.alias || data.alias.length < 5){
return;
}
var exist = gun.path('alias').path(data.alias);
exist.get(function(at){
form.find('.or').addClass('none');
form.find('button:first').toggleClass('none', at.put? false : true);
form.find('button:last').toggleClass('none', at.put? true : false);
});
*/
})).on('click','form.sign button:last', function(){
var but = $(this), form = $(this).closest('form'), data = {alias: form.find('input:first').val(), pass: form.find('input:last').val()};
data.born = Gun.time.is();
if(!data.alias || data.alias.length < 5){
c.tell("Alias needs to be longer than 5 characters.");
return;
}
if(!data.pass || data.pass.length < 9){
c.tell("Passphrase needs to be longer than 9 characters.");
return;
}
but.addClass('pulse');
data.alias = data.alias.toLowerCase();
user.create(data.alias, data.pass, function(ack){
if(!ack.wait){ but.removeClass('pulse') }
if(ack.err){ c.tell(ack.err); return }
if(ack.pub){
gun.get('users').get(data.alias).put(gun.get('~@'+data.alias));
}
session(data);
user.auth(data.alias, data.pass);
});
}).on('click','form.sign button:first', become);
function become(){
var form = $('#sign').find('form'), data = {alias: form.find('input:first').val(), pass: form.find('input:last').val()};
data.alias = data.alias || sessionStorage.alias;
data.pass = data.pass || sessionStorage.tmp;
if(!data.alias || data.alias.length < 5){
c.tell("Alias needs to be longer than 5 characters.");
return;
}
if(!data.pass || data.pass.length < 9){
c.tell("Passphrase needs to be longer than 9 characters.");
return;
}
var but = form.find('button:first');
but.addClass('pulse');
data.alias = data.alias.toLowerCase();
user.auth(data.alias, data.pass, function(ack){
if(!ack.wait){ but.removeClass('pulse') }
if(ack.err){ c.tell(ack.err); return }
session(data);
});
}
if(!location.hash){
as.route('sign');
}
if(!window.sessionStorage){ window.sessionStorage = {clear:function(){}} }
if(sessionStorage.tmp){
become();
}
function session(data){
if(!sessionStorage){ return }
sessionStorage.alias = data.alias;
sessionStorage.tmp = data.pass;
}
c.tell = function(what, n){
var e = $('#tell').find('p');
e.addClass('notify').text(what);
clearTimeout(c.tell.to);
c.tell.to = setTimeout(function(){e.removeClass('notify')}, n || 2500);
}
}());
(function(){
$(document).on('submit', '#say', function(e){
e.preventDefault();
user.get('who').get('said').set({
what: $(this).find('.jot').val()
});
$(this).find('.jot').val('');
});
}());
as.route.page('sign', function(){
$(document.forms.inup.alias).focus();
});
as.route.page('people', function(){
if(!user.is){ return as.route('sign') }
as('#people', gun, function(data, key, el){
if('pub' !== key){ return }
el.closest('a').attr('href', '#person/' + data);
});
});
as.route.page('person', function(){
if(!user.is){ return as.route('sign') }
var pub = location.hash.split('/').slice(-1)[0];
(pub === user._.pub? $('#say').show() : $('#say').hide());
as('#person', window.PUB = gun.get('~'+pub));
});
(function(converse){
as.route.page('converse', function(){
if(!c.me){ return as.route('sign') }
converse.lock = false;
var alias = location.hash.split('/').slice(-1);
$('#converse form').find('input:first').val(alias);
function inbox(msg, id){
if(!sign.verify(msg.pub, msg.what, msg.sig)){ return }
msg.fro = (msg.to === c.me.alias? alias : c.me.alias);
as.route.render(id, '.msg', $('#converse ul'), msg);
}
sign.is(alias, function(acc){
sign.is(c.me.alias).path('msgs').path(acc.pub).map(inbox);
}).path('msgs').path(c.me.pub).map(inbox);
});
$(document).on('submit', '#converse form', function(e){
e.preventDefault();
if(converse.lock){ return console.log('message sending already') }
converse.lock = true; // prevent duplicate sends.
var form = $(this), msg = {};
msg.to = form.find('input:first').val();
msg.what = form.find('textarea').val();
sign.is(msg.to, function(to){
if(!priv){
form.find('.status').text("Failed to send. Please # again.");
converse.lock = false;
return;
}
msg.sig = sign.sig(msg.what, priv);
msg.pub = c.me.pub;
msg.when = Gun.time.is();
console.log("sending msg", msg);
sign.is(c.me.alias).path('msgs').path(to.pub).set(gun.put(msg));
converse.lock = false;
}, function(){
form.attr('disabled', false).find('.status').text(msg.to + " cannot be found! Alias is case sensitive.");
converse.lock = false;
});
});
}({}));
//}());</script>
</body>
</html>