Split friends and blacklist in separete tables
authorDuck (Jakob Munih) <duck@obala.net>
Mon, 16 Feb 2009 09:50:05 +0000 (10:50 +0100)
committerDuck (Jakob Munih) <duck@obala.net>
Mon, 16 Feb 2009 09:50:40 +0000 (10:50 +0100)
folks/config/conf.xml
folks/lib/Friends.php
folks/lib/Friends/letter.php [deleted file]
folks/lib/Friends/shared.php
folks/lib/Friends/sql.php
folks/scripts/sql/folks.mysql.sql
folks/scripts/upgrades/2009-02-16_friends.sql [new file with mode: 0644]

index ea6f447..a5932ae 100644 (file)
 
 <configtab name="general" desc="General">
    <configstring name="support" desc="Email from which support messages will be sent">support@example.com</configstring>
-   <configenum name="friends" desc="Driver for friends storage">sql
-     <values>
-       <value desc="Internal - friends and blacklist only">sql</value>
-       <value desc="Internal - shared lists">shared</value>
-       <value desc="Letter app">letter</value>
-     </values>
-   </configenum>
+   <configswitch name="friends" desc="Driver for friends storage">sql
+       <case name="prefs" desc="Prefs - friends and blacklist only sored"></case>
+       <case name="sql" desc="SQL - friends and blacklist only"></case>
+       <case name="shared" desc="Shared - custom shared groups lists"></case>
+       <case name="application" desc="Application">
+        <configsection name="params">
+        <configenum name="app" desc="The application which is providing friends">letter
+        <values>
+            <configspecial name="list-horde-apps"/>
+        </values>
+        </configenum>
+        </configsection>
+       </case>
+   </configswitch>
 
  <configsection name="images">
    <configheader>Images</configheader>
