Merge branch 'release/0.11.1'

This commit is contained in:
Jonny Barnes 2017-10-22 19:06:21 +01:00
commit c15a395c2b
5 changed files with 89 additions and 70 deletions

View file

@ -4,6 +4,7 @@ namespace App;
use Cache;
use Twitter;
use Debugbar;
use Normalizer;
use GuzzleHttp\Client;
use Laravel\Scout\Searchable;
@ -16,7 +17,6 @@ use Illuminate\Database\Eloquent\Model;
use Jonnybarnes\EmojiA11y\EmojiModifier;
use Illuminate\Database\Eloquent\SoftDeletes;
use Jonnybarnes\CommonmarkLinkify\LinkifyExtension;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class Note extends Model
{
@ -28,7 +28,15 @@ class Note extends Model
*
* @var string
*/
private const USERNAMES_REGEX = '/\[.*?\](*SKIP)(*F)|@(\w+)/';
private const USERNAMES_REGEX = '/@(\w+)/';
protected $contacts;
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->contacts = null;
}
/**
* The database table used by the model.
@ -37,6 +45,24 @@ class Note extends Model
*/
protected $table = 'notes';
/*
* Mass-assignment
*
* @var array
*/
protected $fillable = [
'note',
'in_reply_to',
'client_id',
];
/**
* Hide the column used with Laravel Scout.
*
* @var array
*/
protected $hidden = ['searchable'];
/**
* Define the relationship with tags.
*
@ -87,27 +113,6 @@ class Note extends Model
return $this->hasMany('App\Media');
}
/**
* 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'];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at'];
/**
* Set the attributes to be indexed for searching with Scout.
*
@ -141,9 +146,9 @@ class Note extends Model
{
$emoji = new EmojiModifier();
$html = $this->convertMarkdown($value);
$hcards = $this->makeHCards($html);
$hashtags = $this->autoLinkHashtag($hcards);
$hcards = $this->makeHCards($value);
$html = $this->convertMarkdown($hcards);
$hashtags = $this->autoLinkHashtag($html);
$modified = $emoji->makeEmojiAccessible($hashtags);
return $modified;
@ -297,21 +302,16 @@ class Note extends Model
*/
public function getTwitterContentAttribute()
{
// find @mentions
preg_match_all(self::USERNAMES_REGEX, $this->getOriginal('note'), $matches);
if (count($matches[1]) === 0) {
if ($this->contacts === null) {
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($this->contacts) === 0) {
return;
}
if ($count === 0) {
if (count(array_unique(array_values($this->contacts))) === 1
&& array_unique(array_values($this->contacts))[0] === null) {
return;
}
@ -319,12 +319,11 @@ class Note extends Model
$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
if (is_null($this->contacts[$matches[1]])) {
return $matches[0];
}
$contact = $this->contacts[$matches[1]];
if ($contact->twitter) {
return '@' . $contact->twitter;
}
@ -339,21 +338,12 @@ class Note extends Model
public function getFacebookContentAttribute()
{
// find @mentions
preg_match_all(self::USERNAMES_REGEX, $this->getOriginal('note'), $matches);
if (count($matches[1]) === 0) {
if (count($this->contacts) === 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) {
if (count(array_unique(array_values($this->contacts))) === 1
&& array_unique(array_values($this->contacts))[0] === null) {
return;
}
@ -361,12 +351,11 @@ class Note extends Model
$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
if (is_null($this->contacts[$matches[1]])) {
return $matches[0];
}
$contact = $this->contacts[$matches[1]];
if ($contact->facebook) {
return '<a class="u-category h-card" href="https://facebook.com/'
. $contact->facebook . '">' . $contact->name . '</a>';
@ -405,15 +394,20 @@ class Note extends Model
*/
private function makeHCards($text)
{
$this->getContacts();
if (count($this->contacts) === 0) {
return $text;
}
$hcards = 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
if (is_null($this->contacts[$matches[1]])) {
return '<a href="https://twitter.com/' . $matches[1] . '">' . $matches[0] . '</a>';
}
$contact = $this->contacts[$matches[1]]; // easier to read the following code
$host = parse_url($contact->homepage, PHP_URL_HOST);
$contact->photo = (file_exists(public_path() . '/assets/profile-images/' . $host . '/image')) ?
'/assets/profile-images/' . $host . '/image'
@ -428,6 +422,28 @@ class Note extends Model
return $hcards;
}
public function getContacts()
{
if ($this->contacts === null) {
$this->setContacts();
}
}
public function setContacts()
{
$contacts = [];
if ($this->getOriginal('note')) {
preg_match_all(self::USERNAMES_REGEX, $this->getoriginal('note'), $matches);
foreach ($matches[1] as $match) {
Debugbar::info('query inside getContacts');
$contacts[$match] = Contact::where('nick', mb_strtolower($match))->first();
}
}
$this->contacts = $contacts;
}
/**
* Given a string and section, finds all hashtags matching
* `#[\-_a-zA-Z0-9]+` and wraps them in an `a` element with

View file

@ -1,5 +1,8 @@
# Changelog
## Version 0.11.1 (2017-10-22)
- Improve eloquent queries for rendering notes with contacts
## Version 0.11 (2017-10-19)
- No more built-in micropub client

View file

@ -5,7 +5,5 @@ use Faker\Generator as Faker;
$factory->define(App\Note::class, function (Faker $faker) {
return [
'note' => $faker->paragraph,
'tweet_id' => $faker->randomNumber(9),
'facebook_url' => 'https://facebook.com/' . $faker->randomNumber(9),
];
});

View file

@ -15,8 +15,8 @@ class NotesTableSeeder extends Seeder
sleep(1);
$noteWithPlace = App\Note::create([
'note' => 'Having a #beer at the local. 🍺',
'tweet_id' => '123456789',
]);
$noteWithPlace->tweet_id = '123456789';
$place = App\Place::find(1);
$noteWithPlace->place()->associate($place);
$noteWithPlace->save();
@ -43,15 +43,17 @@ class NotesTableSeeder extends Seeder
}
$noteWithCoords = App\Note::create([
'note' => 'Note from somehwere',
'location' => '53.499,-2.379'
]);
$noteWithCoords->location = '53.499,-2.379';
$noteWithCoords->save();
sleep(1);
$noteSyndicated = App\Note::create([
'note' => 'This note has all the syndication targets',
'tweet_id' => '123456',
'facebook_url' => 'https://www.facebook.com/post/12345789',
'swarm_url' => 'https://www.swarmapp.com/checking/123456789',
'instagram_url' => 'https://www.instagra.com/p/aWsEd123Jh',
]);
$noteSyndicated->tweet_id = '123456';
$noteSyndicated->facebook_url = 'https://www.facebook.com/post/12345789';
$noteSyndicated->swarm_url = 'https://www.swarmapp.com/checking/123456789';
$noteSyndicated->instagram_url = 'https://www.instagram.com/p/aWsEd123Jh';
$noteSyndicated->save();
}
}

View file

@ -1,7 +1,7 @@
@extends('master')
@section('title')
{{ strip_tags($note->note) }} « Notes «
{{ strip_tags($note->getOriginal('note')) }} « Notes «
@stop
@section('content')