summaryrefslogtreecommitdiff
path: root/app/Libraries/Helper.php
diff options
context:
space:
mode:
Diffstat (limited to 'app/Libraries/Helper.php')
-rw-r--r--app/Libraries/Helper.php124
1 files changed, 98 insertions, 26 deletions
diff --git a/app/Libraries/Helper.php b/app/Libraries/Helper.php
index be055b9..a900b87 100644
--- a/app/Libraries/Helper.php
+++ b/app/Libraries/Helper.php
@@ -27,6 +27,10 @@ class Helper {
$feed_title = 'Popular Articles';
$feed_description = 'The most popular articles. Keep exploring.';
break;
+ case( "mastodon"):
+ $feed_title = 'Feed for Mastodon';
+ $feed_description = 'New articles are published automatically to Mastodon.';
+ break;
default:
$feed_title = 'Search for: ' . $title;
$feed_description = 'All articles for "' . $title . '".';
@@ -54,41 +58,89 @@ class Helper {
$feed->pubdate = $model[0]->created_at;
$feed->lang = 'en';
$feed->setShortening(false); // true or false
- $feed->setTextLimit(100); // maximum length of description text
+
+ if ( "mastodon" == $title ) {
+ $feed->setTextLimit(500); // maximum length of description text
+ } else {
+ $feed->setTextLimit(100); // maximum length of description text
+ }
foreach ($model as $post)
{
$desc = ($post->excerpt_html);
- if ( ! $post->getCategories()->get()->isEmpty() ) {
- $categories = "<br>";
- foreach( $post->getCategories()->get() as $cat ) {
- $categories .= "<a href='". \URL::to('/topic/' . $cat->name) ."'>". $cat->name . "</a> | ";
+ if ( "mastodon" == $title ) {
+
+ $cat_len= 0;
+ if ( ! $post->getCategories()->get()->isEmpty() ) {
+ $categories = "#MostDiscussed ";
+ foreach( $post->getCategories()->get() as $cat ) {
+ $categories .= "#". $cat->name . " ";
+ }
+ $cat_counter = $post->getCategories()->get()->count();
+ $cat_len = mb_strlen($categories, "UTF-8");
}
- $categories = rtrim($categories, " | ");
- $desc .= "<br>Topics:";
- $desc .= $categories;
- }
- $discussions = "<br>";
- foreach( $post->getDiscussions()->orderBy('comments', 'desc')->get() as $dis ) {
- $discussions .= "<a href='" . $dis->source_url . "'>" . $dis->title . "</a> ";
- $discussions .= $dis->upvotes . " Upvotes | " . $dis->comments . " Comments<br>";
+ $discussions = "";
+ if ( $post->getDiscussions()->orderBy('created_at', 'desc')->get()->count() > 1 ) {
+ // multiple discussions on HN. show one link where you can find them all
+ $discussions = env('APP_URL') . "/article/" . $post->id;
+ } else {
+ // only on disc link. ### TODO foreach not needed
+ foreach( $post->getDiscussions()->orderBy('created_at', 'desc')->get() as $dis ) {
+ $discussions = $dis->source_url;
+ }
+ }
+
+ // max desc length is 500 - 23 (Link) - $cat_len - 2 (white spaces)
+ $max_len = 500 - 23 - $cat_len - 2;
+
+ $desc = Helper::first_sentence($desc, $max_len);
+
+ $desc .= " " . $categories;# . $discussions;
+
+ // set item's title, author, url, pubdate, description, content, enclosure (optional)*
+ $feed->addItem([
+ 'title' => $desc, // mastofeed.org seems to ignore the description field
+ 'author' => env('APP_NAME'),
+ 'url' => \URL::to($post->url),
+ 'link' => \URL::to($post->url),
+ 'pubdate' => $post->created_at,
+ 'description' => $desc,
+ 'content' => $desc
+ ]);
+
+ } else {
+ if ( ! $post->getCategories()->get()->isEmpty() ) {
+ $categories = "<br>";
+ foreach( $post->getCategories()->get() as $cat ) {
+ $categories .= "<a href='". \URL::to('/topic/' . $cat->name) ."'>". $cat->name . "</a> | ";
+ }
+ $categories = rtrim($categories, " | ");
+ $desc .= "<br>Topics:";
+ $desc .= $categories;
+ }
+
+ $discussions = "<br>";
+ foreach( $post->getDiscussions()->orderBy('comments', 'desc')->get() as $dis ) {
+ $discussions .= "<a href='" . $dis->source_url . "'>" . $dis->title . "</a> ";
+ $discussions .= $dis->upvotes . " Upvotes | " . $dis->comments . " Comments<br>";
+ }
+ $desc .= "<br><br>Discussions:";
+ $desc .= $discussions;
+
+ // set item's title, author, url, pubdate, description, content, enclosure (optional)*
+ $feed->addItem([
+ 'title' => $post->title,
+ 'author' => env('APP_NAME'),
+ 'url' => \URL::to($post->url),
+ 'link' => \URL::to($post->url),
+ 'pubdate' => $post->created_at,
+ 'description' => $desc,
+ 'content' => $desc
+ ]);
}
- $desc .= "<br><br>Discussions:";
- $desc .= $discussions;
-
- // set item's title, author, url, pubdate, description, content, enclosure (optional)*
- $feed->addItem([
- 'title' => $post->title,
- 'author' => env('APP_NAME'),
- 'url' => \URL::to($post->url),
- 'link' => \URL::to($post->url),
- 'pubdate' => $post->created_at,
- 'description' => $desc,
- 'content' => $desc
- ]);
}
}
@@ -98,4 +150,24 @@ class Helper {
// optional: you can set custom cache key with 3rd param as string
return $feed->render('atom', $cache, 'feed_' . $title);
}
+
+ public static function first_sentence($content, $max_len = NULL) {
+
+ $content = html_entity_decode(strip_tags($content));
+ $content = ltrim($content);
+ $pos = strpos($content, '.');
+
+ if($pos === false) {
+ if ( is_null($max_len) ) {
+ return $content;
+ } else {
+ return substr($content, 0, $max_len);
+ }
+ }
+ else {
+ return substr($content, 0, $pos+1);
+ }
+
+ }
}
+