<commit_before>var RootView = require("../views/root");
var Backbone = require("backbone");
var ConversationModel = require("../models/conversation");
var ParticipantModel = require("../models/participant");
var bbFetch = require("../net/bbFetch");
var ConversationsCollection = require("../collections/conversations");
var FaqCollection = require("../collections/faqs");
var FaqContent = require("../faqContent");
var InboxItemForApiView = require('../views/inboxItemForApi');
var InboxView = require("../views/inbox");
var InboxApiTestView = require("../views/inboxApiTest");
var HomepageView = require("../views/homepage");
var CreateConversationFormView = require("../views/create-conversation-form");
var ConversationDetailsView = require("../views/conversation-details");
var ConversationGatekeeperView = require("../views/conversationGatekeeperView");
var ConversationGatekeeperViewCreateUser = require("../views/conversationGatekeeperViewCreateUser");
var ParticipationView = require("../views/participation");
var ExploreView = require("../views/explore");
var EmptyView = require("../views/empty-view");
var LoginFormView = require("../views/login-form");
var metric = require("../util/gaMetric");
var ModerationView = require("../views/moderation");
var PasswordResetView = require("../views/passwordResetView");
var PasswordResetInitView = require("../views/passwordResetInitView");
var SettingsView = require("../views/settings.js");
var ShareLinkView = require("../views/share-link-view");
var SummaryView = require("../views/summary.js");
var PlanUpgradeView = require("../views/plan-upgrade");
var FaqView = require("../views/faq");
var PolisStorage = require("../util/polisStorage");
var UserModel = require("../models/user");
var _ = require("underscore");
var $ = require("jquery");


function authenticated() { return PolisStorage.uid(); }
function hasEmail() { return PolisStorage.hasEmail(); }

// TODO refactor this terrible recursive monster function.
function doJoinConversation(onSuccess, conversation_id, zinvite, singleUse) {
  var that = this;

  var suzinvite;
  if (singleUse) {
    suzinvite = zinvite;
  }

  var uid = PolisStorage.uid();

  if (!uid) {
      console.log("trying to load conversation, but no auth");
      // Not signed in.
      // Or not registered.

      if (singleUse) {

        $.ajax({
          url: "/api/v3/joinWithInvite",
          type: "POST",
          dataType: "json",
          xhrFields: {
              withCredentials: true
          },
          // crossDomain: true,
          data: {
            conversation_id: conversation_id,
            suzinvite: suzinvite
          }
        }).then(function(data) {
          that.participationView(conversation_id);
        }, function(err) {
          if (err.responseText === "polis_err_no_matching_suzinvite") {
            alert("Sorry, this single-use URL has been used.");
          } else {
            that.conversationGatekeeper(conversation_id, suzinvite, singleUse).done(function(ptptData) {
              doJoinConversation.call(that, onSuccess, conversation_id);
            });
          }
        });
      } else if (conversation_id) {
        // Don't require user to explicitly create a user before joining the conversation.
        $.ajax({
          url: "/api/v3/joinWithInvite",
          type: "POST",
          dataType: "json",
          xhrFields: {
              withCredentials: true
          },
          // crossDomain: true,
          data: {
            conversation_id: conversation_id
          }
        }).then(function(data) {
          that.participationView(conversation_id);
        }, function(err) {
          that.conversationGatekeeper(conversation_id, zinvite).done(function(ptptData) {
            doJoinConversation.call(that, onSuccess, conversation_id);
          });
        });
      } else {
        alert("missing conversation ID in URL. Shouldn't hit this.");
        // this.doCreateUserFromGatekeeper(conversation_id, zinvite, singleUse).done(function() {
        //   // Try again, should be ready now.
        //   doJoinConversation.call(that, onSuccess, conversation_id, zinvite);
        // });


        // !!!!!!!!!!TEMP CODE - JOIN WITHOUT A ZINVITE!!!!!
        // Don't require user to explicitly create a user before joining the conversation.
        $.ajax({
          url: "/api/v3/joinWithInvite",
          type: "POST",
          dataType: "json",
          xhrFields: {
              withCredentials: true
          },
          // crossDomain: true,
          data: {
            conversation_id: conversation_id,
            // zinvite: zinvite
          }
        }).then(function(data) {
          that.participationView(conversation_id);
        }, function(err) {
          that.conversationGatekeeper(conversation_id).done(function(ptptData) {
            doJoinConversation.call(that, onSuccess, conversation_id);
          });
        });

      }
  } else {
    var params = {
      conversation_id: conversation_id,
    };
    if (singleUse) {
      params.suzinvite = suzinvite;
    } else {
      params.zinvite = zinvite;
    }

    if (singleUse) {
      // join conversation (may already have joined)
      var ptpt = new ParticipantModel(params);
      ptpt.save().then(function() {
        // Participant record was created, or already existed.
        // Go to the conversation.
        onSuccess(ptpt);
      }, function(err) {
        $.ajax({
          url: "/api/v3/joinWithInvite",
          type: "POST",
          dataType: "json",
          xhrFields: {
              withCredentials: true
          },
          // crossDomain: true,
          data: {
            conversation_id: conversation_id,
            suzinvite: suzinvite
          }
        }).then(function(data) {
          doJoinConversation.call(that, onSuccess, conversation_id);
        }, function(err) {
          if (err.responseText === "polis_err_no_matching_suzinvite") {
            alert("Sorry, this single-use URL has been used.");
          } else {
            that.conversationGatekeeper(conversation_id, suzinvite, singleUse).done(function(ptptData) {
              doJoinConversation.call(that, onSuccess, conversation_id);
            });
          }
        });
      });
    } else {
      // join conversation (may already have joined)
      var ptpt = new ParticipantModel(params);
      ptpt.save().then(function() {
        // Participant record was created, or already existed.
        // Go to the conversation.
        onSuccess(ptpt);
      }, function(err) {
        that.conversationGatekeeper(conversation_id, zinvite).done(function(ptptData) {
          doJoinConversation.call(that, onSuccess, conversation_id, zinvite);
        });
      });
    }
  }
  //  else {
  //   // Found a pid for that conversation_id.
  //   // Go to the conversation.
  //   that.doLaunchConversation(conversation_id);
  // }

}


