jonnybarnes.uk/app/Jobs/SyndicateToTwitter.php
Jonny Barnes 4e6148e038 Squashed commit of the following:
commit ee8c6db5ce1ebf8dbcef81848e8a99e3c102f964
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Wed Oct 5 17:29:02 2016 +0100

    Add info to changelog

commit d0b213673698691f4624d6aef8b9067a21e722fc
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Wed Oct 5 17:27:38 2016 +0100

    Use the correct variable name

commit 5d5dd00409f61403e4097503c6b3d75a3f83c9c8
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Wed Oct 5 17:26:39 2016 +0100

    Add lat/lng values to a tweet if we know them
2016-10-05 17:29:56 +01:00

109 lines
3.4 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
namespace App\Jobs;
use Twitter;
use App\Note;
use App\Contact;
use Illuminate\Bus\Queueable;
use Jonnybarnes\IndieWeb\Numbers;
use Jonnybarnes\IndieWeb\NotePrep;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class SyndicateToTwitter implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
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) {
$explode = explode(':', $this->note->location);
$location = (count($explode) == 2) ? explode(',', $explode[0]) : explode(',', $explode);
$lat = trim($location[0]);
$lng = trim($location[1]);
}
if ($this->note->place) {
$lat = $this->note->place->getLat();
$lng = $this->note->place->getLng();
}
if (isset($lat) && isset($lng)) {
$tweetOpts['lat'] = $lat;
$tweetOpts['long'] = $lng;
}
$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);
}
$responseJson = Twitter::postTweet($tweetOpts);
$response = json_decode($responseJson);
$tweetId = $response->id;
$this->note->tweet_id = $tweetId;
$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;
}
}