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)


207
208
209
210
211
212
213
214
215
216
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
# File 'app/models/activity.rb', line 207

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

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

    val = 
      if relation_ids.present?
        rels = Relation.normalize(relation_ids)

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

        # Only posting to own relations
        foreign_rels.blank? ||
          Relation.
            allow(subject, action, 'activity', :in => foreign_rels).all.size == foreign_rels.size
      else
        contact.reflexive? ||
          receiver.
            relation_customs.allow(sender, 'create', 'activity').
            present?
      end

    return val

  when 'read'
    return true if [contact.sender_id, contact.receiver_id].include?(Actor.normalize_id(subject)) || relations.select{ |r| r.is_a?(Relation::Public) }.any?
  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

#commentsObject

The comments about this activity



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

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)


247
248
249
250
251
252
# File 'app/models/activity.rb', line 247

def delete_object_by?(subject)
  subject.present? &&
  direct_object.present? &&
    ! direct_object.is_a?(Actor) &&
    allow?(subject, 'destroy')
end

#direct_activity_objectObject

The first activity object of this activity



168
169
170
# File 'app/models/activity.rb', line 168

def direct_activity_object
  activity_objects.first
end

#direct_objectObject

The first object of this activity



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

def direct_object
  direct_activity_object.try(:object)
end

#liked_by(user) ⇒ Object

:nodoc:



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

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)


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

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

#likesObject

The ‘like’ qualifications emmited to this activities



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

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



155
156
157
158
159
160
161
162
163
164
165
# File 'app/models/activity.rb', line 155

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

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

  a
end

#notificable?Boolean

Returns:

  • (Boolean)


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

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

#notifyObject



193
194
195
196
197
198
199
200
201
202
203
204
# File 'app/models/activity.rb', line 193

def notify
  return true if !notificable?
  #Avaible verbs: follow, like, make-friend, post, update
  actionview = ActivitiesController.new.view_context

  if ['like','follow','make-friend','post','update'].include? verb and !contact.reflexive?
    notification_subject = actionview.render :partial => 'notifications/activities/' + verb + "_subject", :locals => {:activity => self}
    notification_body = actionview.render :partial =>  'notifications/activities/' + verb + "_body", :locals => {:activity => self}
    receiver.notify(notification_subject, notification_body, 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..)



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

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.



131
132
133
# File 'app/models/activity.rb', line 131

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



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

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.



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

def sender_subject
  contact.sender_subject
end

#title(view) ⇒ Object

The title for this activity in the stream



178
179
180
181
182
183
184
185
186
187
# File 'app/models/activity.rb', line 178

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"
    view.link_name sender_subject
  end.html_safe
end

#verbObject

The name of the verb of this activity



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

def verb
  activity_verb.name
end

#verb=(name) ⇒ Object

Set the name of the verb of this activity



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

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