From fb6207f01f92dff373f12dddfc168adacee79209 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 13 Dec 2020 13:07:12 +0000 Subject: [PATCH 1/5] IFTTT/Twitter: Support delayed posts --- ifttt/ifttt.php | 36 +++++++++++++++++++----------------- twitter/twitter.php | 21 ++++++--------------- 2 files changed, 25 insertions(+), 32 deletions(-) diff --git a/ifttt/ifttt.php b/ifttt/ifttt.php index 5021a63ed..e534b7054 100644 --- a/ifttt/ifttt.php +++ b/ifttt/ifttt.php @@ -6,7 +6,6 @@ * Version: 0.1 * Author: Michael Vogel */ -require_once 'mod/item.php'; use Friendica\App; use Friendica\Content\PageInfo; use Friendica\Core\Hook; @@ -15,6 +14,7 @@ use Friendica\Core\Protocol; use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\Item; +use Friendica\Model\Post; use Friendica\Util\Strings; function ifttt_install() @@ -161,25 +161,22 @@ function ifttt_message($uid, $item) { $a = DI::app(); - $_SESSION['authenticated'] = true; - $_SESSION['uid'] = $uid; - - unset($_REQUEST); - $_REQUEST['api_source'] = true; - $_REQUEST['profile_uid'] = $uid; - $_REQUEST['source'] = 'IFTTT'; - $_REQUEST['title'] = ''; - $_REQUEST['body'] = $item['msg']; - //$_REQUEST['date'] = $item['date']; - //$_REQUEST['uri'] = $item['url']; + $post = []; + $post['uid'] = $uid; + $post['app'] = 'IFTTT'; + $post['title'] = ''; + $post['body'] = $item['msg']; + //$post['date'] = $item['date']; + //$post['uri'] = $item['url']; if (!empty($item['url']) && strstr($item['url'], 'facebook.com')) { $hash = hash('ripemd128', $item['url']); - $_REQUEST['extid'] = Protocol::FACEBOOK; - $_REQUEST['message_id'] = Item::newURI($uid, Protocol::FACEBOOK . ':' . $hash); + $post['extid'] = Protocol::FACEBOOK; + $post['message_id'] = Item::newURI($uid, Protocol::FACEBOOK . ':' . $hash); } if ($item['type'] == 'link') { + $link = $item['link']; $data = PageInfo::queryUrl($item['link']); if (isset($item['title']) && (trim($item['title']) != '')) { @@ -190,10 +187,15 @@ function ifttt_message($uid, $item) $data['text'] = $item['description']; } - $_REQUEST['body'] .= "\n" . PageInfo::getFooterFromData($data); + $post['body'] .= "\n" . PageInfo::getFooterFromData($data); } elseif (($item['type'] == 'photo') && ($item['image'] != '')) { - $_REQUEST['body'] .= "\n\n[img]" . $item['image'] . "[/img]\n"; + $link = $item['image']; + $post['body'] .= "\n\n[img]" . $item['image'] . "[/img]\n"; + } elseif (!empty($item['url'])) { + $link = $item['url']; + } else { + $link = hash('ripemd128', $item['msg']); } - item_post($a); + Post\Delayed::add($link, $post, PRIORITY_MEDIUM, true); } diff --git a/twitter/twitter.php b/twitter/twitter.php index 52ff7ae48..dea7e4df6 100644 --- a/twitter/twitter.php +++ b/twitter/twitter.php @@ -83,6 +83,7 @@ use Friendica\Model\Group; use Friendica\Model\Item; use Friendica\Model\ItemContent; use Friendica\Model\ItemURI; +use Friendica\Model\Post; use Friendica\Model\Tag; use Friendica\Model\User; use Friendica\Protocol\Activity; @@ -1004,11 +1005,7 @@ function twitter_parse_link(App $a, array &$b) */ function twitter_do_mirrorpost(App $a, $uid, $post) { - $datarray['api_source'] = true; - $datarray['profile_uid'] = $uid; $datarray['extid'] = 'twitter::' . $post->id; - $datarray['protocol'] = Conversation::PARCEL_TWITTER; - $datarray['source'] = json_encode($post); $datarray['title'] = ''; if (!empty($post->retweeted_status)) { @@ -1038,7 +1035,7 @@ function twitter_do_mirrorpost(App $a, $uid, $post) $datarray['body'] = $item['body']; } - $datarray['source'] = $item['app']; + $datarray['app'] = $item['app']; $datarray['verb'] = $item['verb']; if (isset($item['location'])) { @@ -1066,9 +1063,6 @@ function twitter_fetchtimeline(App $a, $uid) $application_name = DI::baseUrl()->getHostname(); } - require_once 'mod/item.php'; - require_once 'mod/share.php'; - $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret); $parameters = ["exclude_replies" => true, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended", "include_ext_alt_text" => true]; @@ -1107,20 +1101,17 @@ function twitter_fetchtimeline(App $a, $uid) } if (!stristr($post->source, $application_name)) { - $_SESSION["authenticated"] = true; - $_SESSION["uid"] = $uid; - Logger::info('Preparing mirror post', ['twitter-id' => $post->id_str, 'uid' => $uid]); - $_REQUEST = twitter_do_mirrorpost($a, $uid, $post); + $mirrorpost = twitter_do_mirrorpost($a, $uid, $post); - if (empty($_REQUEST['body'])) { + if (empty($mirrorpost['body'])) { continue; } - Logger::info('Posting mirror post ', ['twitter-id' => $post->id_str, 'uid' => $uid]); + Logger::info('Posting mirror post', ['twitter-id' => $post->id_str, 'uid' => $uid]); - item_post($a); + Post\Delayed::add($mirrorpost['extid'], $mirrorpost, PRIORITY_MEDIUM, true); } } } From 99e96d89230b2d3b9db18816a95d37084ee05caf Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 13 Dec 2020 16:40:50 +0000 Subject: [PATCH 2/5] Added forgotten uid --- twitter/twitter.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/twitter/twitter.php b/twitter/twitter.php index dea7e4df6..ba9ac287b 100644 --- a/twitter/twitter.php +++ b/twitter/twitter.php @@ -1005,6 +1005,7 @@ function twitter_parse_link(App $a, array &$b) */ function twitter_do_mirrorpost(App $a, $uid, $post) { + $datarray['uid'] = $uid; $datarray['extid'] = 'twitter::' . $post->id; $datarray['title'] = ''; @@ -1065,6 +1066,14 @@ function twitter_fetchtimeline(App $a, $uid) $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret); + // Ensure to have the own contact + try { + twitter_fetch_own_contact($a, $uid); + } catch (TwitterOAuthException $e) { + Logger::warning('Error fetching own contact', ['uid' => $uid, 'message' => $e->getMessage()]); + return; + } + $parameters = ["exclude_replies" => true, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended", "include_ext_alt_text" => true]; $first_time = ($lastid == ""); @@ -1228,12 +1237,16 @@ function twitter_fetch_contact($uid, $data, $create_user) if (!empty($cid)) { DBA::update('contact', $fields, ['id' => $cid]); Contact::updateAvatar($cid, $avatar); + } else { + Logger::warning('No contact found', ['fields' => $fields]); } $contact = DBA::selectFirst('contact', [], ['uid' => $uid, 'alias' => "twitter::" . $data->id_str]); - if (!DBA::isResult($contact) && !$create_user) { + if (!DBA::isResult($contact) && empty($cid)) { Logger::warning('User contact not found', ['uid' => $uid, 'twitter-id' => $data->id_str]); return 0; + } else { + return $cid; } if (!DBA::isResult($contact)) { From 6cad529c383732edc2d9459c4e898eb741c82c76 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sun, 13 Dec 2020 19:42:45 +0100 Subject: [PATCH 3/5] IFTTT: Set the extid, removed legacy Facebook stuff --- ifttt/ifttt.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/ifttt/ifttt.php b/ifttt/ifttt.php index e534b7054..a494b5f7b 100644 --- a/ifttt/ifttt.php +++ b/ifttt/ifttt.php @@ -169,12 +169,6 @@ function ifttt_message($uid, $item) //$post['date'] = $item['date']; //$post['uri'] = $item['url']; - if (!empty($item['url']) && strstr($item['url'], 'facebook.com')) { - $hash = hash('ripemd128', $item['url']); - $post['extid'] = Protocol::FACEBOOK; - $post['message_id'] = Item::newURI($uid, Protocol::FACEBOOK . ':' . $hash); - } - if ($item['type'] == 'link') { $link = $item['link']; $data = PageInfo::queryUrl($item['link']); @@ -197,5 +191,7 @@ function ifttt_message($uid, $item) $link = hash('ripemd128', $item['msg']); } + $post['extid'] = $link; + Post\Delayed::add($link, $post, PRIORITY_MEDIUM, true); } From 0d84a9fbaff72725c0edcd03a7805cd6bd3fcd31 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 13 Dec 2020 20:42:51 +0000 Subject: [PATCH 4/5] Handle "create user" setting --- twitter/twitter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/twitter/twitter.php b/twitter/twitter.php index ba9ac287b..42986d162 100644 --- a/twitter/twitter.php +++ b/twitter/twitter.php @@ -1245,7 +1245,7 @@ function twitter_fetch_contact($uid, $data, $create_user) if (!DBA::isResult($contact) && empty($cid)) { Logger::warning('User contact not found', ['uid' => $uid, 'twitter-id' => $data->id_str]); return 0; - } else { + } elseif (!$create_user) { return $cid; } From dca9bb3844ce632b34c6635f5e54fd728b7d0284 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 13 Dec 2020 21:02:19 +0000 Subject: [PATCH 5/5] We don't need to store the extid --- ifttt/ifttt.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/ifttt/ifttt.php b/ifttt/ifttt.php index a494b5f7b..a0cc1ae80 100644 --- a/ifttt/ifttt.php +++ b/ifttt/ifttt.php @@ -191,7 +191,5 @@ function ifttt_message($uid, $item) $link = hash('ripemd128', $item['msg']); } - $post['extid'] = $link; - Post\Delayed::add($link, $post, PRIORITY_MEDIUM, true); }