static function send($user, $group, $text) { if (!$user->hasRight(Right::NEWMESSAGE)) { // XXX: maybe break this out into a separate right // TRANS: Exception thrown when trying to send group private message without having the right to do that. // TRANS: %s is a user nickname. throw new Exception(sprintf(_m('User %s is not allowed to send private messages.'), $user->nickname)); } Group_privacy_settings::ensurePost($user, $group); $text = $user->shortenLinks($text); // We use the same limits as for 'regular' private messages. if (Message::contentTooLong($text)) { // TRANS: Exception thrown when trying to send group private message that is too long. // TRANS: %d is the maximum meggage length. throw new Exception(sprintf(_m('That\'s too long. Maximum message size is %d character.', 'That\'s too long. Maximum message size is %d characters.', Message::maxContent()), Message::maxContent())); } // Valid! Let's do this thing! $gm = new Group_message(); $gm->id = UUID::gen(); $gm->uri = common_local_url('showgroupmessage', array('id' => $gm->id)); $gm->from_profile = $user->id; $gm->to_group = $group->id; $gm->content = $text; // XXX: is this cool?! $gm->rendered = common_render_text($text); $gm->url = $gm->uri; $gm->created = common_sql_now(); // This throws a conniption if there's a problem $gm->insert(); $gm->distribute(); return $gm; }
/** * For initializing members of the class. * * @param array $argarray misc. arguments * * @return boolean true */ function prepare($argarray) { parent::prepare($argarray); $this->user = common_current_user(); if (empty($this->user)) { throw new ClientException(_('Must be logged in.'), 403); } if (!$this->user->hasRight(Right::NEWMESSAGE)) { throw new Exception(sprintf(_('User %s not allowed to send private messages.'), $this->user->nickname)); } $nicknameArg = $this->trimmed('nickname'); $nickname = common_canonical_nickname($nicknameArg); if ($nickname != $nicknameArg) { $url = common_local_url('newgroupmessage', array('nickname' => $nickname)); common_redirect($url, 301); return false; } $localGroup = Local_group::staticGet('nickname', $nickname); if (empty($localGroup)) { throw new ClientException(_('No such group'), 404); } $this->group = User_group::staticGet('id', $localGroup->group_id); if (empty($this->group)) { throw new ClientException(_('No such group'), 404); } // This throws an exception on error Group_privacy_settings::ensurePost($this->user, $this->group); // If we're posted to, check session token and get text if ($this->isPost()) { $this->checkSessionToken(); $this->text = $this->trimmed('content'); } return true; }
/** * To add a "Message" button to the group profile page * * @param Action $action The showgroup action being shown * @param User_group $group The current group * * @return boolean hook value */ function onEndGroupActionsList($action, $group) { $cur = common_current_user(); if (empty($cur)) { return true; } try { Group_privacy_settings::ensurePost($cur, $group); } catch (Exception $e) { return true; } $action->elementStart('li', 'entity_send-a-message'); $action->element('a', array('href' => common_local_url('newgroupmessage', array('nickname' => $group->nickname)), 'title' => _('Send a direct message to this group')), _('Message')); // $form = new GroupMessageForm($action, $group); // $form->hidden = true; // $form->show(); $action->elementEnd('li'); return true; }