Ind.ie is now Small Technology Foundation.
Commit 36f83f7e authored by Aral Balkan's avatar Aral Balkan

Added delta updates for Private timeline. Progress towards #79.

parent f9578d37
window.addEventListener('load', function(){
// Set: don’t run in Node.
if (typeof this.data !== 'undefined')
{
// console.log("In Node: Not running in-browser code.");
return;
}
setInterval(function(){
// console.log("Polling server for new public posts…")
var messages = document.getElementById('messages');
var newestPostID = messages.firstElementChild.getAttribute('id')
// console.log('Getting post after ' + newestPostID)
// TODO: Do not hard code this URL.
// TODO: Fix Set injectData so we don’t have to use this metadata injection workaround.
superagent
.get('http://127.0.0.1:42003/timeline/posts-after/'+newestPostID+'/private')
.end(function(error, posts) {
//
// Optimisation: clone the repeater seed node and only apply the set
// template on that so we don’t end up rendering the whole list of messages.
// (I should pave this cowpath: https://source.ind.ie/project/set/issues/4)
//
posts = JSON.parse(posts.text);
// console.log(posts);
if (posts.length == 0){
console.log('No new posts.');
return;
}
var repeaterNodeHTML =
" <div style='background-color: rgb(200, 50, 50, 0.5);' class='message' data-set-repeat='message messages' data-set-attribute='id message.key' >"
+ " <div class='messageBody' data-set-attribute='id message.key messageBodyIDFormatter'>"
+ " <div class='image-and-body'>"
+ " <img class='profileImage' src='http://localhost:42000/about/me.jpg'>"
+ " <div class='bodyText' data-set-text='html message.value'>Message body HTML</div>"
+ " <div class='meta'><span class='postDate' data-set-attribute='data-timestamp message.key' data-set-text='message.key postDateFormatter'></span></div>"
+ " </div>"
+ " </div>"
+ " </div>";
var messages = document.getElementById('messages');
var div = document.createElement('div');
div.setAttribute('id', posts[0]['key']+'-0');
div.innerHTML = repeaterNodeHTML;
messages.insertBefore(div, messages.firstElementChild);
// messages.innerHTML = repeaterNodeHTML + messages.innerHTML;
repeaterNode = messages.firstElementChild;
// Bug: Formatters are not being passed from the server correctly
// when injectData is true. As a workaround: I’m duplicating them on the
// client also.
set.format['messageBodyIDFormatter'] = function (messageID) {
return messageID + "-body";
}
set.format['messageStatusIDFormatter'] = function (messageID) {
return messageID + "-status";
}
set.format['postDateFormatter'] = function (messageID) {
//
// Parses message IDs in the following forms into separate groups for
// * timeline clock (deprecated)
// * message time (replace underscores with colons to convert to valid timestamp)
// * account handle (optional)
//
// 000000001-2015-08-10T18_49_10.467Z-laura
// 000000001-2015-08-10T18_49_10.467Z
// 2015-08-10T18_49_10.467Z-laura
// 2015-08-10T18_49_10.467Z
//
var messageIDParserRegExp = /^(\d{9})?-?(\d{4}-\d{2}-\d{2}T\d{2}_\d{2}_\d{2}\.\d{3}Z)-?(.*)?/;
var matches = messageID.match(messageIDParserRegExp);
if (matches != null){
var depracatedOptionalMessageClock = matches[1];
var timestamp = matches[2];
var optionalAccountHandle = matches[3];
// Desearialise the timestamp.
timestamp = timestamp.replace (/_/g, ':');
var now = new Date();
var timeOfPost = new Date(timestamp);
// to secs -> mins -> hours -> days
var timeSincePostInDays = (now - timeOfPost)/1000/60/60/24;
var humanTime = moment(timeOfPost).fromNow();
return humanTime + " ";
}
else
{
// This should never happen and probably shows that some sort of corrupted date got through somehow.
return 'No date.';
}
}
// Update the repeater node
set(repeaterNode, {messages: posts})
});
// Update all the message clocks
var postDates = document.getElementsByClassName('postDate');
for (var i = 0; i < postDates.length; i++)
{
var postDate = postDates[i];
var timestamp = (postDate.getAttribute('data-timestamp')).replace (/_/g, ':');
var timeOfPost = new Date(timestamp);
var humanTime = moment(timeOfPost).fromNow();
postDate.innerHTML = humanTime + " ";
}
}, 2000);
});
......@@ -6,8 +6,9 @@
<meta name='viewport' content='width=device-width'>
<link rel='stylesheet' href='/css/styles.css'>
<script type='text/javascript' src='/js/superagent.js'></script>
<script src="/js/set.js"></script>
<script src="/js/all-friends.js"></script>
<script type='text/javascript' src='/js/moment.js'></script>
<script type='text/javascript' src='/js/set.js'></script>
<script type='text/javascript' src='/js/private.js'></script>
<script data-set-text='meta'></script>
</head>
<body>
......@@ -26,7 +27,7 @@
<div class="image-and-body">
<img class="profileImage" src='http://localhost:42000/about/me.jpg'>
<div class="bodyText" data-set-text='html message.value'>Message body HTML</div>
<div class="meta"><span data-set-text='message.key postDateFormatter'></span></div>
<div class='meta'><span class='postDate' data-set-attribute='data-timestamp message.key' data-set-text='message.key postDateFormatter'></span></div>
</div>
</div>
<!-- <div class='messageStatus' data-set-attribute='id message.key messageStatusIDFormatter'></div>-->
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment