<commit_before>// Copyright (c) 2013, the Clean project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of clean_sync.server;

typedef Future<DataProvider> DataGenerator(Map args);
final int MAX = pow(2,16) - 1;
final int prefix_random_part = new Random().nextInt(MAX);

final Logger logger = new Logger('clean_sync');

class Publisher {
  int counter;
  Map<String, DataGenerator> _publishedCollections;
  Map<String, dynamic> _beforeRequestCallbacks;
  Map<String, dynamic> _project;

  Publisher() {
    _publishedCollections = {};
    _beforeRequestCallbacks = {};
    _project = {};
    counter = 0;
  }

  void publish(String collection, DataGenerator callback, {beforeRequest: null, project:null}) {
    _publishedCollections[collection] = callback;
    _beforeRequestCallbacks[collection] = beforeRequest;
    _project[collection] = project;
  }

  bool isPublished(String collection) {
    return _publishedCollections.keys.contains(collection);
  }

  Future handleSyncRequest(ServerRequest request) {
    Map data = request.args;
    logger.finest("REQUEST:  ${data}");
    var beforeRequestCall = _beforeRequestCallbacks[data['collection']];
    var projectCall = _project[data['collection']];


    if (data["action"] == "get_id_prefix") {
      return new Future(getIdPrefix).then((prefix) => {'id_prefix': prefix});
    }

    if(data['args'] == null) {
      data['args'] = {};
    }
    data['args']['_authenticatedUserId'] = request.authenticatedUserId;

    if (data['action'] == 'add' || data['action'] == 'change' || data['action'] == 'remove') {
      if (projectCall != null) {
        throw new Exception('thou shall not modify projected data!');
      }
    }
    Future beforeRequest = new Future.value(null);
    if (_beforeRequestCallbacks[data['collection']] != null) {
      if (data['action'] == 'add' || data['action'] == 'change' || data['action'] == 'remove') {
        var value;
        if (data['action'] == 'add') value = data['data'];
        else if (data['action'] == 'change') value = data['change'];
        else if (data['action'] == 'remove') value = {};
        beforeRequest = beforeRequestCall(value, data['args']);
      }
    }

    return beforeRequest.then((_) => _publishedCollections[data['collection']](data['args'])).then((DataProvider dp) {
      if (data["action"] == "get_data") {
        return dp.data(project: projectCall);
      }
      else if (data["action"] == "get_diff") {
        return dp.diffFromVersion(data["version"], project: projectCall);
      }
      else if (data["action"] == "add") {
        return dp.add(data['data'], data['author']);
      }
      else if (data["action"] == "change") {
        return dp.change(data['_id'], data['change'], data['author']);
      }
      else if (data["action"] == "remove") {
        return dp.remove(data['_id'], data['author']);
      }
    }).catchError((e) {
      return new Future.value({
        'error': e.toString(),
      });
    });
  }

  String getIdPrefix() {
    String prefix =
        new DateTime.now().millisecondsSinceEpoch.toRadixString(36) +<commit_msg>Renaming in publisher a bit.<commit_after>// Copyright (c) 2013, the Clean project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of clean_sync.server;

typedef Future<DataProvider> DataGenerator(Map args);
final int MAX = pow(2,16) - 1;
final int prefix_random_part = new Random().nextInt(MAX);

final Logger logger = new Logger('clean_sync');

class Publisher {
  int counter;
  Map<String, DataGenerator> _publishedCollections;
  Map<String, dynamic> _beforeRequestCallbacks;
  Map<String, dynamic> _projections;

  Publisher() {
    _publishedCollections = {};
    _beforeRequestCallbacks = {};
    _projections = {};
    counter = 0;
  }

  void publish(String collection, DataGenerator callback, {beforeRequest: null,
    projection: null}) {
    _publishedCollections[collection] = callback;
    _beforeRequestCallbacks[collection] = beforeRequest;
    _projections[collection] = projection;
  }

  bool isPublished(String collection) {
    return _publishedCollections.keys.contains(collection);
  }

  Future handleSyncRequest(ServerRequest request) {
    Map data = request.args;
    logger.finest("REQUEST:  ${data}");

    if(data['args'] == null) {
      data['args'] = {};
    }
    data['args']['_authenticatedUserId'] = request.authenticatedUserId;

    var dataGenerator = _publishedCollections[data['collection']];
    var beforeRequestCall = _beforeRequestCallbacks[data['collection']];
    var projectionCall = _projections[data['collection']];
    var action = data["action"];

    if (action == "get_id_prefix") {
      return new Future(getIdPrefix).then((prefix) => {'id_prefix': prefix});
    }

    List<String> modifications = ['add', 'change', 'remove'];

    if (modifications.contains(action) && projectionCall != null) {
      throw new Exception('Thou shall not modify projected data!');
    }

    Future beforeRequest = new Future.value(null);
    if (beforeRequestCall != null && modifications.contains(action)) {
      var value;
      if (action == 'add') value = data['data'];
      else if (action == 'change') value = data['change'];
      else if (action == 'remove') value = {};
      beforeRequest = beforeRequestCall(value, data['args']);
    }

    return beforeRequest
        .then((_) => dataGenerator(data['args']))
        .then((DataProvider dp) {
      if (action == "get_data") {
        return dp.data(projection: projectionCall);
      }
      else if (action == "get_diff") {
        return dp.diffFromVersion(data["version"], projection: projectionCall);
      }
      else if (action == "add") {
        return dp.add(data['data'], data['author']);
      }
      else if (action == "change") {
        return dp.change(data['_id'], data['change'], data['author']);
      }
      else if (action == "remove") {
        return dp.remove(data['_id'], data['author']);
      }
    }).catchError((e) {
      return new Future.value({
        'error': e.toString(),
      });
    });
  }

  String getIdPrefix() {
    String prefix =
        new DateTime.now().millisecondsSinceEpoch.toRadixString(36) +
{'fraction_non_alphanumeric': 0.15160486132751635, 'fraction_numerical': 0.003427859146151449, 'mean_word_length': 3.5236081747709656, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 16, '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': '883583', 'n_tokens_mistral': 1857, 'n_tokens_neox': 1812, 'n_words': 464}