jonnybarnes.uk/app/Jobs/SyndicateToTwitter.php

113 lines
3.5 KiB
PHP
Raw Normal View History

2016-05-19 15:01:28 +01:00
<?php
namespace App\Jobs;
use Twitter;
use App\Note;
2016-10-06 01:04:23 +01:00
use App\Place;
2016-05-19 15:01:28 +01:00
use App\Contact;
2016-09-20 13:13:05 +01:00
use Illuminate\Bus\Queueable;
2016-05-19 15:01:28 +01:00
use Jonnybarnes\IndieWeb\Numbers;
use Jonnybarnes\IndieWeb\NotePrep;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
2016-09-20 13:13:05 +01:00
class SyndicateToTwitter implements ShouldQueue
2016-05-19 15:01:28 +01:00
{
2016-09-20 13:13:05 +01:00
use InteractsWithQueue, Queueable, SerializesModels;
2016-05-19 15:01:28 +01:00
protected $note;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Note $note)
{
$this->note = $note;
}
/**
* Execute the job.
*
* @param \Jonnybarnes\IndieWeb\Numbers $numbers
* @param \Jonnybarnes\IndieWeb\NotePrep $noteprep
* @return void
*/
public function handle(Numbers $numbers, NotePrep $noteprep)
{
$noteSwappedNames = $this->swapNames($this->note->getOriginal('note'));
$shorturl = 'https://' . config('url.shorturl') . '/t/' . $numbers->numto60($this->note->id);
$tweet = $noteprep->createNote($noteSwappedNames, $shorturl, 140, true);
$tweetOpts = ['status' => $tweet, 'format' => 'json'];
if ($this->note->in_reply_to) {
$tweetOpts['in_reply_to_status_id'] = $noteprep->replyTweetId($this->note->in_reply_to);
}
if ($this->note->location) {
2016-05-19 15:01:28 +01:00
$explode = explode(':', $this->note->location);
2016-10-19 22:52:07 +01:00
$location = explode(',', $explode[0]); //the latlng will always be in [0]
2016-05-19 15:01:28 +01:00
$lat = trim($location[0]);
$lng = trim($location[1]);
}
if ($this->note->place_id) {
//we force the job to create a place model to get access
//to the postgis methods
$place = Place::find($this->note->place_id);
$lat = $place->location->getLat();
$lng = $place->location->getLng();
}
if (isset($lat) && isset($lng)) {
2016-05-19 15:01:28 +01:00
$tweetOpts['lat'] = $lat;
$tweetOpts['long'] = $lng;
}
2016-05-19 15:01:28 +01:00
$mediaItems = $this->note->getMedia();
if (count($mediaItems) > 0) {
foreach ($mediaItems as $item) {
$uploadedMedia = Twitter::uploadMedia(['media' => file_get_contents($item->getUrl())]);
$mediaIds[] = $uploadedMedia->media_id_string;
}
$tweetOpts['media_ids'] = implode(',', $mediaIds);
}
2016-10-06 01:04:23 +01:00
$responseJson = Twitter::postTweet($tweetOpts);
$response = json_decode($responseJson);
$this->note->tweet_id = $response->id;
2016-05-19 15:01:28 +01:00
$this->note->save();
}
/**
* Swap @names in a note.
*
* When a note is being saved and we are posting it to twitter, we want
* to swap our @local_name to Twitters @twitter_name so the user gets
* mentioned on Twitter.
*
* @param string $note
* @return string $noteSwappedNames
*/
private function swapNames($note)
{
$regex = '/\[.*?\](*SKIP)(*F)|@(\w+)/'; //match @alice but not [@bob](...)
$noteSwappedNames = preg_replace_callback(
$regex,
function ($matches) {
try {
$contact = Contact::where('nick', '=', mb_strtolower($matches[1]))->firstOrFail();
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
return '@' . $matches[1];
}
$twitterHandle = $contact->twitter;
return '@' . $twitterHandle;
},
$note
);
return $noteSwappedNames;
}
}