Group_member.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. defined('GNUSOCIAL') || die();
  17. /**
  18. * Table Definition for group_member
  19. */
  20. class Group_member extends Managed_DataObject
  21. {
  22. ###START_AUTOCODE
  23. /* the code below is auto generated do not remove the above tag */
  24. public $__table = 'group_member'; // table name
  25. public $group_id; // int(4) primary_key not_null
  26. public $profile_id; // int(4) primary_key not_null
  27. public $is_admin; // bool default_false
  28. public $uri; // varchar(191) not 255 because utf8mb4 takes more space
  29. public $created; // datetime() not_null default_0000-00-00%2000%3A00%3A00
  30. public $modified; // datetime() not_null default_CURRENT_TIMESTAMP
  31. /* the code above is auto generated do not remove the tag below */
  32. ###END_AUTOCODE
  33. public static function schemaDef()
  34. {
  35. return array(
  36. 'fields' => array(
  37. 'group_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to user_group'),
  38. 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'),
  39. 'is_admin' => array('type' => 'bool', 'default' => false, 'description' => 'is this user an admin?'),
  40. 'uri' => array('type' => 'varchar', 'length' => 191, 'description' => 'universal identifier'),
  41. 'created' => array('type' => 'datetime', 'not null' => true, 'default' => '0000-00-00 00:00:00', 'description' => 'date this record was created'),
  42. 'modified' => array('type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'),
  43. ),
  44. 'primary key' => array('group_id', 'profile_id'),
  45. 'unique keys' => array(
  46. 'group_member_uri_key' => array('uri'),
  47. ),
  48. 'foreign keys' => array(
  49. 'group_member_group_id_fkey' => array('user_group', array('group_id' => 'id')),
  50. 'group_member_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
  51. ),
  52. 'indexes' => array(
  53. // @fixme probably we want a (profile_id, created) index here?
  54. 'group_member_profile_id_idx' => array('profile_id'),
  55. 'group_member_created_idx' => array('created'),
  56. 'group_member_profile_id_created_idx' => array('profile_id', 'created'),
  57. 'group_member_group_id_created_idx' => array('group_id', 'created'),
  58. ),
  59. );
  60. }
  61. /**
  62. * Method to add a user to a group.
  63. * In most cases, you should call Profile->joinGroup() instead.
  64. *
  65. * @param integer $group_id Group to add to
  66. * @param integer $profile_id Profile being added
  67. *
  68. * @return Group_member new membership object
  69. */
  70. public static function join($group_id, $profile_id)
  71. {
  72. $member = new Group_member();
  73. $member->group_id = $group_id;
  74. $member->profile_id = $profile_id;
  75. $member->created = common_sql_now();
  76. $member->uri = self::newUri(
  77. Profile::getByID($profile_id),
  78. User_group::getByID($group_id),
  79. $member->created
  80. );
  81. $result = $member->insert();
  82. if (!$result) {
  83. common_log_db_error($member, 'INSERT', __FILE__);
  84. // TRANS: Exception thrown when joining a group fails.
  85. throw new Exception(_("Group join failed."));
  86. }
  87. return $member;
  88. }
  89. public static function leave($group_id, $profile_id)
  90. {
  91. $member = Group_member::pkeyGet(array('group_id' => $group_id,
  92. 'profile_id' => $profile_id));
  93. if (empty($member)) {
  94. // TRANS: Exception thrown when trying to leave a group the user is not a member of.
  95. throw new Exception(_("Not part of group."));
  96. }
  97. $result = $member->delete();
  98. if (!$result) {
  99. common_log_db_error($member, 'INSERT', __FILE__);
  100. // TRANS: Exception thrown when trying to leave a group fails.
  101. throw new Exception(_("Group leave failed."));
  102. }
  103. return true;
  104. }
  105. public function getMember()
  106. {
  107. $member = Profile::getKV('id', $this->profile_id);
  108. if (empty($member)) {
  109. // TRANS: Exception thrown providing an invalid profile ID.
  110. // TRANS: %s is the invalid profile ID.
  111. throw new Exception(sprintf(_("Profile ID %s is invalid."), $this->profile_id));
  112. }
  113. return $member;
  114. }
  115. public function getGroup()
  116. {
  117. $group = User_group::getKV('id', $this->group_id);
  118. if (empty($group)) {
  119. // TRANS: Exception thrown providing an invalid group ID.
  120. // TRANS: %s is the invalid group ID.
  121. throw new Exception(sprintf(_('Group ID %s is invalid.'), $this->group_id));
  122. }
  123. return $group;
  124. }
  125. /**
  126. * Get stream of memberships by member
  127. *
  128. * @param integer $memberId profile ID of the member to fetch for
  129. * @param integer $offset offset from start of stream to get
  130. * @param integer $limit number of memberships to get
  131. *
  132. * @return Group_member stream of memberships, use fetch() to iterate
  133. */
  134. public static function byMember($memberId, $offset = 0, $limit = GROUPS_PER_PAGE)
  135. {
  136. $membership = new Group_member();
  137. $membership->profile_id = $memberId;
  138. $membership->orderBy('created DESC');
  139. $membership->limit($offset, $limit);
  140. $membership->find();
  141. return $membership;
  142. }
  143. public function asActivity()
  144. {
  145. $member = $this->getMember();
  146. if (!$member) {
  147. throw new Exception("No such member: " . $this->profile_id);
  148. }
  149. $group = $this->getGroup();
  150. if (!$group) {
  151. throw new Exception("No such group: " . $this->group_id);
  152. }
  153. $act = new Activity();
  154. $act->id = $this->getUri();
  155. $act->actor = $member->asActivityObject();
  156. $act->verb = ActivityVerb::JOIN;
  157. $act->objects[] = ActivityObject::fromGroup($group);
  158. $act->time = strtotime($this->created);
  159. // TRANS: Activity title.
  160. $act->title = _("Join");
  161. // TRANS: Success message for subscribe to group attempt through OStatus.
  162. // TRANS: %1$s is the member name, %2$s is the subscribed group's name.
  163. $act->content = sprintf(
  164. _('%1$s has joined group %2$s.'),
  165. $member->getBestName(),
  166. $group->getBestName()
  167. );
  168. $url = common_local_url(
  169. 'AtomPubShowMembership',
  170. [
  171. 'profile' => $member->id,
  172. 'group' => $group->id,
  173. ]
  174. );
  175. $act->selfLink = $url;
  176. $act->editLink = $url;
  177. return $act;
  178. }
  179. /**
  180. * Send notifications via email etc to group administrators about
  181. * this exciting new membership!
  182. */
  183. public function notify()
  184. {
  185. mail_notify_group_join($this->getGroup(), $this->getMember());
  186. }
  187. public function getUri()
  188. {
  189. return $this->uri ?: self::newUri($this->getMember(), $this->getGroup()->getProfile(), $this->created);
  190. }
  191. }