2015-12-01 16:59:28 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Cachet.
|
|
|
|
*
|
|
|
|
* (c) Alt Three Services Limited
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace CachetHQ\Cachet\Http\Controllers;
|
|
|
|
|
|
|
|
use CachetHQ\Cachet\Models\ComponentGroup;
|
|
|
|
use CachetHQ\Cachet\Models\Incident;
|
|
|
|
use GrahamCampbell\Markdown\Facades\Markdown;
|
|
|
|
use Illuminate\Routing\Controller;
|
2016-01-29 22:49:06 +00:00
|
|
|
use Illuminate\Support\Facades\Config;
|
2015-12-01 16:59:28 +08:00
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
class FeedController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Feed facade.
|
|
|
|
*
|
2016-02-09 19:51:07 +00:00
|
|
|
* @var \Roumen\Feed\Feed
|
2015-12-01 16:59:28 +08:00
|
|
|
*/
|
|
|
|
protected $feed;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new feed controller instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2016-02-09 19:51:07 +00:00
|
|
|
$this->feed = app('feed');
|
2016-01-29 22:49:06 +00:00
|
|
|
$this->feed->title = Config::get('setting.app_name');
|
2015-12-01 16:59:28 +08:00
|
|
|
$this->feed->description = trans('cachet.feed');
|
2016-01-29 22:49:06 +00:00
|
|
|
$this->feed->link = Str::canonicalize(Config::get('setting.app_domain'));
|
2016-02-09 19:51:07 +00:00
|
|
|
$this->feed->ctype = 'text/xml';
|
2015-12-01 16:59:28 +08:00
|
|
|
$this->feed->setDateFormat('datetime');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates an Atom feed of all incidents.
|
|
|
|
*
|
|
|
|
* @param \CachetHQ\Cachet\Models\ComponentGroup|null $group
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function atomAction(ComponentGroup $group = null)
|
|
|
|
{
|
|
|
|
return $this->feedAction($group, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a Rss feed of all incidents.
|
|
|
|
*
|
|
|
|
* @param \CachetHQ\Cachet\Models\ComponentGroup|null $group
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function rssAction(ComponentGroup $group = null)
|
|
|
|
{
|
2016-01-29 22:49:06 +00:00
|
|
|
$this->feed->lang = Config::get('setting.app_locale');
|
2015-12-01 16:59:28 +08:00
|
|
|
|
|
|
|
return $this->feedAction($group, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a Rss feed of all incidents.
|
|
|
|
*
|
|
|
|
* @param \CachetHQ\Cachet\Models\ComponentGroup|null $group
|
|
|
|
* @param bool $isRss
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
private function feedAction(ComponentGroup &$group, $isRss)
|
|
|
|
{
|
|
|
|
if ($group->exists) {
|
2016-03-05 13:38:06 +00:00
|
|
|
$group->components->map(function ($component) use ($isRss) {
|
2015-12-01 16:59:28 +08:00
|
|
|
$component->incidents()->visible()->orderBy('created_at', 'desc')->get()->map(function ($incident) use ($isRss) {
|
|
|
|
$this->feedAddItem($incident, $isRss);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Incident::visible()->orderBy('created_at', 'desc')->get()->map(function ($incident) use ($isRss) {
|
|
|
|
$this->feedAddItem($incident, $isRss);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->feed->render($isRss ? 'rss' : 'atom');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an item to the feed.
|
|
|
|
*
|
|
|
|
* @param \CachetHQ\Cachet\Models\Incident $incident
|
|
|
|
* @param bool $isRss
|
|
|
|
*/
|
2015-12-23 14:50:45 +00:00
|
|
|
private function feedAddItem(Incident $incident, $isRss)
|
2015-12-01 16:59:28 +08:00
|
|
|
{
|
|
|
|
$this->feed->add(
|
|
|
|
$incident->name,
|
2016-01-29 22:49:06 +00:00
|
|
|
Config::get('setting.app_name'),
|
2015-12-01 16:59:28 +08:00
|
|
|
Str::canonicalize(route('incident', ['id' => $incident->id])),
|
|
|
|
$isRss ? $incident->created_at->toRssString() : $incident->created_at->toAtomString(),
|
|
|
|
$isRss ? $incident->message : Markdown::convertToHtml($incident->message)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|