Add push notification api endpoints

This commit is contained in:
Daniel Supernault 2024-07-22 03:04:04 -06:00
parent 0cac17d8d5
commit 8f7becc58b
No known key found for this signature in database
GPG key ID: 23740873EE6F76A1
5 changed files with 119 additions and 38 deletions

View file

@ -37,6 +37,7 @@ use Jenssegers\Agent\Agent;
use League\Fractal; use League\Fractal;
use League\Fractal\Serializer\ArraySerializer; use League\Fractal\Serializer\ArraySerializer;
use Mail; use Mail;
use NotificationChannels\Expo\ExpoPushToken;
class ApiV1Dot1Controller extends Controller class ApiV1Dot1Controller extends Controller
{ {
@ -1008,4 +1009,65 @@ class ApiV1Dot1Controller extends Controller
return $this->json($account, 200, $rateLimiting ? $limits : []); return $this->json($account, 200, $rateLimiting ? $limits : []);
} }
public function getExpoPushNotifications(Request $request)
{
abort_if(! $request->user() || ! $request->user()->token(), 403);
abort_unless($request->user()->tokenCan('push'), 403);
$user = $request->user();
$res = [
'expo_token' => (bool) $user->expo_token,
'notify_like' => (bool) $user->notify_like,
'notify_follow' => (bool) $user->notify_follow,
'notify_mention' => (bool) $user->notify_mention,
'notify_comment' => (bool) $user->notify_comment,
];
return $this->json($res);
}
public function disableExpoPushNotifications(Request $request)
{
abort_if(! $request->user() || ! $request->user()->token(), 403);
abort_unless($request->user()->tokenCan('push'), 403);
abort_unless(config('services.expo.access_token') && strlen(config('services.expo.access_token')) > 10, 404, 'Push notifications are not supported on this server.');
$request->user()->update([
'expo_token' => null,
]);
return $this->json(['expo_token' => null]);
}
public function updateExpoPushNotifications(Request $request)
{
abort_if(! $request->user() || ! $request->user()->token(), 403);
abort_unless($request->user()->tokenCan('push'), 403);
abort_unless(config('services.expo.access_token') && strlen(config('services.expo.access_token')) > 10, 404, 'Push notifications are not supported on this server.');
$this->validate($request, [
'expo_token' => ['required', ExpoPushToken::rule()],
'notify_like' => 'sometimes',
'notify_follow' => 'sometimes',
'notify_mention' => 'sometimes',
'notify_comment' => 'sometimes',
]);
$user = $request->user()->update([
'expo_token' => $request->input('expo_token'),
'notify_like' => $request->has('notify_like') && $request->boolean('notify_like'),
'notify_follow' => $request->has('notify_follow') && $request->boolean('notify_follow'),
'notify_mention' => $request->has('notify_mention') && $request->boolean('notify_mention'),
'notify_comment' => $request->has('notify_comment') && $request->boolean('notify_comment'),
]);
$res = [
'expo_token' => (bool) $request->user()->expo_token,
'notify_like' => (bool) $request->user()->notify_like,
'notify_follow' => (bool) $request->user()->notify_follow,
'notify_mention' => (bool) $request->user()->notify_mention,
'notify_comment' => (bool) $request->user()->notify_comment,
];
return $this->json($res);
}
} }

View file

