Skip to content

Commit

Permalink
Merge pull request #22 from mikebarlow/remove-public-user-prop
Browse files Browse the repository at this point in the history
Removing use of user model in public property
  • Loading branch information
mikebarlow authored Feb 25, 2023
2 parents e0eb259 + 8ffb541 commit bbd9ffb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [1.2.0] - 2023-02-25

* Removed `public $user` from component and changed loading of announcements to prevent user model data exposure. [PR #22](https://github.com/mikebarlow/megaphone/pull/22)
* Added ability to pass in the notifiableId via component render

## [1.1.0] - 2022-12-27

* Improvement: New SVG Bell Icon [PR #17](https://github.com/mikebarlow/megaphone/pull/17)
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,22 @@ This will clear any "read" Megaphone notifications older than 2 weeks old. This

The 2-week time limit for old notifications is controlled via the Megaphone config file, `config('megaphone.clearAfter')`. So should you wish to alter this cut off point, simply change this value to either extend or shorten the cut off.

## Changing Notifiable Model

Because notifications can be attached to any model via the `Notifiable` trait, Megaphone too can be attached to any model providing the model also has the `Notifiable` trait attached.

As default, Megaphone assumes you will be attaching it to the standard Laravel User model and when loading notifications, it will attempt to retrieve the ID of the logged in user from the Request object.

If you are wanting to attach Megaphone to a Team model for example, change the `model` attribute of the published megaphone config file, `megaphone.php`.

When rendering the Megaphone component, you will then need to pass in the ID of the notifiable model into the component so Megaphone can load the correct notifications

```html
<livewire:megaphone :notifiableId="$user->team->id"></livewire:megaphone>
```



## Testing

If you wish to run the tests, clone out the repository
Expand Down
22 changes: 15 additions & 7 deletions src/Livewire/Megaphone.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class Megaphone extends Component
{
public $user;
public $notifiableId;

public $announcements;

Expand All @@ -23,20 +23,28 @@ class Megaphone extends Component

public function mount(Request $request)
{
$this->user = $request->user();
$this->loadAnnouncements($this->user);
if (empty($this->notifiableId) && $request->user() !== null) {
$this->notifiableId = $request->user()->id;
}

$this->loadAnnouncements($this->getNotifiable());
$this->showCount = config('megaphone.showCount', true);
}

public function loadAnnouncements($user)
public function getNotifiable()
{
return config('megaphone.model')::find($this->notifiableId);
}

public function loadAnnouncements($notifiable)
{
$this->unread = $this->announcements = collect([]);

if ($user === null || get_class($user) !== config('megaphone.model')) {
if ($notifiable === null || get_class($notifiable) !== config('megaphone.model')) {
return;
}

$announcements = $user->announcements()->get();
$announcements = $notifiable->announcements()->get();
$this->unread = $announcements->whereNull('read_at');
$this->announcements = $announcements->whereNotNull('read_at');
}
Expand All @@ -49,6 +57,6 @@ public function render()
public function markAsRead(DatabaseNotification $notification)
{
$notification->markAsRead();
$this->loadAnnouncements($this->user);
$this->loadAnnouncements($this->getNotifiable());
}
}

0 comments on commit bbd9ffb

Please sign in to comment.