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



307
308
309
310
311
312
313
314
# File 'app/models/activity.rb', line 307

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



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'app/models/activity.rb', line 317

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



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

def author_subject
  author.subject
end

#commentsObject

The comments about this activity



166
167
168
# File 'app/models/activity.rb', line 166

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

#direct_activity_objectObject

The first activity object of this activity



200
201
202
203
# File 'app/models/activity.rb', line 200

def direct_activity_object
  @direct_activity_object ||=
    activity_objects.first
end

#direct_objectObject

The first object of this activity



206
207
208
209
# File 'app/models/activity.rb', line 206

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

#liked_by(user) ⇒ Object

:nodoc:



175
176
177
# File 'app/models/activity.rb', line 175

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

#liked_by?(user) ⇒ Boolean

Does user like this activity?

Returns:

  • (Boolean)


180
181
182
# File 'app/models/activity.rb', line 180

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

#likesObject

The ‘like’ qualifications emmited to this activities



171
172
173
# File 'app/models/activity.rb', line 171

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



185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'app/models/activity.rb', line 185

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)


255
256
257
# File 'app/models/activity.rb', line 255

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

#notifyObject



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'app/models/activity.rb', line 259

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

  case
    when direct_object.is_a?(Comment)
      participants.each do |p|
        send_mail = p.subject.notification_settings[:someone_comments_on_my_post]
        p.notify(notification_subject, "Youre not supposed to see this", self, true, nil, send_mail) unless p == sender
      end
    when reflexive?
      return true
    when ['like'].include?(verb)
      send_mail = receiver.subject.notification_settings[:someone_likes_my_post]
      receiver.notify(notification_subject, "Youre not supposed to see this", self, true, nil, send_mail)
    when ['follow'].include?(verb)
      send_mail = receiver.subject.notification_settings[:someone_adds_me_as_a_contact]
      receiver.notify(notification_subject, "Youre not supposed to see this", self, true, nil, send_mail)
    when ['make-friend'].include?(verb)
      send_mail = receiver.subject.notification_settings[:someone_confirms_my_contact_request]
      receiver.notify(notification_subject, "Youre not supposed to see this", self, true, nil, send_mail)
    when ['post', 'update', 'join'].include?(verb)
      receiver.notify(notification_subject, "Youre not supposed to see this", self)
  end
  true
end

#owner_subjectObject

The subject owner



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

def owner_subject
  owner.subject
end

#participantsObject

A list of participants



287
288
289
290
291
292
293
# File 'app/models/activity.rb', line 287

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)


302
303
304
# File 'app/models/activity.rb', line 302

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..)



153
154
155
# File 'app/models/activity.rb', line 153

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.



161
162
163
# File 'app/models/activity.rb', line 161

def receiver_subject
  owner_subject
end

#reflexive?Boolean

Does this Activity have the same sender and receiver?

Returns:

  • (Boolean)


124
125
126
# File 'app/models/activity.rb', line 124

def reflexive?
  author_id == owner_id
end

#represented_author?Boolean

Is the author represented in this Activity?

Returns:

  • (Boolean)


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

def represented_author?
  author_id != user_author_id
end

#same_threadObject

This and related activities



296
297
298
299
# File 'app/models/activity.rb', line 296

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..)



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

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.



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

def sender_subject
  author_subject
end

#stream_contentObject

TODO: detailed description of activity



251
252
253
# File 'app/models/activity.rb', line 251

def stream_content
  stream_title
end

#stream_titleObject

Title for activity streams



237
238
239
240
241
242
243
244
245
246
247
248
# File 'app/models/activity.rb', line 237

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



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'app/models/activity.rb', line 212

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



119
120
121
# File 'app/models/activity.rb', line 119

def user_author_subject
  user_author.subject
end

#verbObject

The name of the verb of this activity



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

def verb
  activity_verb.name
end

#verb=(name) ⇒ Object

Set the name of the verb of this activity



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

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