Skip to content

Commit

Permalink
feat: address on dashboard feed (monicahq/chandler#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Sep 5, 2022
1 parent 29f5204 commit 7289130
Show file tree
Hide file tree
Showing 22 changed files with 482 additions and 19 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,4 @@ LOCATION_IQ_API_KEY=
# A username is also required, this is the one used upon account creation.
MAPBOX_API_KEY=
MAPBOX_USERNAME=
MAPBOX_CUSTOM_STYLE_NAME=
31 changes: 31 additions & 0 deletions app/Helpers/MapHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,35 @@ public static function getAddressAsString(Address $address): ?string
// trim extra whitespaces inside the address
return Str::of($sentence)->replaceMatches('/\s+/', ' ');
}

/**
* Return the URL for a static image for the given place.
*
* @param Address $address
* @param int $width
* @param int $height
* @param int $zoom
* @return string|null
*/
public static function getStaticImage(Address $address, int $width, int $height, int $zoom = 7): ?string
{
if (! config('monica.mapbox_api_key')) {
return null;
}

if (! config('monica.mapbox_username')) {
return null;
}

$url = 'https://api.mapbox.com/styles/v1/';
$url .= config('monica.mapbox_username').'/';
$url .= config('monica.mapbox_custom_style_name').'/static/';
$url .= $address->longitude.',';
$url .= $address->latitude.',';
$url .= $zoom.'/';
$url .= $width.'x'.$height;
$url .= '?access_token='.config('monica.mapbox_api_key');

return $url;
}
}
11 changes: 11 additions & 0 deletions app/Models/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphOne;

class Address extends Model
{
Expand Down Expand Up @@ -70,4 +71,14 @@ public function addressType(): BelongsTo
{
return $this->belongsTo(AddressType::class);
}

/**
* Get the address's feed item.
*
* @return MorphOne
*/
public function feedItem(): MorphOne
{
return $this->morphOne(ContactFeedItem::class, 'feedable');
}
}
6 changes: 6 additions & 0 deletions app/Models/ContactFeedItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ class ContactFeedItem extends Model

public const ACTION_LABEL_REMOVED = 'label_removed';

public const ACTION_CONTACT_ADDRESS_CREATED = 'address_created';

public const ACTION_CONTACT_ADDRESS_UPDATED = 'address_updated';

public const ACTION_CONTACT_ADDRESS_DESTROYED = 'address_destroyed';

public const ACTION_CONTACT_EVENT_CREATED = 'added an event';

public const ACTION_CONTACT_EVENT_UPDATED = 'updated an event';
Expand Down
38 changes: 26 additions & 12 deletions config/monica.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,32 @@

'mapbox_api_key' => env('MAPBOX_API_KEY', null),

/*
|--------------------------------------------------------------------------
| Mapbox username
|--------------------------------------------------------------------------
|
| Used to display static maps. See https://docs.mapbox.com/help/how-mapbox-works/static-maps/
| You need this if you want to display a custom style for your maps.
| If you have defined a custom style, you need to add your username.
|
*/

'mapbox_username' => env('MAPBOX_USERNAME', 'mapbox'),

/*
|--------------------------------------------------------------------------
| Mapbox custom style
|--------------------------------------------------------------------------
|
| Used to render static maps. If you have defined a custom style for your
| maps on Mapbox, you should indicate its name here, along with your
| username (see above).
|
*/

'mapbox_custom_style_name' => env('MAPBOX_CUSTOM_STYLE_NAME', 'streets-v11'),

/*
|--------------------------------------------------------------------------
| API key for geolocation service.
Expand All @@ -50,18 +76,6 @@

'location_iq_url' => env('LOCATION_IQ_URL', 'https://us1.locationiq.com/v1/'),

/*
|--------------------------------------------------------------------------
| Mapbox username
|--------------------------------------------------------------------------
|
| Used to display static maps. See https://docs.mapbox.com/help/how-mapbox-works/static-maps/
| This should be the username used when creating the account.
|
*/

