jonnybarnes.uk/app/Note.php

531 lines
14 KiB
PHP
Raw Normal View History

2016-05-19 15:01:28 +01:00
<?php
namespace App;
use Cache;
use Twitter;
2016-05-19 15:01:28 +01:00
use Normalizer;
use GuzzleHttp\Client;
use Laravel\Scout\Searchable;
2017-07-10 14:02:13 +01:00
use League\CommonMark\Converter;
use League\CommonMark\DocParser;
2016-05-19 15:01:28 +01:00
use Jonnybarnes\IndieWeb\Numbers;
2017-07-10 14:02:13 +01:00
use League\CommonMark\Environment;
use League\CommonMark\HtmlRenderer;
2016-05-19 15:01:28 +01:00
use Illuminate\Database\Eloquent\Model;
use Jonnybarnes\EmojiA11y\EmojiModifier;
2016-05-19 15:01:28 +01:00
use Illuminate\Database\Eloquent\SoftDeletes;
2017-07-10 14:02:13 +01:00
use Jonnybarnes\CommonmarkLinkify\LinkifyExtension;
2016-05-19 15:01:28 +01:00
use Illuminate\Database\Eloquent\ModelNotFoundException;
class Note extends Model
2016-05-19 15:01:28 +01:00
{
use Searchable;
2016-05-19 15:01:28 +01:00
use SoftDeletes;
/**
* The reges for matching lone usernames.
*
* @var string
*/
private const USERNAMES_REGEX = '/\[.*?\](*SKIP)(*F)|@(\w+)/';
2016-05-19 15:01:28 +01:00
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'notes';
/**
* Define the relationship with tags.
*
* @var array
*/
public function tags()
{
return $this->belongsToMany('App\Tag');
}
/**
* Define the relationship with clients.
*
* @var array?
*/
2017-06-22 15:44:41 +00:00
public function client()
{
return $this->belongsTo('App\MicropubClient', 'client_id', 'client_url');
}
2016-05-19 15:01:28 +01:00
/**
* Define the relationship with webmentions.
*
* @var array
*/
public function webmentions()
{
return $this->morphMany('App\WebMention', 'commentable');
}
/**
* Definte the relationship with places.
*
* @var array
*/
public function place()
{
return $this->belongsTo('App\Place');
}
/**
* Define the relationship with media.
*
* @return void
*/
public function media()
{
return $this->hasMany('App\Media');
}
2016-05-19 15:01:28 +01:00
/**
* We shall set a blacklist of non-modifiable model attributes.
*
* @var array
*/
protected $guarded = ['id'];
/**
* Hide the column used with Laravel Scout.
*
* @var array
*/
protected $hidden = ['searchable'];
2016-05-19 15:01:28 +01:00
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at'];
/**
* Set the attributes to be indexed for searching with Scout.
*
* @return array
*/
public function toSearchableArray()
{
return [
'note' => $this->note,
];
}
2016-05-19 15:01:28 +01:00
/**
* Normalize the note to Unicode FORM C.
*
* @param string $value
* @return string
*/
public function setNoteAttribute($value)
{
$this->attributes['note'] = normalizer_normalize($value, Normalizer::FORM_C);
}
/**
* Pre-process notes for web-view.
*
* @param string
* @return string
*/
public function getNoteAttribute($value)
{
$emoji = new EmojiModifier();
2017-07-10 14:02:13 +01:00
$html = $this->convertMarkdown($value);
2016-05-19 15:01:28 +01:00
$hcards = $this->makeHCards($html);
$hashtags = $this->autoLinkHashtag($hcards);
$modified = $emoji->makeEmojiAccessible($hashtags);
2016-05-19 15:01:28 +01:00
return $modified;
2016-05-19 15:01:28 +01:00
}
/**
* Generate the NewBase60 ID from primary ID.
*
* @return string
*/
public function getNb60idAttribute()
{
$numbers = new Numbers();
return $numbers->numto60($this->id);
}
/**
* The Long URL for a note.
*
* @return string
*/
public function getLongurlAttribute()
{
return config('app.url') . '/notes/' . $this->nb60id;
}
/**
* The Short URL for a note.
*
* @return string
*/
public function getShorturlAttribute()
{
return config('app.shorturl') . '/notes/' . $this->nb60id;
}
/**
* Get the ISO8601 value for mf2.
*
* @return string
*/
public function getIso8601Attribute()
{
return $this->updated_at->toISO8601String();
}
/**
* Get the ISO8601 value for mf2.
*
* @return string
*/
public function getHumandiffAttribute()
{
return $this->updated_at->diffForHumans();
}
/**
* Get the pubdate value for RSS feeds.
*
* @return string
*/
public function getPubdateAttribute()
{
return $this->updated_at->toRSSString();
}
2016-05-19 15:01:28 +01:00
/**
* Get the latitude value.
*
* @return string|null
*/
public function getLatitudeAttribute()
{
if ($this->place !== null) {
$lnglat = explode(' ', $this->place->location);
return $lnglat[1];
}
if ($this->location !== null) {
$pieces = explode(':', $this->location);
$latlng = explode(',', $pieces[0]);
return trim($latlng[0]);
}
}
/**
* Get the longitude value.
2016-05-19 15:01:28 +01:00
*
* @return string|null
*/
public function getLongitudeAttribute()
{
if ($this->place !== null) {
$lnglat = explode(' ', $this->place->location);
return $lnglat[1];
}
if ($this->location !== null) {
$pieces = explode(':', $this->location);
$latlng = explode(',', $pieces[0]);
return trim($latlng[1]);
}
}
/**
* Get the address for a note. This is either a reverse geo-code from the
* location, or is derived from the associated place.
*
* @return string|null
*/
public function getAddressAttribute()
{
if ($this->place !== null) {
return $this->place->name;
}
if ($this->location !== null) {
return $this->reverseGeoCode((float) $this->latitude, (float) $this->longitude);
}
}
public function getTwitterAttribute()
{
if ($this->in_reply_to == null || mb_substr($this->in_reply_to, 0, 20, 'UTF-8') !== 'https://twitter.com/') {
return;
}
$tweetId = basename($this->in_reply_to);
if (Cache::has($tweetId)) {
return Cache::get($tweetId);
}
try {
$oEmbed = Twitter::getOembed([
'id' => $tweetId,
'align' => 'center',
'omit_script' => true,
'maxwidth' => 550,
]);
} catch (\Exception $e) {
return;
}
Cache::put($tweetId, $oEmbed, ($oEmbed->cache_age / 60));
return $oEmbed;
2016-05-19 15:01:28 +01:00
}
/**
* Show a specific form of the note for twitter.
*/
public function getTwitterContentAttribute()
{
// find @mentions
preg_match_all(self::USERNAMES_REGEX, $this->getOriginal('note'), $matches);
if (count($matches[1]) === 0) {
return;
}
// check if any @mentions have a contact associated with them
$count = 0;
foreach ($matches[1] as $match) {
$contact = Contact::where('nick', '=', mb_strtolower($match))->first();
if ($contact) {
$count++;
}
}
if ($count === 0) {
return;
}
// swap in twitter usernames
$swapped = preg_replace_callback(
self::USERNAMES_REGEX,
function ($matches) {
try {
$contact = Contact::where('nick', '=', mb_strtolower($matches[1]))->firstOrFail();
} catch (ModelNotFoundException $e) {
//assume its an actual twitter handle
return $matches[0];
}
if ($contact->twitter) {
return '@' . $contact->twitter;
}
return $contact->name;
},
$this->getOriginal('note')
);
return $this->convertMarkdown($swapped);
}
public function getFacebookContentAttribute()
{
// find @mentions
preg_match_all(self::USERNAMES_REGEX, $this->getOriginal('note'), $matches);
if (count($matches[1]) === 0) {
return;
}
// check if any @mentions have a contact associated with them
$count = 0;
foreach ($matches[1] as $match) {
$contact = Contact::where('nick', '=', mb_strtolower($match))->first();
if ($contact) {
$count++;
}
}
if ($count === 0) {
return;
}
// swap in facebook usernames
$swapped = preg_replace_callback(
self::USERNAMES_REGEX,
function ($matches) {
try {
$contact = Contact::where('nick', '=', mb_strtolower($matches[1]))->firstOrFail();
} catch (ModelNotFoundException $e) {
//assume its an actual twitter handle
return $matches[0];
}
if ($contact->facebook) {
return '<a class="u-category h-card" href="https://facebook.com/' . $contact->facebook . '">' . $contact->name . '</a>';
}
return $contact->name;
},
$this->getOriginal('note')
);
return $this->convertMarkdown($swapped);
}
Micropub Update and “Swarm” Support Much better micropub client/endpoint and the ability to support ownyourswarm. Squashed commit of the following: commit b5be117b852b7a598da72325a4eaf831526c700a Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Thu May 18 15:02:00 2017 +0100 Add a token endpoint test commit 71b7ff68d088180770ca8c2fb43e33bf4b385a96 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Thu May 18 13:49:00 2017 +0100 Fix phpcs issue commit 54d96d32f280127061254e7bc6dfe196e2e35a39 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Thu May 18 13:47:08 2017 +0100 Move token code into its own controller commit 3ed2b4d36d57a9b3bf2a532eb262ec71fc9aa046 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Thu May 18 13:01:09 2017 +0100 Improve/fix code for issuing a token commit 8c411a1df1d59f12dd1c1a4ac884f53dbd1b9351 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Thu May 18 12:59:33 2017 +0100 Remove sprurious comment commit 1b8a3b6502a2b982f737fb4b58602995451e38b9 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon May 15 11:57:03 2017 +0100 Wrap helper functions in check for existence commit 75c706b5a6c1fca7bf45038409689416a3b5ba4d Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon May 15 11:40:17 2017 +0100 Fix a test error, array_key_exists must be run on an array commit 685e96501b8dc906c6945fca39721fb79fa34a8b Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Mon May 15 11:11:51 2017 +0100 Remove errant dd() commit 02536ebaa6daec2a78bc79c44392ac5a82c3200e Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Sun May 14 21:37:17 2017 +0100 Test a typical ownyourswarm request commit 31d959e35ccec9dac5986f93732d928c08e246c8 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Sun May 14 21:36:37 2017 +0100 Better tagging of notes commit e3d4ff8b050ba41febed4d3ab0d30898f0056b33 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Sun May 14 21:35:35 2017 +0100 When a note has a “checkin” try and create the place if it doesn’t already exist commit 4fc0dd0121ca09915a13f3c21c97611db1d744a6 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Fri May 12 12:26:25 2017 +0100 better handling of exceptions, `return` the exctacted response method commit 2e3ca3297d2f494eb88af732519bd7ea8bcc3611 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Fri May 12 11:22:52 2017 +0100 compser update and minor style tweak commit b883d03cc349798230986a5cb50e23e370ce5a09 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Thu May 11 19:03:52 2017 +0100 Improve artisan tinker; see https://blog.tighten.co/supercharge-your-laravel-tinker-workflow commit 8de63172fc7d367870624ff25d1ada92af2d61a7 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Thu May 11 17:28:16 2017 +0100 Tweak code, make it slightly more readable by catching custom exception, and moving a repeated response into its own function commit 8ff0a1196d254d8788477d26b548f2ecff0a7986 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Thu May 11 17:21:39 2017 +0100 Add a custom exception to catch commit 3a568da65ef22b1b676ea8378cd51ce88750b6af Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Thu May 11 17:21:11 2017 +0100 composer update && yarn upgrade commit b70242e616827eab6a2132f3e691ec91be689fb5 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed May 10 20:00:03 2017 +0100 modify some returns for style purposes commit b65170ba1515cbb07beb66fcb3358d69d86cf3a2 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed May 10 19:39:46 2017 +0100 composer update commit cb6198db03a8e8c5c365e88d565401dd6420be13 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed May 10 17:07:33 2017 +0100 composer update commit 91cdd9e17ba192b833da76e0243829cd037170f3 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Fri Apr 28 21:41:12 2017 +0100 verify input array contains necessary data commit 6b230278bfcf2707b7ea1af8e15acd0d7cd27623 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Fri Apr 28 21:40:30 2017 +0100 pass the right input content to the data array commit 96f30d25810751328f75964e81899496add7292e Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Fri Apr 28 21:19:28 2017 +0100 Add support for the published property commit 8168a14969711ff5f84d29eca73036980f9b5a6b Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Fri Apr 28 18:48:23 2017 +0100 Fix some bugs in the `normalize_url()` function and add some tests for the helpers functions commit 34adcebefa7cafec8d26d438b0046120751780be Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Fri Apr 28 15:12:08 2017 +0100 Fix PSR-2 issues, exluding group use declarations commit 4b6651c318d534db1fcb83e1b66562c6dd560165 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Fri Apr 28 12:36:54 2017 +0100 composer update commit a0788ffb6bd4d24245986bf83fe349b8e39786cd Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Apr 26 12:44:06 2017 +0100 Make the retreived a Note a model instance instead of collection, update the content of JSON resposes for updating a note commit f444cfd570a8316a8bb961b901ca2beb3ba74cd2 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Apr 26 12:43:23 2017 +0100 Add tests for updating a post commit ada7e513263b1b0519600538a8a2cb757c74d520 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Apr 26 12:42:50 2017 +0100 Add swarm_url column commit f4e1dba1b315b3d923049f9f5c7a47a730267cb7 Merge: 7208ec5 400857a Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Tue Apr 25 15:33:28 2017 +0100 Merge pull request #50 from jonnybarnes/analysis-XNDLxp Apply fixes from StyleCI commit 400857af57f873bf63b452fdf65ed2632eef9311 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Tue Apr 25 14:33:06 2017 +0000 Apply fixes from StyleCI commit 7208ec53ff51f0c5d002c222c465767c46c275b2 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Tue Apr 25 15:30:02 2017 +0100 Correct some tests, for example the error returned for the specific mistake in `getInvalidToken()` has changed commit 60550667156d7306cf750768b89fa329742c3927 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Tue Apr 25 15:29:06 2017 +0100 Fix some typos commit 0324cb010e77606e0f99e8bb68376b79995abffc Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Tue Apr 25 15:20:27 2017 +0100 Allow notes to be updated by a micropub client endpoint commit b3b3170b359548d21ddae9a9572c182d2a7d51be Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Tue Apr 25 15:18:48 2017 +0100 When a note with a photo is being created, adjust for if URL is on my own endpoint, or somehwere else commit a2437879b000728a2e7d2b91fa642f7cdfd1e698 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Tue Apr 25 15:17:25 2017 +0100 Modify method that returns URL to check if path is already a fully qualified URL commit 64eb53e0f87cb5ee55013de5ed8e2487eee36f0c Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Tue Apr 25 15:16:12 2017 +0100 Add a method to find notes based on NewBase60 id
2017-05-18 15:15:53 +01:00
/**
* Scope a query to select a note via a NewBase60 id.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $nb60id
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeNb60($query, $nb60id)
{
$numbers = new Numbers();
return $query->where('id', $numbers->b60tonum($nb60id));
}
2016-05-19 15:01:28 +01:00
/**
* Take note that this method does two things, given @username (NOT [@username](URL)!)
* we try to create a fancy hcard from our contact info. If this is not possible
* due to lack of contact info, we assume @username is a twitter handle and link it
* as such.
*
* @param string The notes text
* @return string
*/
private function makeHCards($text)
{
$hcards = preg_replace_callback(
self::USERNAMES_REGEX,
2016-05-19 15:01:28 +01:00
function ($matches) {
try {
$contact = Contact::where('nick', '=', mb_strtolower($matches[1]))->firstOrFail();
} catch (ModelNotFoundException $e) {
Squashed commit of the following: commit 2840f58c61c906f5ac408e9681cf33f802625f74 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Fri Nov 25 15:52:58 2016 +0000 Add changes commit 86b515a20c65e4956766242db424d84082c7e99e Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Fri Nov 25 15:42:00 2016 +0000 gulp derived assets commit def587e2f3805a0ba669d804b12d83e9f3ec5ea7 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Fri Nov 25 15:41:43 2016 +0000 Include new contact styles commit 8256dc30b0ad23096b3dcde264826fc6cfaa8788 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Fri Nov 25 15:41:19 2016 +0000 Better styled contacts commit f12ce1d6f68857d88ad6f39f8b835d036c793c8a Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 18:11:47 2016 +0000 Sort out views for contacts, better h-card commit 7be5fe82029b20f6cde3ce921f0cb625c27d21d6 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 18:09:00 2016 +0000 quick code tidy commit 71dad7e4918ff4e513715d4ef3a296fa39507ca1 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:58:32 2016 +0000 Get contact editing working commit 0b885190733979be4f5508d28523e8e0b45399a2 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:42:10 2016 +0000 Improve form layout, add facebook entry commit 0a6bd79e384dcd872cb9a89b1232afaf20e729b2 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:41:49 2016 +0000 Add facebook column commit 639d49045c9a213eafd970ceafe288a51cfc95c8 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:30:40 2016 +0000 Update changelog commit f4a018591d50bf9af7e1a64daa9e4a04daa6e1d4 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:27:42 2016 +0000 Reference right view, use a better variable name when parsing a URL commit 583f7d7f7cc577cf31b37bbc2bdcd8865c7b9980 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:26:58 2016 +0000 Move mini-hcard to templates folder, update content commit 2e1b13eff052b65cf2b86796a9509c81d9c86768 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:26:10 2016 +0000 Add facebook column to contacts table
2016-11-25 15:53:14 +00:00
//assume its an actual twitter handle
2016-05-19 15:01:28 +01:00
return '<a href="https://twitter.com/' . $matches[1] . '">' . $matches[0] . '</a>';
}
Squashed commit of the following: commit 2840f58c61c906f5ac408e9681cf33f802625f74 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Fri Nov 25 15:52:58 2016 +0000 Add changes commit 86b515a20c65e4956766242db424d84082c7e99e Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Fri Nov 25 15:42:00 2016 +0000 gulp derived assets commit def587e2f3805a0ba669d804b12d83e9f3ec5ea7 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Fri Nov 25 15:41:43 2016 +0000 Include new contact styles commit 8256dc30b0ad23096b3dcde264826fc6cfaa8788 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Fri Nov 25 15:41:19 2016 +0000 Better styled contacts commit f12ce1d6f68857d88ad6f39f8b835d036c793c8a Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 18:11:47 2016 +0000 Sort out views for contacts, better h-card commit 7be5fe82029b20f6cde3ce921f0cb625c27d21d6 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 18:09:00 2016 +0000 quick code tidy commit 71dad7e4918ff4e513715d4ef3a296fa39507ca1 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:58:32 2016 +0000 Get contact editing working commit 0b885190733979be4f5508d28523e8e0b45399a2 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:42:10 2016 +0000 Improve form layout, add facebook entry commit 0a6bd79e384dcd872cb9a89b1232afaf20e729b2 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:41:49 2016 +0000 Add facebook column commit 639d49045c9a213eafd970ceafe288a51cfc95c8 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:30:40 2016 +0000 Update changelog commit f4a018591d50bf9af7e1a64daa9e4a04daa6e1d4 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:27:42 2016 +0000 Reference right view, use a better variable name when parsing a URL commit 583f7d7f7cc577cf31b37bbc2bdcd8865c7b9980 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:26:58 2016 +0000 Move mini-hcard to templates folder, update content commit 2e1b13eff052b65cf2b86796a9509c81d9c86768 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:26:10 2016 +0000 Add facebook column to contacts table
2016-11-25 15:53:14 +00:00
$host = parse_url($contact->homepage, PHP_URL_HOST);
$contact->photo = (file_exists(public_path() . '/assets/profile-images/' . $host . '/image')) ?
'/assets/profile-images/' . $host . '/image'
2016-05-19 15:01:28 +01:00
:
'/assets/profile-images/default-image';
Squashed commit of the following: commit 2840f58c61c906f5ac408e9681cf33f802625f74 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Fri Nov 25 15:52:58 2016 +0000 Add changes commit 86b515a20c65e4956766242db424d84082c7e99e Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Fri Nov 25 15:42:00 2016 +0000 gulp derived assets commit def587e2f3805a0ba669d804b12d83e9f3ec5ea7 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Fri Nov 25 15:41:43 2016 +0000 Include new contact styles commit 8256dc30b0ad23096b3dcde264826fc6cfaa8788 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Fri Nov 25 15:41:19 2016 +0000 Better styled contacts commit f12ce1d6f68857d88ad6f39f8b835d036c793c8a Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 18:11:47 2016 +0000 Sort out views for contacts, better h-card commit 7be5fe82029b20f6cde3ce921f0cb625c27d21d6 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 18:09:00 2016 +0000 quick code tidy commit 71dad7e4918ff4e513715d4ef3a296fa39507ca1 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:58:32 2016 +0000 Get contact editing working commit 0b885190733979be4f5508d28523e8e0b45399a2 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:42:10 2016 +0000 Improve form layout, add facebook entry commit 0a6bd79e384dcd872cb9a89b1232afaf20e729b2 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:41:49 2016 +0000 Add facebook column commit 639d49045c9a213eafd970ceafe288a51cfc95c8 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:30:40 2016 +0000 Update changelog commit f4a018591d50bf9af7e1a64daa9e4a04daa6e1d4 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:27:42 2016 +0000 Reference right view, use a better variable name when parsing a URL commit 583f7d7f7cc577cf31b37bbc2bdcd8865c7b9980 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:26:58 2016 +0000 Move mini-hcard to templates folder, update content commit 2e1b13eff052b65cf2b86796a9509c81d9c86768 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:26:10 2016 +0000 Add facebook column to contacts table
2016-11-25 15:53:14 +00:00
return trim(view('templates.mini-hcard', ['contact' => $contact])->render());
2016-05-19 15:01:28 +01:00
},
$text
);
return $hcards;
}
/**
* Given a string and section, finds all hashtags matching
* `#[\-_a-zA-Z0-9]+` and wraps them in an `a` element with
* `rel=tag` set and a `href` of 'section/tagged/' + tagname without the #.
*
* @param string The note
* @return string
*/
private function autoLinkHashtag($text)
{
// $replacements = ["#tag" => "<a rel="tag" href="/tags/tag">#tag</a>]
$replacements = [];
$matches = [];
if (preg_match_all('/(?<=^|\s)\#([a-zA-Z0-9\-\_]+)/i', $text, $matches, PREG_PATTERN_ORDER)) {
// Look up #tags, get Full name and URL
foreach ($matches[0] as $name) {
$name = str_replace('#', '', $name);
$replacements[$name] =
2016-12-12 14:03:31 +00:00
'<a rel="tag" class="p-category" href="/notes/tagged/'
. Tag::normalizeTag($name)
. '">#'
. $name
. '</a>';
2016-05-19 15:01:28 +01:00
}
// Replace #tags with valid microformat-enabled link
foreach ($replacements as $name => $replacement) {
$text = str_replace('#' . $name, $replacement, $text);
}
}
return $text;
}
private function convertMarkdown($text)
{
$environment = Environment::createCommonMarkEnvironment();
$environment->addExtension(new LinkifyExtension());
$converter = new Converter(new DocParser($environment), new HtmlRenderer($environment));
return $converter->convertToHtml($text);
}
/**
* Do a reverse geocode lookup of a `lat,lng` value.
*
* @param float The latitude
* @param float The longitude
* @return string The location HTML
*/
public function reverseGeoCode(float $latitude, float $longitude): string
{
$latlng = $latitude . ',' . $longitude;
return Cache::get($latlng, function () use ($latlng, $latitude, $longitude) {
$guzzle = new Client();
$response = $guzzle->request('GET', 'https://nominatim.openstreetmap.org/reverse', [
'query' => [
'format' => 'json',
'lat' => $latitude,
'lon' => $longitude,
'zoom' => 18,
'addressdetails' => 1,
],
'headers' => ['User-Agent' => 'jonnybarnes.uk via Guzzle, email jonny@jonnybarnes.uk'],
]);
$json = json_decode($response->getBody());
if (isset($json->address->town)) {
$address = '<span class="p-locality">'
. $json->address->town
. '</span>, <span class="p-country-name">'
. $json->address->country
. '</span>';
Cache::forever($latlng, $address);
return $address;
}
if (isset($json->address->city)) {
$address = $json->address->city . ', ' . $json->address->country;
Cache::forever($latlng, $address);
return $address;
}
if (isset($json->address->county)) {
$address = '<span class="p-region">'
. $json->address->county
. '</span>, <span class="p-country-name">'
. $json->address->country
. '</span>';
Cache::forever($latlng, $address);
return $address;
}
$adress = '<span class="p-country-name">' . $json->address->country . '</span>';
Cache::forever($latlng, $address);
return $address;
});
}
2016-05-19 15:01:28 +01:00
}