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

Renamed ‘user’ to ‘person’. Removed some unused client-side Set formatters....

Renamed ‘user’ to ‘person’. Removed some unused client-side Set formatters. Working on conversations.
parent 3d774939
......@@ -4,8 +4,6 @@
#
# Filters and pipes events from Pulse to the native Cocoa client
#
# TODO: Refactor. Stop using “user”. Replace with “person”. How did that sneak in? :)
#
# This is independent technology. See ind.ie/manifesto
#
# Copyright © Aral Balkan. Copyright © Ind.ie. All Rights Reserved.
......@@ -164,21 +162,21 @@ class PulseEventPipe
# Data from message repository item
locationOfPersonDelimeter = messageRepository.indexOf '-to'
user = messageRepository.substr 0, locationOfPersonDelimeter
person = messageRepository.substr 0, locationOfPersonDelimeter
messageType = messageRepository.substr locationOfPersonDelimeter+1
# Calculate the message ID
# (It’s the Heartbeat timestamp (sans local timeline order bit at the front) + '-<username>')
# (It’s the Heartbeat timestamp (sans local timeline order bit at the front) + '-<personname>')
messageTimestamp = messageFileName.substring 0, messageFileName.indexOf('/')
messageID = "#{messageTimestamp}-#{user}"
messageID = "#{messageTimestamp}-#{person}"
console.log "User: #{user}"
console.log "Person: #{person}"
console.log "Message type: #{messageType}"
console.log "Message time stamp: #{messageTimestamp}"
console.log "Message ID: #{messageID}"
console.log "File name of changed file: #{fileNameOfChangedFile}"
toAllFriendsMessageType = "#{user}-to-all-friends"
toAllFriendsMessageType = "#{person}-to-all-friends"
friendsCached = @streamWeaver.friendsCached
......@@ -187,20 +185,20 @@ class PulseEventPipe
messageFromFriend = false
for friend in friendsCached
console.log "Checking if #{user} == #{friend}…"
if friend == user
console.log "Checking if #{person} == #{friend}…"
if friend == person
console.log "Message received from friend"
messageFromFriend = true
if messageRepository == toAllFriendsMessageType and fileNameOfChangedFile == 'index.html' and user != @accountHandle
if messageRepository == toAllFriendsMessageType and fileNameOfChangedFile == 'index.html' and person != @accountHandle
#
# Handle to all friends message.
#
console.log "A message just got synced from user #{user} for the all friends timeline."
console.log "A message just got synced from person #{person} for the all friends timeline."
pathToMessageIndexFile = path.join(@homeDirectory, 'Pulse', 'Sync', '3. All your friends', 'from', user, messageFileName)
pathToMessageIndexFile = path.join(@homeDirectory, 'Pulse', 'Sync', '3. All your friends', 'from', person, messageFileName)
console.log "Path to message index file: #{pathToMessageIndexFile}"
......@@ -208,7 +206,7 @@ class PulseEventPipe
console.log "About to weave into the #{allFriendsTimeline} timeline."
@readMessageAndWeaveIntoTimeline pathToMessageIndexFile, messageID, user, allFriendsTimeline
@readMessageAndWeaveIntoTimeline pathToMessageIndexFile, messageID, person, allFriendsTimeline
# Massage the event type to signal to Cocoa that the timeline needs to be updated.
event.type = "AllFriendsTimelineUpdated"
......@@ -219,15 +217,15 @@ class PulseEventPipe
# Handle messages from friends (private conversations)
#
console.log "A message just for synced from user #{user} — private conversation."
console.log "A message just for synced from person #{person} — private conversation."
pathToMessageIndexFile = path.join(@homeDirectory, 'Pulse', 'Sync', '2. Specific friends', user, 'from', messageFileName)
pathToMessageIndexFile = path.join(@homeDirectory, 'Pulse', 'Sync', '2. Specific friends', person, 'from', messageFileName)
console.log "Path to message index file: #{pathToMessageIndexFile}"
console.log "About to weave into the #{user} timeline."
console.log "About to weave into the #{person} timeline."
@readMessageAndWeaveIntoTimeline pathToMessageIndexFile, messageID, user, user
@readMessageAndWeaveIntoTimeline pathToMessageIndexFile, messageID, person, person
.catch (error) =>
console.log "Error reading message and weaving into timeline: #{error}"
......@@ -313,9 +311,9 @@ class PulseEventPipe
@poll()
readMessageAndWeaveIntoTimeline: (pathToMessageIndexFile, messageID, user, timeline) =>
readMessageAndWeaveIntoTimeline: (pathToMessageIndexFile, messageID, person, timeline) =>
console.log "About to readMessageAndWeaveIntoTimeline. path: #{pathToMessageIndexFile}, messageID: #{messageID}, user: #{user}, timeline: #{timeline}"
console.log "About to readMessageAndWeaveIntoTimeline. path: #{pathToMessageIndexFile}, messageID: #{messageID}, person: #{person}, timeline: #{timeline}"
fs.readFileAsync(pathToMessageIndexFile, {encoding: 'utf8'})
.then (data) =>
......@@ -324,7 +322,7 @@ class PulseEventPipe
# Massage URLs
# massagedHTML = data.replace 'img src="/public', 'img src="/public/' + user
# massagedHTML = data.replace 'img src="/public', 'img src="/public/' + person
# console.log "Message HTML with massaged URLS:"
# console.log massagedHTML
......@@ -332,6 +330,6 @@ class PulseEventPipe
# Weave into the LevelDB database.
@streamWeaver.weaveMessage messageID, data, timeline
.catch (error) =>
console.log "Error reading message index file at #{pathToMessageIndexFile} (messageID: #{messageID}, user: #{user}, timeline: #{timeline}."
console.log "Error reading message index file at #{pathToMessageIndexFile} (messageID: #{messageID}, person: #{person}, timeline: #{timeline}."
module.exports = PulseEventPipe
......@@ -82,29 +82,6 @@ module.exports = (app) ->
# TODO: Once the timestamps are in there, just return that.
return ''
addFriendLinkFormatter = (messageID) ->
personHandleDelimeter = messageID.lastIndexOf('Z-')
if personHandleDelimeter != -1
# From someone else
personHandle = messageID.substr(personHandleDelimeter+2)
addFriendLink = "indie://friend/#{personHandle}"
return addFriendLink
else
# This is the person themselves, no need to display a friend link.
return ''
addFriendTextFormatter = (messageID) ->
personHandleDelimeter = messageID.lastIndexOf('Z-')
if personHandleDelimeter != -1
return "<img class='add-friend-icon' src='/images/person_add@2x.png' alt='Send friend request'>"
else
# This is the person themselves, no need to display a friend link.
return ''
# Custom formatter for the profile image
profileImagePathFormatter = (messageID) ->
......@@ -137,4 +114,4 @@ module.exports = (app) ->
# TODO: Add the formatters to the messages themselves so we don’t have to duplicate them on the client.
# Reverse the message order to match that of the Cocoa client.
response.render 'all-friends', {messages: messages, __set: { formatters: {messageBodyIDFormatter: messageBodyIDFormatter, messageStatusIDFormatter: messageStatusIDFormatter, profileImagePathFormatter: profileImagePathFormatter, personFormatter:personFormatter, addFriendLinkFormatter: addFriendLinkFormatter, addFriendTextFormatter:addFriendTextFormatter, postDateFormatter:postDateFormatter}}}
response.render 'all-friends', {messages: messages, __set: { formatters: {messageBodyIDFormatter: messageBodyIDFormatter, messageStatusIDFormatter: messageStatusIDFormatter, profileImagePathFormatter: profileImagePathFormatter, personFormatter:personFormatter, postDateFormatter:postDateFormatter}}}
......@@ -83,31 +83,6 @@ module.exports = (app) ->
# TODO: Once the timestamps are in there, just return that.
return ''
addFriendLinkFormatter = (messageID) ->
personHandleDelimeter = messageID.lastIndexOf('Z-')
profileImagePath = ''
if personHandleDelimeter != -1
# From someone else
personHandle = messageID.substr(personHandleDelimeter+2)
addFriendLink = "indie://friend/#{personHandle}"
return addFriendLink
else
# This is the person themselves, no need to display a friend link.
return ''
addFriendTextFormatter = (messageID) ->
personHandleDelimeter = messageID.lastIndexOf('Z-')
profileImagePath = ''
if personHandleDelimeter != -1
return "<img class='add-friend-icon' src='/images/person_add@2x.png' alt='Send friend request'>"
else
# This is the person themselves, no need to display a friend link.
return ''
# Custom formatter for the profile image
profileImagePathFormatter = (messageID) ->
......@@ -162,4 +137,4 @@ module.exports = (app) ->
# TODO: Add the formatters to the messages themselves so we don’t have to duplicate them on the client.
response.render 'conversation', {personsAccountHandle: person, personsName: personsName, messages: messages, __set: { formatters: {messageBodyIDFormatter: messageBodyIDFormatter, messageStatusIDFormatter: messageStatusIDFormatter, profileImagePathFormatter: profileImagePathFormatter, personFormatter:personFormatter, addFriendLinkFormatter: addFriendLinkFormatter, addFriendTextFormatter:addFriendTextFormatter, postDateFormatter:postDateFormatter}}}
response.render 'conversation', {personsAccountHandle: person, personsName: personsName, messages: messages, __set: { formatters: {messageBodyIDFormatter: messageBodyIDFormatter, messageStatusIDFormatter: messageStatusIDFormatter, profileImagePathFormatter: profileImagePathFormatter, personFormatter:personFormatter, postDateFormatter:postDateFormatter}}}
......@@ -33,7 +33,7 @@
<div class="image-and-body">
<img class="profileImage" data-set-attribute='src message.key profileImagePathFormatter'>
<div class="bodyText" data-set-text='html message.value'>Message body HTML</div>
<div class="meta"><span data-set-text='message.key postDateFormatter'></span><span data-set-text='message.key personFormatter'></span> <a data-set-attribute='href message.key addFriendLinkFormatter' data-set-text='html message.key addFriendTextFormatter'></a></div>
<div class="meta"><span class='postDate' data-set-attribute='data-timestamp message.key' data-set-text='message.key postDateFormatter'></span><span data-set-text='message.key personFormatter'></span></div>
</div>
</div>
<!-- <div class='messageStatus' data-set-attribute='id message.key messageStatusIDFormatter'></div>-->
......
......@@ -6,9 +6,10 @@
<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/conversation.js"></script>
<script data-set-text='meta'></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/conversation.js'></script>
<script type='text/javascript' data-set-text='meta'></script>
</head>
<body>
<section id='public-timeline'>
......@@ -21,12 +22,12 @@
<div id='messages' class='messages' data-set-if='messages'>
<!-- Repeater -->
<div class='message' data-set-repeat='message messages' data-set-attribute='id message.key' >
<div id='messageRepeater' 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" data-set-attribute='src message.key profileImagePathFormatter'>
<div class="bodyText" data-set-text='html message.value'>Message body HTML</div>
<div class="meta"><span data-set-text='message.key postDateFormatter'></span><span data-set-text='message.key personFormatter'></span> <a data-set-attribute='href message.key addFriendLinkFormatter' data-set-text='html message.key addFriendTextFormatter'></a></div>
<div class="meta"><span class='postDate' data-set-attribute='data-timestamp message.key' data-set-text='message.key postDateFormatter'></span><span data-set-text='message.key personFormatter'></span></div>
</div>
</div>
<!-- <div class='messageStatus' data-set-attribute='id message.key messageStatusIDFormatter'></div>-->
......
......@@ -149,38 +149,6 @@ window.addEventListener('load', function(){
}
}
set.format['addFriendLinkFormatter'] = function (messageID) {
personHandleDelimeter = messageID.lastIndexOf('Z-');
if (personHandleDelimeter != -1){
// From someone else
var personHandle = messageID.substr(personHandleDelimeter+2);
var addFriendLink = "indie://friend/#{personHandle}";
return addFriendLink;
}
else {
// This is the person themselves, no need to display a friend link.
return '';
}
}
set.format['addFriendTextFormatter'] = function (messageID) {
var personHandleDelimeter = messageID.lastIndexOf('Z-');
var profileImagePath = '';
if (personHandleDelimeter != -1) {
return "<img class='add-friend-icon' src='/images/person_add@2x.png' alt='Send friend request'>";
}
else {
// This is the person themselves, no need to display a friend link.
return '';
}
}
// Custom formatter for the profile image
set.format['profileImagePathFormatter'] = function (messageID) {
console.log("Formatting profile image for " + messageID)
......
......@@ -12,6 +12,14 @@ window.addEventListener('load', function(){
return;
}
// Check if there are no messages to begin with, remove the pre-rendered repeater.
// (It’s ID will be different if there are.)
var messageRepeater = document.getElementById('messageRepeater');
if (messageRepeater != null)
{
messageRepeater.parentNode.removeChild(messageRepeater);
}
setInterval(function(){
// console.log("Polling server for new public posts…")
......@@ -19,7 +27,12 @@ window.addEventListener('load', function(){
var messages = document.getElementById('messages');
// The latest message is at the bottom for conversation-style timelines.
var newestPostID = messages.lastElementChild.getAttribute('id')
var newestPostID = 0
var lastMessage = messages.lastElementChild
if (lastMessage != null) {
newestPostID = lastMessage.getAttribute('id');
}
// console.log('Getting post after ' + newestPostID)
......@@ -38,35 +51,44 @@ window.addEventListener('load', function(){
// (I should pave this cowpath: https://source.ind.ie/project/set/issues/4)
//
posts = JSON.parse(posts.text);
// console.log(posts);
console.log(posts);
if (posts.length == 0){
console.log('No new posts.');
return;
}
// Make sure that the notice is hidden and that the main section is showing.
// (Set can’t handle this automatically as we are not running it on the
// entire document but only on the delta repeat block below for performance reasons.)
document.getElementById('notice').style.display = 'none';
document.getElementById('messages').style.display = 'block';
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' data-set-attribute='src message.key profileImagePathFormatter'>"
+ " <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')
var div = document.createElement('div');
div.setAttribute('id', posts[0]['key']+'-0');
div.innerHTML = repeaterNodeHTML
div.innerHTML = repeaterNodeHTML;
// Insert new items at the end.
insertAfter(messages.firstElementChild, div)
insertAfter(messages.firstElementChild, div);
// messages.innerHTML = repeaterNodeHTML + messages.innerHTML;
repeaterNode = messages.lastElementChild
repeaterNode = messages.lastElementChild;
console.log(repeaterNode);
// Bug: Formatters are not being passed from the server correctly
// when injectData is true. As a workaround: I’m duplicating them on the
......@@ -79,14 +101,103 @@ window.addEventListener('load', function(){
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.';
}
}
// Format the person’s name
set.format['personFormatter'] = function (messageID) {
var personHandleDelimeter = messageID.lastIndexOf('Z-');
if (personHandleDelimeter != -1) {
// From someone else
personHandle = messageID.substr(personHandleDelimeter+2);
// TODO: Once public profile pages are implemented, link to them.
return " by "+personHandle+".";
}
else {
// This is the person themselves.
// TODO: Once the timestamps are in there, just return that.
return '';
}
}
// Custom formatter for the profile image
set.format['profileImagePathFormatter'] = function (messageID) {
var personHandle = messageID.substr(messageID.lastIndexOf('Z-')+2);
var profileImagePath = "/public/"+personHandle+"/about/me.jpg";
console.log("Formatting profile image for " + messageID)
var personHandleDelimeter = messageID.lastIndexOf('Z-');
var profileImagePath = '';
if (personHandleDelimeter != -1) {
// From someone else
var personHandle = messageID.substr(personHandleDelimeter+2);
console.log("Person handle for message: "+personHandle+".");
profileImagePath = "http://127.0.0.1:42003/all-friends/from/"+personHandle+"/about/me.jpg";
}
else {
// From you
console.log("Message is from you.");
profileImagePath = "http://localhost:42000/about/me.jpg";
}
return profileImagePath;
}
// 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);
});
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