Exemplo n.º 1
0
 /**
  * User checks for new messages
  *
  * @return array sent to client as JSON
  */
 public function action_refresh()
 {
     if (!defined('PHPBB_USE_BOARD_URL_PATH')) {
         define('PHPBB_USE_BOARD_URL_PATH', true);
     }
     $message_first_id = $this->request->variable('message_first_id', 0);
     $message_last_id = $this->request->variable('message_last_id', 0);
     $message_edits = $this->request->variable('message_edits', array(0));
     // Request new messages
     $sql_where = 'm.message_id > ' . (int) $message_last_id;
     // Request edited messages
     if ($this->config['mchat_live_updates'] && $message_last_id > 0) {
         $sql_time_limit = $this->config['mchat_edit_delete_limit'] ? sprintf(' AND m.message_time > %d', time() - $this->config['mchat_edit_delete_limit']) : '';
         $sql_where .= sprintf(' OR (m.message_id BETWEEN %d AND %d AND m.edit_time > 0%s)', (int) $message_first_id, (int) $message_last_id, $sql_time_limit);
     }
     // Exclude post notifications
     if (!$this->user->data['user_mchat_topics']) {
         $sql_where = '(' . $sql_where . ') AND m.forum_id = 0';
     }
     $rows = $this->functions_mchat->mchat_get_messages($sql_where);
     $rows_refresh = array();
     $rows_edit = array();
     foreach ($rows as $row) {
         $message_id = $row['message_id'];
         if ($message_id > $message_last_id) {
             $rows_refresh[] = $row;
         } else {
             if (!isset($message_edits[$message_id]) || $message_edits[$message_id] < $row['edit_time']) {
                 $rows_edit[] = $row;
             }
         }
     }
     // Assign new messages
     $this->assign_global_template_data();
     $this->assign_messages($rows_refresh);
     $response = array('refresh' => true, 'add' => $this->render_template('mchat_messages.html'));
     // Assign edited messages
     if (!empty($rows_edit)) {
         $response['edit'] = array();
         foreach ($rows_edit as $row) {
             $this->assign_messages(array($row));
             $response['edit'][$row['message_id']] = $this->render_template('mchat_messages.html');
         }
     }
     // Request deleted messages
     if ($this->config['mchat_live_updates'] && $message_last_id > 0) {
         $deleted_message_ids = $this->functions_mchat->mchat_missing_ids($message_first_id, $message_last_id);
         if (!empty($deleted_message_ids)) {
             $response['del'] = $deleted_message_ids;
         }
     }
     return $response;
 }