Module: Tekeya::Entity

Extended by:
ActiveSupport::Autoload, ActiveSupport::Concern
Included in:
Group
Defined in:
lib/tekeya/entity.rb,
lib/tekeya.rb,
lib/tekeya/entity/group.rb

Overview

Represents a tekeya entity, the main building block of the engine

Defined Under Namespace

Modules: ClassMethods, Group

Instance Method Summary collapse

Instance Method Details

#block(entity) ⇒ Boolean

Blocks the given entity and removes any tracking relation between both entities

Parameters:

  • entity (Entity)

    the entity to block

Returns:

  • (Boolean)


203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/tekeya/entity.rb', line 203

def block(entity)
  run_callbacks :block_entity do
    check_if_tekeya_entity(entity)
    raise ::Tekeya::Errors::TekeyaRelationAlreadyExists.new("Already blocking #{entity}") if self.blocks?(entity)

    unless entity.is_tekeya_group?
      self.untrack(entity) if self.tracks?(entity)
      entity.untrack(self) if entity.tracks?(self)
    end

    add_tekeya_relation(self, entity, :blocks)
  end
end

#blocked(type = nil) ⇒ Array

Returns a list of entities blocked by this entity

Parameters:

  • type (String) (defaults to: nil)

    used to return a certain type of entities blocked

Returns:

  • (Array)

    the entities blocked by this entity



221
222
223
# File 'lib/tekeya/entity.rb', line 221

def blocked(type = nil)
  tekeya_relations_of(self, :blocks, type)
end

#blocks?(entity) ⇒ Boolean

Checks if this entity is blocking the given entity

Parameters:

  • entity (Entity)

    the entity to check

Returns:

  • (Boolean)

    true if this entity is blocking the given entity, false otherwise



229
230
231
232
# File 'lib/tekeya/entity.rb', line 229

def blocks?(entity)
  check_if_tekeya_entity(entity)
  tekeya_relation_exists?(self, entity, :blocks)
end

#feed(&blck) ⇒ Array

Returns the entity’s feed

Returns:

  • (Array)

    the list of activities for the entities tracked by this entity



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/tekeya/entity.rb', line 340

def feed(&blck)
  acts = []
  fkey = self.feed_key
  recent_activities_count = ::Tekeya.redis.zcard(fkey)
  
  # Check if the cache is not empty
  if recent_activities_count > 0 && !block_given?
    # Retrieve the aggregate keys from redis
    acts_keys = ::Tekeya.redis.zrevrange(fkey, 0, -1)
    # Retrieve the aggregates
    acts_keys.each do |act_key|
      # Make `from_redis` only hit the db if author != entity
      key_components = act_key.split(':')
      actor = if key_components[4] == self.class.to_s && key_components[5] == self.entity_primary_key
        self
      end

      acts << ::Tekeya::Feed::Activity::Item.from_redis(act_key, actor)
    end
  else
    # Retrieve the activities from the DB
    (self.tracking + [self]).each do |tracker|
      db_recent_activities = tracker.activities.recent(&blck)
      db_recent_activities.each do |activity|
        acts << ::Tekeya::Feed::Activity::Item.from_db(activity, tracker)
      end
    end
  end

  return acts
end

#groups(type = nil) ⇒ Array

Return a list of groups joined by this entity

Parameters:

  • type (String) (defaults to: nil)

    used to return a certain type of groups joined

Returns:

  • (Array)

    the groups joined by this entity



275
276
277
# File 'lib/tekeya/entity.rb', line 275

def groups(type = nil)
  tekeya_relations_of(self, :joins, type)
end

#is_tekeya_entity?Boolean

A method to identify the entity

Returns:

  • (Boolean)

    true



387
388
389
# File 'lib/tekeya/entity.rb', line 387

def is_tekeya_entity?
  return true
end

#is_tekeya_group?Boolean

A method to identify the entity as a non group

Returns:

  • (Boolean)

    false



394
395
396
# File 'lib/tekeya/entity.rb', line 394

def is_tekeya_group?
  return false
end

#join(group, track_also = true, notify = true) ⇒ Boolean

Note:

will automatically track the group

Joins the given group and tracks it

Parameters:

  • group (Group)

    the group to track

  • track_also (Boolean) (defaults to: true)

    if set to false automatic tracking is disabled

  • notify (Boolean) (defaults to: true)

    determines whether the joined group’s owner should be notified

Returns:

  • (Boolean)


254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/tekeya/entity.rb', line 254

def join(group, track_also = true, notify=true)
  run_callbacks :join_group do
    check_if_tekeya_group(group)
    raise ::Tekeya::Errors::TekeyaRelationAlreadyExists.new("Already a member of #{group}") if self.member_of?(group)

    ret = add_tekeya_relation(self, group, :joins)
    ret &= self.track(group, false) if track_also && !self.tracks?(group) && ret
    
    if ret
      activity = self.activities.joined(group)
      group.owner.notifications.joined_by self, subject: group if notify
    end

    return ret
  end
end

#leave(group) ⇒ Boolean

Leaves the given group and untracks it

Parameters:

  • group (Group)

    the group to untrack

Returns:

  • (Boolean)


292
293
294
295
296
297
298
299
300
301
302
# File 'lib/tekeya/entity.rb', line 292

