From fa33dd07d50fdb81d4bc1e384956ba3b130d4d56 Mon Sep 17 00:00:00 2001 From: "Duck (Jakob Munih)" Date: Mon, 16 Feb 2009 10:50:05 +0100 Subject: [PATCH] Split friends and blacklist in separete tables --- folks/config/conf.xml | 21 ++++--- folks/lib/Friends.php | 4 +- folks/lib/Friends/letter.php | 91 --------------------------- folks/lib/Friends/shared.php | 74 +++++++++++++++------- folks/lib/Friends/sql.php | 78 ++++++++++++----------- folks/scripts/sql/folks.mysql.sql | 17 +++++ folks/scripts/upgrades/2009-02-16_friends.sql | 8 +++ 7 files changed, 132 insertions(+), 161 deletions(-) delete mode 100644 folks/lib/Friends/letter.php create mode 100644 folks/scripts/upgrades/2009-02-16_friends.sql diff --git a/folks/config/conf.xml b/folks/config/conf.xml index ea6f4478a..a5932ae94 100644 --- a/folks/config/conf.xml +++ b/folks/config/conf.xml @@ -28,13 +28,20 @@ support@example.com - sql - - sql - shared - letter - - + sql + + + + + + letter + + + + + + + Images diff --git a/folks/lib/Friends.php b/folks/lib/Friends.php index d2dc7e4cb..8ad7d8fac 100644 --- a/folks/lib/Friends.php +++ b/folks/lib/Friends.php @@ -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 index f8e58efdf..000000000 --- a/folks/lib/Friends/letter.php +++ /dev/null @@ -1,91 +0,0 @@ - - * @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")); - } -} - diff --git a/folks/lib/Friends/shared.php b/folks/lib/Friends/shared.php index 50a6fe2c4..097983338 100644 --- a/folks/lib/Friends/shared.php +++ b/folks/lib/Friends/shared.php @@ -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 * @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); diff --git a/folks/lib/Friends/sql.php b/folks/lib/Friends/sql.php index 71c9eb684..8241f792d 100644 --- a/folks/lib/Friends/sql.php +++ b/folks/lib/Friends/sql.php @@ -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) { diff --git a/folks/scripts/sql/folks.mysql.sql b/folks/scripts/sql/folks.mysql.sql index eb8c72e1f..a5a6c6aeb 100644 --- a/folks/scripts/sql/folks.mysql.sql +++ b/folks/scripts/sql/folks.mysql.sql @@ -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 index 000000000..62263aa65 --- /dev/null +++ b/folks/scripts/upgrades/2009-02-16_friends.sql @@ -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 -- 2.11.0