define([ "jquery", "helper/Paths", "lib/URI", "helper/HostApp", "helper/Cache", "lib/Timeago" ], function(jQuery, Paths, URI, HostApp, Cache) { function Core() { } Core.prototype.getTemplate = function() { if(this.template == "undefined") { return jQuery.extend(true, {}, this.template); } var a = document.createElement("a"); var item = document.createElement("li"); var aside = document.createElement("aside"); item.appendChild(aside); var reply_to = a.cloneNode(); reply_to.className = "reply_to" reply_to.innerText = " "; reply_to.href = "#"; aside.appendChild(reply_to); var repost = a.cloneNode(); repost.className = "repost"; repost.innerText = " "; repost.href = "#"; aside.appendChild(repost); var remove = a.cloneNode(); remove.className = "remove"; remove.innerText = " "; remove.href = "#"; aside.appendChild(remove); var image = document.createElement("img"); image.className = "image"; image.src = "img/default-avatar.png"; image.onmousedown = function(e) { e.preventDefault(); }; item.appendChild(image); var image_username = a.cloneNode(); image.appendChild(image_username); var data = document.createElement("div"); data.className = "data"; item.appendChild(data); var head = document.createElement("h1"); data.appendChild(head); var username = a.cloneNode(); head.appendChild(username); var space = document.createTextNode(" "); head.appendChild(space); var geo = document.createElement("a"); geo.style.display = "none"; head.appendChild(geo); head.appendChild(space.cloneNode()); var is_private = document.createElement("span") is_private.className = "is_private"; is_private.style.display = "none"; is_private.innerHTML = "P"; is_private.title = "Private"; head.appendChild(is_private); head.appendChild(space.cloneNode()); var pin = document.createElement("img"); pin.src = "img/pin.png"; pin.alt = "Map link"; geo.appendChild(pin); head.appendChild(space.cloneNode()); var reposted_by = a.cloneNode(); reposted_by.className = "reposted_by"; reposted_by.style.display = "none"; head.appendChild(reposted_by) var message = document.createElement("p"); message.className = "message"; data.appendChild(message); var images = document.createElement("p") images.className = "images"; data.appendChild(images); var date = message.cloneNode(); date.className = "date"; data.appendChild(date); var ago = a.cloneNode(); date.appendChild(ago); var from = document.createTextNode(" from "); date.appendChild(from) var source = document.createElement("a"); source.className = "source"; date.appendChild(source) this.template = { item: item, reply_to: reply_to, is_private: is_private, image: image, username: username, repost: repost, reposted_by: reposted_by, message: message, ago: ago, source: source, geo: geo, images: images, remove: remove } return jQuery.extend(true, {}, this.template); } Core.prototype.getStatusDOMElement = function(status) { var _this = this; var template = this.getTemplate(); template.item.id = "post-" + (typeof status.__repost != "undefined" ? status.__repost.id : status.id); if (HostApp.stringForKey("entity") == status.entity && typeof status.__repost == "undefined") { template.remove.onclick = function() { _this.remove(status.id); return false; } } else if (typeof status.__repost != "undefined" && HostApp.stringForKey("entity") == status.__repost.entity) { template.remove.onclick = function() { _this.remove(status.__repost.id); return false; } } else { template.remove.style.display = "none"; } template.reply_to.onclick = function() { var mentions = []; var status_mentions = status.mentions.slice(0); if (typeof status.__repost != "undefined") { status_mentions.push({entity:status.__repost.entity}); } for (var i = 0; i < status_mentions.length; i++) { var mention = status_mentions[i]; if(mention.entity != HostApp.stringForKey("entity")) mentions.push(mention); } _this.replyTo(status.entity, status.id, mentions); return false; } template.repost.onclick = function() { template.repost.className = "hidden"; _this.repost(status.id, status.entity); return false; } template.username.innerText = status.entity; template.username.href = status.entity; template.username.onclick = function() { HostApp.showProfileForEntity(status.entity); return false; } function profile(p) { var basic = p["https://tent.io/types/info/basic/v0.1.0"]; if (p && basic) { if(basic.name) { template.username.title = template.username.innerText; template.username.innerText = basic.name; } if(basic.avatar_url) { template.image.onerror = function() { template.image.src = 'img/default-avatar.png' }; template.image.src = basic.avatar_url; } } } var p = JSON.parse(Cache.profiles.getItem(status.entity)); if (p && p != "null") { profile(p); } else { Paths.findProfileURL(status.entity, function(profile_url) { if (profile_url) { Paths.getURL(profile_url, "GET", function(resp) { var p = JSON.parse(resp.responseText); if (p && p != "null") { Cache.profiles.setItem(status.entity, resp.responseText); profile(p); } }, null, false); // do not send auth-headers } }); } if (typeof status.__repost != "undefined") { template.reposted_by.href = status.__repost.entity; template.reposted_by.innerText = status.__repost.entity; template.reposted_by.title = status.__repost.entity; template.reposted_by.style.display = ''; template.reposted_by.onclick = function(e) { HostApp.showProfileForEntity(status.__repost.entity); return false; } var profile = JSON.parse(Cache.profiles.getItem(status.__repost.entity)) if (profile) { var basic = profile["https://tent.io/types/info/basic/v0.1.0"]; template.reposted_by.innerText = basic.name; } else { Paths.findProfileURL(status.__repost.entity, function(profile_url) { if (profile_url) { Paths.getURL(profile_url, "GET", function(resp) { Cache.profiles.setItem(status.__repost.entity, resp.responseText); var p = JSON.parse(resp.responseText); var profile = p["https://tent.io/types/info/basic/v0.1.0"]; if (profile && profile.name) { template.reposted_by.innerText = profile.name; } }, null, false); // do not send auth-headers } }); } } if (status && status.permissions && !status.permissions.public) { template.is_private.style.display = ''; } var text = ""; if (status.type == "https://tent.io/types/post/photo/v0.1.0") { text = status.content.caption; } else { if (status.content && status.content.text) { text = status.content.text; } } text = text.escapeHTML().replace(/\n/g, "
"); var entities = [status.entity]; status.mentions.map(function (mention) { entities.push(mention.entity) }); template.message.innerHTML = this.replaceUsernamesWithLinks( this.replaceURLWithHTMLLinks(text, entities, template.message) ); if (status.type == "https://tent.io/types/post/photo/v0.1.0") { debug(status.attachments) for (var i = 0; i < status.attachments.length; i++) { // closure needed for the callback (function() { var attachment = status.attachments[i]; var img = new Image(); img.className = "photo"; template.images.appendChild(img); var callback = function(resp) { img.src = "data:" + attachment.type + ";base64," + resp.responseText; } if (status.entity == HostApp.stringForKey("entity")) { var url = Paths.mkApiRootPath("/posts/" + status.id + "/attachments/" + attachment.name); Paths.getURL(url, "GET", callback, null, null, attachment.type); } else { Paths.findProfileURL(status.entity, function(profile_url) { var url = profile_url + "/posts/" + status.id + "/attachments/" + attachment.name; debug(url) Paths.getURL(url, "GET", callback, null, null, attachment.type); }); } })(); } } this.findMentions(template.message, status.mentions); for (var i = 0; i < status.mentions.length; i++) { var mention = status.mentions[i]; if (mention.entity == HostApp.stringForKey("entity")) { this.template.item.className = "mentioned"; break; } } var published_at = typeof status.__repost == "undefined" ? status.published_at : status.__repost.published_at; var time = document.createElement("abbr"); time.innerText = this.ISODateString(new Date(published_at * 1000)); time.title = time.innerText; time.className = "timeago"; jQuery(time).timeago(); template.ago.appendChild(time); template.ago.href = "#" template.ago.onclick = function() { HostApp.showConversation(status.id, status.entity); return false; } // {"type":"Point","coordinates":[57.10803113,12.25854746]} if (status.content && status.content.location && (typeof status.content.location.type == "undefined" || status.content.location.type == "Point")) { template.geo.href = "http://maps.google.com/maps?q=" + status.content.location.coordinates[0] + "," + status.content.location.coordinates[1]; template.geo.style.display = ""; } if (typeof status.__repost != "undefined") { template.source.href = status.__repost.app.url; template.source.innerHTML = status.__repost.app.name; template.source.title = status.__repost.app.url; } else { template.source.href = status.app.url; template.source.innerHTML = status.app.name; template.source.title = status.app.url; } return template.item; } Core.prototype.getRepost = function(repost, before_node) { var _this = this; var callback = function(resp) { if (resp.status >= 200 && resp.status < 300) { var status = JSON.parse(resp.responseText); status.__repost = repost; var li = _this.getStatusDOMElement(status); before_node.parentNode.insertBefore(li, before_node); } } var profile = JSON.parse(Cache.profiles.getItem(repost.content.entity)); if (profile && profile != "null") { var server = profile["https://tent.io/types/info/core/v0.1.0"].servers[0]; Paths.getURL(URI(server + "/posts/" + repost.content.id).toString(), "GET", callback, null, false); } else { Paths.findProfileURL(repost.content.entity, function(profile_url) { if (profile_url) { Paths.getURL(profile_url, "GET", function(resp) { var profile = JSON.parse(resp.responseText); var server = profile["https://tent.io/types/info/core/v0.1.0"].servers[0]; Paths.getURL(URI(server + "/posts/" + repost.content.id).toString(), "GET", callback, null, false); }, null, false); // do not send auth-headers } }) } } Core.prototype.sendNewMessage = function(content, in_reply_to_status_id, in_reply_to_entity, location, image_file_path, callback) { if (image_file_path) { this.sendNewMessageWithImage(content, in_reply_to_status_id, in_reply_to_entity, location, image_file_path, callback); } else { var url = URI(Paths.mkApiRootPath("/posts")); var http_method = "POST"; var data = { "type": "https://tent.io/types/post/status/v0.1.0", "published_at": parseInt(new Date().getTime() / 1000, 10), "permissions": { "public": true }, "content": { "text": content, }, }; if (location) { data["content"]["location"] = { "type": "Point", "coordinates": location } } var mentions = this.parseMentions(content, in_reply_to_status_id, in_reply_to_entity); if (mentions.length > 0) { data["mentions"] = mentions; } Paths.getURL(url.toString(), http_method, callback, JSON.stringify(data)); } } Core.prototype.repost = function(id, entity, callback) { var url = URI(Paths.mkApiRootPath("/posts")); var data = { "type": "https://tent.io/types/post/repost/v0.1.0", "published_at": parseInt(new Date().getTime() / 1000, 10), "permissions": { "public": true }, "content": { "entity": entity, "id": id }, "mentions": [ { "entity": entity, "post": id } ] }; Paths.getURL(url.toString(), "POST", callback, JSON.stringify(data)); } Core.prototype.sendNewMessageWithImage = function(content, in_reply_to_status_id, in_reply_to_entity, location, image_data_uri, callback) { var url = URI(Paths.mkApiRootPath("/posts")); var data = { "type": "https://tent.io/types/post/photo/v0.1.0", "published_at": parseInt(new Date().getTime() / 1000, 10), "permissions": { "public": true }, "content": { "caption": content, }, }; if (location) { data["content"]["location"] = { "type": "Point", "coordinates": location } } var mentions = this.parseMentions(content, in_reply_to_status_id, in_reply_to_entity); if (mentions.length > 0) { data["mentions"] = mentions; } var data_string = JSON.stringify(data); var boundary = "TentAttachment----------TentAttachment"; var post = "--" + boundary + "\r\n"; post += 'Content-Disposition: form-data; name="post"; filename="post.json"\r\n'; post += 'Content-Length: ' + data_string.length + '\r\n'; post += 'Content-Type: application/vnd.tent.v0+json\r\n'; post += 'Content-Transfer-Encoding: binary\r\n\r\n'; post += data_string; post += "\r\n--" + boundary + "\r\n"; var blob_string = image_data_uri.split(',')[1]; var mime_type = image_data_uri.split(',')[0].split(':')[1].split(';')[0]; var ext = "png"; if (mime_type == "image/jpeg") { ext = "jpeg"; } else if (mime_type == "image/gif") { ext = "gif"; } post += 'Content-Disposition: form-data; name="photos[0]"; filename="photo.' + ext + '"\r\n'; post += 'Content-Length: ' + blob_string.length + "\r\n"; post += 'Content-Type: ' + mime_type + "\r\n"; post += 'Content-Transfer-Encoding: base64\r\n\r\n'; post += blob_string; post += "\r\n--" + boundary + "--\r\n"; Paths.postMultipart(url.toString(), callback, post, boundary); } Core.prototype.remove = function(id, callback) { if (confirm("Really delete this post?")) { var url = URI(Paths.mkApiRootPath("/posts/" + id)); Paths.getURL(url.toString(), "DELETE", callback); } } Core.prototype.logout = function() { this.body.innerHTML = ""; } // Helper functions Core.prototype.replaceShortened = function(url, message_node) { var api = "http://api.bitly.com"; if(url.startsWith("http://j.mp/")) { api = "http://api.j.mp"; } var api_url = api + "/v3/expand?format=json&apiKey=R_4fc2a1aa461d076556016390fa6400f6&login=twittia&shortUrl=" + url; // FIXME: new api key jQuery.ajax({ url: api_url, success: function(data) { var new_url = data.data.expand[0].long_url; if (new_url) { var regex = new RegExp(url, "g"); message_node.innerHTML = message_node.innerHTML.replace(regex, new_url); } }, error:function (xhr, ajaxOptions, thrownError) { console.error(xhr.status); console.error(thrownError); } }); } Core.prototype.findMentions = function(node, mentions) { var text = node.innerHTML; var mentions_in_text = []; var res = text.match(/(\^[\w:/.]+)/ig); if (res) { for (var i = 0; i < res.length; i++) { var name = res[i]; var e = name.substring(1); if (e.substring(0,7) != "http://" && e.substring(0,8) != "https://") { e = "https://" + e; } for (var j = 0; j < mentions.length; j++) { var m = mentions[j]; if(m.entity.startsWith(e)) { mentions_in_text.push({ entity: m.entity, text: name }); } } } } var _this = this; for (var i = 0; i < mentions_in_text.length; i++) { var mention = mentions_in_text[i]; (function(mention) { // need this closure var profile = function(profile) { var basic = profile["https://tent.io/types/info/basic/v0.1.0"]; if (profile && basic) { if(basic.name) { var new_text = node.innerHTML.replace( mention.text, "" + basic.name + "" ); node.innerHTML = new_text; $(node).find("a.name").click(function(e) { HostApp.showProfileForEntity(e.target.title); return false; }); } } } var p = JSON.parse(Cache.profiles.getItem(mention.entity)); if (p) { profile(p); } else { Paths.findProfileURL(mention.entity, function(profile_url) { if (profile_url) { Paths.getURL(profile_url, "GET", function(resp) { Cache.profiles.setItem(mention.entity, resp.responseText); var p = JSON.parse(resp.responseText); profile(p) }, null, false); // do not send auth-headers } }); } })(mention); } } Core.prototype.parseMentions = function(text, post_id, entity) { var mentions = []; if (post_id && entity && post_id != "(null)" && entity != "(null)") { mentions.push({ post: post_id, entity: entity }) } var res = text.match(/(\^[\w:/.]+)/ig); if (res) { for (var i = 0; i < res.length; i++) { var e = res[i].substring(1); if (e.substring(0,7) != "http://" && e.substring(0,8) != "https://") { e = "https://" + e; } if (e != entity) { mentions.push({entity:e}); } } } return mentions; } Core.prototype.ISODateString = function(d){ function pad(n){return n<10 ? '0'+n : n} return d.getUTCFullYear()+'-' + pad(d.getUTCMonth()+1)+'-' + pad(d.getUTCDate())+'T' + pad(d.getUTCHours())+':' + pad(d.getUTCMinutes())+':' + pad(d.getUTCSeconds())+'Z' } Core.prototype.replaceURLWithHTMLLinks = function(text, entities, message_node) { var callback = function(url) { var result; if (entities && entities.some(function(x) { return x == url })) { result = url; } else { var protocol = ""; if (!url.startsWith("http://") && !url.startsWith("https://")) { protocol = "http://"; } result = '' + url + ''; } return result; } return URI.withinString(text, callback); } Core.prototype.replaceUsernamesWithLinks = function(text, mentions) { var hash = /(^|\s)(#)(\w+)/ig; return text.replace(hash, "$1$2$3"); } Core.prototype.replyTo = function(entity, status_id, mentions) { var string = "^" + entity.replace("https://", "") + " "; for (var i = 0; i < mentions.length; i++) { var e = mentions[i].entity.replace("https://", ""); if(string.indexOf(e) == -1) string += "^" + e + " "; } HostApp.openNewMessageWidow(entity, status_id, string); } return Core; });