<commit_before>/*----------------------------- MESSAGING RULES ------------------------------*\
|------------------ This note created by Isaac on 2017-01-14 ------------------|

When a user views their inbox, all of their messages will be returned from the
database. The front end logic will then decide how to display each message.
Below is a guide to the logic required.

For a message to appear in a student's inbox, it must:
- Have the student's id in the 'student_id' column

For a message to appear in a student's inbox, but have a 'replied' status:
- Have text in the 'reply' column
- Have a date in the 'date_replied' column

For a message to appear in a mentor's inbox, it must:
- Have the mentor's id in the 'mentor_id' column
- Have text in the 'message' column
- Have a date in the 'date_sent' column

For a message to appear in a mentor's inbox, but have a 'replied' status:
- Have text in the 'reply' column

For a message to appear in a mentor's inbox, but have a 'read but not replied'
status:
- Not have text in the 'reply' column
- Have 'TRUE' in the 'read' column

\*----------------------------------------------------------------------------*/

/*------------------------------- AJAX REQUESTS ------------------------------*\
|------------------ This note created by Isaac on 2017-01-14 ------------------|

To retreive all messages for a user
- Send a `GET` request to `/message`
- Must send token to be decoded
- Must send user `type` on headers (or query)

To create a new message when a student clicks "Send Message"
- Send a `POST` request to `/message/new-message`
- Must send token to be decoded
- Must send a `message` object on data following this template:
message {
mentorEmail: 'mentor's email address',
subject: 'subject text',
body: 'body text',
studentName: 'optional student name'
}

To mark a message as read, send a `PUT` request to `/message/read-message`
- Must send token to be decoded
- Must send `message.id` on headers

To reply to a message, send a `PUT` request to `/message/reply`
- Must send token to be decoded
- Must send `message` object on `data` following this template:
message {
mentorEmail: 'mentor's email address',
messageId: 'message's ID',
reply: 'reply text',
}

\*----------------------------------------------------------------------------*/

app.controller('InboxController', ['$http', 'AuthFactory', 'MessageFactory', '$mdDialog', 'BioFactory', function($http, AuthFactory, MessageFactory, $mdDialog, sBioFactory) {
  console.log('InboxController running');
  var self = this;
  self.userStatus = AuthFactory.userStatus;
  self.messages = [];

  // Makes sure that currentUser is set before getting messages from the server
  AuthFactory.auth.$onAuthStateChanged(function(currentUser) {
    getMessages(currentUser);
  });

  self.person = BioFactory.mentorBio;

  // Get all messages from the database for a specific user
  function getMessages(currentUser) {
    console.log('checking');
    if(currentUser){
      return currentUser.getToken().then(function(idToken) {
        return $http({
          method: 'GET',
          url: '/message/get-all-messages',
          headers: {
            id_token: idToken
          }
        })
        .then(function(response) {
          self.messages = response.data;
          console.log('Messages list: ', self.messages);<commit_msg>Fix bug in inbox conttroller<commit_after>/*----------------------------- MESSAGING RULES ------------------------------*\
|------------------ This note created by Isaac on 2017-01-14 ------------------|

When a user views their inbox, all of their messages will be returned from the
database. The front end logic will then decide how to display each message.
Below is a guide to the logic required.

For a message to appear in a student's inbox, it must:
- Have the student's id in the 'student_id' column

For a message to appear in a student's inbox, but have a 'replied' status:
- Have text in the 'reply' column
- Have a date in the 'date_replied' column

For a message to appear in a mentor's inbox, it must:
- Have the mentor's id in the 'mentor_id' column
- Have text in the 'message' column
- Have a date in the 'date_sent' column

For a message to appear in a mentor's inbox, but have a 'replied' status:
- Have text in the 'reply' column

For a message to appear in a mentor's inbox, but have a 'read but not replied'
status:
- Not have text in the 'reply' column
- Have 'TRUE' in the 'read' column

\*----------------------------------------------------------------------------*/

/*------------------------------- AJAX REQUESTS ------------------------------*\
|------------------ This note created by Isaac on 2017-01-14 ------------------|

To retreive all messages for a user
- Send a `GET` request to `/message`
- Must send token to be decoded
- Must send user `type` on headers (or query)

To create a new message when a student clicks "Send Message"
- Send a `POST` request to `/message/new-message`
- Must send token to be decoded
- Must send a `message` object on data following this template:
message {
mentorEmail: 'mentor's email address',
subject: 'subject text',
body: 'body text',
studentName: 'optional student name'
}

To mark a message as read, send a `PUT` request to `/message/read-message`
- Must send token to be decoded
- Must send `message.id` on headers

To reply to a message, send a `PUT` request to `/message/reply`
- Must send token to be decoded
- Must send `message` object on `data` following this template:
message {
mentorEmail: 'mentor's email address',
messageId: 'message's ID',
reply: 'reply text',
}

\*----------------------------------------------------------------------------*/

app.controller('InboxController', ['$http', 'AuthFactory', 'MessageFactory', '$mdDialog', 'BioFactory', function($http, AuthFactory, MessageFactory, $mdDialog, BioFactory) {
  console.log('InboxController running');
  var self = this;
  self.userStatus = AuthFactory.userStatus;
  self.messages = [];

  // Makes sure that currentUser is set before getting messages from the server
  AuthFactory.auth.$onAuthStateChanged(function(currentUser) {
    getMessages(currentUser);
  });

  self.person = BioFactory.mentorBio;

  // Get all messages from the database for a specific user
  function getMessages(currentUser) {
    console.log('checking');
    if(currentUser){
      return currentUser.getToken().then(function(idToken) {
        return $http({
          method: 'GET',
          url: '/message/get-all-messages',
          headers: {
            id_token: idToken
          }
        })
        .then(function(response) {
          self.messages = response.data;
          console.log('Messages list: ', self.messages);
{'fraction_non_alphanumeric': 0.19195230998509688, 'fraction_numerical': 0.004769001490312966, 'mean_word_length': 2.3571785892946475, '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': '672413', 'n_tokens_mistral': 1840, 'n_tokens_neox': 1745, 'n_words': 771}