Class: Activity
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Activity
- 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
-
#allow?(subject, action) ⇒ Boolean
Is subject allowed to perform action on this Activity?.
-
#audience_in_words(subject, options = {}) ⇒ Object
The Relation with which activity is shared.
-
#comments ⇒ Object
The comments about this activity.
-
#delete_object_by?(subject) ⇒ Boolean
Can subject delete the object of this activity?.
-
#direct_activity_object ⇒ Object
The first activity object of this activity.
-
#direct_object ⇒ Object
The first object of this activity.
-
#edit_object_by?(subject) ⇒ Boolean
Can subject edit the object of this activity?.
-
#liked_by(user) ⇒ Object
:nodoc:.
-
#liked_by?(user) ⇒ Boolean
Does user like this activity?.
-
#likes ⇒ Object
The ‘like’ qualifications emmited to this activities.
-
#new_like(subject) ⇒ Object
Build a new children activity where subject like this.
- #notificable? ⇒ Boolean
- #notify ⇒ Object
-
#receiver ⇒ Object
The wall where the activity is shown belongs to receiver.
-
#receiver_subject ⇒ Object
The wall where the activity is shown belongs to the receiver.
-
#sender ⇒ Object
The Actor author of this activity.
-
#sender_subject ⇒ Object
The Subject author of this activity.
-
#title(view) ⇒ Object
The title for this activity in the stream.
-
#verb ⇒ Object
The name of the verb of this activity.
-
#verb=(name) ⇒ Object
Set the name of the verb of this activity.
Instance Method Details
#allow?(subject, action) ⇒ Boolean
Is subject allowed to perform action on this Activity?
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, = {}) [: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 |
#comments ⇒ Object
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?
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_object ⇒ Object
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_object ⇒ Object
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?
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?
155 156 157 |
# File 'app/models/activity.rb', line 155 def liked_by?(user) liked_by(user).present? end |
#likes ⇒ Object
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
202 203 204 |
# File 'app/models/activity.rb', line 202 def notificable? is_root? or ['post','update'].include?(root.verb) end |
#notify ⇒ Object
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 |
#receiver ⇒ Object
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_subject ⇒ Object
136 137 138 |
# File 'app/models/activity.rb', line 136 def receiver_subject contact.receiver_subject end |
#sender ⇒ Object
112 113 114 |
# File 'app/models/activity.rb', line 112 def sender contact.sender end |
#sender_subject ⇒ Object
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 |
#verb ⇒ Object
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 |