jonnybarnes.uk/app/Services/NoteService.php

88 lines
2.8 KiB
PHP
Raw Normal View History

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;
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)
{
if ($request->header('Content-Type') == 'application/json') {
$content = $request->input('properties.content')[0];
$inReplyTo = $request->input('properties.in-reply-to')[0];
$place = $request->input('properties.location');
if (is_array($place)) {
$place = $place[0];
}
} else {
$content = $request->input('content');
$inReplyTo = $request->input('in-reply-to');
$place = $request->input('location');
}
2016-05-19 15:01:28 +01:00
$note = Note::create(
[
'note' => $content,
'in_reply_to' => $inReplyTo,
2016-05-19 15:01:28 +01:00
'client_id' => $clientId,
]
);
if ($place !== null && $place !== 'no-location') {
if (substr($place, 0, strlen(config('app.url'))) == config('app.url')) {
//uri of form http://host/place/slug, we want slug so chop off start
//thats the apps url plus `/place/`
$slug = mb_substr($place, mb_strlen(config('app.url')) + 7);
$placeModel = Place::where('slug', '=', $slug)->first();
$note->place()->associate($placeModel);
$note->save();
}
if (substr($place, 0, 4) == 'geo:') {
preg_match_all(
'/([0-9\.\-]+)/',
$place,
$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) {
$note->addMedia($file)->toMediaLibrary('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
if (//micropub request, syndication sent as array
(is_array($request->input('syndicate-to'))
2016-05-19 15:01:28 +01:00
&&
(in_array('https://twitter.com/jonnybarnes', $request->input('syndicate-to')))
2016-05-19 15:01:28 +01:00
|| //micropub request, syndication sent as string
($request->input('syndicate-to') == 'https://twitter.com/jonnybarnes')
2016-05-19 15:01:28 +01:00
|| //local admin cp request
($request->input('twitter') == true))
) {
2016-09-20 13:13:05 +01:00
dispatch(new SyndicateToTwitter($note));
2016-05-19 15:01:28 +01:00
}
return $note;
}
}