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

Actually committed the conversation route files.

parent e111e7fb
#
# All-Friends Timeline
#
# StreamWeaver = require '../../StreamWeaver'
moment = require 'moment'
module.exports = (app) ->
route = (request, response) ->
#
# TODO: Refactor these formatter to remove redundancy.
#
# Custom formatter for message body ids
messageBodyIDFormatter = (messageID) ->
return messageID + '-body'
# Custom formatter for message status ids
messageStatusIDFormatter = (messageID) ->
return messageID + '-status'
# Format the post date
postDateFormatter = (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
#
messageIDParserRegExp = /^(\d{9})?-?(\d{4}-\d{2}-\d{2}T\d{2}_\d{2}_\d{2}\.\d{3}Z)-?(.*)?/
matches = messageID.match messageIDParserRegExp
if matches != null
depracatedOptionalMessageClock = matches[1]
timestamp = matches[2]
optionalAccountHandle = matches[3]
# Desearialise the timestamp.
timestamp = timestamp.replace /_/g, ':'
console.log "Depracated message clock: #{depracatedOptionalMessageClock}" if depracatedOptionalMessageClock
console.log "Timestamp: #{timestamp}"
console.log (if depracatedOptionalMessageClock then "Account handle #{optionalAccountHandle}" else "Account handle: this is your message")
now = new Date()
timeOfPost = new Date timestamp
# to secs -> mins -> hours -> days
timeSincePostInDays = (now - timeOfPost)/1000/60/60/24
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
personFormatter = (messageID) ->
personHandleDelimeter = messageID.lastIndexOf('Z-')
profileImagePath = ''
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 ''
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) ->
personHandleDelimeter = messageID.lastIndexOf('Z-')
profileImagePath = ''
if personHandleDelimeter != -1
# From someone else
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
conversationTimelineForPerson = request.params['person']
console.log "Getting conversation timeline for person #{conversationTimelineForPerson}."
#
# Massage the messages.
#
streamWeaver = app.get('config').streamWeaver
streamWeaver.getTimeline(conversationTimelineForPerson).then (messages) ->
# Massage the asset URLs
for message in messages
# Replace asset URLs
personHandleDelimeter = message.key.lastIndexOf 'Z-'
if personHandleDelimeter != -1
personHandle = message.key.substr(personHandleDelimeter+2)
message.value = message.value.replace new RegExp('/specific-friends/.*?/to/', 'g'), "/specific-friends/#{personHandle}/from/"
#
# Get the person name
# TODO: Create a friend-handle-to-friend-name sublevel in the database for this.
#
streamWeaver.getFriends().then (friends) ->
personsName = "Anonymous (#{conversationTimelineForPerson})"
console.log friends
for friend in friends
friendKeyComponents = friend.key.split('_')
if friendKeyComponents[1] == conversationTimelineForPerson
if friendKeyComponents[0] != ''
console.log "Person’s name: #{personsName}"
personsName = friendKeyComponents[0]
# 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 'conversation', {personsName: personsName, messages: messages, __set: { formatters: {messageBodyIDFormatter: messageBodyIDFormatter, messageStatusIDFormatter: messageStatusIDFormatter, profileImagePathFormatter: profileImagePathFormatter, personFormatter:personFormatter, addFriendLinkFormatter: addFriendLinkFormatter, addFriendTextFormatter:addFriendTextFormatter, postDateFormatter:postDateFormatter}}}
<!doctype html>
<html lang='en'>
<head>
<title data-set-text='personsName'></title>
<meta charset='utf-8'>
<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 data-set-text='meta'></script>
</head>
<body>
<section id='public-timeline'>
<h1 data-set-text='personsName'></h1>
<!-- This recreates what we’re doing via JavaScript on the Cocoa client. -->
<!-- TODO: Evaluate using Set on the Cocoa client also. -->
<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 class='messageBody' data-set-attribute='id message.key messageBodyIDFormatter'>
<!-- <p class="timestamp" data-set-text='message.key'>Message timestamp</p> -->
<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>
</div>
<!-- <div class='messageStatus' data-set-attribute='id message.key messageStatusIDFormatter'></div>-->
</div>
</div>
<section>
</body>
</html>
\ No newline at end of file
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