jonnybarnes.uk/app/Services/NoteService.php

81 lines
2.6 KiB
PHP
Raw Normal View History

2016-05-19 15:01:28 +01:00
<?php
declare(strict_types=1);
2016-05-19 15:01:28 +01:00
namespace App\Services;
use App\{Media, Note, Place};
use App\Jobs\{SendWebMentions, SyndicateToFacebook, SyndicateToTwitter};
2016-05-19 15:01:28 +01:00
class NoteService
{
/**
* Create a new note.
*
* @param array $data
2016-05-19 15:01:28 +01:00
* @return \App\Note $note
*/
public function createNote(array $data): Note
2016-05-19 15:01:28 +01:00
{
$note = Note::create(
[
'note' => $data['content'],
'in_reply_to' => $data['in-reply-to'],
'client_id' => $data['client-id'],
2016-05-19 15:01:28 +01:00
]
);
if (array_key_exists('location', $data) && $data['location'] !== null && $data['location'] !== 'no-location') {
if (starts_with($data['location'], config('app.url'))) {
//uri of form http://host/places/slug, we want slug
//get the URL path, then take last part, we can hack with basename
//as path looks like file path.
$slug = basename(parse_Url($data['location'], PHP_URL_PATH));
$place = Place::where('slug', $slug)->first();
$note->place()->associate($place);
}
if (substr($data['location'], 0, 4) == 'geo:') {
preg_match_all(
'/([0-9\.\-]+)/',
$data['location'],
$matches
);
$note->location = $matches[0][0] . ', ' . $matches[0][1];
}
2016-05-19 15:01:28 +01:00
}
/* drop image support for now
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
}
}
*/
//add support for media uploaded as URLs
foreach ($data['photo'] as $photo) {
// check the media was uploaded to my endpoint
if (starts_with($photo, config('filesystems.disks.s3.url'))) {
$path = substr($photo, strlen(config('filesystems.disks.s3.url')));
$media = Media::where('path', ltrim($path, '/'))->firstOrFail();
$note->media()->save($media);
}
}
$note->save();
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
//syndication targets
if (in_array('twitter', $data['syndicate'])) {
dispatch(new SyndicateToTwitter($note));
}
2017-03-01 21:01:00 +00:00
if (in_array('facebook', $data['syndicate'])) {
dispatch(new SyndicateToFacebook($note));
}
2016-05-19 15:01:28 +01:00
return $note;
}
}