<commit_before>const firebase = require("firebase");

module.exports = function ($scope) {

  //Create Prototype to generate hash code from string
  String.prototype.hashCode = function () {
    var hash = 0;
    for (var i = 0; i < this.length; i++) {
      var char = this.charCodeAt(i);
      hash = char + (hash << 6) + (hash << 16) - hash;
    }
    return hash;
  }

  $scope.nick = "Anon";
  //$scope.currentroom = document.domain;
  $scope.rooms = [];
  $scope.messages = [];
  $scope.newMessage = {};

  //For localhost testing
  if (document.domain == 'localhost') {
    $scope.currentroom = 'localhost:8080';
    $scope.rooms.push({
      url: 'localhost:8080',
      key:$scope.currentroom.hashCode()
    });
  }
  //Firebase Config used to connect to your firebase app (cant download this under Firebase Project Settings)
  var config = {
    apiKey: "<KEY>",
    authDomain: "rabbiteer-bbb91.firebaseapp.com",
    databaseURL: "https://rabbiteer-bbb91.firebaseio.com",
    storageBucket: "rabbiteer-bbb91.appspot.com"
  };

  //Provider used to log into firebase (Can enable multipe providers under the Firebase Auth Section)
  var providerGitHub = new firebase.auth.GithubAuthProvider();

  //Check if App is initialized if not initialize 
  if (firebase.apps.length == 0)
    firebase.initializeApp(config);

  //Database Reference to current chatroom
  var messagesdbRef = firebase.database().ref('/messages/').child($scope.currentroom.hashCode());
  //Database Reference to list of chat rooms
  var roomsRef = firebase.database().ref('/chat_rooms/');

  //Listen for authentication state changes
  firebase.auth().onAuthStateChanged(function (user) {

    //If the user is not undefined
    if (user) {
      // If the user is logged in, set the nickname and store the Firebase User for later use 
      if (user.displayName != undefined && user.displayName != null)
        $scope.nick = user.displayName;
      else
        $scope.nick = user.email;

      $scope.user = user;
      $scope.$apply();

      //Register char room for current room (Domain)
      roomsRef.child($scope.currentroom.hashCode()).update({
        url: $scope.currentroom
      });

    }
  });

  //Listener for rooms
  roomsRef.on('child_added', function (data) {
    let room = data.val()

    //Push room info to rooms array
    
    $scope.rooms.push({
      key: data.key,
      url: room.url
    });
    $scope.$apply();
  });

  //Listener for currentroom's messages
  messagesdbRef.on('child_added', function (data) {
    let message = data.val()

    //Push message info to messages array
    $scope.messages.push({
      id: data.key,
      current: message.userid == $scope.user.uid ? true : false,
      nick: message.nick,
      message: message.message
    });
    $scope.$apply();
  
  });<commit_msg>Fixed Duplicate chatroom in dropdown.<commit_after>const firebase = require("firebase");

module.exports = function ($scope) {

  //Create Prototype to generate hash code from string
  String.prototype.hashCode = function () {
    var hash = 0;
    for (var i = 0; i < this.length; i++) {
      var char = this.charCodeAt(i);
      hash = char + (hash << 6) + (hash << 16) - hash;
    }
    return hash;
  }

  $scope.nick = "Anon";
  //$scope.currentroom = document.domain;
  $scope.rooms = [];
  $scope.messages = [];
  $scope.newMessage = {};

  //For localhost testing
  if (document.domain == 'localhost') {
    $scope.currentroom = 'localhost:8080';
    $scope.rooms.push({
      url: 'localhost:8080',
      key:$scope.currentroom.hashCode()
    });
  }
  //Firebase Config used to connect to your firebase app (cant download this under Firebase Project Settings)
  var config = {
    apiKey: "AIzaSyBnRqWBvsbR-5bnvaRAZZbJc_DQK2JzxHo",
    authDomain: "rabbiteer-bbb91.firebaseapp.com",
    databaseURL: "https://rabbiteer-bbb91.firebaseio.com",
    storageBucket: "rabbiteer-bbb91.appspot.com"
  };

  //Provider used to log into firebase (Can enable multipe providers under the Firebase Auth Section)
  var providerGitHub = new firebase.auth.GithubAuthProvider();

  //Check if App is initialized if not initialize 
  if (firebase.apps.length == 0)
    firebase.initializeApp(config);

  //Database Reference to current chatroom
  var messagesdbRef = firebase.database().ref('/messages/').child($scope.currentroom.hashCode());
  //Database Reference to list of chat rooms
  var roomsRef = firebase.database().ref('/chat_rooms/');

  //Listen for authentication state changes
  firebase.auth().onAuthStateChanged(function (user) {

    //If the user is not undefined
    if (user) {
      // If the user is logged in, set the nickname and store the Firebase User for later use 
      if (user.displayName != undefined && user.displayName != null)
        $scope.nick = user.displayName;
      else
        $scope.nick = user.email;

      $scope.user = user;
      $scope.$apply();

      //Register char room for current room (Domain)
      roomsRef.child($scope.currentroom.hashCode()).update({
        url: $scope.currentroom
      });

    }
  });

  //Listener for rooms
  roomsRef.on('child_added', function (data) {
    let room = data.val()

    //Push room info to rooms array
    if ($scope.currentroom != room.url) {
      $scope.rooms.push({
        key: data.key,
        url: room.url
      });
      $scope.$apply();
    }
  });

  //Listener for currentroom's messages
  messagesdbRef.on('child_added', function (data) {
    let message = data.val()

    //Push message info to messages array
    $scope.messages.push({
      id: data.key,
      current: message.userid == $scope.user.uid ? true : false,
      nick: message.nick,
      message: message.message
    });
    $scope.$apply();
  
  });
{'fraction_non_alphanumeric': 0.13478639930252834, 'fraction_numerical': 0.007323452484742807, 'mean_word_length': 2.8652291105121295, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 4, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4093417', 'n_tokens_mistral': 1811, 'n_tokens_neox': 1732, 'n_words': 481}