From cde0de965f8f2fde2b289ebcb1c1814ffa303ca6 Mon Sep 17 00:00:00 2001 From: Zach Prezkuta Date: Sat, 2 Jun 2012 23:56:42 -0600 Subject: [PATCH] first shot at getting like/unlike functions to work consistently --- include/delivery.php | 12 ++--- include/diaspora.php | 22 ++++++--- include/items.php | 8 ++- include/notifier.php | 9 ++-- mod/like.php | 113 ++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 142 insertions(+), 22 deletions(-) diff --git a/include/delivery.php b/include/delivery.php index 235b766c5f..e6cfc81554 100644 --- a/include/delivery.php +++ b/include/delivery.php @@ -509,17 +509,17 @@ function delivery_run($argv, $argc){ // unsupported break; } - elseif(($target_item['deleted']) && ($target_item['uri'] === $target_item['parent-uri']) && ($target_item['verb'] !== ACTIVITY_LIKE)) { - logger('delivery: diaspora retract: ' . $loc); - // diaspora delete, + elseif(($target_item['deleted']) && ($target_item['uri'] === $target_item['parent-uri'])) { + // top-level retraction + logger('delivery: diaspora retract: ' . $loc); + diaspora_send_retraction($target_item,$owner,$contact,$public_message); break; } elseif($target_item['uri'] !== $target_item['parent-uri']) { + // we are the relay - send comments, likes and relayable_retractions to our conversants + logger('delivery: diaspora relay: ' . $loc); - logger('delivery: diaspora relay: ' . $loc); - - // we are the relay - send comments, likes, unlikes and relayable_retractions to our conversants diaspora_send_relay($target_item,$owner,$contact,$public_message); break; } diff --git a/include/diaspora.php b/include/diaspora.php index 10c56564b7..3bf4a9cc61 100644 --- a/include/diaspora.php +++ b/include/diaspora.php @@ -2125,7 +2125,8 @@ function diaspora_send_followup($item,$owner,$contact,$public_batch = false) { $tpl = get_markup_template('diaspora_like.tpl'); $like = true; $target_type = 'Post'; - $positive = (($item['deleted']) ? 'false' : 'true'); +// $positive = (($item['deleted']) ? 'false' : 'true'); + $positive = 'true'; } else { $tpl = get_markup_template('diaspora_comment.tpl'); @@ -2202,7 +2203,8 @@ function diaspora_send_relay($item,$owner,$contact,$public_batch = false) { $tpl = get_markup_template('diaspora_like_relay.tpl'); $like = true; $target_type = 'Post'; - $positive = (($item['deleted']) ? 'false' : 'true'); +// $positive = (($item['deleted']) ? 'false' : 'true'); + $positive = 'true'; } else { $tpl = get_markup_template('diaspora_comment_relay.tpl'); @@ -2220,9 +2222,6 @@ function diaspora_send_relay($item,$owner,$contact,$public_batch = false) { // owner (parent author) signature // Note that mod/item.php seems to take care of creating a signature for Diaspora for replies // created on our own account - // - // comments from other networks will be relayed under our name, with a brief - // preamble to describe what's happening and noting the real author $r = q("select * from sign where " . $sql_sign_id . " = %d limit 1", intval($item['id']) @@ -2235,6 +2234,12 @@ function diaspora_send_relay($item,$owner,$contact,$public_batch = false) { } else { + // Author signature information (for likes, comments, and retractions of likes or comments, + // whether from Diaspora or Friendica) must be placed in the `sign` table before this + // function is called + logger('diaspora_send_relay: original author signature not found, cannot send relayable'); + return; +/* $itemcontact = q("select * from contact where `id` = %d limit 1", intval($item['contact-id']) ); @@ -2273,6 +2278,7 @@ function diaspora_send_relay($item,$owner,$contact,$public_batch = false) { dbesc($handle) ); } +*/ } // sign it with the top-level owner's signature @@ -2306,11 +2312,11 @@ function diaspora_send_retraction($item,$owner,$contact,$public_batch = false) { $a = get_app(); $myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3); - // Check if the retraction is for a top-level post, or whether it's for a comment - if( $item['id'] !== $item['parent'] ) { + // Check whether the retraction is for a top-level post or whether it's a relayable + if( $item['uri'] !== $item['parent-uri'] ) { $tpl = get_markup_template('diaspora_relay_retraction.tpl'); - $target_type = 'Comment'; + $target_type = (($item['verb'] === ACTIVITY_LIKE) ? 'Like' : 'Comment'); } else { diff --git a/include/items.php b/include/items.php index 897036c26f..6b16810b22 100644 --- a/include/items.php +++ b/include/items.php @@ -3281,9 +3281,11 @@ function drop_item($id,$interactive = true) { } // Add a relayable_retraction signature for Diaspora. Note that we can't add a target_author_signature - // if the comment/like was deleted by a remote user. That should be ok, because if a remote user is deleting - // the comment/like, that means we're the home of the post, and Diaspora will only + // if the comment was deleted by a remote user. That should be ok, because if a remote user is deleting + // the comment, that means we're the home of the post, and Diaspora will only // check the parent_author_signature of retractions that it doesn't have to relay further + // + // I don't think this function gets called for an "unlike," but I'll check anyway $signed_text = $item['guid'] . ';' . ( ($item['verb'] === ACTIVITY_LIKE) ? 'Like' : 'Comment'); if(local_user() == $item['uid']) { @@ -3296,6 +3298,8 @@ function drop_item($id,$interactive = true) { $item['contact-id'] ); if(count($r)) { + // The below handle only works for NETWORK_DFRN. I think that's ok, because this function + // only handles DFRN deletes $handle = $r['nick'] . '@' . substr($r['url'], strpos($r['url'],'://') + 3, strpos($r['url'],'/profile') - 1); $authorsig = ''; } diff --git a/include/notifier.php b/include/notifier.php index be50366d5c..f0a1940d49 100644 --- a/include/notifier.php +++ b/include/notifier.php @@ -738,18 +738,19 @@ function notifier_run($argv, $argc){ // unsupported break; } - elseif(($target_item['deleted']) && (($target_item['uri'] === $target_item['parent-uri']) || $followup) && ($target_item['verb'] !== ACTIVITY_LIKE)) { - // diaspora delete, including relayable_retractions that need to be relayed + elseif(($target_item['deleted']) && (($target_item['uri'] === $target_item['parent-uri']) || $followup)) { + // send both top-level retractions and relayable retractions for owner to relay diaspora_send_retraction($target_item,$owner,$contact); break; } elseif($followup) { - // send comments, likes and retractions of likes to owner to relay + // send comments and likes to owner to relay diaspora_send_followup($target_item,$owner,$contact); break; } elseif($target_item['uri'] !== $target_item['parent-uri']) { - // we are the relay - send comments, likes, unlikes and relayable_retractions to our conversants + // we are the relay - send comments, likes and relayable_retractions + // (of comments and likes) to our conversants diaspora_send_relay($target_item,$owner,$contact); break; } diff --git a/mod/like.php b/mod/like.php index 6b97fafb56..06d27659bb 100644 --- a/mod/like.php +++ b/mod/like.php @@ -104,6 +104,7 @@ function like_content(&$a) { return; } + $r = q("SELECT `id` FROM `item` WHERE `verb` = '%s' AND `deleted` = 0 AND `contact-id` = %d AND ( `parent` = '%s' OR `parent-uri` = '%s') LIMIT 1", dbesc($activity), @@ -118,7 +119,58 @@ function like_content(&$a) { intval($r[0]['id']) ); - proc_run('php',"include/notifier.php","like","$post_id"); + // Clean up the `sign` table + $r2 = q("DELETE FROM `sign` WHERE `iid` = %d", + intval($r[0]['id']) + ); + + // Save the author information for the unlike in case we need to relay to Diaspora + // Note that we can only create a signature for a user of the local server. We don't have + // a key for remote users. That is ok, because if a remote user is "unlike"ing a post, it + // means we are the relay, and for relayable_retractions, Diaspora + // only checks the parent_author_signature if it doesn't have to relay further + // + // If $item['resource-id'] exists, it means the item is a photo. Diaspora doesn't support + // likes on photos, so don't bother. + + if(($activity === ACTIVITY_LIKE) && (! $item['resource-id'])) { + $signed_text = $r[0]['guid'] . ';' . 'Like'; + + if( contact['network'] === NETWORK_DIASPORA) + $diaspora_handle = $contact['addr']; + else { // Only works for NETWORK_DFRN + $contact_baseurl = substr($contact['url'], strpos($contact['url'],'://') + 3, strpos($contact['url'],'/profile') - 1); + $diaspora_handle = $contact['nick'] . '@' . $contact_baseurl; + + // Get contact's private key if he's a user of the local Friendica server + $r2 = q("SELECT `contact`.`uid` FROM `contact` WHERE `url` = '%s' AND `self` = 1 LIMIT 1", + dbesc(contact['url']) + ); + + if( $r2) { + $contact_uid = $r2['uid']; + $r2 = q("SELECT prvkey FROM user WHERE id = %d LIMIT 1", + intval($contact_uid) + ); + + if( $r2) + $authorsig = base64_encode(rsa_sign($signed_text,$r2['prvkey'],'sha256')); + } + } + + if(! isset($authorsig)) + $authorsig = ''; + + q("insert into sign (`retract_iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ", + intval($r[0]['id']), + dbesc($signed_text), + dbesc($authorsig), + dbesc($diaspora_handle) + ); + } + +// proc_run('php',"include/notifier.php","like","$post_id"); // $post_id isn't defined here! + proc_run('php',"include/notifier.php","like","$r[0]['id']"); return; } @@ -191,6 +243,63 @@ EOT; ); } + + // Save the author information for the like in case we need to relay to Diaspora + // Note that we can only create a signature for a user of the local server. We don't have + // a key for remote users. That is ok, because if a remote user is "unlike"ing a post, it + // means we are the relay, and for relayable_retractions, Diaspora + // only checks the parent_author_signature if it doesn't have to relay further + + if(($activity === ACTIVITY_LIKE) && ($post_type === t('status'))) { + if( contact['network'] === NETWORK_DIASPORA) + $diaspora_handle = $contact['addr']; + else { // Only works for NETWORK_DFRN + $contact_baseurl = substr($contact['url'], strpos($contact['url'],'://') + 3, strpos($contact['url'],'/profile') - 1); + $diaspora_handle = $contact['nick'] . '@' . $contact_baseurl; + + // Get contact's private key if he's a user of the local Friendica server + $r2 = q("SELECT `contact`.`uid` FROM `contact` WHERE `url` = '%s' AND `self` = 1 LIMIT 1", + dbesc(contact['url']) + ); + + if( $r2) { + $contact_uid = $r2['uid']; + $r2 = q("SELECT prvkey FROM user WHERE id = %d LIMIT 1", + intval($contact_uid) + ); + + if( $r2) + $contact_uprvkey = $r2['prvkey']; + } + } + + $r = q("SELECT guid, parent FROM `item` WHERE id = %d LIMIT 1", + intval($post_id) + ); + if( $r) { + $p = q("SELECT guid FROM `item` WHERE id = %d AND parent = %d LIMIT 1", + intval($r[0]['parent'), + intval($r[0]['parent') + ); + if( $p) { + $signed_text = $r[0]['guid'] . ';Post;' . $p[0]['guid'] . ';true;' . $diaspora_handle; + + if(isset($contact_uprvkey)) + $authorsig = base64_encode(rsa_sign($signed_text,$contact_uprvkey,'sha256')); + else + $authorsig = ''; + + q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ", + intval($post_id), + dbesc($signed_text), + dbesc($authorsig), + dbesc($diaspora_handle) + ); + } + } + } + + $arr['id'] = $post_id; call_hooks('post_local_end', $arr); @@ -199,4 +308,4 @@ EOT; killme(); // return; // NOTREACHED -} \ No newline at end of file +}