<commit_before>/*  Server Methods  */
import sizeOf from 'image-size';
import url from 'url';
import http from 'http';

var saveFiles;
var updateFile;
var updateUser;
var updateLog;
var getImageSize;
var usersTaggedPattern = /\B@[a-z0-9_-]+/g;

Meteor.methods({
  'saveMessage': function (message, files, logId) {
    // Find users tagged and add links to their profile
    var matches = message.match(usersTaggedPattern);
    if (matches && matches.length > 0) {
      _.map(matches, function (item) {
        var username = item.replace('@', '');
        var exists = !!Meteor.users.findOne({username: username});
        if (!exists) return;

        message = message.replace(item, '<a href="/' + username + '">' + item + '</a>');
      });
    }

    var item = {
      userId: Meteor.userId(),
      createdAt: new Date(),
      content: message
    };
    var isMember = Members.findOne({ logId: logId, userId: Meteor.userId() });

    if (!item.userId) {
      throw new Meteor.Error(500,
        'You cannot send messages while you are not connected');
    }

    if (!isMember) {
      throw new Meteor.Error(500,
        'You cannot send messages if you did not join the log');
    }

    return Messages.insert(item, function (error, response) {
      if (error) throw error;

      saveFiles(files, response, logId);
      updateLog(logId, {lastMessage: new Date()});

      Meteor.call('saveEvent',
                  response,
                  item.userId,
                  logId,
                  'message_created',
                  'Messages');

      return response;
    });

    // return saveFiles(item).then(Meteor.bindEnvironment(function (value) {
    //   if (value) item.files = value;
    //   console.log('value from saveFiles', value);
    //   return saveMessage(item);
    // })).then(Meteor.bindEnvironment(function (value) {
    //   console.log('value from saveMessage', value);
    //   return saveEvent(value,
    //                    item.userId,
    //                    logId,
    //                    'message_created',
    //                    'Messages');

    // }));
  },

  'editMessage': function (message) {
    // Wrap tagged users with <a> tags in message.content
    var matches = message.content.match(usersTaggedPattern);
    var event = Events.findOne({messageId: message._id});
    var oldMessage = Messages.findOne({_id: message._id});
    var oldMatches = oldMessage.content.match(usersTaggedPattern);

    if (matches && matches.length > 0) {
      _.map(matches, function (item) {
        var username = item.replace('@', '');
        var exists = !!Meteor.users.findOne({username: username});
        if (!exists) return;

        message.content = message.content.replace(item,
          '<a href="/' + username + '">' + item + '</a>');

        // If new user was tagged, save notification
        if (oldMatches && oldMatches.length > 0) {
          var index = oldMatches.indexOf(item);
        }
        var currentUser = Meteor.user();
        if (username === currentUser.username) return;
        if (!oldMatches || index < 0) {
          Meteor.call('saveTaggedNotification', username, event._id);
        }
      });
    }

    // If user was untagged, remove their notification
    if (oldMatches && oldMatches.length > 0) {
      _.map(oldMatches, function (item) {
        var username = item.replace('@', '');
        var user = Meteor.users.findOne({username: username});

        // Return if we can't find the user or if user is still tagged
        if (!user || !user._id) return;
        if (matches && matches.length > 0) {
          var index = matches.indexOf(item);
          if (index > -1) return;
        }

        Notifications.remove({userId: user._id, eventId: event._id})
      });
    }

    // Fucking FINALLY update the message
    return Messages.update(message._id, {$set: {content: message.content}},
      function (error, response) {
      if (error) throw error;

      return response;
    });
  },

  'saveEvent': function (id, userId, logId, type, refType, hidden) {
    var item = {
      type: type,
      createdAt: new Date()
    };

    if (logId) {
      item.logId = logId;
    }

    if (userId) item.userId = userId;
    if (hidden) item.hidden = true;

    if (refType === 'Messages') {<commit_msg>Log console test<commit_after>/*  Server Methods  */
import sizeOf from 'image-size';
import url from 'url';
import http from 'http';

var saveFiles;
var updateFile;
var updateUser;
var updateLog;
var getImageSize;
var usersTaggedPattern = /\B@[a-z0-9_-]+/g;

Meteor.methods({
  'saveMessage': function (message, files, logId) {
    // Find users tagged and add links to their profile
    var matches = message.match(usersTaggedPattern);
    if (matches && matches.length > 0) {
      _.map(matches, function (item) {
        var username = item.replace('@', '');
        var exists = !!Meteor.users.findOne({username: username});
        if (!exists) return;

        message = message.replace(item, '<a href="/' + username + '">' + item + '</a>');
      });
    }

    var item = {
      userId: Meteor.userId(),
      createdAt: new Date(),
      content: message
    };
    var isMember = Members.findOne({ logId: logId, userId: Meteor.userId() });

    if (!item.userId) {
      throw new Meteor.Error(500,
        'You cannot send messages while you are not connected');
    }

    if (!isMember) {
      throw new Meteor.Error(500,
        'You cannot send messages if you did not join the log');
    }

    return Messages.insert(item, function (error, response) {
      if (error) throw error;

      saveFiles(files, response, logId);
      updateLog(logId, {lastMessage: new Date()});

      Meteor.call('saveEvent',
                  response,
                  item.userId,
                  logId,
                  'message_created',
                  'Messages');

      return response;
    });

    // return saveFiles(item).then(Meteor.bindEnvironment(function (value) {
    //   if (value) item.files = value;
    //   console.log('value from saveFiles', value);
    //   return saveMessage(item);
    // })).then(Meteor.bindEnvironment(function (value) {
    //   console.log('value from saveMessage', value);
    //   return saveEvent(value,
    //                    item.userId,
    //                    logId,
    //                    'message_created',
    //                    'Messages');

    // }));
  },

  'editMessage': function (message) {
    // Wrap tagged users with <a> tags in message.content
    var matches = message.content.match(usersTaggedPattern);
    var event = Events.findOne({messageId: message._id});
    var oldMessage = Messages.findOne({_id: message._id});
    var oldMatches = oldMessage.content.match(usersTaggedPattern);

    if (matches && matches.length > 0) {
      _.map(matches, function (item) {
        var username = item.replace('@', '');
        var exists = !!Meteor.users.findOne({username: username});
        if (!exists) return;

        message.content = message.content.replace(item,
          '<a href="/' + username + '">' + item + '</a>');

        // If new user was tagged, save notification
        if (oldMatches && oldMatches.length > 0) {
          var index = oldMatches.indexOf(item);
        }
        var currentUser = Meteor.user();
        if (username === currentUser.username) return;
        if (!oldMatches || index < 0) {
          Meteor.call('saveTaggedNotification', username, event._id);
        }
      });
    }

    // If user was untagged, remove their notification
    if (oldMatches && oldMatches.length > 0) {
      _.map(oldMatches, function (item) {
        var username = item.replace('@', '');
        var user = Meteor.users.findOne({username: username});

        // Return if we can't find the user or if user is still tagged
        if (!user || !user._id) return;
        if (matches && matches.length > 0) {
          var index = matches.indexOf(item);
          if (index > -1) return;
        }

        Notifications.remove({userId: user._id, eventId: event._id})
      });
    }

    // Fucking FINALLY update the message
    return Messages.update(message._id, {$set: {content: message.content}},
      function (error, response) {
      if (error) throw error;

      return response;
    });
  },

  'saveEvent': function (id, userId, logId, type, refType, hidden) {
    console.log('ALLAHU AKBAR');
    console.log('ALLAHU AKBAR');
    console.log('JAH RASTAFARI');
    console.log('JAH RASTAFARI');
    var item = {
      type: type,
      createdAt: new Date()
    };

    if (logId) {
      item.logId = logId;
    }

    if (userId) item.userId = userId;
    if (hidden) item.hidden = true;

    if (refType === 'Messages') {
{'fraction_non_alphanumeric': 0.14114696157792725, 'fraction_numerical': 0.003420362558431194, 'mean_word_length': 2.238095238095238, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 25, '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': '7110233', 'n_tokens_mistral': 2665, 'n_tokens_neox': 2558, 'n_words': 702}