<commit_before>/* eslint-disable new-cap */
import { Meteor } from 'meteor/meteor';
import { check, Match } from 'meteor/check';
import { settingsLive } from './collections';
import defaultSettings from './defaultSettings';
import { checkSiteIdOwnership, checkUserLoggedIn, purgeSite } from '../utils';

const addSettings = ({ name, namespace, siteId }) => {
  check(name, String);
  check(namespace, Match.OneOf(String, undefined));
  check(siteId, String);

  return settingsLive.findOne({ 'woronaInfo.name': name, 'woronaInfo.siteId': siteId }) ? false :
    settingsLive.insert({ woronaInfo: {
      name,
      namespace,
      siteId,
      active: true,
      init: false,
    } });
};


Meteor.methods({
  saveSettings(settings) {
    const userId = this.userId;
    const name = settings.woronaInfo.name;
    const siteId = settings.woronaInfo.siteId;

    check(name, String);
    check(siteId, String);
    checkUserLoggedIn(userId);
    checkSiteIdOwnership(siteId, userId);

    const newSettingData = {};
    Object.assign(newSettingData, settings);
    delete newSettingData.woronaInfo;
    newSettingData['woronaInfo.init'] = true;

    const id = settingsLive.findOne({ 'woronaInfo.name': name, 'woronaInfo.siteId': siteId })._id;

    purgeSite(siteId);

    return settingsLive.update(id, { $set: newSettingData });
  },

  addSettings(options) { return addSettings(options); },

  addDefaultSettings(siteId) {
    defaultSettings.forEach(pkg => {
      addSettings({ name: pkg.name, namespace: pkg.namespace, siteId });
    });
  },
});
<commit_msg>Package activation working with Meteor method.<commit_after>/* eslint-disable new-cap */
import { Meteor } from 'meteor/meteor';
import { check, Match } from 'meteor/check';
import { settingsLive } from './collections';
import defaultSettings from './defaultSettings';
import { checkSiteIdOwnership, checkUserLoggedIn, purgeSite } from '../utils';

const addSettings = ({ name, namespace, siteId }) => {
  check(name, String);
  check(namespace, Match.OneOf(String, undefined));
  check(siteId, String);

  return settingsLive.findOne({
    'woronaInfo.name': name,
    'woronaInfo.siteId': siteId,
  })
    ? false
    : settingsLive.insert({
        woronaInfo: {
          name,
          namespace,
          siteId,
          active: true,
          init: false,
        },
      });
};

Meteor.methods({
  activateExtension({ name, siteId }) {
    const userId = this.userId;
    checkSiteIdOwnership(siteId, userId);

    const settings = settingsLive.findOne({
      'woronaInfo.name': name,
      'woronaInfo.siteId': siteId,
    }, { fields: { _id: 1 }});

    if (settings) {
      settingsLive.update(settings._id, { $set: { 'woronaInfo.active': true }});
    } else {
      addSettings({ name, siteId });
    }
  },

  saveSettings(settings) {
    const userId = this.userId;
    const name = settings.woronaInfo.name;
    const siteId = settings.woronaInfo.siteId;

    check(name, String);
    check(siteId, String);
    checkUserLoggedIn(userId);
    checkSiteIdOwnership(siteId, userId);

    const newSettingData = {};
    Object.assign(newSettingData, settings);
    delete newSettingData.woronaInfo;
    newSettingData['woronaInfo.init'] = true;

    const id = settingsLive.findOne({
      'woronaInfo.name': name,
      'woronaInfo.siteId': siteId,
    })._id;

    purgeSite(siteId);

    return settingsLive.update(id, { $set: newSettingData });
  },

  addSettings(options) {
    return addSettings(options);
  },

  addDefaultSettings(siteId) {
    defaultSettings.forEach(pkg => {
      addSettings({ name: pkg.name, namespace: pkg.namespace, siteId });
    });
  },
});

{'fraction_non_alphanumeric': 0.13935096809380965, 'fraction_numerical': 0.0002727024815925825, 'mean_word_length': 3.4460606060606063, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '5599744', 'n_tokens_mistral': 1156, 'n_tokens_neox': 1128, 'n_words': 241}