+
+ function getMember()
+ {
+ $member = Profile::getKV('id', $this->profile_id);
+
+ if (empty($member)) {
+ // TRANS: Exception thrown providing an invalid profile ID.
+ // TRANS: %s is the invalid profile ID.
+ throw new Exception(sprintf(_("Profile ID %s is invalid."),$this->profile_id));
+ }
+
+ return $member;
+ }
+
+ function getGroup()
+ {
+ $group = User_group::getKV('id', $this->group_id);
+
+ if (empty($group)) {
+ // TRANS: Exception thrown providing an invalid group ID.
+ // TRANS: %s is the invalid group ID.
+ throw new Exception(sprintf(_("Group ID %s is invalid."),$this->group_id));
+ }
+
+ return $group;
+ }
+
+ /**
+ * Get stream of memberships by member
+ *
+ * @param integer $memberId profile ID of the member to fetch for
+ * @param integer $offset offset from start of stream to get
+ * @param integer $limit number of memberships to get
+ *
+ * @return Group_member stream of memberships, use fetch() to iterate
+ */
+
+ static function byMember($memberId, $offset=0, $limit=GROUPS_PER_PAGE)
+ {
+ $membership = new Group_member();
+
+ $membership->profile_id = $memberId;
+
+ $membership->orderBy('created DESC');
+
+ $membership->limit($offset, $limit);
+
+ $membership->find();
+
+ return $membership;
+ }
+
+ function asActivity()
+ {
+ $member = $this->getMember();
+
+ if (!$member) {
+ throw new Exception("No such member: " . $this->profile_id);
+ }
+
+ $group = $this->getGroup();
+
+ if (!$group) {
+ throw new Exception("No such group: " . $this->group_id);
+ }
+
+ $act = new Activity();
+
+ $act->id = $this->getURI();
+
+ $act->actor = $member->asActivityObject();
+ $act->verb = ActivityVerb::JOIN;
+ $act->objects[] = ActivityObject::fromGroup($group);
+
+ $act->time = strtotime($this->created);
+ // TRANS: Activity title.
+ $act->title = _("Join");
+
+ // TRANS: Success message for subscribe to group attempt through OStatus.
+ // TRANS: %1$s is the member name, %2$s is the subscribed group's name.
+ $act->content = sprintf(_('%1$s has joined group %2$s.'),
+ $member->getBestName(),
+ $group->getBestName());
+
+ $url = common_local_url('AtomPubShowMembership',
+ array('profile' => $member->id,
+ 'group' => $group->id));
+
+ $act->selfLink = $url;
+ $act->editLink = $url;
+
+ return $act;
+ }
+
+ /**
+ * Send notifications via email etc to group administrators about
+ * this exciting new membership!
+ */
+ public function notify()
+ {
+ mail_notify_group_join($this->getGroup(), $this->getMember());
+ }
+
+ function getURI()
+ {
+ if (!empty($this->uri)) {
+ return $this->uri;
+ } else {
+ return self::newURI($this->profile_id, $this->group_id, $this->created);
+ }
+ }
+
+ static function newURI($profile_id, $group_id, $created)
+ {
+ return TagURI::mint('join:%d:%d:%s',
+ $profile_id,
+ $group_id,
+ common_date_iso8601($created));
+ }