var polisRouter = Backbone.Router.extend({
  initialize: function(options) {
    this.r("homepage", "homepageView");
    this.r("conversation/create", "createConversation");
    this.r("conversation/view/:id(/:zinvite)", "participationView");<commit_msg>ga events for auto join<commit_after>var RootView = require("../views/root");
var Backbone = require("backbone");
var ConversationModel = require("../models/conversation");
var ParticipantModel = require("../models/participant");
var bbFetch = require("../net/bbFetch");
var ConversationsCollection = require("../collections/conversations");
var FaqCollection = require("../collections/faqs");
var FaqContent = require("../faqContent");
var InboxItemForApiView = require('../views/inboxItemForApi');
var InboxView = require("../views/inbox");
var InboxApiTestView = require("../views/inboxApiTest");
var HomepageView = require("../views/homepage");
var CreateConversationFormView = require("../views/create-conversation-form");
var ConversationDetailsView = require("../views/conversation-details");
var ConversationGatekeeperView = require("../views/conversationGatekeeperView");
var ConversationGatekeeperViewCreateUser = require("../views/conversationGatekeeperViewCreateUser");
var ParticipationView = require("../views/participation");
var ExploreView = require("../views/explore");
var EmptyView = require("../views/empty-view");
var LoginFormView = require("../views/login-form");
var metric = require("../util/gaMetric");
var ModerationView = require("../views/moderation");
var PasswordResetView = require("../views/passwordResetView");
var PasswordResetInitView = require("../views/passwordResetInitView");
var SettingsView = require("../views/settings.js");
var ShareLinkView = require("../views/share-link-view");
var SummaryView = require("../views/summary.js");
var PlanUpgradeView = require("../views/plan-upgrade");
var FaqView = require("../views/faq");
var PolisStorage = require("../util/polisStorage");
var UserModel = require("../models/user");
var _ = require("underscore");
var $ = require("jquery");


function authenticated() { return PolisStorage.uid(); }
function hasEmail() { return PolisStorage.hasEmail(); }

// TODO refactor this terrible recursive monster function.
function doJoinConversation(onSuccess, conversation_id, zinvite, singleUse) {
  var that = this;

  var suzinvite;
  if (singleUse) {
    suzinvite = zinvite;
  }

  var uid = PolisStorage.uid();

  if (!uid) {
      console.log("trying to load conversation, but no auth");
      // Not signed in.
      // Or not registered.

      if (singleUse) {

        $.ajax({
          url: "/api/v3/joinWithInvite",
          type: "POST",
          dataType: "json",
          xhrFields: {
              withCredentials: true
          },
          // crossDomain: true,
          data: {
            conversation_id: conversation_id,
            suzinvite: suzinvite
          }
        }).then(function(data) {
          that.participationView(conversation_id);
          gaEvent("Session", "create", "empty");
        }, function(err) {
          if (err.responseText === "polis_err_no_matching_suzinvite") {
            gaEvent("Session", "createFail", "polis_err_no_matching_suzinvite");
            setTimeout(function() {
              alert("Sorry, this single-use URL has been used.");
            },99);
          } else {
            that.conversationGatekeeper(conversation_id, suzinvite, singleUse).done(function(ptptData) {
              doJoinConversation.call(that, onSuccess, conversation_id);
            });
          }
        });
      } else if (conversation_id) {
        // Don't require user to explicitly create a user before joining the conversation.
        $.ajax({
          url: "/api/v3/joinWithInvite",
          type: "POST",
          dataType: "json",
          xhrFields: {
              withCredentials: true
          },
          // crossDomain: true,
          data: {
            conversation_id: conversation_id
          }
        }).then(function(data) {
          that.participationView(conversation_id);
          gaEvent("Session", "create", "empty");
        }, function(err) {
          that.conversationGatekeeper(conversation_id, zinvite).done(function(ptptData) {
            doJoinConversation.call(that, onSuccess, conversation_id);
          });
        });
      } else {
        gaEvent("Session", "createFail", "polis_err_unexpected_conv_join_condition_1");
        setTimeout(function() {
          alert("missing conversation ID in URL. Shouldn't hit this.");
        },99);
        // this.doCreateUserFromGatekeeper(conversation_id, zinvite, singleUse).done(function() {
        //   // Try again, should be ready now.
        //   doJoinConversation.call(that, onSuccess, conversation_id, zinvite);
        // });


        // !!!!!!!!!!TEMP CODE - JOIN WITHOUT A ZINVITE!!!!!
        // Don't require user to explicitly create a user before joining the conversation.
        $.ajax({
          url: "/api/v3/joinWithInvite",
          type: "POST",
          dataType: "json",
          xhrFields: {
              withCredentials: true
          },
          // crossDomain: true,
          data: {
            conversation_id: conversation_id,
            // zinvite: zinvite
          }
        }).then(function(data) {
          gaEvent("Session", "create", "empty");
          that.participationView(conversation_id);
        }, function(err) {
          that.conversationGatekeeper(conversation_id).done(function(ptptData) {
            doJoinConversation.call(that, onSuccess, conversation_id);
          });
        });

      }
  } else {
    var params = {
      conversation_id: conversation_id,
    };
    if (singleUse) {
      params.suzinvite = suzinvite;
    } else {
      params.zinvite = zinvite;
    }

    if (singleUse) {
      // join conversation (may already have joined)
      var ptpt = new ParticipantModel(params);
      ptpt.save().then(function() {
        // Participant record was created, or already existed.
        // Go to the conversation.
        onSuccess(ptpt);
      }, function(err) {
        $.ajax({
          url: "/api/v3/joinWithInvite",
          type: "POST",
          dataType: "json",
          xhrFields: {
              withCredentials: true
          },
          // crossDomain: true,
          data: {
            conversation_id: conversation_id,
            suzinvite: suzinvite
          }
        }).then(function(data) {
          doJoinConversation.call(that, onSuccess, conversation_id);
          // no ga session event, since they already have a uid
        }, function(err) {
          if (err.responseText === "polis_err_no_matching_suzinvite") {
            alert("Sorry, this single-use URL has been used.");
          } else {
            that.conversationGatekeeper(conversation_id, suzinvite, singleUse).done(function(ptptData) {
              doJoinConversation.call(that, onSuccess, conversation_id);
            });
          }
        });
      });
    } else {
      // join conversation (may already have joined)
      var ptpt = new ParticipantModel(params);
      ptpt.save().then(function() {
        // Participant record was created, or already existed.
        // Go to the conversation.
        onSuccess(ptpt);
        // no ga session event, since they already have a uid
      }, function(err) {
        that.conversationGatekeeper(conversation_id, zinvite).done(function(ptptData) {
          doJoinConversation.call(that, onSuccess, conversation_id, zinvite);
        });
      });
    }
  }
  //  else {
  //   // Found a pid for that conversation_id.
  //   // Go to the conversation.
  //   that.doLaunchConversation(conversation_id);
  // }

}


var polisRouter = Backbone.Router.extend({
  initialize: function(options) {
    this.r("homepage", "homepageView");
    this.r("conversation/create", "createConversation");
    this.r("conversation/view/:id(/:zinvite)", "participationView");
{'fraction_non_alphanumeric': 0.13530357860876557, 'fraction_numerical': 0.0008711968904972524, 'mean_word_length': 2.4769338303821065, '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': '221963', 'n_tokens_mistral': 4221, 'n_tokens_neox': 4173, 'n_words': 874}