jonnybarnes.uk/app/Services/NoteService.php
Jonny Barnes e3fff4b9a8 Squashed commit of the following:
commit 41ab44ed1d86a1d0788089cf2d79e3c9ab8e2ba6
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Wed Aug 9 20:58:13 2017 +0100

    Add a test for a swarm checkin with no text

commit b4986ba3207374d11438e00d05a6f1ae1720bd49
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Wed Aug 9 20:57:44 2017 +0100

    A migration that makes the note column of the notes table nullable

commit 80ac4d38c992e59733f60e844f376e36507fb8ee
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Wed Aug 9 20:57:04 2017 +0100

    Don’t fail when the text content of a note is empty

commit 874acdd3b31028c06d19cdbe9ef34bbc9660a704
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Wed Aug 9 20:55:12 2017 +0100

    Allow no content for the actual note, maybe its just a picture. Also provide some context for swarm checkins
2017-08-09 20:59:22 +01:00

116 lines
4.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\{Media, Note, Place};
use App\Jobs\{SendWebMentions, SyndicateToFacebook, SyndicateToTwitter};
class NoteService
{
/**
* Create a new note.
*
* @param array $data
* @return \App\Note $note
*/
public function createNote(array $data): Note
{
//check the input
if (array_key_exists('content', $data) === false) {
$data['content'] = null;
}
if (array_key_exists('in-reply-to', $data) === false) {
$data['in-reply-to'] = null;
}
if (array_key_exists('client-id', $data) === false) {
$data['client-id'] = null;
}
$note = Note::create(
[
'note' => $data['content'],
'in_reply_to' => $data['in-reply-to'],
'client_id' => $data['client-id'],
]
);
if (array_key_exists('published', $data) && empty($data['published']) === false) {
$carbon = new \Carbon\Carbon($data['published']);
$note->created_at = $note->updated_at = $carbon->toDateTimeString();
}
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.
$place = Place::where('slug', basename(parse_url($data['location'], PHP_URL_PATH)))->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];
}
}
if (array_key_exists('checkin', $data) && $data['checkin'] !== null) {
$place = Place::where('slug', basename(parse_url($data['checkin'], PHP_URL_PATH)))->first();
if ($place !== null) {
$note->place()->associate($place);
$note->swarm_url = $data['swarm-url'];
if ($note->note === null || $note->note == '') {
$note->note = 'Ive just checked in with Swarm';
}
}
}
/* 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');
}
}
*/
//add support for media uploaded as URLs
if (array_key_exists('photo', $data)) {
foreach ($data['photo'] as $photo) {
// check the media was uploaded to my endpoint, and use path
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();
} else {
$media = Media::firstOrNew(['path' => $photo]);
// currently assuming this is a photo from Swarm or OwnYourGram
$media->type = 'image';
$media->save();
}
$note->media()->save($media);
}
if (array_key_exists('instagram-url', $data)) {
$note->instagram_url = $data['instagram-url'];
}
}
$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;
}
}