Merge branch 'groupalias' into 0.8.x

This commit is contained in:
Evan Prodromou 2009-06-14 23:48:24 -07:00
commit 4dad3191f6
11 changed files with 262 additions and 10 deletions

8
README
View File

@ -1196,7 +1196,6 @@ reporturl: URL to post statistics to. Defaults to Laconica developers'
set 'run' to 'never' than to set this value to something
nonsensical.
attachments
-----------
@ -1226,6 +1225,13 @@ user_quota: total size in bytes a user can store on this server. Each user
monthly_quota: total size permitted in the current month. This is the total
size in bytes that a user can upload each month.
group
-----
Options for group functionality.
maxaliases: maximum number of aliases a group can have. Default 3. Set
to 0 or less to prevent aliases in a group.
Troubleshooting
===============

View File

@ -171,6 +171,7 @@ class EditgroupAction extends Action
$homepage = $this->trimmed('homepage');
$description = $this->trimmed('description');
$location = $this->trimmed('location');
$aliasstring = $this->trimmed('aliases');
if (!Validate::string($nickname, array('min_length' => 1,
'max_length' => 64,
@ -201,6 +202,39 @@ class EditgroupAction extends Action
return;
}
if (!empty($aliasstring)) {
$aliases = array_map('common_canonical_nickname', array_unique(preg_split('/[\s,]+/', $aliasstring)));
} else {
$aliases = array();
}
if (count($aliases) > common_config('group', 'maxaliases')) {
$this->showForm(sprintf(_('Too many aliases! Maximum %d.'),
common_config('group', 'maxaliases')));
return;
}
foreach ($aliases as $alias) {
if (!Validate::string($alias, array('min_length' => 1,
'max_length' => 64,
'format' => NICKNAME_FMT))) {
$this->showForm(sprintf(_('Invalid alias: "%s"'), $alias));
return;
}
if ($this->nicknameExists($alias)) {
$this->showForm(sprintf(_('Alias "%s" already in use. Try another one.'),
$alias));
return;
}
// XXX assumes alphanum nicknames
if (strcmp($alias, $nickname) == 0) {
$this->showForm(_('Alias can\'t be the same as nickname.'));
return;
}
}
$this->group->query('BEGIN');
$orig = clone($this->group);
$this->group->nickname = $nickname;
@ -217,6 +251,14 @@ class EditgroupAction extends Action
$this->serverError(_('Could not update group.'));
}
$result = $this->group->setAliases($aliases);
if (!$result) {
$this->serverError(_('Could not create aliases.'));
}
$this->group->query('COMMIT');
if ($this->group->nickname != $orig->nickname) {
common_redirect(common_local_url('editgroup',
array('nickname' => $nickname)),
@ -229,9 +271,20 @@ class EditgroupAction extends Action
function nicknameExists($nickname)
{
$group = User_group::staticGet('nickname', $nickname);
return (!is_null($group) &&
$group != false &&
$group->id != $this->group->id);
if (!empty($group) &&
$group->id != $this->group->id) {
return true;
}
$alias = Group_alias::staticGet('alias', $nickname);
if (!empty($alias) &&
$alias->group_id != $this->group->id) {
return true;
}
return false;
}
}

View File

@ -123,6 +123,7 @@ class NewgroupAction extends Action
$homepage = $this->trimmed('homepage');
$description = $this->trimmed('description');
$location = $this->trimmed('location');
$aliasstring = $this->trimmed('aliases');
if (!Validate::string($nickname, array('min_length' => 1,
'max_length' => 64,
@ -153,6 +154,37 @@ class NewgroupAction extends Action
return;
}
if (!empty($aliasstring)) {
$aliases = array_map('common_canonical_nickname', array_unique(preg_split('/[\s,]+/', $aliasstring)));
} else {
$aliases = array();
}
if (count($aliases) > common_config('group', 'maxaliases')) {
$this->showForm(sprintf(_('Too many aliases! Maximum %d.'),
common_config('group', 'maxaliases')));
return;
}
foreach ($aliases as $alias) {
if (!Validate::string($alias, array('min_length' => 1,
'max_length' => 64,
'format' => NICKNAME_FMT))) {
$this->showForm(sprintf(_('Invalid alias: "%s"'), $alias));
return;
}
if ($this->nicknameExists($alias)) {
$this->showForm(sprintf(_('Alias "%s" already in use. Try another one.'),
$alias));
return;
}
// XXX assumes alphanum nicknames
if (strcmp($alias, $nickname) == 0) {
$this->showForm(_('Alias can\'t be the same as nickname.'));
return;
}
}
$cur = common_current_user();
// Checked in prepare() above
@ -177,6 +209,12 @@ class NewgroupAction extends Action
$this->serverError(_('Could not create group.'));
}
$result = $group->setAliases($aliases);
if (!$result) {
$this->serverError(_('Could not create aliases.'));
}
$member = new Group_member();
$member->group_id = $group->id;
@ -199,7 +237,18 @@ class NewgroupAction extends Action
function nicknameExists($nickname)
{
$group = User_group::staticGet('nickname', $nickname);
return (!is_null($group) && $group != false);
if (!empty($group)) {
return true;
}
$alias = Group_alias::staticGet('alias', $nickname);
if (!empty($alias)) {
return true;
}
return false;
}
}

41
classes/Group_alias.php Normal file
View File

@ -0,0 +1,41 @@
<?php
/**
* Table Definition for group_alias
*
* Laconica - a distributed open-source microblogging tool
* Copyright (C) 2009, Control Yourself, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
if (!defined('LACONICA')) { exit(1); }
require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
class Group_alias extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
public $__table = 'group_alias'; // table name
public $alias; // varchar(64) primary_key not_null
public $group_id; // int(4) not_null
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
/* Static get */
function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Group_alias',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
}

View File

@ -752,9 +752,9 @@ class Notice extends Memcached_DataObject
foreach (array_unique($match[1]) as $nickname) {
/* XXX: remote groups. */
$group = User_group::staticGet('nickname', $nickname);
$group = User_group::getForNickname($nickname);
if (!$group) {
if (empty($group)) {
continue;
}

View File

@ -165,4 +165,78 @@ class User_group extends Memcached_DataObject
{
return ($this->fullname) ? $this->fullname : $this->nickname;
}
function getAliases()
{
$aliases = array();
// XXX: cache this
$alias = new Group_alias();
$alias->group_id = $this->id;
if ($alias->find()) {
while ($alias->fetch()) {
$aliases[] = $alias->alias;
}
}
$alias->free();
return $aliases;
}
function setAliases($newaliases) {
$newaliases = array_unique($newaliases);
$oldaliases = $this->getAliases();
# Delete stuff that's old that not in new
$to_delete = array_diff($oldaliases, $newaliases);
# Insert stuff that's in new and not in old
$to_insert = array_diff($newaliases, $oldaliases);
$alias = new Group_alias();
$alias->group_id = $this->id;
foreach ($to_delete as $delalias) {
$alias->alias = $delalias;
$result = $alias->delete();
if (!$result) {
common_log_db_error($alias, 'DELETE', __FILE__);
return false;
}
}
foreach ($to_insert as $insalias) {
$alias->alias = $insalias;
$result = $alias->insert();
if (!$result) {
common_log_db_error($alias, 'INSERT', __FILE__);
return false;
}
}
return true;
}
static function getForNickname($nickname)
{
$nickname = common_canonical_nickname($nickname);
$group = User_group::staticGet('nickname', $nickname);
if (!empty($group)) {
return $group;
}
$alias = Group_alias::staticGet('alias', $nickname);
if (!empty($alias)) {
return User_group::staticGet('id', $alias->group_id);
}
return null;
}
}

View File

@ -158,6 +158,14 @@ id = K
service = K
uri = U
[group_alias]
alias = 130
group_id = 129
modified = 384
[group_alias__keys]
alias = K
[group_block]
group_id = 129
blocked = 129

View File

@ -493,3 +493,13 @@ create table group_block (
constraint primary key (group_id, blocked)
) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;
create table group_alias (
alias varchar(64) primary key comment 'additional nickname for the group',
group_id integer not null comment 'group profile is blocked from' references user_group (id),
modified timestamp comment 'date alias was created',
index group_alias_group_id_idx (group_id)
) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;

View File

@ -198,6 +198,8 @@ $config =
'user_quota' => 50000000,
'monthly_quota' => 15000000,
),
'group' =>
array('maxaliases' => 3),
);
$config['db'] = &PEAR::getStaticProperty('DB_DataObject','options');

