Group_member.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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()
  30. public $modified; // timestamp() 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', 'description' => 'date this record was created'),
  42. 'modified' => array('type' => 'timestamp', 'not null' => true, '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. 'group_member_profile_id_created_group_id_idx' => array('profile_id', 'created', 'group_id'),
  54. 'group_member_group_id_created_profile_id_idx' => array('profile_id', 'created', 'group_id'),
  55. ),
  56. );
  57. }
  58. /**
  59. * Method to add a user to a group.
  60. * In most cases, you should call Profile->joinGroup() instead.
  61. *
  62. * @param integer $group_id Group to add to
  63. * @param integer $profile_id Profile being added
  64. *
  65. * @return Group_member new membership object
  66. */
  67. public static function join($group_id, $profile_id)
  68. {
  69. $member = new Group_member();
  70. $member->group_id = $group_id;
  71. $member->profile_id = $profile_id;
  72. $member->created = common_sql_now();
  73. $member->uri = self::newUri(
  74. Profile::getByID($profile_id),
  75. User_group::getByID($group_id),
  76. $member->created
  77. );
  78. $result = $member->insert();
  79. if (!$result) {
  80. common_log_db_error($member, 'INSERT', __FILE__);
  81. // TRANS: Exception thrown when joining a group fails.
  82. throw new Exception(_("Group join failed."));
  83. }
  84. return $member;
  85. }
  86. public static function leave($group_id, $profile_id)
  87. {
  88. $member = Group_member::pkeyGet(array('group_id' => $group_id,
  89. 'profile_id' => $profile_id));
  90. if (empty($member)) {
  91. // TRANS: Exception thrown when trying to leave a group the user is not a member of.
  92. throw new Exception(_("Not part of group."));
  93. }
  94. $result = $member->delete();
  95. if (!$result) {
  96. common_log_db_error($member, 'INSERT', __FILE__);
  97. // TRANS: Exception thrown when trying to leave a group fails.
  98. throw new Exception(_("Group leave failed."));
  99. }
  100. return true;
  101. }
  102. public function getMember()
  103. {
  104. $member = Profile::getKV('id', $this->profile_id);
  105. if (empty($member)) {
  106. // TRANS: Exception thrown providing an invalid profile ID.
  107. // TRANS: %s is the invalid profile ID.
  108. throw new Exception(sprintf(_("Profile ID %s is invalid."), $this->profile_id));
  109. }
  110. return $member;
  111. }
  112. public function getGroup()
  113. {
  114. $group = User_group::getKV('id', $this->group_id);
  115. if (empty($group)) {
  116. // TRANS: Exception thrown providing an invalid group ID.
  117. // TRANS: %s is the invalid group ID.
  118. throw new Exception(sprintf(_('Group ID %s is invalid.'), $this->group_id));
  119. }
  120. return $group;
  121. }
  122. /**
  123. * Get stream of memberships by member
  124. *
  125. * @param integer $memberId profile ID of the member to fetch for
  126. * @param integer $offset offset from start of stream to get
  127. * @param integer $limit number of memberships to get
  128. *
  129. * @return Group_member stream of memberships, use fetch() to iterate
  130. */
  131. public static function byMember($memberId, $offset = 0, $limit = GROUPS_PER_PAGE)
  132. {
  133. $membership = new Group_member();
  134. $membership->profile_id = $memberId;
  135. $membership->orderBy('created DESC, group_id DESC');
  136. $membership->limit($offset, $limit);
  137. $membership->find();
  138. return $membership;
  139. }
  140. public function asActivity()
  141. {
  142. $member = $this->getMember();
  143. if (!$member) {
  144. throw new Exception("No such member: " . $this->profile_id);
  145. }
  146. $group = $this->getGroup();
  147. if (!$group) {
  148. throw new Exception("No such group: " . $this->group_id);
  149. }
  150. $act = new Activity();
  151. $act->id = $this->getUri();
  152. $act->actor = $member->asActivityObject();
  153. $act->verb = ActivityVerb::JOIN;
  154. $act->objects[] = ActivityObject::fromGroup($group);
  155. $act->time = strtotime($this->created);
  156. // TRANS: Activity title.
  157. $act->title = _("Join");
  158. // TRANS: Success message for subscribe to group attempt through OStatus.
  159. // TRANS: %1$s is the member name, %2$s is the subscribed group's name.
  160. $act->content = sprintf(
  161. _('%1$s has joined group %2$s.'),
  162. $member->getBestName(),
  163. $group->getBestName()
  164. );
  165. $url = common_local_url(
  166. 'AtomPubShowMembership',
  167. [
  168. 'profile' => $member->id,
  169. 'group' => $group->id,
  170. ]
  171. );
  172. $act->selfLink = $url;
  173. $act->editLink = $url;
  174. return $act;
  175. }
  176. /**
  177. * Send notifications via email etc to group administrators about
  178. * this exciting new membership!
  179. */
  180. public function notify()
  181. {
  182. mail_notify_group_join($this->getGroup(), $this->getMember());
  183. }
  184. public function getUri()
  185. {
  186. return $this->uri ?: self::newUri($this->getMember(), $this->getGroup()->getProfile(), $this->created);
  187. }
  188. }