Group_member.php 7.8 KB

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