<commit_before>Bongo          = require "bongo"
{Relationship} = require "jraphical"

{secure, daisy, Base} = Bongo

module.exports = class ActivityTicker extends Base
  @share()

  @set
    sharedMethods :
      static      : ["fetch"]

  relationshipNames = ["follower", "like", "member", "user"]
  constructorNames  = ["JAccount", "JApp", "JGroup", "JTag"]

  @fetch = secure (client, options = {}, callback) ->
    {connection: {delegate}} = client

    selector     =
      sourceName : "$in": constructorNames
      as         : "$in": relationshipNames
      targetName : "$in": constructorNames

    options      =
      limit      : options.limit or 20
      skip       : options.skip  or 0
      sort       : timestamp  : -1

    Relationship.some selector, options, (err, relationships) ->
      buckets = []
      return  callback err, buckets  if err
      daisy queue = relationships.map (relationship) ->
        ->
          relationship.fetchTeaser ->
            {source, target, as} = relationship
            buckets.push {source, target, as}
            queue.next()        

      queue.push ->
        callback null, buckets<commit_msg>Activity Ticker: Decorate relationships for ticker<commit_after>Bongo          = require "bongo"
{Relationship} = require "jraphical"

{secure, daisy, Base} = Bongo

module.exports = class ActivityTicker extends Base
  @share()

  @set
    sharedMethods :
      static      : ["fetch"]

  relationshipNames = ["follower", "like", "member", "user"]
  constructorNames  = ["JAccount", "JApp", "JGroup", "JTag", "JStatusUpdate"]

  JAccount = require './account'

  decorateEvents = (relationship, callback) ->
    {source, target, as, timestamp} = relationship

    if as is "like"
      # there is a flipped relationship between JAccount and JStatusUpdate
      # source is status update
      # target is account, we should correct it here
      # and also we should add the origin account here
      JAccount.one {"_id": source.originId}, (err, targetAccount)->
        return callback err if err

        modifiedEvent =
          source    : target
          target    : targetAccount
          subject   : source
          as        : as
          timestamp : timestamp

        callback null, modifiedEvent
    else
      callback null, {source, target, as, timestamp}



  @fetch = secure (client, options = {}, callback) ->
    {connection: {delegate}} = client

    from = options.from or +(new Date())
    selector     =
      sourceName : "$in": constructorNames
      as         : "$in": relationshipNames
      targetName : "$in": constructorNames
      timestamp : {"$lt" : new Date(from)}

    options      =
      # do not fetch more than 15 at once
      limit      : Math.min options.limit ? 15, 15
      sort       : timestamp  : -1

    Relationship.some selector, options, (err, relationships) ->
      buckets = []
      return  callback err, buckets  if err
      daisy queue = relationships.map (relationship) ->
        ->
          relationship.fetchTeaser ->
            decorateEvents relationship, (err, decoratedEvent)=>
              buckets.push decoratedEvent
              queue.next()

      queue.push ->
        callback null, buckets
{'fraction_non_alphanumeric': 0.10622483741096314, 'fraction_numerical': 0.0034066274388355527, 'mean_word_length': 2.249496981891348, 'pattern_counts': {'":': 7, '<': 3, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2491895', 'n_tokens_mistral': 888, 'n_tokens_neox': 842, 'n_words': 273}