From 066f8d123516df99cd2cc9f4efbc5e428dc10ee1 Mon Sep 17 00:00:00 2001
From: Dan Hansen <mokker1234@gmail.com>
Date: Mon, 31 Oct 2011 22:16:40 -0500
Subject: [PATCH] Mention the person who invited a user on first message

---
 app/helpers/interim_stream_hackiness_helper.rb | 14 ++++++++++++--
 app/views/shared/_publisher.html.haml          |  4 ++--
 config/locales/diaspora/en.yml                 |  3 ++-
 lib/publisher.rb                               | 18 +++++++++++++++---
 lib/stream/multi.rb                            |  5 +++++
 public/javascripts/publisher.js                |  5 ++++-
 spec/lib/publisher_spec.rb                     |  9 +++++++--
 spec/lib/stream/multi_spec.rb                  | 18 ++++++++++++++++--
 8 files changed, 63 insertions(+), 13 deletions(-)

diff --git a/app/helpers/interim_stream_hackiness_helper.rb b/app/helpers/interim_stream_hackiness_helper.rb
index 9b57ddb7ab..14450f2339 100644
--- a/app/helpers/interim_stream_hackiness_helper.rb
+++ b/app/helpers/interim_stream_hackiness_helper.rb
@@ -11,11 +11,21 @@ module InterimStreamHackinessHelper
   end
 
   ##### These methods need to go away once we pass publisher object into the partial ######
-  def publisher_prefill_text
+  def publisher_formatted_text
     if params[:prefill].present?
       params[:prefill]
     elsif defined?(@stream)
-      @stream.publisher.prefill
+      @stream.publisher.text
+    else
+      nil
+    end
+  end
+
+  def publisher_hidden_text
+    if params[:prefill].present?
+      params[:prefill]
+    elsif defined?(@stream)
+      @stream.publisher.prefill 
     else
       nil
     end
diff --git a/app/views/shared/_publisher.html.haml b/app/views/shared/_publisher.html.haml
index 087a2a3450..20982f675b 100644
--- a/app/views/shared/_publisher.html.haml
+++ b/app/views/shared/_publisher.html.haml
@@ -56,9 +56,9 @@
           #publisher_textarea_wrapper
             = link_to( image_tag('deletelabel.png'), "#", :id => "hide_publisher", :title => t('.discard_post'))
             %ul#photodropzone
-            = status.text_area :fake_text, :rows => 2, :value => h(publisher_prefill_text), :tabindex => 1, :placeholder => t('.whats_on_your_mind'),
+            = status.text_area :fake_text, :rows => 2, :value => h(publisher_formatted_text), :tabindex => 1, :placeholder => t('.whats_on_your_mind'),
               :title => "1. #{t('shared.public_explain.share')}", 'data-content' => t('shared.public_explain.new_user_welcome_message')
-            = status.hidden_field :text, :value => '', :class => 'clear_on_submit'
+            = status.hidden_field :text, :value => h(publisher_hidden_text), :class => 'clear_on_submit'
 
             #file-upload{:title => t('.upload_photos')}
               = image_tag 'icons/camera.svg', :height => 14
diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml
index bd915ca2f9..8332f02ab0 100644
--- a/config/locales/diaspora/en.yml
+++ b/config/locales/diaspora/en.yml
@@ -742,7 +742,8 @@ en:
       discard_post: "Discard post"
       new_user_prefill:
         hello: "Hey everyone, I'm #%{new_user_tag}. "
-        i_like: "I'm interested in %{tags}."
+        i_like: "I'm interested in %{tags}. "
+        invited_by: "Thanks for the invite, "
     add_contact:
       enter_a_diaspora_username: "Enter a Diaspora username:"
       your_diaspora_username_is: "Your Diaspora username is: %{diaspora_handle}"
diff --git a/lib/publisher.rb b/lib/publisher.rb
index 68ba13a61a..2746935f07 100644
--- a/lib/publisher.rb
+++ b/lib/publisher.rb
@@ -3,10 +3,14 @@ class Publisher
 
   def initialize(user, opts={})
     self.user = user
