From 47718b5eec00a07de1cf42e18f3bb47a41990ed1 Mon Sep 17 00:00:00 2001 From: husimo <50374438+husim0@users.noreply.github.com> Date: Thu, 20 Jun 2019 22:24:18 +0200 Subject: [PATCH 01/10] Create MastodonBridge.php --- bridges/MastodonBridge.php | 89 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 bridges/MastodonBridge.php diff --git a/bridges/MastodonBridge.php b/bridges/MastodonBridge.php new file mode 100644 index 00000000000..5f9112d5358 --- /dev/null +++ b/bridges/MastodonBridge.php @@ -0,0 +1,89 @@ + [ + 'name' => 'Canonical username (ex : @sebsauvage@framapiaf.org)', + 'required' => true, + ], + 'norep' => [ + 'name' => 'Without replies', + 'type' => 'checkbox', + 'title' => 'Only return initial toots' + ], + 'noboost' => [ + 'name' => 'Without boosts', + 'required' => false, + 'type' => 'checkbox', + 'title' => 'Hide boosts' + ] + ]]; + + public function getName(){ + switch($this->queriedContext) { + case 'By username': + $param = 'canusername'; + break; + default: return parent::getName(); + } + + return $this->getInput($param); + } + + protected function parseItem($newItem){ + $item = parent::parseItem($newItem); + + $content = str_get_html($item['content']); + $title = str_get_html($item['title']); + + $item['title'] = substr($content->plaintext,0,75) . (strlen($content->plaintext) >= 75 ? '...' : ''); + + if(strpos($title, 'shared a status by') !== false) { + if($this->getInput('noboost')) { + return null; + } + + preg_match("/shared a status by (\S{0,})/", $title, $matches); + $item['title'] = 'Boost ' . $matches[1] . ' ' . $item['title']; + $item['author'] = $matches[1]; + } else { + $item['author'] = $this->getInput('canusername'); + } + + // Check if it's a initial toot or a response + if($this->getInput('norep') && preg_match("/^@.+/", trim($content->plaintext))) { + return null; + } + + return $item; + } + + public function getInstance(){ + preg_match("/^@[a-zA-Z0-9_]+@(.+)/", $this->getInput('canusername'), $matches); + return $matches[1]; + } + + public function getUsername(){ + preg_match("/^@([a-zA-Z_0-9_]+)@.+/", $this->getInput('canusername'), $matches); + return $matches[1]; + } + + public function getURI(){ + return 'https://' . $this->getInstance() . '/users/' . $this->getUsername() . '.atom'; + } + + public function collectData(){ + try{ + $this->collectExpandableDatas($this->getURI()); + } catch (Exception $e) { + $this->collectExpandableDatas($this->getURI()); + } + } +} From 0673ff7e4a958b439e2f34623fd2937218f31aec Mon Sep 17 00:00:00 2001 From: husimo <50374438+husim0@users.noreply.github.com> Date: Thu, 20 Jun 2019 22:26:18 +0200 Subject: [PATCH 02/10] Update MastodonBridge.php --- bridges/MastodonBridge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bridges/MastodonBridge.php b/bridges/MastodonBridge.php index 5f9112d5358..6b0126e7f55 100644 --- a/bridges/MastodonBridge.php +++ b/bridges/MastodonBridge.php @@ -4,7 +4,7 @@ class MastodonBridge extends FeedExpander { const MAINTAINER = 'husim0'; const NAME = 'Mastodon Bridge'; - const CACHE_TIMEOUT = 0; // 1h + const CACHE_TIMEOUT = 900; // 15mn const DESCRIPTION = 'Returns toots'; const URI = 'https://mastodon.social'; From 3f9a5278d0351b25fc32e92ec8b2515388c243c7 Mon Sep 17 00:00:00 2001 From: husimo <50374438+husim0@users.noreply.github.com> Date: Fri, 21 Jun 2019 14:59:22 +0200 Subject: [PATCH 03/10] Take whitespaces into account Co-Authored-By: LogMANOriginal --- bridges/MastodonBridge.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bridges/MastodonBridge.php b/bridges/MastodonBridge.php index 6b0126e7f55..512b2b9dd5c 100644 --- a/bridges/MastodonBridge.php +++ b/bridges/MastodonBridge.php @@ -43,7 +43,10 @@ protected function parseItem($newItem){ $content = str_get_html($item['content']); $title = str_get_html($item['title']); - $item['title'] = substr($content->plaintext,0,75) . (strlen($content->plaintext) >= 75 ? '...' : ''); + $item['title'] = $content->plaintext; + if(strlen($item['title']) > 75) { + $item['title'] = substr($item['title'], 0, strpos(wordwrap($item['title'], 75), "\n")) . '...'; + } if(strpos($title, 'shared a status by') !== false) { if($this->getInput('noboost')) { From 8292ebd71da712b310ba69d3565aaa31192bc36f Mon Sep 17 00:00:00 2001 From: husimo <50374438+husim0@users.noreply.github.com> Date: Fri, 21 Jun 2019 14:59:50 +0200 Subject: [PATCH 04/10] Simplification Co-Authored-By: LogMANOriginal --- bridges/MastodonBridge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bridges/MastodonBridge.php b/bridges/MastodonBridge.php index 512b2b9dd5c..6c768268ce8 100644 --- a/bridges/MastodonBridge.php +++ b/bridges/MastodonBridge.php @@ -29,7 +29,7 @@ class MastodonBridge extends FeedExpander { public function getName(){ switch($this->queriedContext) { case 'By username': - $param = 'canusername'; + return $this->getInput('canusername'); break; default: return parent::getName(); } From 88ac70af9dca87e21917d160d95d6ff3c4fd151e Mon Sep 17 00:00:00 2001 From: husimo <50374438+husim0@users.noreply.github.com> Date: Fri, 21 Jun 2019 15:03:19 +0200 Subject: [PATCH 05/10] Update MastodonBridge.php --- bridges/MastodonBridge.php | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/bridges/MastodonBridge.php b/bridges/MastodonBridge.php index 6c768268ce8..5ee259e7e06 100644 --- a/bridges/MastodonBridge.php +++ b/bridges/MastodonBridge.php @@ -8,33 +8,30 @@ class MastodonBridge extends FeedExpander { const DESCRIPTION = 'Returns toots'; const URI = 'https://mastodon.social'; - const PARAMETERS = [[ - 'canusername' => [ + const PARAMETERS = array(array( + 'canusername' => array( 'name' => 'Canonical username (ex : @sebsauvage@framapiaf.org)', 'required' => true, - ], - 'norep' => [ + ), + 'norep' => array( 'name' => 'Without replies', 'type' => 'checkbox', 'title' => 'Only return initial toots' - ], - 'noboost' => [ + ), + 'noboost' => array( 'name' => 'Without boosts', 'required' => false, 'type' => 'checkbox', 'title' => 'Hide boosts' - ] - ]]; + ) + )); public function getName(){ switch($this->queriedContext) { case 'By username': return $this->getInput('canusername'); - break; default: return parent::getName(); } - - return $this->getInput($param); } protected function parseItem($newItem){ @@ -44,6 +41,7 @@ protected function parseItem($newItem){ $title = str_get_html($item['title']); $item['title'] = $content->plaintext; + if(strlen($item['title']) > 75) { $item['title'] = substr($item['title'], 0, strpos(wordwrap($item['title'], 75), "\n")) . '...'; } @@ -83,10 +81,6 @@ public function getURI(){ } public function collectData(){ - try{ - $this->collectExpandableDatas($this->getURI()); - } catch (Exception $e) { - $this->collectExpandableDatas($this->getURI()); - } + return $this->collectExpandableDatas($this->getURI()); } } From d287a87dbcc8e5e494e2d377a30d5db147f67617 Mon Sep 17 00:00:00 2001 From: husimo <50374438+husim0@users.noreply.github.com> Date: Fri, 21 Jun 2019 15:08:58 +0200 Subject: [PATCH 06/10] Removed double quotes --- bridges/MastodonBridge.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bridges/MastodonBridge.php b/bridges/MastodonBridge.php index 5ee259e7e06..db852cba045 100644 --- a/bridges/MastodonBridge.php +++ b/bridges/MastodonBridge.php @@ -51,7 +51,7 @@ protected function parseItem($newItem){ return null; } - preg_match("/shared a status by (\S{0,})/", $title, $matches); + preg_match('/shared a status by (\S{0,})/', $title, $matches); $item['title'] = 'Boost ' . $matches[1] . ' ' . $item['title']; $item['author'] = $matches[1]; } else { @@ -59,7 +59,7 @@ protected function parseItem($newItem){ } // Check if it's a initial toot or a response - if($this->getInput('norep') && preg_match("/^@.+/", trim($content->plaintext))) { + if($this->getInput('norep') && preg_match('/^@.+/', trim($content->plaintext))) { return null; } @@ -67,12 +67,12 @@ protected function parseItem($newItem){ } public function getInstance(){ - preg_match("/^@[a-zA-Z0-9_]+@(.+)/", $this->getInput('canusername'), $matches); + preg_match('/^@[a-zA-Z0-9_]+@(.+)/', $this->getInput('canusername'), $matches); return $matches[1]; } public function getUsername(){ - preg_match("/^@([a-zA-Z_0-9_]+)@.+/", $this->getInput('canusername'), $matches); + preg_match('/^@([a-zA-Z_0-9_]+)@.+/', $this->getInput('canusername'), $matches); return $matches[1]; } From 79d4fd25ab926b644257e40d2bc80ec88844dd21 Mon Sep 17 00:00:00 2001 From: husimo <50374438+husim0@users.noreply.github.com> Date: Fri, 21 Jun 2019 17:08:49 +0200 Subject: [PATCH 07/10] Make getInstance private Co-Authored-By: LogMANOriginal --- bridges/MastodonBridge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bridges/MastodonBridge.php b/bridges/MastodonBridge.php index db852cba045..483fbcc0c6b 100644 --- a/bridges/MastodonBridge.php +++ b/bridges/MastodonBridge.php @@ -66,7 +66,7 @@ protected function parseItem($newItem){ return $item; } - public function getInstance(){ + private function getInstance(){ preg_match('/^@[a-zA-Z0-9_]+@(.+)/', $this->getInput('canusername'), $matches); return $matches[1]; } From 2e2d192e192351ba6f8ee3f362de2a06eef0abae Mon Sep 17 00:00:00 2001 From: husimo <50374438+husim0@users.noreply.github.com> Date: Fri, 21 Jun 2019 17:09:03 +0200 Subject: [PATCH 08/10] Make getUsername private Co-Authored-By: LogMANOriginal --- bridges/MastodonBridge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bridges/MastodonBridge.php b/bridges/MastodonBridge.php index 483fbcc0c6b..3b5836b12b5 100644 --- a/bridges/MastodonBridge.php +++ b/bridges/MastodonBridge.php @@ -71,7 +71,7 @@ private function getInstance(){ return $matches[1]; } - public function getUsername(){ + private function getUsername(){ preg_match('/^@([a-zA-Z_0-9_]+)@.+/', $this->getInput('canusername'), $matches); return $matches[1]; } From d5f4a958cf3bef91a3b3ba51c13bad0bcabc7f54 Mon Sep 17 00:00:00 2001 From: husimo <50374438+husim0@users.noreply.github.com> Date: Fri, 21 Jun 2019 17:09:50 +0200 Subject: [PATCH 09/10] Check if parameter is set Co-Authored-By: LogMANOriginal --- bridges/MastodonBridge.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bridges/MastodonBridge.php b/bridges/MastodonBridge.php index 3b5836b12b5..660066f0fae 100644 --- a/bridges/MastodonBridge.php +++ b/bridges/MastodonBridge.php @@ -77,7 +77,10 @@ private function getUsername(){ } public function getURI(){ - return 'https://' . $this->getInstance() . '/users/' . $this->getUsername() . '.atom'; + if($this->getInput('canusername')) + return 'https://' . $this->getInstance() . '/users/' . $this->getUsername() . '.atom'; + + return parent::getURI(); } public function collectData(){ From 2d38f2b94db5d65c43f9ddbe1de2a9157db3fd49 Mon Sep 17 00:00:00 2001 From: LogMANOriginal Date: Fri, 21 Jun 2019 17:21:53 +0200 Subject: [PATCH 10/10] [MastodonBridge] Fix typo --- bridges/MastodonBridge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bridges/MastodonBridge.php b/bridges/MastodonBridge.php index 660066f0fae..9e131b7d588 100644 --- a/bridges/MastodonBridge.php +++ b/bridges/MastodonBridge.php @@ -79,7 +79,7 @@ private function getUsername(){ public function getURI(){ if($this->getInput('canusername')) return 'https://' . $this->getInstance() . '/users/' . $this->getUsername() . '.atom'; - + return parent::getURI(); }