From 040a82a7ab69775014b5b2b36d0db40cea1e6042 Mon Sep 17 00:00:00 2001 From: Antonio Tapiador del Dujo Date: Wed, 30 Mar 2011 16:34:32 +0200 Subject: [PATCH] Refactoring of activities in stream --- app/helpers/subjects_helper.rb | 5 ++ app/models/activity.rb | 70 ++++++++++++++++------- app/models/activity_verb.rb | 2 +- app/models/tie.rb | 40 ++++++------- app/views/activities/_options.html.erb | 4 +- app/views/activities/_root.html.erb | 4 +- app/views/subjects/_toolbar_logo.html.erb | 2 +- app/views/ties/_new.html.erb | 6 -- config/locales/en.yml | 10 ++-- 9 files changed, 86 insertions(+), 57 deletions(-) diff --git a/app/helpers/subjects_helper.rb b/app/helpers/subjects_helper.rb index 68c9d888..f18ada80 100644 --- a/app/helpers/subjects_helper.rb +++ b/app/helpers/subjects_helper.rb @@ -1,4 +1,9 @@ module SubjectsHelper + # Return a link to this subject with the name + def link_name(subject, options = {}) + link_to subject.name, subject, options + end + # Define the toolbar content for your view. There are two typical cases, depending on the value of # options[:profile] # * If present, render the profile menu for the {SocialStream::Models::Subject subject} diff --git a/app/models/activity.rb b/app/models/activity.rb index 31885917..2cb40882 100644 --- a/app/models/activity.rb +++ b/app/models/activity.rb @@ -10,6 +10,12 @@ # There are two types of wall, :home and :profile. Check {Actor#wall} for more information # class Activity < ActiveRecord::Base + # The direction of the sender and receiver subjects compared with the attached tie + SUBJECT_DIRECTIONS = { + :direct => %w(follow make-friend), + :inverse => %w(like post update) + } + has_ancestry belongs_to :activity_verb @@ -59,32 +65,36 @@ def verb=(name) self.activity_verb = ActivityVerb[name] end - # The author of the activity is the receiver of the tie + # The {Actor} author of this activity # - # This method provides the actor. Use sender_subject for the subject (user, group, etc..) + # This method provides the {Actor}. Use {#sender_subject} for the {SocialStream::Models::Subject Subject} + # ({User}, {Group}, etc..) def sender - tie.receiver + direct? ? tie.sender : tie.receiver end - # The author of the activity is the receiver of the tie + # The {SocialStream::Models::Subject Subject} author of this activity # - # This method provides the subject (user, group, etc...). Use sender for the actor. + # This method provides the {SocialStream::Models::Subject Subject} ({User}, {Group}, etc...). + # Use {#sender} for the {Actor}. def sender_subject - tie.receiver_subject + direct? ? tie.sender_subject : tie.receiver_subject end - # The wall where the activity is shown belongs to the sender of the tie + # The wall where the activity is shown belongs to receiver # - # This method provides the actor. Use sender_subject for the subject (user, group, etc..) + # This method provides the {Actor}. Use {#receiver_subject} for the {SocialStream::Models::Subject Subject} + # ({User}, {Group}, etc..) def receiver - tie.sender + direct? ? tie.receiver : tie.sender end - # The wall where the activity is shown belongs to the sender of the tie + # The wall where the activity is shown belongs to the receiver # - # This method provides the subject (user, group, etc...). Use sender for the actor. + # This method provides the {SocialStream::Models::Subject Subject} ({User}, {Group}, etc...). + # Use {#receiver} for the {Actor}. def receiver_subject - tie.sender_subject + direct? ? tie.receiver_subject : tie.sender_subject end # The comments about this activity @@ -123,14 +133,16 @@ def direct_object direct_activity_object.try(:object) end - def title - if activity_verb == ActivityVerb["start_following"] - return (I18n.t "activity.verb.start_following", :active=>tie.sender.name , :pasive=>tie.receiver.name) - elsif activity_verb == ActivityVerb["make_friend"] - return (I18n.t "activity.verb.make_friend", :active=>tie.sender.name , :pasive=>tie.receiver.name) - elsif activity_verb == ActivityVerb["post"] - return tie.sender_subject.name - end + # The title for this activity in the stream + def title view + case verb + when "follow", "make-friend" + I18n.t "activity.verb.#{ verb }.title", + :subject => view.link_name(sender_subject), + :contact => view.link_name(receiver_subject) + when "post" + view.link_name sender_subject + end.html_safe end private @@ -157,4 +169,22 @@ def like_direct_object activity_objects << parent.direct_activity_object end + + private + + # The {#sender} and the {#receiver} of the {Activity} depends on its {ActivityVerb} + # + # Contact activities such as make-friend or follow are direct. They have the same sender + # and receiver as their tie. On the other hand, post activities are inverse. The sender of + # the activity is the receiver of the tie and the receiver of the activity is the sender of + # the tie + def direct? + if SUBJECT_DIRECTIONS[:direct].include?(verb) + true + elsif SUBJECT_DIRECTIONS[:inverse].include?(verb) + false + else + raise "Unknown direction for verb #{ verb }" + end + end end diff --git a/app/models/activity_verb.rb b/app/models/activity_verb.rb index 80b307b7..e99134e4 100644 --- a/app/models/activity_verb.rb +++ b/app/models/activity_verb.rb @@ -1,6 +1,6 @@ class ActivityVerb < ActiveRecord::Base # Activity Strems verbs - Available = %w(start_following like make_friend update post) + Available = %w(follow like make-friend post update) validates_uniqueness_of :name diff --git a/app/models/tie.rb b/app/models/tie.rb index a6cae036..41d01b0c 100644 --- a/app/models/tie.rb +++ b/app/models/tie.rb @@ -102,7 +102,7 @@ class Tie < ActiveRecord::Base before_validation :find_relation after_create :complete_weak_set - after_create :create_activity_after_add_contact + after_create :create_activity after_create :send_message def relation_name @@ -127,6 +127,11 @@ def reflexive? sender_id == receiver_id end + # Is there any tie from receiver to sender? + def replied? + receiver.ties_to?(sender) + end + # The set of ties between sender and receiver # # Options:: @@ -254,29 +259,20 @@ def complete_weak_set end end - - def create_activity_after_add_contact - if self.original - if isBidirectional - a = Activity.create :_tie => self, :activity_verb => ActivityVerb["make_friend"] - else - a = Activity.create :_tie => self, :activity_verb => ActivityVerb["start_following"] - end - end - end - - def isBidirectional - return self.receiver.ties_to?(self.sender) + def create_activity + return if ! original? || reflexive? + + Activity.create! :_tie => self, :activity_verb => ActivityVerb[contact_verb] end - - # Values of "receiver.subject_type": "User", "Group" + + # Send a message to the receiver of the tie def send_message - if self.original - if((message!=nil)&&(message!="")&&(receiver.subject_type=="User")) - sender.send_message(receiver, message,(I18n.t "activity.verb.start_following_message", :name=>sender.name)) - end + if original? && message.present? + sender.send_message(receiver, message, I18n.t("activity.verb.#{ contact_verb }.message", :name => sender.name)) end end - - + + def contact_verb + replied? ? "make-friend" : "follow" + end end diff --git a/app/views/activities/_options.html.erb b/app/views/activities/_options.html.erb index aa0298bf..50979f12 100644 --- a/app/views/activities/_options.html.erb +++ b/app/views/activities/_options.html.erb @@ -5,6 +5,8 @@
  • · <%= link_to t('activity.to_comment'), "#", :class => "to_comment" %>
  • <% end %>
  • · <%= link_like(activity)%>
  • -
  • · <%= link_to t('activity.delete'), activity.direct_object , :confirm => t('confirm_delete', :scope => activity.direct_object.class.to_s.underscore), :method => :delete, :remote => true %>
  • + <% if activity.direct_object.present? %> +
  • · <%= link_to t('activity.delete'), activity.direct_object , :confirm => t('confirm_delete', :scope => activity.direct_object.class.to_s.underscore), :method => :delete, :remote => true %>
  • + <% end %> diff --git a/app/views/activities/_root.html.erb b/app/views/activities/_root.html.erb index c8fae96a..4463e6b4 100644 --- a/app/views/activities/_root.html.erb +++ b/app/views/activities/_root.html.erb @@ -9,7 +9,7 @@
    - <%= link_to(activity.title, activity.sender_subject) %> + <%= activity.title(self) %>
    @@ -36,4 +36,4 @@
    <%end%>
    -<% end %> \ No newline at end of file +<% end %> diff --git a/app/views/subjects/_toolbar_logo.html.erb b/app/views/subjects/_toolbar_logo.html.erb index e56e5559..003f26af 100644 --- a/app/views/subjects/_toolbar_logo.html.erb +++ b/app/views/subjects/_toolbar_logo.html.erb @@ -1,4 +1,4 @@
    -
    <%= subject.name %> (<%= ties_to(subject) %>)
    +
    <%= subject.name %>
    <%= link_to image_tag(subject.logo.url, :alt => subject.name , :size => "119x108"), subject %>
    diff --git a/app/views/ties/_new.html.erb b/app/views/ties/_new.html.erb index eb4aeb06..7c73b2e2 100644 --- a/app/views/ties/_new.html.erb +++ b/app/views/ties/_new.html.erb @@ -1,11 +1,5 @@
    -
    <%= t('message.one')+":" %>
    diff --git a/config/locales/en.yml b/config/locales/en.yml index 77820a23..2aa8df30 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -15,10 +15,12 @@ en: to_comment: "Comment" unlike: "Already I do not like" verb: - start_following: %{active} add %{pasive} as a contact. - start_following_message: %{name} add you as a contact. - make_friend: %{active} add %{pasive} are now friends. - make_friend_message: %{name} are now your friend. + follow: + title: "%{subject} added %{contact} as contact." + message: "%{name} added you as contact." + make-friend: + title: "%{subject} and %{contact} are now connected." + message: "%{name} also added you as contact." avatar: crop_image: "Crop the uploaded image" crop_submit: "Crop"