<commit_before>angular.module('app.services').factory('socialActivity', function ($http, serverConfig, socialActivityTabManager, JsModel, JsCollection, follows, $q) {

  var typeToIcons = {
    'follow-request': 'sa-icon-request-1',
    'micropetition-created': function (activity) {
      if (activity.get('target').type === 'quorum') {
        return 'sa-icon-post';
      }
      return 'sa-icon-petition';
    },
    'answered': 'sa-icon-community',
    'groupPermissions-changed': 'sa-icon-request-2',
    'joinToGroup-approved': 'sa-icon-request-2'
  };

  var ActivityModel = JsModel.extend({
    parsers: {
      created_at: 'date'
    },
    initialize: function () {
      this.build();
    },
    build: function () {
      var avatar;
      var avatarTitle;
      if (this.get('following')) {
        avatar = this.get('following').avatar_file_name;
        avatarTitle = this.get('following').full_name;
      } else if (this.get('group')) {
        avatar = this.get('group').avatar_file_path;
        avatarTitle = this.get('group').official_title;
      }
      this.set('avatar', avatar);
      this.set('avatar_title', avatarTitle);
      var icon = typeToIcons[this.get('type')];
      this.set('sa-icon', typeof icon === 'function' ? icon(this) : icon);
      if (this.isFollowRequest()) {
        this.set('userFollow', follows.getByFollowerId(this.get('following').id));
      }
    },
    isRequest: function () {
      return this.get('type') === 'follow-request';
    },
    isFollowRequest: function () {
      return this.get('type') === 'follow-request';
    },
    isActiveRequest: function () {
      return this.isFollowRequest() &&
        this.get('userFollow') &&
        this.get('userFollow').get('id') === this.get('target').id &&
        !this.get('ignore');
    },
    getWidgetType: function () {
      return this.isFollowRequest() ? 'follow-request' : 'link';
    },
    getHtmlMessage: function(){
      if(this.getWidgetType() !== 'follow-request'){
        return this.get('html_message');
      }
      //when being shown to owner
      if(this.get('userFollow').get('id') === this.get('target').id){
        if(!this.get('userFollow').isApproved() && !this.get('ignore')){
          return '<p><strong>' + this.get('userFollow').get('follower').full_name + '</strong> requested to follow you.</p>';
        }
        if(this.get('userFollow').isApproved() && !this.get('userFollow').isFollow()){
          return '<p><strong>' + this.get('userFollow').get('follower').full_name + '</strong> is now following you. Follow back?</p>';
        }
        if(this.get('userFollow').isApproved() && this.get('userFollow').isFollow()){
          return '<p><strong>' + this.get('userFollow').get('follower').full_name + '</strong> and you are now following each other.</p>';
        }
      }
      return this.get('html_message');
    },
    ignore: function () {
      this.set('ignore', true);
      $http.put(serverConfig.url + '/api/social-activities/' + this.get('id'), {ignore: 1});
    }
  });

  var activities = new JsCollection([], {
    model: ActivityModel,
    comparator: function (activity) {
      return -activity.get('created_at').getTime();
    }
  });
  activities.prepare = function () {
    socialActivityTabManager.prepare(this);

    return this;
  };
  activities.add = function (models) {
    var self = this;
    models = _.isArray(models) ? models : [models];
    _(models).each(function (data) {
      var model = self.get(data[self.id]);
      if (model) {
        model.clear();
        for (var key in data) {
          model.set(key, data[key]);
        }
        model.build();
      } else {
        model = new self.model(data);
        self.models.push(model);
        self.byId[model.get(self.id)] = model;
      }
    });

    return this.sort();
  };
  activities.serverTimeDiff = 0;

  return {
    getActivities: function () {
      return activities;
    },
    load: function () {
      var data = [];
      return $q.all([
        follows.load(),
        $http.get(serverConfig.url + '/api/social-activities/').then(function (response) {
          data = response.data;
          activities.serverTimeDiff = (new Date(response.headers('Server-Time'))).getTime() - Date.now();
        })
      ]).finally(function () {
        activities.add(data).prepare();
      });
    }
  };
}).factory('socialActivityHandler', function (socialActivity, socialActivityTabManager, serverConfig, $http, follows, navigateTo) {
  return {
    navigateToOwner: function (item) {<commit_msg>fix bug #76: Cannot approve follower - no notification arrives<commit_after>angular.module('app.services').factory('socialActivity', function ($http, serverConfig, socialActivityTabManager, JsModel, JsCollection, follows, $q) {

  var typeToIcons = {
    'follow-request': 'sa-icon-request-1',
    'micropetition-created': function (activity) {
      if (activity.get('target').type === 'quorum') {
        return 'sa-icon-post';
      }
      return 'sa-icon-petition';
    },
    'answered': 'sa-icon-community',
    'groupPermissions-changed': 'sa-icon-request-2',
    'joinToGroup-approved': 'sa-icon-request-2'
  };

  var ActivityModel = JsModel.extend({
    parsers: {
      created_at: 'date'
    },
    initialize: function () {
      this.build();
    },
    build: function () {
      var avatar;
      var avatarTitle;
      if (this.get('following')) {
        avatar = this.get('following').avatar_file_name;
        avatarTitle = this.get('following').full_name;
      } else if (this.get('group')) {
        avatar = this.get('group').avatar_file_path;
        avatarTitle = this.get('group').official_title;
      }
      this.set('avatar', avatar);
      this.set('avatar_title', avatarTitle);
      var icon = typeToIcons[this.get('type')];
      this.set('sa-icon', typeof icon === 'function' ? icon(this) : icon);
      if (this.isFollowRequest()) {
        this.set('userFollow', follows.getByFollowerId(this.get('following').id));
      }
    },
    isRequest: function () {
      return this.get('type') === 'follow-request';
    },
    isFollowRequest: function () {
      return this.get('type') === 'follow-request';
    },
    isActiveRequest: function () {
      return this.isFollowRequest() &&
        this.get('userFollow') &&
        this.get('userFollow').get('id') === this.get('target').id &&
        !this.get('ignore');
    },
    getWidgetType: function () {
      return this.isFollowRequest() ? 'follow-request' : 'link';
    },
    getHtmlMessage: function(){
      if(this.getWidgetType() !== 'follow-request'){
        return this.get('html_message');
      }
      //when being shown to owner
      if(this.get('userFollow').get('id') === this.get('target').id){
        if(!this.get('userFollow').isApproved() && !this.get('ignore')){
          return '<p><strong>' + this.get('userFollow').get('follower').full_name + '</strong> requested to follow you.</p>';
        }
        if(this.get('userFollow').isApproved() && !this.get('userFollow').isFollow()){
          return '<p><strong>' + this.get('userFollow').get('follower').full_name + '</strong> is now following you. Follow back?</p>';
        }
        if(this.get('userFollow').isApproved() && this.get('userFollow').isFollow()){
          return '<p><strong>' + this.get('userFollow').get('follower').full_name + '</strong> and you are now following each other.</p>';
        }
      }
      return this.get('html_message');
    },
    ignore: function () {
      this.set('ignore', true);
      $http.put(serverConfig.url + '/api/social-activities/' + this.get('id'), {ignore: 1});
    }
  });

  var activities = new JsCollection([], {
    model: ActivityModel,
    comparator: function (activity) {
      return -activity.get('created_at').getTime();
    }
  });
  activities.prepare = function () {
    socialActivityTabManager.prepare(this);

    return this;
  };
  activities.add = function (models) {
    var self = this;
    models = _.isArray(models) ? models : [models];
    _(models).each(function (data) {
      var model = self.get(data[self.id]);
      if (model) {
        model.clear();
        for (var key in data) {
          model.set(key, data[key]);
        }
        model.build();
      } else {
        model = new self.model(data);
        self.models.push(model);
        self.byId[model.get(self.id)] = model;
      }
    });

    return this.sort();
  };
  activities.serverTimeDiff = 0;

  return {
    getActivities: function () {
      return activities;
    },
    load: function () {
      var data = [];
      return $q.all([
        follows.load(),
        $http.get(serverConfig.url + '/api/social-activities/').then(function (response) {
          data = response.data;
          activities.serverTimeDiff = (new Date(response.headers('Server-Time'))).getTime() - Date.now();
        })
      ]).finally(function () {
        try{
          activities.add(data.payload).prepare();
        } catch(e){
          console.log('failed to prepare social activities')
          console.log(e)
          console.log(e.stack)
        }
      });
    }
  };
}).factory('socialActivityHandler', function (socialActivity, socialActivityTabManager, serverConfig, $http, follows, navigateTo) {
  return {
    navigateToOwner: function (item) {
{'fraction_non_alphanumeric': 0.17829208186006643, 'fraction_numerical': 0.0012857602057216328, 'mean_word_length': 2.729125049940072, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '6869044', 'n_tokens_mistral': 2875, 'n_tokens_neox': 2831, 'n_words': 508}