View File

@ -111,7 +111,6 @@ class GroupEditForm extends Form
}
}
/**
* Name of the form
*
@ -157,6 +156,16 @@ class GroupEditForm extends Form
($this->out->arg('location')) ? $this->out->arg('location') : $this->group->location,
_('Location for the group, if any, like "City, State (or Region), Country"'));
$this->out->elementEnd('li');
if (common_config('group', 'maxaliases') > 0) {
$aliases = (empty($this->group)) ? array() : $this->group->getAliases();
$this->out->elementStart('li');
$this->out->input('aliases', _('Aliases'),
($this->out->arg('aliases')) ? $this->out->arg('aliases') :
(!empty($aliases)) ? implode(' ', $aliases) : '',
sprintf(_('Extra nicknames for the group, comma- or space- separated, max %d'),
common_config('group', 'maxaliases')));;
$this->out->elementEnd('li');
}
$this->out->elementEnd('ul');
}

View File

@ -591,7 +591,7 @@ function common_at_link($sender_id, $nickname)
function common_group_link($sender_id, $nickname)
{
$sender = Profile::staticGet($sender_id);
$group = User_group::staticGet('nickname', common_canonical_nickname($nickname));
$group = User_group::getForNickname($nickname);
if ($group && $sender->isMember($group)) {
$attrs = array('href' => $group->permalink(),
'class' => 'url');