def leave(group)
  run_callbacks :leave_group do
    check_if_tekeya_group(group)
    raise ::Tekeya::Errors::TekeyaRelationNonExistent.new("Can't leave an unjoined group") unless self.member_of?(group)

    ret = delete_tekeya_relation(self, group, :joins)
    ret &= self.untrack(group) if self.tracks?(group) && ret

    return ret
  end
end

#member_of?(group) ⇒ Boolean

Checks if this entity is a member of the given group

Parameters:

  • group (Group)

    the group to check

Returns:

  • (Boolean)

    true if this entity is a member of the given group, false otherwise



283
284
285
286
# File 'lib/tekeya/entity.rb', line 283

def member_of?(group)
  check_if_tekeya_group(group)
  tekeya_relation_exists?(self, group, :joins)
end

#profile_feed(&blck) ⇒ Array

Returns the entity’s recent activities

Returns:

  • (Array)

    the list of recent activities by this entity



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/tekeya/entity.rb', line 307

def profile_feed(&blck)
  acts = []
  pkey = self.profile_feed_key
  recent_activities_count = ::Tekeya.redis.zcard(pkey)

  # Check if the cache is not empty
  if recent_activities_count > 0 && !block_given?
    # Retrieve the aggregate keys from redis
    acts_keys = ::Tekeya.redis.zrevrange(pkey, 0, -1)
    # Retrieve the aggregates
    acts_keys.each do |act_key|
      # Make `from_redis` only hit the db if author != entity
      key_components = act_key.split(':')
      actor = if key_components[4] == self.class.to_s && key_components[5] == self.entity_primary_key
        self
      end

      acts << ::Tekeya::Feed::Activity::Item.from_redis(act_key, actor)
    end
  else
    # Retrieve the activities from the DB
    db_recent_activities = self.activities.recent(&blck)
    db_recent_activities.each do |activity|
      acts << ::Tekeya::Feed::Activity::Item.from_db(activity, activity.author)
    end
  end

  return acts
end

#track(entity, notify = true) ⇒ Boolean

Tracks the given entity and copies it’s recent feed to the tracker feed

Parameters:

  • entity (Entity)

    the entity to track

  • notify (Boolean) (defaults to: true)

    determines whether the tracked entity should be notified

Returns:

  • (Boolean)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/tekeya/entity.rb', line 139

def track(entity, notify=true)
  run_callbacks :track_entity do
    check_if_tekeya_entity(entity)
    raise ::Tekeya::Errors::TekeyaRelationAlreadyExists.new("Already tracking #{entity}") if self.tracks?(entity)

    ret = add_tekeya_relation(self, entity, :tracks)

    if ret
      ::Resque.enqueue(::Tekeya::Feed::Activity::Resque::FeedCopy, entity.profile_feed_key, self.feed_key)
      
      activity = self.activities.tracked(entity)
      entity.notifications.tracked_by self if notify
    end

    return ret
  end
end

#trackers(type = nil) ⇒ Array

Returns a list of entities tracking this entity

Parameters:

  • type (String) (defaults to: nil)

    used to return a certain type of entities being tracked

Returns:

  • (Array)

    the entities tracking this entity



169
170
171
# File 'lib/tekeya/entity.rb', line 169

def trackers(type = nil)
  tekeya_relations_of(self, :tracks, type, true)
end

#tracking(type = nil) ⇒ Array

Return a list of entities being tracked by this entity

Parameters:

  • type (String) (defaults to: nil)

    used to return a certain type of entities being tracked

Returns:

  • (Array)

    the entities tracked by this entity



161
162
163
# File 'lib/tekeya/entity.rb', line 161

def tracking(type = nil)
  tekeya_relations_of(self, :tracks, type)
end

#tracks?(entity) ⇒ Boolean

Checks if this entity is tracking the given entity

Parameters:

  • entity (Entity)

    the entity to check

Returns:

  • (Boolean)

    true if this entity is tracking the given entity, false otherwise



177
178
179
180
# File 'lib/tekeya/entity.rb', line 177

def tracks?(entity)
  check_if_tekeya_entity(entity)
  tekeya_relation_exists?(self, entity, :tracks)
end

#unblock(entity) ⇒ Boolean

Unblock the given entity

Parameters:

  • entity (Entity)

    the entity to unblock

Returns:

  • (Boolean)


238
239
240
241
242
243
244
245
# File 'lib/tekeya/entity.rb', line 238

def unblock(entity)
  run_callbacks :unblock_entity do
    check_if_tekeya_entity(entity)
    raise ::Tekeya::Errors::TekeyaRelationNonExistent.new("Can't unblock an unblocked entity") unless self.blocks?(entity)

    delete_tekeya_relation(self, entity, :blocks)
  end
end

#untrack(entity) ⇒ Boolean

Untracks the given entity and deletes recent activities of the untracked entity from this entity’s feed

Parameters:

  • entity (Entity)

    the entity to untrack

Returns:

  • (Boolean)


186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/tekeya/entity.rb', line 186

def untrack(entity)
  run_callbacks :untrack_entity do
    check_if_tekeya_entity(entity)
    raise ::Tekeya::Errors::TekeyaRelationNonExistent.new("Can't untrack an untracked entity") unless self.tracks?(entity)

    ret = delete_tekeya_relation(self, entity, :tracks)
    
    ::Resque.enqueue(::Tekeya::Feed::Activity::Resque::UntrackFeed, entity.profile_feed_key, self.feed_key) if ret

    return ret
  end
end