index d2dc7e4..8ad7d8f 100644 (file)
@@ -56,7 +56,7 @@ class Folks_Friends {
      * @return Folks_Friends  The newly created concrete Folks_Friends
      *                          instance, or false on an error.
      */
-    static public function factory($driver = null, $params = null)
+    private static function factory($driver = null, $params = null)
     {
         if ($driver === null) {
             $driver = $GLOBALS['conf']['friends'];
@@ -102,7 +102,7 @@ class Folks_Friends {
      * @param array $params   A hash containing any additional configuration
      *                        or connection parameters a subclass might need.
      */
-    public function __construct($params)
+    protected function __construct($params)
     {
         $this->_user = empty($params['user']) ? Auth::getAuth() : $params['user'];
 
diff --git a/folks/lib/Friends/letter.php b/folks/lib/Friends/letter.php
deleted file mode 100644 (file)
index f8e58ef..0000000
+++ /dev/null
@@ -1,91 +0,0 @@
-<?php
-/**
- * Folks_Friends:: defines an API for implementing storage backends for
- * Folks.
- *
- * $Id: letter.php 777 2008-08-21 09:23:07Z duck $
- *
- * Copyright Obala d.o.o. (www.obala.si)
- *
- * See the enclosed file COPYING for license information (GPL). If you
- * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
- *
- * @author Duck <duck@obala.net>
- * @package Folks
- */
-class Folks_Friends_letter extends Folks_Friends {
-
-    /**
-     * Get user blacklist
-     *
-     * @return array of users blacklist
-     */
-    protected function _getBlacklist()
-    {
-        return $GLOBALS['registry']->callByPackage('letter', 'getBlacklist', array($this->_user));
-    }
-
-    /**
-     * Add user to a blacklist list
-     *
-     * @param string $user   Usersame
-     */
-    protected function _addBlacklisted($user)
-    {
-        return $GLOBALS['registry']->callByPackage('letter', 'addFriend', array($user, 'blacklist', $this->_user));
-    }
-    
-    /**
-     * Remove user from a fiend list
-     *
-     * @param string $user   Usersame
-     */
-    protected function _removeBlacklisted($user)
-    {
-        return $GLOBALS['registry']->callByPackage('letter', 'addFriend', array($user, 'blacklist', $this->_user));
-    }
-    
-    /**
-     * Add user to a friend list
-     *
-     * @param string $friend   Friend's usersame
-     * @param string $group  Group to add friend to
-     */
-    protected function _addFriend($friend, $group = null)
-    {
-        return $GLOBALS['registry']->callByPackage('letter', 'addFriend', array($friend, 'whitelist', $this->_user));
-    }
-    
-    /**
-     * Remove user from a fiend list
-     *
-     * @param string $friend   Friend's usersame
-     * @param string $group   Group to remove friend from
-     */
-    protected function _removeFriend($friend,  $group = null)
-    {
-        return $GLOBALS['registry']->callByPackage('letter', 'addFriend', array($friend, 'whitelist', $this->_user));
-    }
-
-    /**
-     * Get user friends
-     *
-     * @param string $group  Get friens only from this group
-     *
-     * @return array of users (in group)
-     */
-    protected function _getFriends($group = null)
-    {
-        return $GLOBALS['registry']->callByPackage('letter', 'getFriends', array($this->_user, 'whitelist'));
-    }
-    
-    /**
-     * Get user groups
-     */
-    public function getGroups()
-    {
-        return array('whitelist' => _("Whitelist"),
-                                'blacklist' => _("Blacklist"));
-    }
-}
-
index 50a6fe2..0979833 100644 (file)
@@ -8,7 +8,7 @@ require_once dirname(__FILE__) . '/sql.php';
  *
  * $Id: shared.php 1247 2009-01-30 15:01:34Z duck $
  *
- * Copyright Obala d.o.o. (www.obala.si)
+ * Copyright 2007-2009 The Horde Project (http://www.horde.org/)
  *
  * See the enclosed file COPYING for license information (GPL). If you
  * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
@@ -16,14 +16,16 @@ require_once dirname(__FILE__) . '/sql.php';
  * @author Duck <duck@obala.net>
  * @package Folks
  */
-class Folks_Friends_shared extends  Folks_Friends_sql  {
+class Folks_Friends_shared extends  Folks_Friends_sql {
+
+    const CUSTOM = 3;
 
     /**
      * friends list ID
      *
      * @var int
      */
-    private $_friends;
+    private $_whitelist;
 
     /**
      * Black list ID
@@ -33,11 +35,30 @@ class Folks_Friends_shared extends  Folks_Friends_sql  {
     private $_blacklist;
 
     /**
+     * Get whitelist ID
+     */
+    protected function _id($id)
+    {
+        switch ($id) {
+
+        case self::BLACKLIST;
+            return $this->_blacklist;
+
+        case self::WHITELIST;
+            return $this->_whitelist;
+
+        default:
+            return $id;
+
+        }
+    }
+
+    /**
      * Get user friends and blacklist group id
      */
     private function _getIds()
     {
-        if ($this->_friends && $this->_blacklist) {
+        if ($this->_whitelist && $this->_blacklist) {
             return;
         }
 
@@ -46,12 +67,12 @@ class Folks_Friends_shared extends  Folks_Friends_sql  {
         if ($groups instanceof PEAR_Error) {
             return $groups;
         }
-
+var_dump($groups);
         foreach ($groups as $id => $group) {
             if ($group->get('type') == self::BLACKLIST) {
                 $this->_blacklist = $group->getId();
             } elseif ($group->get('type') == self::WHITELIST) {
-                $this->_friends = $group->getId();
+                $this->_whitelist = $group->getId();
             }
         }
     }
@@ -65,11 +86,12 @@ class Folks_Friends_shared extends  Folks_Friends_sql  {
     {
         $this->_getIds();
 
+        // No blacklist even created
         if (empty($this->_blacklist)) {
             return array();
         }
 
-        parent::_getBlacklist();
+        return parent::_getBlacklist();
     }
 
     /**
@@ -81,14 +103,16 @@ class Folks_Friends_shared extends  Folks_Friends_sql  {
     {
         $this->_getIds();
 
+        // Create blacklist
         if (empty($this->_blacklist)) {
-            $result = $this->addGroup('_BLACKLIST_', self::BLACKLIST);
-            if ($result instanceof PEAR_Error) {
-                return $result;
+            $group_id = $this->addGroup('_BLACKLIST_', self::BLACKLIST);
+            if ($group_id instanceof PEAR_Error) {
+                return $group_id;
             }
+            $this->_blacklist = $group_id;
         }
 
-        parent::_addBlacklisted($user);
+        return parent::_addBlacklisted($user);
     }
 
     /**
@@ -117,11 +141,12 @@ class Folks_Friends_shared extends  Folks_Friends_sql  {
     {
         $this->_getIds();
 
-        if (empty($this->_friends)) {
-            $result = $this->addGroup('_FRIENDS_', self::WHITELIST);
-            if ($result instanceof PEAR_Error) {
-                return $result;
+        if (empty($this->_whitelist)) {
+            $group_id = $this->addGroup('_FRIENDS_', self::WHITELIST);
+            if ($group_id instanceof PEAR_Error) {
+                return $group_id;
             }
+            $this->_whitelist = $group_id;
         }
 
         parent::_addFriend($friend, $group);
@@ -137,7 +162,7 @@ class Folks_Friends_shared extends  Folks_Friends_sql  {
     {
         $this->_getIds();
 
-        if (empty($this->_friends)) {
+        if (empty($this->_whitelist)) {
             return true;
         }
 
@@ -148,19 +173,18 @@ class Folks_Friends_shared extends  Folks_Friends_sql  {
      * Get user friends
      *
      * @param string $group  Get friens only from this group
-     * @param boolean $ask   Show all users or only them who approved us
      *
      * @return array of users (in group)
      */
-    protected function _getFriends($group = null, $ask = false)
+    protected function _getFriends($group = null)
     {
         $this->_getIds();
 
-        if (empty($this->_friends)) {
+        if (empty($this->_whitelist)) {
             return array();
         }
 
-        parent::_getFriends($group, $ask);
+        parent::_getFriends($group);
     }
 
     /**
@@ -176,13 +200,15 @@ class Folks_Friends_shared extends  Folks_Friends_sql  {
 
         $list = array();
         foreach ($groups as $id => $group) {
-            $list[$id] = $group->get('name');
-
             // set friends ids
             if ($group->get('type') == self::BLACKLIST) {
                 $this->_blacklist = $id;
+                $list[$id] = _("Blacklist");
             } elseif ($group->get('type') == self::WHITELIST) {
-                $this->_friends = $id;
+                $this->_whitelist = $id;
+                $list[$id] = _("Friends");
+            } else {
+                $list[$id] = $group->get('name');
             }
         }
 
@@ -230,6 +256,8 @@ class Folks_Friends_shared extends  Folks_Friends_sql  {
 
         if ($type !== null) {
             $share->set('type', $type);
+        } else {
+            $share->set('type', self::CUSTOM);
         }
 
         return $GLOBALS['folks_shares']->addShare($share);
index 71c9eb6..8241f79 100644 (file)
@@ -5,7 +5,7 @@
  *
  * $Id: sql.php 1247 2009-01-30 15:01:34Z duck $
  *
- * Copyright Obala d.o.o. (www.obala.si)
+ * Copyright 2007-2009 The Horde Project (http://www.horde.org/)
  *
  * See the enclosed file COPYING for license information (GPL). If you
  * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
@@ -38,9 +38,10 @@ class Folks_Friends_sql extends Folks_Friends {
      *
      * @param array $params  A hash containing connection parameters.
      */
-    public function __construct($params = array())
+    protected function __construct($params = array())
     {
         parent::__construct($params);
+
         $this->_params = $params;
         $this->_connect();
     }
@@ -52,11 +53,11 @@ class Folks_Friends_sql extends Folks_Friends {
      */
     protected function _getBlacklist()
     {
-        $query = 'SELECT friend_uid FROM ' . $this->_params['friends']
-                . ' WHERE user_uid = ? AND group_id = ? '
+        $query = 'SELECT friend_uid FROM ' . $this->_params['blacklist']
+                . ' WHERE user_uid = ? '
                 . ' ORDER BY friend_uid ASC';
 
-        return $this->_db->getCol($query, 0, array($this->_user, self::BLACKLIST));
+        return $this->_db->getCol($query, 0, array($this->_user));
     }
 
     /**
@@ -66,9 +67,10 @@ class Folks_Friends_sql extends Folks_Friends {
      */
     protected function _addBlacklisted($user)
     {
-        $query = 'INSERT INTO ' . $this->_params['friends']
-                        . ' (user_uid, group_id, friend_uid, friend_ask) VALUES (?, ?, ?, ?)';
-        return $this->_write_db->query($query, array($this->_user, self::BLACKLIST, $user, 0));
+        $query = 'INSERT INTO ' . $this->_params['blacklist']
+                        . ' (user_uid, friend_uid) VALUES (?, ?)';
+
+        return $this->_write_db->query($query, array($this->_user, $user));
     }
 
     /**
@@ -78,22 +80,24 @@ class Folks_Friends_sql extends Folks_Friends {
      */
     protected function _removeBlacklisted($user)
     {
-        $query = 'DELETE FROM ' . $this->_params['friends'] . ' WHERE user_uid = ? AND group_id = ? AND friend_uid = ?';
-        return $this->_write_db->query($query, array($this->_user, self::BLACKLIST, $user));
+        $query = 'DELETE FROM ' . $this->_params['blacklist']
+                    . ' WHERE user_uid = ? AND friend_uid = ?';
+
+        return $this->_write_db->query($query, array($this->_user, $user));
     }
 
     /**
      * Add user to a friend list
      *
      * @param string $friend   Friend's usersame
-     * @param string $group   Group to add friend to
      */
-    protected function _addFriend($friend, $group = null)
+    protected function _addFriend($friend)
     {
         $approve = $this->needsApproval($friend) ? 1 : 0;
         $query = 'INSERT INTO ' . $this->_params['friends']
-                . ' (user_uid, group_id, friend_uid, friend_ask) VALUES (?, ?, ?, ?)';
-        return $this->_write_db->query($query, array($this->_user, self::WHITELIST, $friend, $approve));
+                . ' (user_uid, friend_uid, friend_ask) VALUES (?, ?, ?, ?)';
+
+        return $this->_write_db->query($query, array($this->_user, $friend, $approve));
     }
 
     /**
@@ -105,6 +109,7 @@ class Folks_Friends_sql extends Folks_Friends {
     {
         $query = 'UPDATE ' . $this->_params['friends']
                 . ' SET friend_ask = ? WHERE user_uid = ? AND friend_uid = ?';
+
         $result = $this->_write_db->query($query, array(0, $friend, $this->_user));
         if ($result instanceof PEAR_Error) {
             return $result;
@@ -112,36 +117,36 @@ class Folks_Friends_sql extends Folks_Friends {
 
         // Add user even to firend's friend list
         $query = 'REPLACE INTO ' . $this->_params['friends']
-                . ' (user_uid, group_id, friend_uid, friend_ask) VALUES (?, ?, ?, ?)';
-        return $this->_write_db->query($query, array($this->_user, self::WHITELIST, $friend, 0));
+                . ' (user_uid, friend_uid, friend_ask) VALUES (?, ?, ?)';
+
+        return $this->_write_db->query($query, array($this->_user, $friend, 0));
     }
 
     /**
      * Remove user from a fiend list
      *
      * @param string $friend   Friend's usersame
-     * @param string $group   Group to remove friend from
      */
-    protected function _removeFriend($friend, $group = null)
+    protected function _removeFriend($friend)
     {
-        $query = 'DELETE FROM ' . $this->_params['friends'] . ' WHERE user_uid = ? AND group_id = ? AND friend_uid = ?';
-        return $this->_write_db->query($query, array($this->_user, self::WHITELIST, $friend));
+        $query = 'DELETE FROM ' . $this->_params['friends']
+                    . ' WHERE user_uid = ? AND friend_uid = ?';
+
+        return $this->_write_db->query($query, array($this->_user, $friend));
     }
 
     /**
      * Get user friends
      *
-     * @param string $group  Get friens only from this group
-     *
-     * @return array of users (in group)
+     * @return array of user's friends
      */
-    protected function _getFriends($group = null)
+    protected function _getFriends()
     {
         $query = 'SELECT friend_uid FROM ' . $this->_params['friends']
-                . ' WHERE user_uid = ? AND group_id = ?'
+                . ' WHERE user_uid = ?'
                 . ' ORDER BY friend_uid ASC';
 
-        return $this->_db->getCol($query, 0, array($this->_user, self::WHITELIST));
+        return $this->_db->getCol($query, 0, array($this->_user));
     }
 
     /**
@@ -176,20 +181,10 @@ class Folks_Friends_sql extends Folks_Friends {
     public function getPossibleFriends()
     {
         $query = 'SELECT user_uid FROM ' . $this->_params['friends']
-                . ' WHERE friend_uid = ? AND group_id = ? AND friend_ask = ?'
+                . ' WHERE friend_uid = ? AND friend_ask = ?'
                 . ' ORDER BY friend_uid ASC';
 
-        return $this->_db->getCol($query, 0, array($this->_user, self::WHITELIST, 0));
-    }
-
-    /**
-     * Get users friends birthdays
-     *
-     * @return array users
-     */
-    public function getBirthdays()
-    {
-        return false;
+        return $this->_db->getCol($query, 0, array($this->_user, 0));
     }
 
     /**
@@ -219,8 +214,15 @@ class Folks_Friends_sql extends Folks_Friends {
         if (!isset($this->_params['hostspec'])) {
             $this->_params['hostspec'] = '';
         }
+        if (!isset($this->_params['friends'])) {
+            $this->_params['friends'] = 'folks_friends';
+        }
+        if (!isset($this->_params['blacklist'])) {
+            $this->_params['blacklist'] = 'folks_blacklist';
+        }
 
         /* Connect to the SQL server using the supplied parameters. */
+        require_once 'DB.php';
         $this->_write_db = DB::connect($this->_params,
                                         array('persistent' => !empty($this->_params['persistent'])));
         if ($this->_write_db instanceof PEAR_Error) {
index eb8c72e..a5a6c6a 100644 (file)
@@ -9,6 +9,16 @@ CREATE TABLE IF NOT EXISTS folks_attributes (
   KEY attributes_group (attributes_group)
 );
 
+-- friends SQL
+CREATE TABLE IF NOT EXISTS `folks_friends` (
+  `user_uid` VARCHAR(32) NOT NULL,
+  `friend_uid` VARCHAR(32) NOT NULL,
+  `friend_ask` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
+  PRIMARY KEY  (`user_uid`, `friend_uid`),
+  KEY `friend_ask` (`friend_ask`)
+);
+
+-- friends SHARED
 CREATE TABLE IF NOT EXISTS `folks_friends` (
   `user_uid` VARCHAR(32) NOT NULL,
   `group_id` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
@@ -21,6 +31,13 @@ CREATE TABLE IF NOT EXISTS `folks_friends` (
   KEY `friend_ask` (`friend_ask`)
 );
 
+-- BLACKLIST
+CREATE TABLE IF NOT EXISTS `folks_blacklist` (
+  `user_uid` VARCHAR(32) NOT NULL,
+  `friend_uid` VARCHAR(32) NOT NULL,
+  PRIMARY KEY  (`user_uid`, `friend_uid`)
+);
+
 CREATE TABLE IF NOT EXISTS folks_notify_counts (
   user_uid VARCHAR(32) NOT NULL,
   count_news SMALLINT(5) UNSIGNED NOT NULL DEFAULT '0',
diff --git a/folks/scripts/upgrades/2009-02-16_friends.sql b/folks/scripts/upgrades/2009-02-16_friends.sql
new file mode 100644 (file)
index 0000000..62263aa
--- /dev/null
@@ -0,0 +1,8 @@
+CREATE TABLE IF NOT EXISTS `folks_blacklist` (
+  `user_uid` VARCHAR(32) NOT NULL,
+  `friend_uid` VARCHAR(32) NOT NULL,
+  PRIMARY KEY  (`user_uid`, `friend_uid`)
+);
+
+INSERT folks_blacklist (`user_uid`, `friend_uid`)
+    SELECT `user_uid`, `friend_uid` FROM `folks_friends` WHERE `group_id` = 0;
\ No newline at end of file