2016-05-19 15:01:28 +01:00
|
|
|
|
<?php
|
|
|
|
|
|
2017-03-01 20:15:35 +00:00
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2016-05-19 15:01:28 +01:00
|
|
|
|
namespace App\Services;
|
2017-06-11 21:05:17 +01:00
|
|
|
|
|
2017-03-18 20:09:11 +00:00
|
|
|
|
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.
|
|
|
|
|
*
|
2017-03-01 20:15:35 +00:00
|
|
|
|
* @param array $data
|
2016-05-19 15:01:28 +01:00
|
|
|
|
* @return \App\Note $note
|
|
|
|
|
*/
|
2017-03-01 20:15:35 +00:00
|
|
|
|
public function createNote(array $data): Note
|
2016-05-19 15:01:28 +01:00
|
|
|
|
{
|
2017-06-11 21:05:17 +01:00
|
|
|
|
|
2017-05-18 15:15:53 +01:00
|
|
|
|
//check the input
|
|
|
|
|
if (array_key_exists('content', $data) === false) {
|
2017-08-09 20:59:22 +01:00
|
|
|
|
$data['content'] = null;
|
2017-05-18 15:15:53 +01:00
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
2016-05-19 15:01:28 +01:00
|
|
|
|
$note = Note::create(
|
|
|
|
|
[
|
2017-03-01 20:15:35 +00:00
|
|
|
|
'note' => $data['content'],
|
|
|
|
|
'in_reply_to' => $data['in-reply-to'],
|
|
|
|
|
'client_id' => $data['client-id'],
|
2016-05-19 15:01:28 +01:00
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
2017-05-18 15:15:53 +01:00
|
|
|
|
if (array_key_exists('published', $data) && empty($data['published']) === false) {
|
2017-09-13 16:13:58 +01:00
|
|
|
|
$carbon = carbon($data['published']);
|
2017-05-18 15:15:53 +01:00
|
|
|
|
$note->created_at = $note->updated_at = $carbon->toDateTimeString();
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-01 20:15:35 +00:00
|
|
|
|
if (array_key_exists('location', $data) && $data['location'] !== null && $data['location'] !== 'no-location') {
|
2017-03-22 19:36:45 +00:00
|
|
|
|
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.
|
2017-06-13 18:17:27 +01:00
|
|
|
|
$place = Place::where('slug', basename(parse_url($data['location'], PHP_URL_PATH)))->first();
|
2016-10-07 15:27:48 +01:00
|
|
|
|
$note->place()->associate($place);
|
2016-10-05 16:10:00 +01:00
|
|
|
|
}
|
2017-03-01 20:15:35 +00:00
|
|
|
|
if (substr($data['location'], 0, 4) == 'geo:') {
|
2016-10-05 16:10:00 +01:00
|
|
|
|
preg_match_all(
|
|
|
|
|
'/([0-9\.\-]+)/',
|
2017-03-01 20:15:35 +00:00
|
|
|
|
$data['location'],
|
2016-10-05 16:10:00 +01:00
|
|
|
|
$matches
|
|
|
|
|
);
|
|
|
|
|
$note->location = $matches[0][0] . ', ' . $matches[0][1];
|
|
|
|
|
}
|
2016-05-19 15:01:28 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-18 15:15:53 +01:00
|
|
|
|
if (array_key_exists('checkin', $data) && $data['checkin'] !== null) {
|
2017-06-13 18:17:27 +01:00
|
|
|
|
$place = Place::where('slug', basename(parse_url($data['checkin'], PHP_URL_PATH)))->first();
|
2017-05-18 15:15:53 +01:00
|
|
|
|
if ($place !== null) {
|
|
|
|
|
$note->place()->associate($place);
|
2017-06-11 20:54:10 +01:00
|
|
|
|
$note->swarm_url = $data['swarm-url'];
|
2017-08-09 20:59:22 +01:00
|
|
|
|
if ($note->note === null || $note->note == '') {
|
|
|
|
|
$note->note = 'I’ve just checked in with Swarm';
|
|
|
|
|
}
|
2017-05-18 15:15:53 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-01 20:15:35 +00: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
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-01 20:15:35 +00:00
|
|
|
|
*/
|
2017-03-18 20:09:11 +00:00
|
|
|
|
//add support for media uploaded as URLs
|
2017-05-19 13:17:27 +01:00
|
|
|
|
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]);
|
2017-06-11 20:54:10 +01:00
|
|
|
|
// currently assuming this is a photo from Swarm or OwnYourGram
|
2017-05-19 13:17:27 +01:00
|
|
|
|
$media->type = 'image';
|
|
|
|
|
$media->save();
|
|
|
|
|
}
|
|
|
|
|
$note->media()->save($media);
|
2017-03-18 20:09:11 +00:00
|
|
|
|
}
|
2017-06-11 20:54:10 +01:00
|
|
|
|
if (array_key_exists('instagram-url', $data)) {
|
|
|
|
|
$note->instagram_url = $data['instagram-url'];
|
|
|
|
|
}
|
2017-03-18 20:09:11 +00:00
|
|
|
|
}
|
2017-03-01 20:15:35 +00:00
|
|
|
|
|
|
|
|
|
$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
|
|
|
|
|
2016-10-26 22:21:26 +01:00
|
|
|
|
//syndication targets
|
2017-03-01 20:15:35 +00:00
|
|
|
|
if (in_array('twitter', $data['syndicate'])) {
|
2017-10-13 13:45:57 +01:00
|
|
|
|
dispatch(new SyndicateNoteToTwitter($note));
|
2016-10-26 22:21:26 +01:00
|
|
|
|
}
|
2017-03-01 21:01:00 +00:00
|
|
|
|
if (in_array('facebook', $data['syndicate'])) {
|
2017-10-13 13:45:57 +01:00
|
|
|
|
dispatch(new SyndicateNoteToFacebook($note));
|
2016-10-26 22:21:26 +01:00
|
|
|
|
}
|
2016-05-19 15:01:28 +01:00
|
|
|
|
|
|
|
|
|
return $note;
|
|
|
|
|
}
|
|
|
|
|
}
|