Class: Activity

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
NotificationsHelper
Defined in:
app/models/activity.rb

Overview

Activities follow the Activity Streams standard.

Every Activity has an #author, #user_author and #owner

author

Is the subject that originated the activity. The entity that posted something, liked, etc..

user_author

The User logged in when the Activity was created. If the User has not changed the session to represent other entity (a Group for example), the user_author will be the same as the author.

owner

The subject whose wall was posted or comment was liked, etc..

Audiences and visibility

Each activity is attached to one or more relations, which define the subject that can reach the activity

In the case of a public relation everyone will be able to see the activity.

In the case of custom relations, only the subjects that have a Tie with that relation (in other words, the contacts that have been added as friends with that relation} will be able to reach the Activity

Instance Method Summary collapse

Methods included from NotificationsHelper

#description_of, #locale_as, #notification_url_of, #title_of

Instance Method Details

#audienceObject

The Actors this activity is shared with



278
279
280
281
282
283
284
285
# File 'app/models/activity.rb', line 278

def audience
  raise "Cannot get the audience of a public activity!" if public?

  [ author, user_author, owner ].uniq |
    Actor.
      joins(:received_ties).
      merge(Tie.where(:relation_id => relation_ids))
end

#audience_in_words(subject, options = {}) ⇒ Object

The Relation with which activity is shared



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'app/models/activity.rb', line 288

def audience_in_words(subject, options = {})
  options[:details] ||= :full

  public_relation = relations.select{ |r| r.is_a?(Relation::Public) }

  visibility, audience =
    if public_relation.present?
      [ :public, nil ]
    else
      visible_relations =
        relations.select{ |r| r.actor_id == Actor.normalize_id(subject)}

      if visible_relations.present?
        [ :visible, visible_relations.map(&:name).uniq.join(", ") ]
      else
        [ :hidden, relations.map(&:actor).map(&:name).uniq.join(", ") ]
      end
    end

  I18n.t "activity.audience.#{ visibility }.#{ options[:details] }", :audience => audience
end

#author_subjectObject

The subject author



93
94
95
# File 'app/models/activity.rb', line 93

def author_subject
  author.subject
end

#commentsObject

The comments about this activity



150
151
152
# File 'app/models/activity.rb', line 150

def comments
  children.includes(:activity_objects).where('activity_objects.object_type' => "Comment")
end

#direct_activity_objectObject

The first activity object of this activity



184
185
186
187
# File 'app/models/activity.rb', line 184

def direct_activity_object
  @direct_activity_object ||=
    activity_objects.first
end

#direct_objectObject

The first object of this activity



190
191
192
193
# File 'app/models/activity.rb', line 190

def direct_object
  @direct_object ||=
    direct_activity_object.try(:object)
end

#liked_by(user) ⇒ Object

:nodoc:



159
160
161
# File 'app/models/activity.rb', line 159

def liked_by(user) #:nodoc:
  likes.authored_by(user)
end

#liked_by?(user) ⇒ Boolean

Does user like this activity?

Returns:

  • (Boolean)


164
165
166
# File 'app/models/activity.rb', line 164

def liked_by?(user)
  liked_by(user).present?
end

#likesObject

The ‘like’ qualifications emmited to this activities



155
156
157
# File 'app/models/activity.rb', line 155

def likes
  children.joins(:activity_verb).where('activity_verbs.name' => "like")
end

#new_like(subject, user) ⇒ Object

Build a new children activity where subject like this



169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'app/models/activity.rb', line 169

def new_like(subject, user)
  a = children.new :verb           => "like",
                   :author_id      => Actor.normalize_id(subject),
                   :user_author_id => Actor.normalize_id(user),
                   :owner_id       => owner_id,
                   :relation_ids   => self.relation_ids

  if direct_activity_object.present?
    a.activity_objects << direct_activity_object
  end

  a
end

#notificable?Boolean

Returns:

  • (Boolean)


239
240
241
# File 'app/models/activity.rb', line 239

def notificable?
  is_root? or ['post','update'].include?(root.verb)
end

#notifyObject



243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'app/models/activity.rb', line 243

def notify
  return true unless notificable?
  #Avaible verbs: follow, like, make-friend, post, update, join

  if direct_object.is_a? Comment
    participants.each do |p|
      p.notify(notification_subject, "Youre not supposed to see this", self) unless p == sender
    end
  elsif ['like','follow','make-friend','post','update', 'join'].include? verb and !reflexive?
    receiver.notify(notification_subject, "Youre not supposed to see this", self)
  end
  true
