<commit_before>// server & github api methods
// dlog is debugger, see server/setup.js

Meteor.methods({

  //////////////////
  // FILE MANAGEMENT
  //////////////////

  newFile: function() { // create a new file, unnamed, return id
    return Meteor.call('createFile', 'untitled');
  },

  createFile: function(ft) { // make new file with filetitle (ft), return id
    Meteor.call('addMessage', ' created file: ' + ft);
    Files.insert(
      {title: ft, repo: Meteor.user().profile.repo},
      function(err, id){ // create sharejs document with same id
        if(!err) newShareJSDoc(id);
        else dlog(err);
      }
    );
  },

  deleteFile: function(id) { // with id, delete a file from system
    ShareJS.model.delete(id);
    Files.remove(id);
    Docs.remove(id);
  },

  resetFiles: function() { // reset db and hard code the file structure
    Files.find({}).map(function delFile(f){ Meteor.call('deleteFile', f._id)});
    var base = [{'title':'site.html'},{'title':'site.css'},{'title':'site.js'}];
    base.map(function(f){ Meteor.call('createFile', f.title) });
  },



  /////////////////////
  // SHAREJS MANAGEMENT
  /////////////////////

  newShareJSDoc: function(id) { // create sharejs document with same id
    return Async.runSync(function(done) {
      var time = Math.round( new Date() / 1000 );
      ShareJS.model.create(
        id, 'text', { mtime: time, ctime: time },
        function(error, doc){ done(error, doc); }
      );
    });
  },

  getShareJSDoc: function(file) { // give live editor copy, v and snapshot
    var sjs = Docs.find( file._id );
    if (sjs.count())
      return sjs.fetch()[0].data;
    else
      Meteor.call('newShareJSDoc', file._id);
    return Meteor.call('getShareJSDoc', file._id);
  },

  postShareJSDoc: function(file) { // update files with their ids
    var sjs = Meteor.call('getShareJSDoc', file) // get doc and version
    ShareJS.model.applyOp( file._id, {
      op: [
        { p:0, d: sjs.snapshot }, // delete old content
        { p:0, i: file.content } // insert new blob content
      ],
      meta: null,
      v: sjs.v // apply it to last seen version
    });
  },

  testShareJS: function() { // update contents from sjs
    Files.find({}).map(function readSJS(f){
      Files.update(
        {'_id': f._id},
        {$set:
          { content: Meteor.call('getShareJSDoc',f).snapshot }
        });
    });<commit_msg>'handle' errors<commit_after>// server & github api methods
// dlog is debugger, see server/setup.js

Meteor.methods({

  //////////////////
  // FILE MANAGEMENT
  //////////////////

  newFile: function() { // create a new file, unnamed, return id
    return Meteor.call('createFile', 'untitled');
  },

  createFile: function(ft) { // make new file with filetitle (ft), return id
    Meteor.call('addMessage', ' created file: ' + ft);
    Files.insert(
      {title: ft, repo: Meteor.user().profile.repo},
      function(err, id){ // create sharejs document with same id
        if(!err) newShareJSDoc(id);
        else dlog(err);
      }
    );
  },

  deleteFile: function(id) { // with id, delete a file from system
    ShareJS.model.delete(id);
    Files.remove(id);
    Docs.remove(id);
  },

  resetFiles: function() { // reset db and hard code the file structure
    Files.find({}).map(function delFile(f){ Meteor.call('deleteFile', f._id)});
    var base = [{'title':'site.html'},{'title':'site.css'},{'title':'site.js'}];
    base.map(function(f){ Meteor.call('createFile', f.title) });
  },



  /////////////////////
  // SHAREJS MANAGEMENT
  /////////////////////

  newShareJSDoc: function(id) { // create sharejs document with same id
    return Async.runSync(function(done) {
      var time = Math.round( new Date() / 1000 );
      ShareJS.model.create(
        id, 'text', { mtime: time, ctime: time },
        function(error, doc){ done(error, doc); }
      );
    });
  },

  getShareJSDoc: function(file) { // give live editor copy, v and snapshot
    if(! file._id ) return null;
    var sjs = Docs.find( file._id );
    if (sjs.count())
      return sjs.fetch()[0].data;
    else
      Meteor.call('newShareJSDoc', file._id);
    return Meteor.call('getShareJSDoc', file._id);
  },

  postShareJSDoc: function(file) { // update files with their ids
    var sjs = Meteor.call('getShareJSDoc', file) // get doc and version
    if( !sjs ) return null; // if file id broke, don't propagate error
    ShareJS.model.applyOp( file._id, {
      op: [
        { p:0, d: sjs.snapshot }, // delete old content
        { p:0, i: file.content } // insert new blob content
      ],
      meta: null,
      v: sjs.v // apply it to last seen version
    });
  },

  testShareJS: function() { // update contents from sjs
    Files.find({}).map(function readSJS(f){
      Files.update(
        {'_id': f._id},
        {$set:
          { content: Meteor.call('getShareJSDoc',f).snapshot }
        });
    });
{'fraction_non_alphanumeric': 0.18645790971939813, 'fraction_numerical': 0.0028466856445709637, 'mean_word_length': 2.334915254237288, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7018637', 'n_tokens_mistral': 1633, 'n_tokens_neox': 1576, 'n_words': 406}