@ -2,32 +2,35 @@
namespace App; namespace App;
use Laravel\Passport\HasApiTokens; use App\Services\AvatarService;
use Illuminate\Notifications\Notifiable; use App\Util\RateLimit\User as UserRateLimit;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Util\RateLimit\User as UserRateLimit; use Illuminate\Notifications\Notifiable;
use App\Services\AvatarService; use Laravel\Passport\HasApiTokens;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use NotificationChannels\WebPush\HasPushSubscriptions;
use NotificationChannels\Expo\ExpoPushToken; use NotificationChannels\Expo\ExpoPushToken;
use NotificationChannels\WebPush\HasPushSubscriptions;
class User extends Authenticatable class User extends Authenticatable
{ {
use Notifiable, SoftDeletes, HasApiTokens, UserRateLimit, HasFactory, HasPushSubscriptions; use HasApiTokens, HasFactory, HasPushSubscriptions, Notifiable, SoftDeletes, UserRateLimit;
/** /**
* The attributes that should be mutated to dates. * The attributes that should be mutated to dates.
* *
* @var array * @var array
*/ */
protected $casts = [ protected function casts(): array
'deleted_at' => 'datetime', {
'email_verified_at' => 'datetime', return [
'2fa_setup_at' => 'datetime', 'deleted_at' => 'datetime',
'last_active_at' => 'datetime', 'email_verified_at' => 'datetime',
'expo_token' => ExpoPushToken::class '2fa_setup_at' => 'datetime',
]; 'last_active_at' => 'datetime',
'expo_token' => ExpoPushToken::class,
];
}
/** /**
* The attributes that are mass assignable. * The attributes that are mass assignable.
@ -42,7 +45,12 @@ class User extends Authenticatable
'app_register_ip', 'app_register_ip',
'email_verified_at', 'email_verified_at',
'last_active_at', 'last_active_at',
'register_source' 'register_source',
'expo_token',
'notify_like',
'notify_follow',
'notify_mention',
'notify_comment',
]; ];
/** /**
@ -54,7 +62,7 @@ class User extends Authenticatable
'email', 'password', 'is_admin', 'remember_token', 'email', 'password', 'is_admin', 'remember_token',
'email_verified_at', '2fa_enabled', '2fa_secret', 'email_verified_at', '2fa_enabled', '2fa_secret',
'2fa_backup_codes', '2fa_setup_at', 'deleted_at', '2fa_backup_codes', '2fa_setup_at', 'deleted_at',
'updated_at' 'updated_at',
]; ];
public function profile() public function profile()
@ -97,7 +105,7 @@ class User extends Authenticatable
public function storageUsedKey() public function storageUsedKey()
{ {
return 'profile:storage:used:' . $this->id; return 'profile:storage:used:'.$this->id;
} }
public function accountLog() public function accountLog()
@ -112,8 +120,8 @@ class User extends Authenticatable
public function avatarUrl() public function avatarUrl()
{ {
if(!$this->profile_id || $this->status) { if (! $this->profile_id || $this->status) {
return config('app.url') . '/storage/avatars/default.jpg'; return config('app.url').'/storage/avatars/default.jpg';
} }
return AvatarService::get($this->profile_id); return AvatarService::get($this->profile_id);

View file

@ -19,7 +19,7 @@
"doctrine/dbal": "^3.0", "doctrine/dbal": "^3.0",
"intervention/image": "^2.4", "intervention/image": "^2.4",
"jenssegers/agent": "^2.6", "jenssegers/agent": "^2.6",
"laravel-notification-channels/expo": "^1.3.0|^2.0", "laravel-notification-channels/expo": "~1.3.0|~2.0.0",
"laravel-notification-channels/webpush": "^8.0", "laravel-notification-channels/webpush": "^8.0",
"laravel/framework": "^11.0", "laravel/framework": "^11.0",
"laravel/helpers": "^1.1", "laravel/helpers": "^1.1",

41
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "fbadeaf1fbbd9e0f64feaa1433ca7dd0", "content-hash": "c055c4b1ba26004ab6951e9dba4b4508",
"packages": [ "packages": [
{ {
"name": "aws/aws-crt-php", "name": "aws/aws-crt-php",
@ -2249,29 +2249,34 @@
}, },
{ {
"name": "laravel-notification-channels/expo", "name": "laravel-notification-channels/expo",
"version": "1.3.1", "version": "v2.0.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/laravel-notification-channels/expo.git", "url": "https://github.com/laravel-notification-channels/expo.git",
"reference": "d718a89dfc4997aba69b673f5db416ac833188e9" "reference": "29d038b6409077ac4c671cc5587a4dc7986260b0"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/laravel-notification-channels/expo/zipball/d718a89dfc4997aba69b673f5db416ac833188e9", "url": "https://api.github.com/repos/laravel-notification-channels/expo/zipball/29d038b6409077ac4c671cc5587a4dc7986260b0",
"reference": "d718a89dfc4997aba69b673f5db416ac833188e9", "reference": "29d038b6409077ac4c671cc5587a4dc7986260b0",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"ext-json": "*", "ext-json": "*",
"guzzlehttp/guzzle": "~6.0 || ~7.0", "guzzlehttp/guzzle": "^7.1",
"illuminate/notifications": "~6.0 || ~7.0 || ~8.0 || ~9.0 || ~10.0 || ~11.0", "illuminate/contracts": "^11.0",
"illuminate/support": "~6.0 || ~7.0 || ~8.0 || ~9.0 || ~10.0 || ~11.0", "illuminate/notifications": "^11.0",
"php": ">=7.4 || ^8.0 || ^8.1 || ^8.2 || ^8.3" "illuminate/support": "^11.0",
"php": "~8.3"
}, },
"require-dev": { "require-dev": {
"mockery/mockery": "^1.0", "larastan/larastan": "^2.0",
"orchestra/testbench": "^6.18", "laravel/pint": "^1.0",
"phpunit/phpunit": "^8.0" "orchestra/testbench": "^9.0",
"phpunit/phpunit": "^11.0"
},
"suggest": {
"ext-zlib": "Required for compressing payloads exceeding 1 KiB in size."
}, },
"type": "library", "type": "library",
"extra": { "extra": {
@ -2292,19 +2297,19 @@
], ],
"authors": [ "authors": [
{ {
"name": "Nick Pratley", "name": "Muhammed Sari",
"email": "nick@npratley.net", "email": "muhammed@dive.be",
"homepage": "https://npratley.net/", "homepage": "https://dive.be",
"role": "Developer" "role": "Developer"
} }
], ],
"description": "Expo Notifications driver for Laravel", "description": "Expo Notifications Channel for Laravel",
"homepage": "https://github.com/laravel-notification-channels/expo", "homepage": "https://github.com/laravel-notification-channels/expo",
"support": { "support": {
"issues": "https://github.com/laravel-notification-channels/expo/issues", "issues": "https://github.com/laravel-notification-channels/expo/issues",
"source": "https://github.com/laravel-notification-channels/expo/tree/1.3.1" "source": "https://github.com/laravel-notification-channels/expo/tree/v2.0.0"
}, },
"time": "2024-03-15T00:24:58+00:00" "time": "2024-03-18T07:49:28+00:00"
}, },
{ {
"name": "laravel-notification-channels/webpush", "name": "laravel-notification-channels/webpush",

View file

@ -194,6 +194,12 @@ Route::group(['prefix' => 'api'], function() use($middleware) {
Route::post('invite/admin/uc', 'AdminInviteController@apiUsernameCheck')->middleware('throttle:20,120'); Route::post('invite/admin/uc', 'AdminInviteController@apiUsernameCheck')->middleware('throttle:20,120');
Route::post('invite/admin/ec', 'AdminInviteController@apiEmailCheck')->middleware('throttle:10,1440'); Route::post('invite/admin/ec', 'AdminInviteController@apiEmailCheck')->middleware('throttle:10,1440');
}); });
Route::group(['prefix' => 'expo'], function() use($middleware) {
Route::get('push-notifications', 'Api\ApiV1Dot1Controller@getExpoPushNotifications')->middleware($middleware);
Route::post('push-notifications/update', 'Api\ApiV1Dot1Controller@updateExpoPushNotifications')->middleware($middleware);
Route::post('push-notifications/disable', 'Api\ApiV1Dot1Controller@disableExpoPushNotifications')->middleware($middleware);
});
}); });
Route::group(['prefix' => 'live'], function() use($middleware) { Route::group(['prefix' => 'live'], function() use($middleware) {