end

#owner_subjectObject

The subject owner



98
99
100
# File 'app/models/activity.rb', line 98

def owner_subject
  owner.subject
end

#participantsObject

A list of participants



258
259
260
261
262
263
264
# File 'app/models/activity.rb', line 258

def participants
  parts=Set.new
  same_thread.map{|a| a.activity_objects.first}.each do |ao|
    parts << ao.author if ao.respond_to? :author and !ao.author.nil?
  end
  parts
end

#public?Boolean

Is this activity public?

Returns:

  • (Boolean)


273
274
275
# File 'app/models/activity.rb', line 273

def public?
  relation_ids.include? Relation::Public.instance.id
end

#receiverObject

The wall where the activity is shown belongs to receiver

This method provides the Actor. Use #receiver_subject for the Subject (User, Group, etc..)



137
138
139
# File 'app/models/activity.rb', line 137

def receiver
  owner
end

#receiver_subjectObject

The wall where the activity is shown belongs to the receiver

This method provides the Subject (User, Group, etc…). Use #receiver for the Actor.



145
146
147
# File 'app/models/activity.rb', line 145

def receiver_subject
  owner_subject
end

#reflexive?Boolean

Does this Activity have the same sender and receiver?

Returns:

  • (Boolean)


108
109
110
# File 'app/models/activity.rb', line 108

def reflexive?
  author_id == owner_id
end

#represented_author?Boolean

Is the author represented in this Activity?

Returns:

  • (Boolean)


113
114
115
# File 'app/models/activity.rb', line 113

def represented_author?
  author_id != user_author_id
end

#same_threadObject

This and related activities



267
268
269
270
# File 'app/models/activity.rb', line 267

def same_thread
  return [self] if is_root?
  [parent] + siblings
end

#senderObject

The Actor author of this activity

This method provides the Actor. Use #sender_subject for the Subject (User, Group, etc..)



121
122
123
# File 'app/models/activity.rb', line 121

def sender
  author
end

#sender_subjectObject

The Subject author of this activity

This method provides the Subject (User, Group, etc…). Use #sender for the Actor.



129
130
131
# File 'app/models/activity.rb', line 129

def sender_subject
  author_subject
end

#stream_contentObject

TODO: detailed description of activity



235
236
237
# File 'app/models/activity.rb', line 235

def stream_content
  stream_title
end

#stream_titleObject

Title for activity streams



221
222
223
224
225
226
227
228
229
230
231
232
# File 'app/models/activity.rb', line 221

def stream_title
  # FIXMEEEEEEEEEEEEEEEEE
  object = ( direct_object.present? ? 
             ( direct_object.is_a?(SocialStream::Models::Subject) ? 
               direct_object.name :
               direct_object.title ) :
             receiver.name )

  I18n.t "activity.stream.title.#{ verb }",
         :author => sender_subject.name,
         :activity_object => object
end

#title(view) ⇒ Object

The title for this activity in the stream



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'app/models/activity.rb', line 196

def title view
  case verb
  when "follow", "make-friend", "like"
    I18n.t "activity.verb.#{ verb }.#{ receiver.subject_type }.title",
    :subject => view.link_name(sender_subject),
    :contact => view.link_name(receiver_subject)
  when "post", "update"
    if sender == receiver
      view.link_name sender_subject
    else
      I18n.t "activity.verb.post.title.other_wall",
             :sender => view.link_name(sender_subject),
             :receiver => view.link_name(receiver_subject)
    end
  when 'join'
    I18n.t('notification.join.one', 
          :sender => view.link_name(sender_subject),
          :thing => I18n.t(direct_object.class.to_s.underscore+'.one'),
          :title => title_of(direct_object))
  else
    "Must define activity title"
  end.html_safe
end

#user_author_subjectObject

The subject user actor



103
104
105
# File 'app/models/activity.rb', line 103

def user_author_subject
  user_author.subject
end

#verbObject

The name of the verb of this activity



83
84
85
# File 'app/models/activity.rb', line 83

def verb
  activity_verb.name
end

#verb=(name) ⇒ Object

Set the name of the verb of this activity



88
89
90
# File 'app/models/activity.rb', line 88

def verb=(name)
  self.activity_verb = ActivityVerb[name]
end