summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhorus2024-01-15 23:03:17 +0100
committerhorus2024-01-15 23:03:17 +0100
commit32f972b2ac74e4fa553ef27ef004c8cce85aad36 (patch)
treeec0bf1a2fb20a12ce1d7432e4dc69d201ac3eb0c
parent67e074289a05e2b21235ea369b3a0c04211aee87 (diff)
downloadcurious-32f972b2ac74e4fa553ef27ef004c8cce85aad36.tar.gz
adds support for mastofeed.org to publish automatically to Mastodon
-rw-r--r--app/Http/Controllers/FeedController.php6
-rw-r--r--app/Http/Controllers/IndexController.php6
-rw-r--r--app/Libraries/Helper.php124
-rw-r--r--resources/views/about.blade.php4
-rw-r--r--resources/views/layouts/app.blade.php2
-rw-r--r--resources/views/layouts/footer.blade.php2
-rw-r--r--resources/views/list.blade.php2
-rw-r--r--routes/web.php2
8 files changed, 122 insertions, 26 deletions
diff --git a/app/Http/Controllers/FeedController.php b/app/Http/Controllers/FeedController.php
index 9fd73a5..5b9c36a 100644
--- a/app/Http/Controllers/FeedController.php
+++ b/app/Http/Controllers/FeedController.php
@@ -19,6 +19,12 @@ class FeedController extends Controller
return Helper::makeFeed($articles, "new");
}
+ public function mastodon()
+ {
+ $articles = Article::orderBy('created_at', 'desc')->take(20)->get();
+ return Helper::makeFeed($articles, "mastodon");
+ }
+
public function popular()
{
diff --git a/app/Http/Controllers/IndexController.php b/app/Http/Controllers/IndexController.php
index 50f2bf4..824a2e2 100644
--- a/app/Http/Controllers/IndexController.php
+++ b/app/Http/Controllers/IndexController.php
@@ -47,6 +47,12 @@ class IndexController extends Controller
return view('list', ["articles" => $articles, "count" => $count]);
}
+ public function show( $id ) {
+ $articles = Article::where('id', $id);
+ $articles = $articles->simplePaginate(10);
+ return view('list', ["articles" => $articles, "count" => 1]);
+ }
+
public function search(Request $request) {
$search_unsafe = $request->input("q");
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);
+ }
+
+ }
}
+
diff --git a/resources/views/about.blade.php b/resources/views/about.blade.php
index e76f15f..14b62dc 100644
--- a/resources/views/about.blade.php
+++ b/resources/views/about.blade.php
@@ -46,6 +46,10 @@ For starters I recommend to start with the <a href="{{ route('popular') }}">most
If you have feedback, want to report a bug or just say hello, hit me up by e-mail: <em>hi @ thisdomain without the www .com</em>
+<br>
+<br>
+You can now also follow on Mastodon <a href="https://mstdn.social/@MostDiscussed" target=_blank>@MostDiscussed</a>.
+
<!--
I was curious what HN may find interesting, so I used upvoted or discussed articles from Wikipedia as a proxy and build this. All articles are <a href="{{ route('popular_topics') }}">structured and categorized</a> to get a better overview. Unsurprisingly <a href="{{ route('topic', 'computing') }}">computing</a> was on the top, but then it might get interesting.
<br>
diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php
index cf30b01..64bc3dc 100644
--- a/resources/views/layouts/app.blade.php
+++ b/resources/views/layouts/app.blade.php
@@ -7,6 +7,8 @@
<!-- CSRF Token -->
<meta name="csrf-token" content="{{ csrf_token() }}">
+ <link rel="me" href="https://mstdn.social/@MostDiscussed">
+
<title>{{ config('app.name', 'Laravel') }}@if( "index" != Request::route()->getName() ) - {{ ucwords(Request::route()->getName()) }} Articles @endif</title>
<!-- Scripts -->
diff --git a/resources/views/layouts/footer.blade.php b/resources/views/layouts/footer.blade.php
index a037c25..1f56b8e 100644
--- a/resources/views/layouts/footer.blade.php
+++ b/resources/views/layouts/footer.blade.php
@@ -11,6 +11,8 @@
</h3>
<strong>
<a href="{{ route('about') }}">About {{ env('APP_NAME') }}</a>
+ <br>
+ <a href="https://mstdn.social/@MostDiscussed">Follow on Mastodon</a>
</strong>
</div>
</footer>
diff --git a/resources/views/list.blade.php b/resources/views/list.blade.php
index 5b64214..575af7c 100644
--- a/resources/views/list.blade.php
+++ b/resources/views/list.blade.php
@@ -75,6 +75,8 @@
Have a deep view into what people are curious about.
</p>
</div>
+ @elseif ( "show" == Request::route()->getName() )
+ <!-- single article. show nothing -->
@else
<h1 class="card-header">
{{ ucwords(Request::route()->getName()) }} Articles
diff --git a/routes/web.php b/routes/web.php
index 0693118..ff3046b 100644
--- a/routes/web.php
+++ b/routes/web.php
@@ -22,8 +22,10 @@ Route::get('/search', 'IndexController@search')->name('search');
Route::get('/popular/topics', 'IndexController@populartopics')->name('popular_topics');
Route::get('/popular', 'IndexController@popular')->name('popular');
Route::get('/random', 'IndexController@random')->name('random');
+Route::get('/article/{id}', 'IndexController@show')->name('show')->where('id', '[0-9]+');
Route::get('/feed/new', 'FeedController@new')->name('feed_new');
+Route::get('/feed/mastodon', 'FeedController@mastodon')->name('feed_mastodon');
Route::get('/feed/popular', 'FeedController@popular')->name('feed_popular');
Route::get('/feed/search', 'FeedController@search')->name('feed_search');