Skip to content

Commit 677ac04

Browse files
committed
Adding push notifications
1 parent 7979c67 commit 677ac04

15 files changed

+485
-193
lines changed

app/Lio/Accounts/User.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Illuminate\Auth\UserInterface;
44
use Illuminate\Auth\Reminders\RemindableInterface;
55
use Lio\Core\EloquentBaseModel;
6-
use Eloquent;
6+
use App, Eloquent;
77

88
class User extends EloquentBaseModel implements UserInterface, RemindableInterface
99
{
@@ -137,4 +137,9 @@ public function getLatestRepliesPaginated($max = 5)
137137
{
138138
return $this->forumReplies()->paginate($max);
139139
}
140+
141+
public function getPusherChannel()
142+
{
143+
return App::make('hashids')->encrypt($this->id);
144+
}
140145
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php namespace Lio\Notifications;
2+
3+
use Lio\Core\EloquentBaseModel;
4+
5+
class Notification extends EloquentBaseModel
6+
{
7+
protected $table = 'notifications';
8+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php namespace Lio\Notifications;
2+
3+
class NotificationCreator
4+
{
5+
protected $notifications;
6+
7+
public function __construct(NotificationRepository $notifications, NotificationPusher $pusher)
8+
{
9+
$this->notifications = $notifications;
10+
$this->notificationPusher = $pusher;
11+
}
12+
13+
public function create($message, $model, $user, $push = false)
14+
{
15+
$notification = $this->notifications->getNew();
16+
$notification->message = $message;
17+
$notification->owner_type = get_class($model);
18+
$notification->owner_id = $model->id;
19+
$notification->user_id = $user->id;
20+
21+
$notification->save();
22+
23+
if($push) {
24+
$this->notificationPusher->push($message, $notification->url, $user);
25+
}
26+
}
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php namespace Lio\Notifications;
2+
3+
use App;
4+
5+
class NotificationPusher
6+
{
7+
public function __construct()
8+
{
9+
$this->pusher = App::make('pusher');
10+
}
11+
12+
public function push($message, $url, $user)
13+
{
14+
$message = \View::make('layouts._notification')->with(compact('message', 'url'))->render();
15+
$this->pusher->trigger( $user->getPusherChannel(), 'message', ['message' => $message, 'url' => $url]);
16+
}
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php namespace Lio\Notifications;
2+
3+
use Lio\Core\EloquentBaseRepository;
4+
5+
class NotificationRepository extends EloquentBaseRepository
6+
{
7+
public function __construct(Notification $model)
8+
{
9+
$this->model = $model;
10+
}
11+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php namespace Lio\ServiceProviders;
2+
3+
use Illuminate\Support\ServiceProvider;
4+
use Hashids\Hashids;
5+
use Config;
6+
7+
class HashidsServiceProvider extends ServiceProvider
8+
{
9+
public function register()
10+
{
11+
$this->app->bind('hashids', function()
12+
{
13+
return new Hashids(Config::get('app.key'), 12);
14+
});
15+
}
16+
17+
public function provides()
18+
{
19+
return ['hashids'];
20+
}
21+
}
22+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php namespace Lio\ServiceProviders;
2+
3+
use Illuminate\Support\ServiceProvider;
4+
use Config;
5+
6+
class PusherIoServiceProvider extends ServiceProvider
7+
{
8+
public function register()
9+
{
10+
$this->app->bind('pusher', function()
11+
{
12+
$config = Config::get('packages/pusher/pusher-php-server/config');
13+
return new \Pusher( $config['app_key'], $config['app_secret'], $config['app_id']);
14+
});
15+
}
16+
17+
public function provides()
18+
{
19+
return ['pusher'];
20+
}
21+
}

app/config/app.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@
124124
'Lio\ServiceProviders\CommentServiceProvider',
125125
'Lio\ServiceProviders\ForumServiceProvider',
126126
'Lio\ServiceProviders\MarkdownServiceProvider',
127+
'Lio\ServiceProviders\PusherIoServiceProvider',
128+
'Lio\ServiceProviders\HashidsServiceProvider',
127129
),
128130

129131
/*

app/controllers/ForumReplyController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public function postCreateReply()
7171
{
7272
$thread = App::make('slugModel');
7373

74+
\App::make('Lio\Notifications\NotificationCreator')->create('test message', $thread, $thread->author, true);
75+
7476
return App::make('Lio\Forum\ForumReplyCreator')->create($this, [
7577
'body' => Input::get('body'),
7678
'author_id' => Auth::user()->id,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
5+
class NotificationsCreateTable extends Migration {
6+
7+
/**
8+
* Run the migrations.
9+
*
10+
* @return void
11+
*/
12+
public function up()
13+
{
14+
Schema::table('notifications', function($t) {
15+
$t->create();
16+
17+
$t->increments('id');
18+
$t->text('message');
19+
$t->string('owner_type');
20+
$t->integer('owner_id');
21+
$t->integer('user_id');
22+
23+
$t->timestamps();
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::drop('notifications');
35+
}
36+
37+
}

0 commit comments

Comments
 (0)