Class: Actor

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
SocialStream::Models::Supertype
Defined in:
app/models/actor.rb

Overview

An actor is a social entity. This includes individuals, but also groups, departments, organizations even nations or states. Actors are linked by ties.

Constant Summary collapse

SuggestedRelations =

This is an scaffold for a recomendations engine

{
  'User'  => 'friend_request',
  'Group' => 'follower'
}

Instance Method Summary collapse

Instance Method Details

#active_ties_to(subject) ⇒ Object

All the ties this actor has with subject that support activities



142
143
144
# File 'app/models/actor.rb', line 142

def active_ties_to(subject)
  sent_ties.received_by(subject).active
end

#pending_tiesObject



146
147
148
149
150
151
152
153
154
155
# File 'app/models/actor.rb', line 146

def pending_ties
  #TODO: optimize by SQL
  @pending_ties ||=
    received_ties.pending.
      select{ |t| ! receivers.include?(t.sender) }.
      map{ |u| Tie.new :sender_id => u.receiver_id,
                       :receiver_id => u.sender_id,
                       :relation_id => u.relation.granted_id
      }
end

#receiver_subjects(subject_type, options = {}) ⇒ Object

All the subject actors of class subject_type that receive at least one tie from this actor

Options
  • relations: Restrict the relations of considered ties

  • include_self: False by default, don’t include this actor as subject even they

have ties with themselves.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/models/actor.rb', line 77

def receiver_subjects(subject_type, options = {})
  # FIXME: DRY!
  subject_class = subject_type.to_s.classify.constantize

  cs = subject_class.
         select("DISTINCT #{ subject_class.quoted_table_name }.*").
         with_received_ties &
         Tie.sent_by(self)

  if options[:include_self].blank?
    cs = cs.where("#{ self.class.quoted_table_name }.id != ?", self.id)
  end

  if options[:relations].present?
    cs &=
      Tie.related_by(Tie.Relation(options[:relations], :mode => [ subject.class, subject_class ]))
  end

  cs
end

#sender_subjects(subject_type, options = {}) ⇒ Object

All the subject actors of class subject_type that send at least one tie to this actor

Options
  • relations: Restrict the relations of considered ties

  • include_self: False by default, don’t include this actor as subject even they

have ties with themselves.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/models/actor.rb', line 49

def sender_subjects(subject_type, options = {})
  # FIXME: DRY!
  subject_class = subject_type.to_s.classify.constantize

  cs = subject_class.
         select("DISTINCT #{ subject_class.quoted_table_name }.*").
         with_sent_ties &
         Tie.received_by(self)

  if options[:include_self].blank?
    cs = cs.where("#{ self.class.quoted_table_name }.id != ?", self.id)
  end

  if options[:relations].present?
    cs &=
      Tie.related_by(Tie.Relation(options[:relations], :mode => [ subject_class, self.subject.class ]))
  end

  cs
end

#subjectObject

The subject instance for this actor



32
33
34
35
# File 'app/models/actor.rb', line 32

def subject
  subtype_instance ||
    activity_object.try(:object)
end

#suggestion(options = {}) ⇒ Tie

By now, it returns a tie suggesting a relation from SuggestedRelations to another subject without any current relation

Options
  • type: the class of the recommended subject

Returns:



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/models/actor.rb', line 118

def suggestion(options = {})
  candidates_types = options[:type].present? ?
    Array(options[:type].to_s.classify) :
    SuggestedRelations.keys

  candidates_classes = candidates_types.map(&:constantize)
  
  # Candidates are all the instance of "type" minus all the subjects
  # that are receiving any tie from this actor
  candidates = candidates_classes.inject([]) do |cs, klass|
    cs += klass.all - receiver_subjects(klass)
    cs -= Array(subject) if subject.is_a?(klass)
    cs
  end

  candidate = candidates[rand(candidates.size)]

  return nil unless candidate.present?

  sent_ties.build :receiver_id => candidate,
                  :relation => Relation.mode(subject_type, candidate.class).find_by_name(SuggestedRelations[candidate.class.to_s])
end

#suggestions(n) ⇒ Object

Make n suggestions TODO: make more



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

def suggestions(n)
  n.times.map{ |m| suggestion }
end

#tiesObject

All the ties sent or received by this actor



38
39
40
# File 'app/models/actor.rb', line 38

def ties
  Tie.sent_or_received_by(self)
end

#wallObject

The set of activities in the wall of this actor, includes all the activities from the ties the actor has access to

TODO: ties, authorization



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

def wall
  Activity.wall ties
end

#wall_profileObject

The set of activities in the wall profile of this actor, includes the activities from the ties of this actor TODO: authorization



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

def wall_profile
  Activity.wall ties
end