Skip to content
This repository has been archived by the owner on Oct 2, 2023. It is now read-only.

Commit

Permalink
use invitation shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
b1conrad committed Mar 18, 2021
1 parent 106eade commit 5f5d300
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
14 changes: 11 additions & 3 deletions krl/io.picolabs.aca.connections.krl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ruleset io.picolabs.aca.connections {
global {
__testing = { "queries":
[ { "name": "__testing" }
, { "name": "invitation", "args": [ "label" ] }
, { "name": "invitation" }
] , "events":
[ //{ "domain": "d1", "type": "t1" }
//, { "domain": "d2", "type": "t2", "attrs": [ "a1", "a2" ] }
Expand Down Expand Up @@ -88,8 +88,8 @@ ruleset io.picolabs.aca.connections {
invitation = function(){
ent:invitation
}
html = function(c_i){
invite:html(c_i)
html = function(c_i,orig){
invite:html(c_i,orig)
}
}
//
Expand Down Expand Up @@ -250,6 +250,14 @@ ruleset io.picolabs.aca.connections {
if ent:invitation.isnull() then noop()
fired {
ent:invitation := make_invitation()
raise s event "u" attributes {"url": ent:invitation, "tag": aca:label()}
}
}
rule recordShortcut {
select when shortcut registered
if not ent:invitation.match(re#&orig=#) then noop()
fired {
ent:invitation := ent:invitation + "&orig=" + event:attr("shortcut")
}
}
}
15 changes: 9 additions & 6 deletions krl/io.picolabs.aca.connections.ui.krl
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@ ruleset io.picolabs.aca.connections.ui {
//, { "domain": "d2", "type": "t2", "attrs": [ "a1", "a2" ] }
]
}
invite = function(map){
invite = function(map,orig){
<<<pre class="no-print">
#{orig || ""}
<script type="text/javascript">document.write(JSON.stringify(#{map},null,2))</script>
</pre>
>>
}
scripts = <<<script src="/js/jquery-3.1.0.min.js"></script>
scripts = function(orig){
<<<script src="/js/jquery-3.1.0.min.js"></script>
<!-- thanks to Jerome Etienne http://jeromeetienne.github.io/jquery-qrcode/ -->
<script type="text/javascript" src="/js/jquery.qrcode.js"></script>
<script type="text/javascript" src="/js/qrcode.js"></script>
<script type="text/javascript">
$(function(){
$("div").qrcode({ text: location.href, foreground: "#000000" });
$("div").qrcode({ text: "#{orig || ""}" || location.href, foreground: "#000000" });
});
</script>
<style type="text/css">
Expand All @@ -40,6 +42,7 @@ $(function(){
}
</style>
>>
}
explain = function(owner){
<<<p>You are looking at an invitation from #{owner}.
<span class="no-print">(wait, <a href="#confusion"><em>I'm</em> #{owner}!</a>)</span></p>
Expand All @@ -52,15 +55,15 @@ or copy the URL from the location bar of your browser
and paste it into your app</span>.</p>
>>
}
html = function(c_i){
html = function(c_i,orig){
map = math:base64decode(c_i);
owner = map.decode(){"label"};
html:header("invitation", scripts) + explain(owner)
html:header("invitation", scripts(orig)) + explain(owner)
+ <<<div style="border:1px dashed silver;padding:5px;width:max-content"></div>
>>
+ <<<p class="no-print">Technical details:</p>
>>
+ invite(map)
+ invite(map,orig)
+ <<<a name="confusion"><p class="no-print">You're #{owner}:</p></a>
<p class="no-print">You'll need to give this URL to the person with whom you want
a secure message-passing connection.
Expand Down
5 changes: 3 additions & 2 deletions krl/s.krl
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ ruleset s {
})
}
fired {
ent:s{channel{"id"}} := url
shortcut = <<#{meta:host}/sky/event/#{channel{"id"}}/#{eid}/s/u>>
ent:s{channel{"id"}} := url + "&orig=" + shortcut
raise shortcut event "registered" attributes {
"tag":tag,
"eci":channel{"id"},
"shortcut":<<#{meta:host}/sky/event/#{channel{"id"}}/#{eid}/s/u>>,
"shortcut":shortcut,
"url":url
}
}
Expand Down

2 comments on commit 5f5d300

@b1conrad
Copy link
Member Author

@b1conrad b1conrad commented on 5f5d300 Mar 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of notes. First, our implementation of shortcuts differs from the official RFC which requires that, "The shortened URL MUST include an id query parameter, for example:" (emphasis in original)

https://example.com/ssi?id=5f0e3ffb-3f92-4648-9868-0d6f8889e6f3

However, Trinsic currently uses, for example:

https://trinsic.studio/url/c593b792-1344-4f13-8c93-e7f1196201ad

and this code change will use something like:

https://manifold.picolabs.io:9090/sky/event/7NjtLM89RxJ1izE5VjWbYW/alice/s/u

We wouldn't be able to get as short a URL if we followed the spec. It would be about 30 characters longer, with redundancy:

https://manifold.picolabs.io:9090/sky/event/7NjtLM89RxJ1izE5VjWbYW/alice/s/u?id=7NjtLM89RxJ1izE5VjWbYW

@b1conrad
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Second note is about embedding KRL expressions in Javascript expressions in an HTML script tag. See, for example, this code:

<<<script type="text/javascript">
$(function(){
  $("div").qrcode({ text: "#{orig || ""}" || location.href, foreground: "#000000" });
});
</script>
>>

The HTML code is embedded in chevron quotes << and >> and the closing chevron quote cannot immediately follow the closing script tag (because >>> stumps the KRL parser).

KRL expressions are embedded in the JavaScript code using the beesting #{...}.

There are two occurrences of the in-line operater ||, the first inside a beestring (and thus executed as KRL code), and the second is just Javascript code. The overall effect is that if the KRL name orig happens to be null it will be replaced by the KRL empty string, resulting in a JavaScript empty string, so that the Javascript/DOM value of location.href will be used as the text for the QR Code. The same thing would happen if the orig value was the empty string. Otherwise, the text value for the QR Code will be the KRL value of orig converted to a JavaScript string by the double quotes surrounding the beesting.

Please sign in to comment.