Class: Activity

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

Overview

Activities follow the Activity Streams standard.

Activities, Contacts and Audiences

Every activity is attached to a Contact, which defines the sender and the receiver of the Activity

Besides, the activity is attached to one or more relations, which define the audicence of the activity, the Actor that can reach it and their Permission

Wall

The Activity.wall(args) scope provides all the activities appearing in a wall

There are two types of wall, :home and :profile. Check Actor#wall for more information

Instance Method Summary collapse

Instance Method Details

#allow?(subject, action) ⇒ Boolean

Is subject allowed to perform action on this Activity?

Returns:

  • (Boolean)


217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'app/models/activity.rb', line 217

def allow?(subject, action)
  return false if contact.blank?

  case action
  when 'create'
    return false if contact.sender_id != Actor.normalize_id(subject)

    rels = Relation.normalize(relation_ids)

    own_rels     = rels.select{ |r| r.actor_id == contact.sender_id }
    foreign_rels = rels - own_rels

    # Only posting to own relations or allowed to post to foreign relations
    return foreign_rels.blank? && own_rels.present? ||
           foreign_rels.present? && Relation.allow(subject, 
                                                   action,
                                                   'activity',
                                                   :in => foreign_rels).
                                             all.size == foreign_rels.size

  when 'read'
    return true if relations.select{ |r| r.is_a?(Relation::Public) }.any?

    return false if subject.blank?

    return true if [contact.sender_id, contact.receiver_id].include?(Actor.normalize_id(subject))
  when 'update'
    return true if contact.sender_id == Actor.normalize_id(subject)
  when 'destroy'
    # We only allow destroying to sender and receiver by now
    return [contact.sender_id, contact.receiver_id].include?(Actor.normalize_id(subject))
  end

  Relation.
    allow?(subject, action, 'activity', :in => self.relation_ids, :public => false)
end

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

The Relation with which activity is shared



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'app/models/activity.rb', line 273

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

#commentsObject

The comments about this activity



141
142
143
# File 'app/models/activity.rb', line 141

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

#delete_object_by?(subject) ⇒ Boolean

Can subject delete the object of this activity?

Returns:

  • (Boolean)


255
256
257
258
259
260
261
# File 'app/models/activity.rb', line 255

def delete_object_by?(subject)
  subject.present? &&
  direct_object.present? &&
    ! direct_object.is_a?(Actor) &&
    ! direct_object.class.ancestors.include?(SocialStream::Models::Subject) &&
    allow?(subject, 'destroy')
end

#direct_activity_objectObject

The first activity object of this activity



173
174
175
# File 'app/models/activity.rb', line 173

def direct_activity_object
  activity_objects.first
end

#direct_objectObject

The first object of this activity



178
179
180
# File 'app/models/activity.rb', line 178

def direct_object
  direct_activity_object.try(:object)
end

#edit_object_by?(subject) ⇒ Boolean

Can subject edit the object of this activity?

Returns:

  • (Boolean)


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

def edit_object_by?(subject)
  subject.present? &&
  direct_object.present? &&
    ! direct_object.is_a?(Actor) &&
    ! direct_object.class.ancestors.include?(SocialStream::Models::Subject) &&
    allow?(subject, 'update')
end

#liked_by(user) ⇒ Object

:nodoc:



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

def liked_by(user) #:nodoc:
  likes.joins(:contact).merge(Contact.sent_by(user))
end

#liked_by?(user) ⇒ Boolean

Does user like this activity?

Returns:

  • (Boolean)


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

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

#likesObject

The ‘like’ qualifications emmited to this activities



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

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

#new_like(subject) ⇒ Object

Build a new children activity where subject like this



160
161
162
163
164
165
166
167
168
169
170
# File 'app/models/activity.rb', line 160

def new_like(subject)
  a = children.new :verb => "like",
                   :contact => subject.contact_to!(receiver),
                   :relation_ids => self.relation_ids

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

  a
end

#notificable?Boolean

Returns:

  • (Boolean)


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

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

#notifyObject



206
207
208
209
210
211
212
213
214
# File 'app/models/activity.rb', line 206

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

  if ['like','follow','make-friend','post','update'].include? verb and !contact.reflexive?
    receiver.notify(notification_subject, "Youre not supposed to see this", self)
  end
  true
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..)



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

def receiver
  contact.receiver
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.



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

def receiver_subject
  contact.receiver_subject
end

#senderObject

The Actor author of this activity

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



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

def sender
  contact.sender
end

#sender_subjectObject

The Subject author of this activity

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



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

def sender_subject
  contact.sender_subject
end

#title(view) ⇒ Object

The title for this activity in the stream



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

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
  else
    "Must define activity title"
  end.html_safe
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