'mapbox_api_username' => env('MAPBOX_USERNAME', null),

/*
|--------------------------------------------------------------------------
| URL of the documentation center
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace App\Contact\ManageContactAddresses\Services;

use App\Contact\ManageContactAddresses\Jobs\FetchAddressGeocoding;
use App\Helpers\MapHelper;
use App\Interfaces\ServiceInterface;
use App\Models\Address;
use App\Models\AddressType;
use App\Models\ContactFeedItem;
use App\Services\BaseService;
use Carbon\Carbon;

Expand Down Expand Up @@ -89,11 +91,25 @@ public function execute(array $data): Address

$this->geocodeAddress();

$this->createFeedItem();

return $this->address;
}

private function geocodeAddress(): void
{
FetchAddressGeocoding::dispatch($this->address)->onQueue('low');
}

private function createFeedItem(): void
{
$feedItem = ContactFeedItem::create([
'author_id' => $this->author->id,
'contact_id' => $this->contact->id,
'action' => ContactFeedItem::ACTION_CONTACT_ADDRESS_CREATED,
'description' => MapHelper::getAddressAsString($this->address),
]);

$this->address->feedItem()->save($feedItem);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace App\Contact\ManageContactAddresses\Services;

use App\Helpers\MapHelper;
use App\Interfaces\ServiceInterface;
use App\Models\Address;
use App\Models\ContactFeedItem;
use App\Services\BaseService;
use Carbon\Carbon;

Expand Down Expand Up @@ -56,7 +58,19 @@ public function execute(array $data): void

$this->address->delete();

$this->createFeedItem();

$this->contact->last_updated_at = Carbon::now();
$this->contact->save();
}

private function createFeedItem(): void
{
ContactFeedItem::create([
'author_id' => $this->author->id,
'contact_id' => $this->contact->id,
'action' => ContactFeedItem::ACTION_CONTACT_ADDRESS_DESTROYED,
'description' => MapHelper::getAddressAsString($this->address),
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace App\Contact\ManageContactAddresses\Services;

use App\Contact\ManageContactAddresses\Jobs\FetchAddressGeocoding;
use App\Helpers\MapHelper;
use App\Interfaces\ServiceInterface;
use App\Models\Address;
use App\Models\AddressType;
use App\Models\ContactFeedItem;
use App\Services\BaseService;
use Carbon\Carbon;

Expand Down Expand Up @@ -102,6 +104,8 @@ private function update(): void

$this->geocodeAddress();

$this->createFeedItem();

$this->contact->last_updated_at = Carbon::now();
$this->contact->save();
}
Expand All @@ -110,4 +114,16 @@ private function geocodeAddress(): void
{
FetchAddressGeocoding::dispatch($this->address)->onQueue('low');
}

private function createFeedItem(): void
{
$feedItem = ContactFeedItem::create([
'author_id' => $this->author->id,
'contact_id' => $this->contact->id,
'action' => ContactFeedItem::ACTION_CONTACT_ADDRESS_UPDATED,
'description' => MapHelper::getAddressAsString($this->address),
]);

$this->address->feedItem()->save($feedItem);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Contact\ManageContactAddresses\Web\Controllers;

use App\Helpers\MapHelper;
use App\Http\Controllers\Controller;
use App\Models\Address;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Response;

class ContactModuleAddressImageController extends Controller
{
public function show(Request $request, int $vaultId, int $contactId, int $addressId, int $width, int $height)
{
$address = Address::where('contact_id', $contactId)
->findOrFail($addressId);

$url = MapHelper::getStaticImage($address, $width, $height);

$response = Http::get($url)
->throw();

return Response::stream(function () use ($response) {
echo $response->body();
},
200,
Arr::only($response->headers(), [
'Content-Length',
'Content-Type',
'Cache-Control',
'Date',
'ETag',
])
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace App\Contact\ManageContactFeed\Web\ViewHelpers\Actions;

use App\Helpers\MapHelper;
use App\Models\ContactFeedItem;
use App\Models\User;

class ActionFeedAddress
{
public static function data(ContactFeedItem $item, User $user): array
{
$contact = $item->contact;
$address = $item->feedable;

return [
'address' => [
'object' => $address ? [
'id' => $address->id,
'street' => $address->street,
'city' => $address->city,
'province' => $address->province,
'postal_code' => $address->postal_code,
'country' => $address->country,
'type' => $address->addressType ? [
'id' => $address->addressType->id,
'name' => $address->addressType->name,
] : null,
'image' => route('contact.address.image.show', [
'vault' => $contact->vault_id,
'contact' => $contact->id,
'address' => $address->id,
'width' => 300,
'height' => 100,
]),
'url' => [
'show' => MapHelper::getMapLink($address, $user),
],
] : null,
'description' => $item->description,
],
'contact' => [
'id' => $contact->id,
'name' => $contact->name,
'age' => $contact->age,
'avatar' => $contact->avatar,
'url' => route('contact.show', [
'vault' => $contact->vault_id,
'contact' => $contact->id,
]),
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Contact\ManageContactFeed\Web\ViewHelpers;

use App\Contact\ManageContactFeed\Web\ViewHelpers\Actions\ActionFeedAddress;
use App\Contact\ManageContactFeed\Web\ViewHelpers\Actions\ActionFeedContactInformation;
use App\Contact\ManageContactFeed\Web\ViewHelpers\Actions\ActionFeedGenericContactInformation;
use App\Contact\ManageContactFeed\Web\ViewHelpers\Actions\ActionFeedLabelAssigned;
Expand All @@ -20,7 +21,7 @@ public static function data($items, User $user): array
'action' => $item->action,
'author' => self::getAuthor($item),
'sentence' => self::getSentence($item),
'data' => self::getData($item),
'data' => self::getData($item, $user),
'created_at' => DateHelper::format($item->created_at, $user),
];
});
Expand Down Expand Up @@ -63,13 +64,18 @@ private static function getAuthor(ContactFeedItem $item): ?array
return UserHelper::getInformationAboutContact($author, $item->contact->vault);
}

private static function getData(ContactFeedItem $item)
private static function getData(ContactFeedItem $item, User $user)
{
switch ($item->action) {
case 'label_assigned':
case 'label_removed':
return ActionFeedLabelAssigned::data($item);

case 'address_created':
case 'address_updated':
case 'address_destroyed':
return ActionFeedAddress::data($item, $user);

case 'contact_information_created':
case 'contact_information_updated':
case 'contact_information_destroyed':
Expand Down
3 changes: 3 additions & 0 deletions lang/en/contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
'feed_item_important_date_created' => 'added an important date',
'feed_item_important_date_updated' => 'updated an important date',
'feed_item_important_date_destroyed' => 'deleted an important date',
'feed_item_address_created' => 'added an address',
'feed_item_address_updated' => 'updated an address',
'feed_item_address_destroyed' => 'deleted an address',
'feed_item_contact_information_created' => 'added a contact information',
'feed_item_contact_information_updated' => 'updated a contact information',
'feed_item_contact_information_destroyed' => 'deleted a contact information',
Expand Down
3 changes: 3 additions & 0 deletions lang/fr/contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
'feed_item_important_date_created' => 'a ajouté une date importante',
'feed_item_important_date_updated' => 'a mis à jour une date importante',
'feed_item_important_date_destroyed' => 'a supprimé une date importante',
'feed_item_address_created' => 'a ajouté une adresse',
'feed_item_address_updated' => 'a mis à jour une adresse',
'feed_item_address_destroyed' => 'a supprimé une adresse',
'feed_item_contact_information_created' => 'a ajouté une information de contact',
'feed_item_contact_information_updated' => 'a mis à jour une information de contact',
'feed_item_contact_information_destroyed' => 'a supprimé une information de contact',
Expand Down
Loading

0 comments on commit 7289130

Please sign in to comment.