You are here

function drupalchat_poll in DrupalChat 6.2

Same name and namespace in other branches
  1. 6 drupalchat.module \drupalchat_poll()
  2. 7.2 drupalchat.module \drupalchat_poll()
  3. 7 drupalchat.module \drupalchat_poll()

Process and get messages

1 string reference to 'drupalchat_poll'
drupalchat_menu in ./drupalchat.module
Implementaiton of hook_menu()

File

./drupalchat.module, line 583
Module code for DrupalChat.

Code

function drupalchat_poll() {
  global $user;
  $initial_time = time();
  $message_count = 0;
  if ($_POST['drupalchat_last_timestamp'] > 0) {
    $last_timestamp = check_plain($_POST['drupalchat_last_timestamp']);
  }
  else {
    $last_timestamp = $initial_time;
  }
  $buddylist = _drupalchat_buddylist($user->uid);
  $buddylist_online_old = _drupalchat_buddylist_online($buddylist);
  $polling_method = variable_get('drupalchat_polling_method', DRUPALCHAT_AJAX);
  if ($polling_method == DRUPALCHAT_AJAX) {

    //$messages = db_query('SELECT u.name, m.uid1, m.uid2, m.message, m.timestamp FROM {drupalchat_msg} m INNER JOIN {users} u ON m.uid1 = u.uid WHERE m.uid2 = %d AND m.timestamp > %d ORDER BY m.timestamp ASC', $user->uid, $last_timestamp);
    _drupalchat_touch_user($user->uid);
    module_invoke_all('drupalchat_ajaxpoll');

    // AJAX poll hook
  }
  elseif ($polling_method == DRUPALCHAT_LONGPOLL) {
    do {
      sleep(3);
      $checkUnlog = "SELECT timestamp FROM {sessions} WHERE sid='%s' AND uid =0";
      $result = db_query($checkUnlog, $user->sid);
      $filas = db_affected_rows($result);
      if ($filas != 0) {
        $user->uid = 0;

        //exit(1);
      }
      $buddylist_online = _drupalchat_buddylist_online($buddylist);
      if ($user->uid > 0) {
        $message_count = db_result(db_query('SELECT COUNT(*) FROM {drupalchat_msg} m WHERE (m.uid2 IN (\'%s\',\'c-0\') OR m.uid1 = \'%s\') AND m.timestamp > %d', $user->uid, $user->uid, $last_timestamp));
      }
      else {
        $message_count = db_result(db_query('SELECT COUNT(*) FROM {drupalchat_msg} m WHERE (m.uid2 IN (\'%s\',\'c-0\') OR m.uid1 = \'%s\') AND m.timestamp > %d', '0-' . _drupalchat_get_sid(), '0-' . _drupalchat_get_sid(), $last_timestamp));
      }
      _drupalchat_touch_user($user->uid);
      module_invoke_all('drupalchat_longpoll');

      // Long poll hook
    } while (time() - $initial_time < ini_get('max_execution_time') - 5 && $message_count == 0 && _drupalchat_buddylist_diff($buddylist_online_old, $buddylist_online));
  }
  $json['messages'] = array();
  if ($message_count > 0 || $polling_method == DRUPALCHAT_AJAX) {
    if ($user->uid > 0) {
      $messages = db_query('SELECT m.message_id, m.uid1, m.uid2, m.message, m.timestamp FROM {drupalchat_msg} m WHERE (m.uid2 IN (\'%s\',\'c-0\') OR m.uid1 = \'%s\') AND m.timestamp > %d ORDER BY m.timestamp ASC', $user->uid, $user->uid, $last_timestamp);
    }
    else {
      $messages = db_query('SELECT m.message_id, m.uid1, m.uid2, m.message, m.timestamp FROM {drupalchat_msg} m WHERE (m.uid2 IN (\'%s\',\'c-0\') OR m.uid1 = \'%s\') AND m.timestamp > %d ORDER BY m.timestamp ASC', '0-' . _drupalchat_get_sid(), '0-' . _drupalchat_get_sid(), $last_timestamp);
    }

    //$last_timestamp = time();
    while ($message = db_fetch_object($messages)) {
      if (!strpos($message->uid1, '-') && $message->uid1 != $user->uid || strpos($message->uid1, '-') && $message->uid1 != '0-' . _drupalchat_get_sid()) {
        if (!strpos($message->uid1, '-')) {
          $json['messages'][] = array(
            'message' => $message->message,
            'timestamp' => date("H:i", $message->timestamp),
            'uid1' => $message->uid1,
            'name' => _drupalchat_get_username($message->uid1),
            'uid2' => $message->uid2,
          );
        }
        else {
          $arr = explode("-", $message->uid1, 2);
          $sid = $arr[1];
          $name = db_result(db_query('SELECT name FROM {drupalchat_users} WHERE uid = %d AND session = \'%s\'', 0, $sid));
          $json['messages'][] = array(
            'message' => $message->message,
            'timestamp' => date("H:i", $message->timestamp),
            'uid1' => $message->uid1,
            'name' => $name,
            'uid2' => $message->uid2,
          );
        }
      }
      else {
        if (!strpos($message->uid2, '-')) {
          $json['messages'][] = array(
            'message' => $message->message,
            'timestamp' => date("H:i", $message->timestamp),
            'uid1' => $message->uid1,
            'name' => _drupalchat_get_username($message->uid2),
            'uid2' => $message->uid2,
            'message_id' => $message->message_id,
          );
        }
        else {
          $arr = explode("-", $message->uid2, 2);
          $sid = $arr[1];
          $name = db_result(db_query('SELECT name FROM {drupalchat_users} WHERE uid = %d AND session = \'%s\'', 0, $sid));
          $json['messages'][] = array(
            'message' => $message->message,
            'timestamp' => date("H:i", $message->timestamp),
            'uid1' => $message->uid1,
            'name' => $name,
            'uid2' => $message->uid2,
            'message_id' => $message->message_id,
          );
        }
      }
      if ($message->timestamp > $last_timestamp) {
        $last_timestamp = $message->timestamp;
      }
    }
  }

  /*else {
      $last_timestamp = time();
    }*/
  $json['status'] = 0;
  $json['total_messages'] = $message_count;
  $json['last_timestamp'] = $last_timestamp;
  $json['buddylist'] = isset($buddylist_online) ? $buddylist_online : $buddylist_online_old;
  drupal_json($json);
}