jonnybarnes.uk/app/Services/NoteService.php

73 lines
2.1 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
declare(strict_types=1);
namespace App\Services;
use App\Note;
use App\Place;
use App\Jobs\SendWebMentions;
use App\Jobs\SyndicateToTwitter;
use App\Jobs\SyndicateToFacebook;
class NoteService
{
/**
* Create a new note.
*
* @param array $data
* @return \App\Note $note
*/
public function createNote(array $data): Note
{
$note = Note::create(
[
'note' => $data['content'],
'in_reply_to' => $data['in-reply-to'],
'client_id' => $data['client-id'],
]
);
if (array_key_exists('location', $data) && $data['location'] !== null && $data['location'] !== 'no-location') {
if (substr($data['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);
}
if (substr($data['location'], 0, 4) == 'geo:') {
preg_match_all(
'/([0-9\.\-]+)/',
$data['location'],
$matches
);
$note->location = $matches[0][0] . ', ' . $matches[0][1];
}
}
/* drop image support for now
//add images to media library
if ($request->hasFile('photo')) {
$files = $request->file('photo');
foreach ($files as $file) {
$note->addMedia($file)->toCollectionOnDisk('images', 's3');
}
}
*/
$note->save();
dispatch(new SendWebMentions($note));
//syndication targets
if (in_array('twitter', $data['syndicate'])) {
dispatch(new SyndicateToTwitter($note));
}
if (in_array('facebook', $data['syndicate'])) {
dispatch(new SyndicateToFacebook($note));
}
return $note;
}
}