/
tie.rb
278 lines (233 loc) · 8.91 KB
/
tie.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# A tie is a link between two {Actor actors} with a {Relation relation}.
#
# The first {Actor actor} is the sender of the {Tie tie}. The second {Actor actor}
# is the receiver of the {Tie tie}.
#
# = {Tie Ties} and {Activity activities}
# {Activity Activities} are attached to {Tie ties}.
# * The sender of the tie is the target of the {Activity}.
# The wall-profile of an {Actor} is composed by the resources assigned to
# the {Tie Ties} in which the {Actor} is the sender.
# * The receiver {Actor} of the {Tie} is the author of the {Activity}.
# It is the user that uploads a resource to the website or the social entity that
# originates the {Activity}.
# * The {Relation} sets up the mode in which the {Activity} is shared.
# It sets the rules, or {Permission Permissions}, by which {Actor} have access
# to the {Activity}.
#
# = Tie strengh
#
# Because each {Tie} belongs to a {Relation} and {Relation Relations} have strength
# hierarchies, {Tie Ties} also have them. A {Tie} is stronger than other if its
# {Relation} is stronger than the other's. For example, if _Alice_ has a _friend_ {Tie}
# with _Bob_, and an _acquaintance_ {Tie} with _Charlie_, given that _friend_ {Relation}
# is stronger than _acquaintance_, the {Tie} with _Bob_ is stronger than the {Tie} with
# _Charlie_.
#
# = Authorization
# When an {Actor} establishes a {Tie} with other, she is granting a set of
# {Permission Permissions} to them (posting to her wall, reading her posts, etc..)
# The set of {Permission Permissions} granted are associated with the {Relation} of
# the {Tie}.
#
# Usually, stronger ties (and relations) have more permissions than weaker ones.
#
# = Scopes
# There are several scopes defined:
#
# original:: ties created by actors. It excludes ties created in the weaker set.
# sent_by(actor):: ties whose sender is actor
# received_by(actor):: ties whose receiver is actor
# sent_or_received_by(actor):: the union of the former
# related_by(relation):: ties with this relation. Accepts relation, relation_name,
# integer, array
# replied:: ties having at least another tie in the other way, a tie from a to b
# is replied if there is a tie from b to a
#
class Tie < ActiveRecord::Base
attr_accessor :message
# Facilitates relation assigment along with find_relation callback
attr_writer :relation_name
belongs_to :sender,
:class_name => "Actor",
:include => SocialStream.subjects
belongs_to :receiver,
:class_name => "Actor",
:include => SocialStream.subjects
belongs_to :relation
has_many :tie_activities, :dependent => :destroy
has_many :activities, :through => :tie_activities
scope :original, where(:original => true)
scope :recent, order("#{ quoted_table_name }.created_at DESC")
scope :sent_by, lambda { |a|
where(:sender_id => Actor.normalize_id(a))
}
scope :received_by, lambda { |a|
where(:receiver_id => Actor.normalize_id(a))
}
scope :sent_or_received_by, lambda { |a|
where(arel_table[:sender_id].eq(Actor.normalize_id(a)).
or(arel_table[:receiver_id].eq(Actor.normalize_id(a))))
}
scope :related_by, lambda { |r|
if r.present?
where(:relation_id => Relation.normalize_id(r))
end
}
scope :replied, lambda {
select("DISTINCT ties.*").
from("ties, ties as ties_2").
where("ties.sender_id = ties_2.receiver_id AND ties.receiver_id = ties_2.sender_id")
}
scope :following, lambda { |a|
where(:receiver_id => Actor.normalize_id(a)).
joins(:relation => :permissions).merge(Permission.follow)
}
validates_presence_of :sender_id, :receiver_id, :relation_id
before_validation :find_relation
after_create :complete_weak_set
after_create :create_activity
after_create :send_message
def relation_name
@relation_name || relation.try(:name)
end
def relation!
relation ||
find_relation
end
def sender_subject
sender.try(:subject)
end
def receiver_subject
receiver.try(:subject)
end
# Does this tie have the same sender and receiver?
def reflexive?
sender_id == receiver_id
end
# Is there any tie from receiver to sender?
def replied?
receiver.ties_to?(sender)
end
# The set of ties between sender and receiver
#
# Options::
# * relations: Only ties with relations
def relation_set(options = {})
set = self.class.where(:sender_id => sender_id,
:receiver_id => receiver_id)
if options.key?(:relations)
set =
set.related_by Relation.normalize_id(options[:relations],
:sender => sender)
end
set
end
# The tie with relation r inside this relation_set
def related(r)
relation_set(:relations => r).first
end
# = Access Control
#
# Access control enforcement in ties come from the permissions assigned to other ties through relations.
# The access_set is the set of ties that grant some permission on a particular tie.
#
# Enforcing access control on activities and ties are a matter of finding its access set.
# There are two approaches for this, checking the permissions on particular tie or finding all the ties
# granted some permission.
#
# == Particular tie
# ------------------ ------------------
# | particular tie |--------| access_set |
# | t | | ties |
# ------------------ ------------------
#
# Because t is given, the scopes are applied to the ties table
# We get the set of ties that allow permission on t
#
# == Finding ties
# ------------------ join ------------------
# | finding ties |--------| access_set |
# | ties | | ties_as |
# ------------------ ------------------
#
# Because we want to find ties, an additional join table (ties_as) is needed for applying access set conditions
# We get the set of ties that are allowing certain permission
#
scope :with_permissions, lambda { |action, object|
joins(:relation => :permissions).
where('permissions.action' => action).
where('permissions.object' => object)
}
# Given a given permission (action, object), the access_set are the set of ties that grant that permission.
scope :access_set, lambda { |tie, action, object|
with_permissions(action, object).
where(Permission.parameter_conditions(tie))
}
scope :allowing_set, lambda { |action, object|
query =
select("DISTINCT ties.*").
from("ties INNER JOIN relations ON relations.id = ties.relation_id, ties as ties_as INNER JOIN relations AS relations_as ON relations_as.id = ties_as.relation_id INNER JOIN relation_permissions ON relations_as.id = relation_permissions.relation_id INNER JOIN permissions ON permissions.id = relation_permissions.permission_id").
where("permissions.action = ?", action).
where("permissions.object = ?", object)
conds = Permission.parameter_conditions
# FIXME: Patch to support public activities
if action == 'read' && object == 'activity'
conds = sanitize_sql([ "( #{ conds } ) OR relations.name = ?", "public" ])
end
query.where(conds)
}
scope :allowing, lambda { |actor, action, object|
allowing_set(action, object).
where("ties_as.receiver_id" => Actor.normalize_id(actor))
}
# The set of ties that permit this tie allow to allow action on object
def access_set(action, object)
self.class.access_set(self, action, object)
end
# The set of ties allowing user to perform action on object
def allowing(user, action, object)
access_set(action, object).received_by(user)
end
# Does this tie allows user to perform action on object?
def allows?(user, action, object)
# FIXME: Patch to support public activities
return true if relation.name == 'public' && action == 'read' && object == 'activity'
allowing(user, action, object).any?
end
private
# Before validation callback
# Infers relation from its name and the type of the actors
def find_relation
if relation_name.present? &&
relation_name != relation.try(:name) &&
sender.present?
self.relation = sender.relation(relation_name)
end
end
# After create callback
# Creates ties with weaker relations in the strength hierarchy of this tie
def complete_weak_set
return if reflexive?
relation.weaker.each do |r|
if relation_set(:relations => r).blank?
t = relation_set.build :relation => r,
:original => false
t.save!
end
end
end
def create_activity
return if ! original? || reflexive?
Activity.create! :_tie => self, :activity_verb => ActivityVerb[contact_verb]
end
# Send a message to the receiver of the tie
def send_message
if original? && message.present?
sender.send_message(receiver, message, I18n.t("activity.verb.#{ contact_verb }.message", :name => sender.name))
end
end
def contact_verb
replied? ? "make-friend" : "follow"
end
end