-    self.open = (opts[:open] == true)? true : false
+    self.open = opts[:open]
     self.prefill = opts[:prefill]
-    self.public = (opts[:public] == true)? true : false
-    self.explain = (opts[:explain] == true)? true : false
+    self.public = opts[:public]
+    self.explain = opts[:explain]
+  end
+
+  def text
+    formatted_message
   end
 
   def open?
@@ -20,4 +24,12 @@ class Publisher
   def explain?
     self.explain
   end
+
+  private
+  def formatted_message
+    if self.prefill.present?
+      StatusMessage.new(:text => self.prefill).
+        format_mentions(self.prefill, :plain_text => true)
+    end
+  end
 end
diff --git a/lib/stream/multi.rb b/lib/stream/multi.rb
index ef8fa4a9d5..da513e649c 100644
--- a/lib/stream/multi.rb
+++ b/lib/stream/multi.rb
@@ -55,6 +55,11 @@ class Stream::Multi < Stream::Base
       prefill << I18n.t("shared.publisher.new_user_prefill.i_like", :tags => tag_string)
     end
 
+    if inviter = self.user.invited_by.try(:person)
+      prefill << I18n.t("shared.publisher.new_user_prefill.invited_by")
+      prefill << "@{#{inviter.name} ; #{inviter.diaspora_handle}}!"
+    end
+
     prefill
   end
 
diff --git a/public/javascripts/publisher.js b/public/javascripts/publisher.js
index abad86f9aa..0e0b8ff1e1 100644
--- a/public/javascripts/publisher.js
+++ b/public/javascripts/publisher.js
@@ -489,7 +489,10 @@ var Publisher = {
     });
 
     Publisher.autocompletion.initialize();
-    Publisher.hiddenInput().val(Publisher.input().val());
+
+    if(Publisher.hiddenInput().val() === "") {
+      Publisher.hiddenInput().val(Publisher.input().val());
+    }
     Publisher.input().autoResize();
     Publisher.input().keydown(Publisher.autocompletion.keyDownHandler);
     Publisher.input().keyup(Publisher.autocompletion.keyUpHandler);
diff --git a/spec/lib/publisher_spec.rb b/spec/lib/publisher_spec.rb
index 049c4fea90..ea2d84281c 100644
--- a/spec/lib/publisher_spec.rb
+++ b/spec/lib/publisher_spec.rb
@@ -9,8 +9,6 @@ describe Publisher do
     @publisher = Publisher.new(alice)
   end
 
-
-
   describe "#prefill" do
     it 'defaults to nothing' do
       @publisher.prefill.should be_blank
@@ -21,6 +19,13 @@ describe Publisher do
     end
   end
 
+  describe '#text' do
+    it 'is a formatted version of the prefill' do
+      p = Publisher.new(alice, :prefill => "@{ alice ; alice@pod.com }")
+
+      p.text.should == "alice"
+    end
+  end
 
   ["open", "public", "explain"].each do |property|
     describe "##{property}?" do
diff --git a/spec/lib/stream/multi_spec.rb b/spec/lib/stream/multi_spec.rb
index d60ffdd68a..f669d478d6 100644
--- a/spec/lib/stream/multi_spec.rb
+++ b/spec/lib/stream/multi_spec.rb
@@ -44,11 +44,25 @@ describe Stream::Multi do
     end
 
     it 'returns includes new user hashtag' do
-      @stream.send(:publisher_prefill).include?("#NewHere").should be_true
+      @stream.send(:publisher_prefill).should include("#NewHere")
     end
 
     it 'includes followed hashtags' do
-      @stream.send(:publisher_prefill).include?("#cats").should be_true
+      @stream.send(:publisher_prefill).should include("#cats")
+    end
+
+    context 'when invited by another user' do
+      before do
+        @user = Factory(:user, :invited_by => alice)
+        @inviter = alice.person
+
+        @stream = Stream::Multi.new(@user)
+      end
+
+      it 'includes a mention of the inviter' do
+        mention = "@{#{@inviter.name} ; #{@inviter.diaspora_handle}}"
+        @stream.send(:publisher_prefill).should include(mention)
+      end
     end
   end
 
-- 
GitLab