Skip to content

Commit

Permalink
handle h-cite values
Browse files Browse the repository at this point in the history
fix for #6
  • Loading branch information
aaronpk committed Oct 6, 2015
1 parent 8130e03 commit 804e386
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
36 changes: 23 additions & 13 deletions src/indieweb/comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,22 @@ function truncate($text, $maxTextLength, $maxLines) {
return $text;
}

function removeScheme(&$url) {
if(is_array($url)) {
foreach($url as $i=>$u) {
removeScheme($url[$i]);
// Collects all URLs found in the input array, and remove the scheme.
// An input object may be a string URL or also an mf2 object with properties.url
function collectURLs(&$urls) {
if(is_array($urls) && array_key_exists(0, $urls)) {
foreach($urls as $i=>$u) {
collectURLs($urls[$i]);
}
} else {
$url = preg_replace('/^https?/', '', $url);
} elseif(is_array($urls)
&& array_key_exists('type', $urls)
&& array_key_exists('properties', $urls)
&& array_key_exists('url', $urls['properties'])
) {
// Flatten the object and turn it just into the URL
$urls = preg_replace('/^https?/', '', $urls['properties']['url'][0]);
} elseif(is_string($urls)) {
$urls = preg_replace('/^https?/', '', $urls);
}
}

Expand All @@ -48,7 +57,7 @@ function parse($mf, $refURL=false, $maxTextLength=150, $maxLines=2) {
// This is used to check for an explicit in-reply-to property set to this URL.

// Remove the scheme from the refURL and treat http and https links as the same
removeScheme($refURL);
collectURLs($refURL);

$type = 'mention';
$published = false;
Expand Down Expand Up @@ -98,7 +107,7 @@ function parse($mf, $refURL=false, $maxTextLength=150, $maxLines=2) {
if($refURL && array_key_exists('in-reply-to', $properties)) {
// in-reply-to may be a string or an h-cite
foreach($properties['in-reply-to'] as $check) {
removeScheme($check);
collectURLs($check);
if(is_string($check) && $check == $refURL) {
$type = 'reply';
continue;
Expand Down Expand Up @@ -145,27 +154,28 @@ function parse($mf, $refURL=false, $maxTextLength=150, $maxLines=2) {

// Check if this post is a "repost"
if($refURL && array_key_exists('repost-of', $properties)) {
removeScheme($properties['repost-of']);
collectURLs($properties['repost-of']);
if(in_array($refURL, $properties['repost-of']))
$type = 'repost';
}

// Also check for "u-repost" since some people are sending that. Probably "u-repost-of" will win out.
if($refURL && array_key_exists('repost', $properties)) {
removeScheme($properties['repost']);
collectURLs($properties['repost']);
if(in_array($refURL, $properties['repost']))
$type = 'repost';
}

// Check if this post is a "like-of"
if($refURL && array_key_exists('like-of', $properties)) {
removeScheme($properties['like-of']);
collectURLs($properties['like-of']);
if(in_array($refURL, $properties['like-of']))
$type = 'like';
}

// Check if this post is a "like"
// Check if this post is a "like" (Should be deprecated in the future)
if($refURL && array_key_exists('like', $properties)) {
removeScheme($properties['like']);
collectURLs($properties['like']);
if(in_array($refURL, $properties['like']))
$type = 'like';
}
Expand Down
25 changes: 25 additions & 0 deletions tests/BasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,16 @@ public function testIsLike() {
$this->assertEquals('liked this post', $result['text']);
}

public function testIsLikeOf() {
$result = IndieWeb\comments\parse($this->buildHEntry(array(
'name' => 'Liked this',
'content' => 'liked this post',
'like-of' => $this->_refURL
)), $this->_refURL, 200);
$this->assertEquals('like', $result['type']);
$this->assertEquals('liked this post', $result['text']);
}

public function testIsRepostOf() {
$result = IndieWeb\comments\parse($this->buildHEntry(array(
'name' => 'Reposted this',
Expand Down Expand Up @@ -335,6 +345,21 @@ public function testIsNotInReplyTo() {
$this->assertEquals('The name of the post is different ...', $result['text']);
}

public function testIsLikeOfHCite() {
$result = IndieWeb\comments\parse($this->buildHEntry(array(
'name' => 'Liked this',
'content' => 'liked this post',
'like-of' => array(
'type' => 'h-cite',
'properties' => array(
'url' => array($this->_refURL)
)
)
)), $this->_refURL, 200);
$this->assertEquals('like', $result['type']);
$this->assertEquals('liked this post', $result['text']);
}

/***************************************************************************
* Author tests
*/
Expand Down

0 comments on commit 804e386

Please sign in to comment.