2016-05-19 15:01:28 +01:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
|
|
use App\Note;
|
|
|
|
|
use App\Place;
|
|
|
|
|
use Illuminate\Http\Request;
|
2016-06-23 14:27:00 +01:00
|
|
|
|
use App\Jobs\SendWebMentions;
|
2016-05-19 15:01:28 +01:00
|
|
|
|
use App\Jobs\SyndicateToTwitter;
|
2016-10-26 23:05:58 +01:00
|
|
|
|
use App\Jobs\SyndicateToFacebook;
|
2016-05-19 15:01:28 +01:00
|
|
|
|
|
|
|
|
|
class NoteService
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Create a new note.
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @param string $clientId
|
|
|
|
|
* @return \App\Note $note
|
|
|
|
|
*/
|
|
|
|
|
public function createNote(Request $request, $clientId = null)
|
|
|
|
|
{
|
2016-07-11 17:09:14 +01:00
|
|
|
|
if ($request->header('Content-Type') == 'application/json') {
|
|
|
|
|
$content = $request->input('properties.content')[0];
|
|
|
|
|
$inReplyTo = $request->input('properties.in-reply-to')[0];
|
2016-10-07 15:27:48 +01:00
|
|
|
|
$location = $request->input('properties.location');
|
|
|
|
|
if (is_array($location)) {
|
|
|
|
|
$location = $location[0];
|
2016-07-11 17:09:14 +01:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$content = $request->input('content');
|
|
|
|
|
$inReplyTo = $request->input('in-reply-to');
|
2016-10-07 15:27:48 +01:00
|
|
|
|
$location = $request->input('location');
|
2016-07-11 17:09:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-19 15:01:28 +01:00
|
|
|
|
$note = Note::create(
|
|
|
|
|
[
|
2016-07-11 17:09:14 +01:00
|
|
|
|
'note' => $content,
|
|
|
|
|
'in_reply_to' => $inReplyTo,
|
2016-05-19 15:01:28 +01:00
|
|
|
|
'client_id' => $clientId,
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
2016-10-07 15:27:48 +01:00
|
|
|
|
if ($location !== null && $location !== 'no-location') {
|
|
|
|
|
if (substr($location, 0, strlen(config('app.url'))) == config('app.url')) {
|
2016-10-07 12:15:17 +01:00
|
|
|
|
//uri of form http://host/places/slug, we want slug so chop off start
|
|
|
|
|
//that’s the app’s url plus `/places/`
|
2016-10-07 15:27:48 +01:00
|
|
|
|
$slug = mb_substr($location, mb_strlen(config('app.url')) + 8);
|
|
|
|
|
$place = Place::where('slug', '=', $slug)->first();
|
|
|
|
|
$note->place()->associate($place);
|
2016-10-05 16:10:00 +01:00
|
|
|
|
$note->save();
|
|
|
|
|
}
|
2016-10-07 15:27:48 +01:00
|
|
|
|
if (substr($location, 0, 4) == 'geo:') {
|
2016-10-05 16:10:00 +01:00
|
|
|
|
preg_match_all(
|
|
|
|
|
'/([0-9\.\-]+)/',
|
2016-10-07 15:27:48 +01:00
|
|
|
|
$location,
|
2016-10-05 16:10:00 +01:00
|
|
|
|
$matches
|
|
|
|
|
);
|
|
|
|
|
$note->location = $matches[0][0] . ', ' . $matches[0][1];
|
|
|
|
|
$note->save();
|
|
|
|
|
}
|
2016-05-19 15:01:28 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//add images to media library
|
|
|
|
|
if ($request->hasFile('photo')) {
|
|
|
|
|
$files = $request->file('photo');
|
|
|
|
|
foreach ($files as $file) {
|
2016-10-07 14:39:05 +01:00
|
|
|
|
$note->addMedia($file)->toCollectionOnDisk('images', 's3');
|
2016-05-19 15:01:28 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-20 13:13:05 +01:00
|
|
|
|
dispatch(new SendWebMentions($note));
|
2016-05-19 15:01:28 +01:00
|
|
|
|
|
2016-10-26 22:21:26 +01:00
|
|
|
|
//syndication targets
|
2016-12-16 11:49:15 +00:00
|
|
|
|
//from admin CP
|
|
|
|
|
if ($request->input('twitter')) {
|
2016-10-26 22:21:26 +01:00
|
|
|
|
dispatch(new SyndicateToTwitter($note));
|
|
|
|
|
}
|
2016-12-16 11:49:15 +00:00
|
|
|
|
if ($request->input('facebook')) {
|
2016-10-26 22:21:26 +01:00
|
|
|
|
dispatch(new SyndicateToFacebook($note));
|
|
|
|
|
}
|
2016-12-16 11:49:15 +00:00
|
|
|
|
//from a micropub request
|
|
|
|
|
$targets = array_pluck(config('syndication.targets'), 'uid', 'service.name');
|
2017-01-27 20:41:17 +00:00
|
|
|
|
if (is_string($request->input('mp-syndicate-to'))) {
|
|
|
|
|
$service = array_search($request->input('mp-syndicate-to'));
|
2016-12-16 11:49:15 +00:00
|
|
|
|
if ($service == 'Twitter') {
|
|
|
|
|
dispatch(new SyndicateToTwitter($note));
|
|
|
|
|
}
|
|
|
|
|
if ($service == 'Facebook') {
|
|
|
|
|
dispatch(new SyndicateToFacebook($note));
|
|
|
|
|
}
|
2016-05-19 15:01:28 +01:00
|
|
|
|
}
|
2017-01-27 20:41:17 +00:00
|
|
|
|
if (is_array($request->input('mp-syndicate-to'))) {
|
2016-12-16 11:49:15 +00:00
|
|
|
|
foreach ($targets as $service => $target) {
|
2017-01-27 20:41:17 +00:00
|
|
|
|
if (in_array($target, $request->input('mp-syndicate-to'))) {
|
2016-12-16 11:49:15 +00:00
|
|
|
|
if ($service == 'Twitter') {
|
|
|
|
|
dispatch(new SyndicateToTwitter($note));
|
|
|
|
|
}
|
|
|
|
|
if ($service == 'Facebook') {
|
|
|
|
|
dispatch(new SyndicateToFacebook($note));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-26 22:21:26 +01:00
|
|
|
|
}
|
2016-05-19 15:01:28 +01:00
|
|
|
|
|
|
|
|
|
return $note;
|
|
|
|
|
}
|
|
|
|
|
}
|