3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * Show a single membership as an Activity Streams entry
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * @author Evan Prodromou <evan@status.net>
26 * @copyright 2010 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28 * @link http://status.net/
31 if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
34 * Show (or delete) a single membership event as an ActivityStreams entry
38 * @author Evan Prodromou <evan@status.net>
39 * @copyright 2010 StatusNet, Inc.
40 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
41 * @link http://status.net/
43 class AtompubshowmembershipAction extends AtompubAction
45 private $_private = null;
46 private $_group = null;
47 private $_membership = null;
49 protected function atompubPrepare()
51 $this->_profile = Profile::getKV('id', $this->trimmed('profile'));
53 if (!$this->_profile instanceof Profile) {
54 // TRANS: Client exception.
55 throw new ClientException(_('No such profile.'), 404);
58 $this->_group = User_group::getKV('id', $this->trimmed('group'));
60 if (!$this->_group instanceof User_group) {
61 // TRANS: Client exception thrown when referencing a non-existing group.
62 throw new ClientException(_('No such group.'), 404);
65 $kv = array('group_id' => $groupId,
66 'profile_id' => $this->_profile->id);
68 $this->_membership = Group_member::pkeyGet($kv);
70 if (!$this->_membership instanceof Group_member) {
71 // TRANS: Client exception thrown when trying to show membership of a non-subscribed group
72 throw new ClientException(_('Not a member.'), 404);
78 protected function handleGet() {
79 return $this->showMembership();
82 protected function handleDelete() {
83 return $this->deleteMembership();
87 * show a single membership
91 function showMembership()
93 $activity = $this->_membership->asActivity();
95 header('Content-Type: application/atom+xml; charset=utf-8');
98 $this->raw($activity->asString(true, true, true));
105 * Delete the membership (leave the group)
109 function deleteMembership()
111 if (empty($this->auth_user) ||
112 $this->auth_user->id != $this->_profile->id) {
113 // TRANS: Client exception thrown when deleting someone else's membership.
114 throw new ClientException(_("Cannot delete someone else's".
115 " membership."), 403);
118 $this->auth_user->leaveGroup($this->_group);
124 * Return last modified, if applicable.
126 * Because the representation depends on the profile and group,
127 * our last modified value is the maximum of their mod time
128 * with the actual membership's mod time.
130 * @return string last modified http header
132 function lastModified()
134 return max(strtotime($this->_profile->modified),
135 strtotime($this->_group->modified),
136 strtotime($this->_membership->modified));
140 * Return etag, if applicable.
142 * A "weak" Etag including the profile and group id as well as
143 * the admin flag and ctime of the membership.
145 * @return string etag http header
149 $ctime = strtotime($this->_membership->created);
151 $adminflag = ($this->_membership->is_admin) ? 't' : 'f';
153 return 'W/"' . implode(':', array('AtomPubShowMembership',