diff --git a/app/models/person.rb b/app/models/person.rb index ad66a4dfc..a9713901c 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -15,6 +15,9 @@ class Person < ActiveRecord::Base api_accessible :backbone do |t| t.add :id t.add :name + t.add lambda { |person| + person.diaspora_handle + }, :as => :diaspora_id t.add lambda { |person| {:small => person.profile.image_url(:small), :medium => person.profile.image_url(:medium), @@ -71,7 +74,7 @@ class Person < ActiveRecord::Base scope :profile_tagged_with, lambda{|tag_name| joins(:profile => :tags).where(:profile => {:tags => {:name => tag_name}}).where('profiles.searchable IS TRUE') } - scope :who_have_reshared_a_users_posts, lambda{|user| + scope :who_have_reshared_a_users_posts, lambda{|user| joins(:posts).where(:posts => {:root_guid => StatusMessage.guids_for_author(user.person), :type => 'Reshare'} ) } @@ -286,7 +289,7 @@ class Person < ActiveRecord::Base def self.url_batch_update(people, url) people.each do |person| person.update_url(url) - end + end end # @param person [Person] diff --git a/app/models/post.rb b/app/models/post.rb index 10cfd823d..82b952e5c 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -34,6 +34,7 @@ class Post < ActiveRecord::Base t.add :root t.add :o_embed_cache t.add :user_like + t.add :mentioned_people t.add lambda { |post| if post.photos_count > 0 post.photos @@ -65,6 +66,10 @@ class Post < ActiveRecord::Base "" end + def mentioned_people + [] + end + # gives the last three comments on the post def last_three_comments return if self.comments_count == 0 diff --git a/public/javascripts/app/views/post_content_view.js b/public/javascripts/app/views/post_content_view.js index e57cddfd2..ff594f784 100644 --- a/public/javascripts/app/views/post_content_view.js +++ b/public/javascripts/app/views/post_content_view.js @@ -1,21 +1,44 @@ (function(){ var postContentView = app.views.StreamObject.extend({ presenter : function(){ + var model = this.model return _.extend(this.defaultPresenter(), { - text : metafyText(this.model.get("text")) + text : metafyText(model.get("text")) }) function metafyText(text) { //we want it to return at least a

from markdown text = text || "" - return hashtagify(markdown.toHTML(text) || text) + return mentionify( + hashtagify( + markdownify(text) + ) + ) + } + + function markdownify(text){ + //markdown returns falsy when it performs no substitutions, apparently... + return markdown.toHTML(text) || text } function hashtagify(text){ - return text.replace(/(#([\u0080-\uFFFF|\w|-]+|<3))/g, function(tagText) { + var utf8WordCharcters =/(#([\u0080-\uFFFF|\w|-]+|<3))/g + return text.replace(utf8WordCharcters, function(tagText) { return "" + tagText + "" }) } + + function mentionify(text) { + var mentionRegex = /@\{([^;]+); ([^\}]+)\}/g + return text.replace(mentionRegex, function(mentionText, fullName, diasporaId) { + var personId = _.find(model.get("mentioned_people"), function(person){ + return person.diaspora_id == diasporaId + }).id + + return "" + fullName + "" + }) + return text + } } }) diff --git a/spec/javascripts/app/views/post_view_spec.js b/spec/javascripts/app/views/post_view_spec.js index 53586cabe..e7b676994 100644 --- a/spec/javascripts/app/views/post_view_spec.js +++ b/spec/javascripts/app/views/post_view_spec.js @@ -66,6 +66,38 @@ describe("app.views.Post", function(){ }) }) + context("changes mention markup to links", function(){ + beforeEach(function(){ + this.alice = factory.author({ + name : "Alice Smith", + diaspora_id : "alice@example.com", + id : "555" + }) + + this.bob = factory.author({ + name : "Bob Grimm", + diaspora_id : "bob@example.com", + id : "666" + }) + }) + + it("links to the mentioned person's page", function(){ + this.statusMessage.set({mentioned_people : [this.alice]}) + this.statusMessage.set({text: "sup, @{Alice Smith; alice@example.com}?"}) + + var view = new app.views.Post({model : this.statusMessage}).render(); + expect(view.$("a:contains('Alice Smith')").attr('href')).toBe('/people/555') + }) + + it("matches all mentions", function(){ + this.statusMessage.set({mentioned_people : [this.alice, this.bob]}) + this.statusMessage.set({text: "hey there @{Alice Smith; alice@example.com} and @{Bob Grimm; bob@example.com}"}) + + var view = new app.views.Post({model : this.statusMessage}).render(); + expect(view.$("a.mention").length).toBe(2) + }) + }) + context("user not signed in", function(){ it("does not provide a Feedback view", function(){ window.current_user = app.user(null);