diff --git a/app/Models/Thread.php b/app/Models/Thread.php index 4158e1215..d0c02de6b 100644 --- a/app/Models/Thread.php +++ b/app/Models/Thread.php @@ -17,14 +17,20 @@ use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Support\Carbon; +use Illuminate\Support\Collection as SupportCollection; use Illuminate\Support\Str; +use Spatie\Feed\Feedable; +use Spatie\Feed\FeedItem; -final class Thread extends Model implements ReplyAble, SubscriptionAble +final class Thread extends Model implements ReplyAble, SubscriptionAble, Feedable { use HasAuthor, HasSlug, HasTimestamps, ModelHelpers, ProvidesSubscriptions, ReceivesReplies, HasTags; const TABLE = 'threads'; + const FEED_PAGE_SIZE = 20; + /** * {@inheritdoc} */ @@ -109,6 +115,19 @@ public function delete() parent::delete(); } + public function toFeedItem(): FeedItem + { + $updatedAt = Carbon::parse($this->latest_creation); + + return FeedItem::create() + ->id($this->id) + ->title($this->subject) + ->summary($this->body) + ->updated($updatedAt) + ->link(route('thread', $this->slug)) + ->author($this->author()->name); + } + /** * @return \App\Models\Thread[] */ @@ -173,4 +192,11 @@ public static function resolutionTime() return false; } } + + public static function getFeedItems(): SupportCollection + { + return static::feedQuery() + ->paginate(static::FEED_PAGE_SIZE) + ->getCollection(); + } } diff --git a/composer.json b/composer.json index c261367d7..caa5fdbf8 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,7 @@ "ramsey/uuid": "^3.7", "roave/security-advisories": "dev-master", "spatie/laravel-backup": "^6.4", + "spatie/laravel-feed": "^2.0", "spatie/laravel-robots-middleware": "^1.0" }, "require-dev": { @@ -82,4 +83,4 @@ }, "minimum-stability": "dev", "prefer-stable": true -} +} \ No newline at end of file diff --git a/composer.lock b/composer.lock index 33487c624..791701e2c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "4fab6d98a10722817670581505029cc5", + "content-hash": "d2a2a90bd126aed9f3b95a590fd5f9be", "packages": [ { "name": "aws/aws-sdk-php", @@ -3160,6 +3160,17 @@ { "name": "roave/security-advisories", "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/Roave/SecurityAdvisories.git", + "reference": "3a9ab646603efdccb4f7c4acbb3b36974ef257d8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Roave/SecurityAdvisories/zipball/3a9ab646603efdccb4f7c4acbb3b36974ef257d8", + "reference": "3a9ab646603efdccb4f7c4acbb3b36974ef257d8", + "shasum": "" + }, "conflict": { "3f/pygmentize": "<1.2", "adodb/adodb-php": "<5.20.12", @@ -3360,7 +3371,7 @@ } ], "description": "Prevents installation of composer packages with known security vulnerabilities: no API, simply require it", - "time": "2019-10-09T14:04:58+00:00" + "time": "2019-09-26T17:56:56+00:00" }, { "name": "scrivo/highlight.php", @@ -3405,18 +3416,18 @@ "authors": [ { "name": "Geert Bergman", - "role": "Project Author", - "homepage": "http://www.scrivo.org/" + "homepage": "http://www.scrivo.org/", + "role": "Project Author" }, { "name": "Vladimir Jimenez", - "role": "Contributor", - "homepage": "https://allejo.io" + "homepage": "https://allejo.io", + "role": "Contributor" }, { "name": "Martin Folkers", - "role": "Contributor", - "homepage": "https://twobrain.io" + "homepage": "https://twobrain.io", + "role": "Contributor" } ], "description": "Server side syntax highlighter that supports 185 languages. It's a PHP port of highlight.js", @@ -3554,6 +3565,78 @@ ], "time": "2019-10-16T14:55:55+00:00" }, + { + "name": "spatie/laravel-feed", + "version": "2.6.0", + "source": { + "type": "git", + "url": "https://github.com/spatie/laravel-feed.git", + "reference": "b8a824fc4452a0b901db2dd4f94c48f7826597b8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/laravel-feed/zipball/b8a824fc4452a0b901db2dd4f94c48f7826597b8", + "reference": "b8a824fc4452a0b901db2dd4f94c48f7826597b8", + "shasum": "" + }, + "require": { + "illuminate/contracts": "~5.8.0|^6.0", + "illuminate/http": "~5.8.0|^6.0", + "illuminate/support": "~5.8.0|^6.0", + "php": "^7.2" + }, + "require-dev": { + "orchestra/testbench": "~3.8.0|^4.0", + "phpunit/phpunit": "^8.0", + "spatie/phpunit-snapshot-assertions": "^2.0" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Spatie\\Feed\\FeedServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "Spatie\\Feed\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jolita Grazyte", + "email": "jolita@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + }, + { + "name": "Freek Van der Herten", + "email": "freek@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + }, + { + "name": "Sebastian De Deyne", + "email": "sebastian@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + } + ], + "description": "Generates rss feed", + "homepage": "https://github.com/spatie/laravel-feed", + "keywords": [ + "laravel", + "laravel-feed", + "rss", + "spatie" + ], + "time": "2019-11-08T15:10:51+00:00" + }, { "name": "spatie/laravel-robots-middleware", "version": "1.1.0", diff --git a/config/feed.php b/config/feed.php new file mode 100644 index 000000000..4d69f43af --- /dev/null +++ b/config/feed.php @@ -0,0 +1,26 @@ + [ + 'forum' => [ + /* + * Here you can specify which class and method will return + * the items that should appear in the feed. For example: + * 'App\Model@getAllFeedItems' + * + * You can also pass an argument to that method: + * ['App\Model@getAllFeedItems', 'argument'] + */ + 'items' => [Thread::class.'@getFeedItems'], + + /* + * The feed will be available on this url. + */ + 'url' => '/forum/feed', + + 'title' => 'Laravel.io Forum RSS Feed', + ], + ], +]; diff --git a/resources/views/forum/overview.blade.php b/resources/views/forum/overview.blade.php index 29f6cdb5e..e3ac3d91e 100644 --- a/resources/views/forum/overview.blade.php +++ b/resources/views/forum/overview.blade.php @@ -82,9 +82,17 @@ class="button button-primary">
- - Create Thread + + Create Thread + + + + RSS Feed @include('layouts._ads._forum_sidebar') diff --git a/resources/views/layouts/base.blade.php b/resources/views/layouts/base.blade.php index 8859ac413..eff8c8f49 100644 --- a/resources/views/layouts/base.blade.php +++ b/resources/views/layouts/base.blade.php @@ -17,6 +17,7 @@ window.Laravel = {!! json_encode(['csrfToken' => csrf_token()]) !!}; + @include('feed::links') @include('layouts._favicons') @include('layouts._cookie_consent') @include('layouts._google_analytics') diff --git a/routes/web.php b/routes/web.php index 34d327de8..5864ec6f5 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,5 +1,7 @@ name('home'); Route::get('rules', 'HomeController@rules')->name('rules');