function test_find_unique_users() { $users = array( array('Remote_URL' => 'http://localhost/messages/follow/fred'), array('Remote_URL' => 'http://localhost/messages/follow/abcder'), array('Remote_URL' => 'http://localhost/messages/follow/sue'), array('Remote_URL' => 'http://localhost/messages/follow/fred')); $result = find_unique_users($users); $this->assertEquals(count($result), 3); $this->assertEquals(preg_match('/fred/', $result[0]['Remote_URL']), 1); $this->assertEquals(preg_match('/abcder/', $result[1]['Remote_URL']), 1); $this->assertEquals(preg_match('/sue/', $result[2]['Remote_URL']), 1); }
function create($rmt = false) { // if not logged in, display list of users registered on this node if(!isset($_SESSION['active_user'])) redirect_to(make_url('users', 'login')); $this->outer_template = null; $message = $_POST['message']; try { validate_message($message); } catch(exception $e) { new_flash("Invalid message", 1); redirect_to($_SESSION['direct_to']); } // Instance models $usr = instance_model('users'); $msg = instance_model("messages"); $rel = instance_model("relations"); if($rmt == false) $rmt = instance_model("remotes"); $time = time(); $user = $usr->get_user_by_id($_SESSION['active_user']['id']); if($user == array()) throw new exception('Databse eror'); // Check for at tags $at_to_user = extract_at($message); foreach($at_to_user as $row) { try { validate_username($row); } catch (exception $e) { continue; } // Get followers and followings with the user name $following = $rel->get_following_by_rmt_name ($_SESSION['active_user']['id'], $row); $followers = $rel->get_followers_by_rmt_name ($_SESSION['active_user']['id'], $row); $retrieved = array_merge($following, $followers); if(count($retrieved) > 0) { if(count($retrieved) > 1) $fetched_users = find_unique_users($retrieved); else $fetched_users = $retrieved; // send message in message pingback foreach($fetched_users as $rmt_user) { $xml = new SimpleXMLElement("<data></data>"); $xml->addChild('remote_name', $user[0]['User_name']); $xml->addChild('remote_profile', make_profile_url($_SESSION['active_user']['name'])); $xml->addChild('remote_avatar', $user[0]['Avatar']); $xml->addChild('remote_message', $message); $xml->addChild('remote_time', $time); $response = $rmt->send_ping($rmt_user['Message_pingback'], "public", $rmt_user['Remote_name'], $user[0]['Pub_key'], $user[0]['Priv_key'], $xml->asXML()); try { $response = $rmt->decode_ping_response($response); } catch(exception $e) { die; } if(defined('APP_MODE') && APP_MODE == 'test' && $response->state == 'fail') throw new exception($response->error_msg); } } else new_flash("User $row not found in follwing or followers", 1); } // Add to local database $msg->create($_SESSION['active_user']['id'], $message, $time); // Send pings to update remote caches $remote_users = $rel->get_followers($_SESSION['active_user']['id']); foreach($remote_users as $rmt_user) $rmt->send_ping($rmt_user['Message_pingback'], "update", 'null', $user[0]['Pub_key'], $user[0]['Priv_key'], make_follow_url($_SESSION['active_user']['name'])); // redirect redirect_to($_SESSION['direct_to']); }