2017-10-12 18:33:08 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2017-10-13 12:31:31 +01:00
|
|
|
use App\Tag;
|
2017-10-12 18:33:08 +01:00
|
|
|
use App\Bookmark;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Jobs\ProcessBookmark;
|
2017-10-13 13:45:57 +01:00
|
|
|
use App\Jobs\SyndicateBookmarkToTwitter;
|
|
|
|
use App\Jobs\SyndicateBookmarkToFacebook;
|
2017-10-12 18:33:08 +01:00
|
|
|
|
|
|
|
class BookmarkService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Create a new Bookmark.
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
*/
|
2017-10-13 12:31:31 +01:00
|
|
|
public function createBookmark(Request $request): Bookmark
|
2017-10-12 18:33:08 +01:00
|
|
|
{
|
|
|
|
if ($request->header('Content-Type') == 'application/json') {
|
|
|
|
//micropub request
|
|
|
|
$url = normalize_url($request->input('properties.bookmark-of.0'));
|
|
|
|
$name = $request->input('properties.name.0');
|
|
|
|
$content = $request->input('properties.content.0');
|
|
|
|
$categories = $request->input('properties.category');
|
|
|
|
}
|
|
|
|
if (
|
2017-10-13 12:31:31 +01:00
|
|
|
($request->header('Content-Type') == 'application/x-www-form-urlencoded')
|
2017-10-12 18:33:08 +01:00
|
|
|
||
|
2017-10-13 12:31:31 +01:00
|
|
|
(str_contains($request->header('Content-Type'), 'multipart/form-data'))
|
2017-10-12 18:33:08 +01:00
|
|
|
) {
|
|
|
|
$url = normalize_url($request->input('bookmark-of'));
|
|
|
|
$name = $request->input('name');
|
|
|
|
$content = $request->input('content');
|
2017-10-13 12:31:31 +01:00
|
|
|
$categories = $request->input('category');
|
2017-10-12 18:33:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$bookmark = Bookmark::create([
|
|
|
|
'url' => $url,
|
|
|
|
'name' => $name,
|
|
|
|
'content' => $content,
|
|
|
|
]);
|
|
|
|
|
2017-10-13 12:31:31 +01:00
|
|
|
foreach((array) $categories as $category) {
|
2017-10-12 18:33:08 +01:00
|
|
|
$tag = Tag::firstOrCreate(['tag' => $category]);
|
|
|
|
$bookmark->tags()->save($tag);
|
|
|
|
}
|
|
|
|
|
2017-10-13 13:45:57 +01:00
|
|
|
$targets = array_pluck(config('syndication.targets'), 'uid', 'service.name');
|
|
|
|
$mpSyndicateTo = null;
|
|
|
|
if ($request->has('mp-syndicate-to')) {
|
|
|
|
$mpSyndicateTo = $request->input('mp-syndicate-to');
|
|
|
|
}
|
|
|
|
if ($request->has('properties.mp-syndicate-to')) {
|
|
|
|
$mpSyndicateTo = $request->input('properties.mp-syndicate-to');
|
|
|
|
}
|
|
|
|
if (is_string($mpSyndicateTo)) {
|
|
|
|
$service = array_search($mpSyndicateTo, $targets);
|
|
|
|
if ($service == 'Twitter') {
|
|
|
|
SyndicateBookmarkToTwitter::dispatch($bookmark);
|
|
|
|
}
|
|
|
|
if ($service == 'Facebook') {
|
|
|
|
SyndicateBookmarkToFacebook::dispatch($bookmark);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_array($mpSyndicateTo)) {
|
|
|
|
foreach ($mpSyndicateTo as $uid) {
|
|
|
|
$service = array_search($uid, $targets);
|
|
|
|
if ($service == 'Twitter') {
|
|
|
|
SyndicateBookmarkToTwitter::dispatch($bookmark);
|
|
|
|
}
|
|
|
|
if ($service == 'Facebook') {
|
|
|
|
SyndicateBookmarkToFacebook::dispatch($bookmark);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-12 18:33:08 +01:00
|
|
|
ProcessBookmark::dispatch($bookmark);
|
|
|
|
|
2017-10-13 12:31:31 +01:00
|
|
|
return $bookmark;
|
2017-10-12 18:33:08 +01:00
|
|
|
}
|
|
|
|
}
|