jonnybarnes.uk/app/Services/NoteService.php
Jonny Barnes e8c847579c Squashed commit of the following:
commit d0c1c0083ced0470500060f14d37cdfff3028795
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Dec 16 11:46:40 2016 +0000

    remove done @todo

commit f61a968bb8a7743c2b34ea3efcb25cfd73452cfc
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Dec 16 11:43:22 2016 +0000

    Comment for using empty array

commit 9ad7cde20d259ff4527d315049e3df83f145fe0f
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Dec 16 11:42:18 2016 +0000

    Syndication targets are now dynamically checked
2016-12-16 11:49:15 +00:00

108 lines
3.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Services;
use App\Note;
use App\Place;
use Illuminate\Http\Request;
use App\Jobs\SendWebMentions;
use App\Jobs\SyndicateToTwitter;
use App\Jobs\SyndicateToFacebook;
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];
$location = $request->input('properties.location');
if (is_array($location)) {
$location = $location[0];
}
} else {
$content = $request->input('content');
$inReplyTo = $request->input('in-reply-to');
$location = $request->input('location');
}
$note = Note::create(
[
'note' => $content,
'in_reply_to' => $inReplyTo,
'client_id' => $clientId,
]
);
if ($location !== null && $location !== 'no-location') {
if (substr($location, 0, strlen(config('app.url'))) == config('app.url')) {
//uri of form http://host/places/slug, we want slug so chop off start
//thats the apps url plus `/places/`
$slug = mb_substr($location, mb_strlen(config('app.url')) + 8);
$place = Place::where('slug', '=', $slug)->first();
$note->place()->associate($place);
$note->save();
}
if (substr($location, 0, 4) == 'geo:') {
preg_match_all(
'/([0-9\.\-]+)/',
$location,
$matches
);
$note->location = $matches[0][0] . ', ' . $matches[0][1];
$note->save();
}
}
//add images to media library
if ($request->hasFile('photo')) {
$files = $request->file('photo');
foreach ($files as $file) {
$note->addMedia($file)->toCollectionOnDisk('images', 's3');
}
}
dispatch(new SendWebMentions($note));
//syndication targets
//from admin CP
if ($request->input('twitter')) {
dispatch(new SyndicateToTwitter($note));
}
if ($request->input('facebook')) {
dispatch(new SyndicateToFacebook($note));
}
//from a micropub request
$targets = array_pluck(config('syndication.targets'), 'uid', 'service.name');
if (is_string($request->input('syndicate-to'))) {
$service = array_search($request->input('syndicate-to'));
if ($service == 'Twitter') {
dispatch(new SyndicateToTwitter($note));
}
if ($service == 'Facebook') {
dispatch(new SyndicateToFacebook($note));
}
}
if (is_array($request->input('syndicate-to'))) {
foreach ($targets as $service => $target) {
if (in_array($target, $request->input('syndicate-to'))) {
if ($service == 'Twitter') {
dispatch(new SyndicateToTwitter($note));
}
if ($service == 'Facebook') {
dispatch(new SyndicateToFacebook($note));
}
}
}
}
return $note;
}
}