chatroom use db_rewrite_sql() so third-parties can customize

Dave Cohen - June 9, 2009 - 22:39
Project:Chat Room
Version:6.x-1.0-beta2
Component:Code
Category:feature request
Priority:normal
Assigned:justinrandell
Status:closed
Description

Kudos on this awesome module. I'm very new to it, but so far its working great.

I work with Facebook, and wanted to make a view of each chatroom which shows only comments made by the current users facebook friends. I found that with a bit of drupal-fu I am able to do most of what I need. But I did need to patch chatroom.module and that's why I'm submitting this now.

This patch is against DRUPAL-6--1 branch, there was no option for me to choose that on the issue submit.

The patch simply calls db_rewrite_sql around the calls that build the comment list and user list for a given chatroom. So hopefully it will be easy to check in. If not let me know and let's talk about the best way to do what I'm trying to do.

Just so you see the way I'm working things, here's the bulk of my module (a prototype). Where normally there is simply a chatroom page, I create two local tabs. One for every comment in the chat, and another showing only my facebook friends. All this requires Drupal for Facebook to work.

<?php

define('FB_CHATROOM_GLOBAL_FILTER', 'fb_chatroom_filter');

function fb_chatroom_menu_alter(&$items) {
  if ($items['chatroom/chat/%']) {
    $items['chatroom/chat/%']['type'] = MENU_CALLBACK;

    $items['chatroom/chat/%/all'] = array(
      'title' => t('Everyone'),
      'type' => MENU_DEFAULT_LOCAL_TASK,
      'weight' => -10,
    );

    $items['chatroom/chat/%/fb/friends'] = array(
      'type' => MENU_LOCAL_TASK,
      'title' => t('My Friends'),
      'page callback' => 'fb_chatroom_view_chat',
      'page arguments' => array('friends', 2),
      'access callback' => 'fb_chatroom_access_helper',
      'access arguments' => array('access chat rooms', 'friends'),
    );

    $items['fb_chatroom/js/friends'] = array(
      'type' => MENU_CALLBACK,
      'page callback' => 'fb_chatroom_js',
      'page arguments' => array('friends'),
      'access callback' => 'fb_chatroom_access_helper',
      'access arguments' => array('access chat rooms', 'friends'),
    );

  }
}

function fb_chatroom_access_helper($perm, $filter) {
  global $fb;
  if ($fb && fb_facebook_user()) {
    return user_access($perm);
  }
}

function fb_chatroom_view_chat($filter, $ccid = NULL, $archive = FALSE) {
  $GLOBALS[FB_CHATROOM_GLOBAL_FILTER] = $filter;

  return chatroom_view_chat($ccid, $archive);
}

function fb_chatroom_js($filter) {
  $GLOBALS[FB_CHATROOM_GLOBAL_FILTER] = $filter;
 
  return chatroom_js();
}

function fb_chatroom_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'chatroom_chat_form' && $GLOBALS[FB_CHATROOM_GLOBAL_FILTER] == 'friends') {
    //dpm(func_get_args(), 'fb_chatroom_form_alter');
   
    if ($form['control']['submit']['#ahah']['path'] == 'chatroom/js') {
      // For ahah polling, use the friend filter.
      $form['control']['submit']['#ahah']['path'] = 'fb_chatroom/js/friends';
    }
  }
}

function fb_chatroom_db_rewrite_sql($query, $primary_table, $primary_field, $args) {
  if ($GLOBALS[FB_CHATROOM_GLOBAL_FILTER] == 'friends') {
    // Only show message by the current user and her friends.
    if ($args['chatroom_msg']) {
      $fbu = fb_facebook_user();
     
      $friends = fb_get_friends($fbu);
     
      $return = array();
      global $user;
      if (count($friends)) {
        $return['join'] = "LEFT JOIN {fb_user_app} fua ON fua.uid = $primary_table.uid";
        $return['where'] = "fua.fbu IN (" . implode(',', $friends) . ") OR $primary_table.uid = $user->uid";
      }
      else {
        $return['where'] = "$primary_table.uid = $user->uid";
      }
      return $return;
    }
    else if ($args['chatroom_online_list']) {
      $fbu = fb_facebook_user();
      $friends = fb_get_friends($fbu);
      $return = array();
      global $user;
      if (count($friends)) {
        $return['join'] = "LEFT JOIN {fb_user_app} fua ON fua.uid = u.uid";
        $return['where'] = "fua.fbu in (" . implode(',', $friends) . ") OR u.uid = $user->uid";
      }
      else {
        $return['where'] = "u.uid = $user->uid";
      }
      return $return;
    }
  }
}

AttachmentSize
chatroom_db_rewrite.diff1.4 KB

#1

Dave Cohen - June 9, 2009 - 22:41
Title:use db_rewrite_sql so third-parties can customize» chatroom use db_rewrite_sql() so third-parties can customize

One more time, without the debug output.

AttachmentSize
chatroom_db_rewrite.diff 1.18 KB

#2

Dave Cohen - June 11, 2009 - 23:25

Hey, I'm working on what I think could be a really slick use of chatroom. I've submitted two issues in the past days and received no reply. Can I get some feedback? Is chatroom actively supported?

Check out this prototype on my local server (not guaranteed to be up for long). If you click the facebook button, you'll see two tabs on the chat. One limits the view to only your friends.

You'll see from the size of the patch that I need limited changes to chatroom in order to support this. Don't be confused by the size of the code I pasted into the post above.

#3

Dave Cohen - July 21, 2009 - 04:52
Status:active» needs review

Any chance to get this change in?

#4

justinrandell - September 12, 2009 - 14:50
Assigned to:Anonymous» justinrandell

thanks, i've committed this to DRUPAL-6--1. the module is still pretty ugly, but getting towards usable again.

#5

justinrandell - September 12, 2009 - 14:50
Status:needs review» fixed

#6

System Message - September 26, 2009 - 15:00
Status:fixed» closed

Automatically closed -- issue fixed for 2 weeks with no activity.

 
 

Drupal is a registered trademark of Dries Buytaert.