/** * get the 'other' view of this friendship */ public function reciprocal() { return Friendship::get()->filter(array('InitiatorID' => $this->OtherID, 'OtherID' => $this->InitiatorID))->first(); }
/** * Create a friendship relationship object * * @param DataObject $member * "me", as in the person who triggered the follow * @param DataObject $followed * "them", the person "me" is wanting to add * @return \Friendship * @throws PermissionDeniedException */ public function addFriendship(DataObject $member, DataObject $followed) { if (!$member || !$followed) { throw new PermissionDeniedException('Read', 'Cannot read those users'); } if ($member->ID != $this->securityContext->getMember()->ID) { throw new PermissionDeniedException('Write', 'Cannot create a friendship for that user'); } $existing = Friendship::get()->filter(array('InitiatorID' => $member->ID, 'OtherID' => $followed->ID))->first(); if ($existing) { return $existing; } // otherwise, we have a new one! $friendship = new Friendship(); $friendship->InitiatorID = $member->ID; $friendship->OtherID = $followed->ID; // we add the initiator into the // lets see if we have the reciprocal; if so, we can mark these as verified $reciprocal = $friendship->reciprocal(); // so we definitely add the 'member' to the 'followers' group of $followed $followers = $followed->getGroupFor(MicroBlogMember::FOLLOWERS); $followers->Members()->add($member); if ($reciprocal) { $reciprocal->Status = 'Approved'; $reciprocal->write(); $friendship->Status = 'Approved'; // add to each other's friends groups $friends = $followed->getGroupFor(MicroBlogMember::FRIENDS); $friends->Members()->add($member); $friends = $member->getGroupFor(MicroBlogMember::FRIENDS); $friends->Members()->add($followed); } $friendship->write(); return $friendship; }