Init
This commit is contained in:
56
app/Override/Comments/CommentPolicy.php
Normal file
56
app/Override/Comments/CommentPolicy.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace Laravelista\Comments;
|
||||
|
||||
use Laravelista\Comments\Comment;
|
||||
|
||||
class CommentPolicy
|
||||
{
|
||||
/**
|
||||
* Can user create the comment
|
||||
*
|
||||
* @param $user
|
||||
* @return bool
|
||||
*/
|
||||
public function create($user) : bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Can user delete the comment
|
||||
*
|
||||
* @param $user
|
||||
* @param Comment $comment
|
||||
* @return bool
|
||||
*/
|
||||
public function delete($user, Comment $comment) : bool
|
||||
{
|
||||
return ($user->getKey() == $comment->commenter_id) || $user->is_admin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Can user update the comment
|
||||
*
|
||||
* @param $user
|
||||
* @param Comment $comment
|
||||
* @return bool
|
||||
*/
|
||||
public function update($user, Comment $comment) : bool
|
||||
{
|
||||
return $user->getKey() == $comment->commenter_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Can user reply to the comment
|
||||
*
|
||||
* @param $user
|
||||
* @param Comment $comment
|
||||
* @return bool
|
||||
*/
|
||||
public function reply($user, Comment $comment) : bool
|
||||
{
|
||||
return $user->getKey() != $comment->commenter_id;
|
||||
}
|
||||
}
|
||||
|
139
app/Override/Comments/CommentService.php
Normal file
139
app/Override/Comments/CommentService.php
Normal file
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
namespace Laravelista\Comments;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
use App\Notifications\CommentNotification;
|
||||
use App\Models\User;
|
||||
use App\Models\Episode;
|
||||
|
||||
class CommentService
|
||||
{
|
||||
/**
|
||||
* Handles creating a new comment for given model.
|
||||
* @return mixed the configured comment-model
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
// If guest commenting is turned off, authorize this action.
|
||||
if (Config::get('comments.guest_commenting') == false) {
|
||||
Gate::authorize('create-comment', Comment::class);
|
||||
}
|
||||
|
||||
// Define guest rules if user is not logged in.
|
||||
if (!Auth::check()) {
|
||||
$guest_rules = [
|
||||
'guest_name' => 'required|string|max:255',
|
||||
'guest_email' => 'required|string|email|max:255',
|
||||
];
|
||||
}
|
||||
|
||||
// Merge guest rules, if any, with normal validation rules.
|
||||
Validator::make($request->all(), array_merge($guest_rules ?? [], [
|
||||
'commentable_type' => 'required|string',
|
||||
'commentable_id' => 'required|string|min:1',
|
||||
'message' => 'required|string'
|
||||
]))->validate();
|
||||
|
||||
$model = $request->commentable_type::findOrFail($request->commentable_id);
|
||||
|
||||
$commentClass = Config::get('comments.model');
|
||||
$comment = new $commentClass;
|
||||
|
||||
if (!Auth::check()) {
|
||||
$comment->guest_name = $request->guest_name;
|
||||
$comment->guest_email = $request->guest_email;
|
||||
} else {
|
||||
$comment->commenter()->associate(Auth::user());
|
||||
}
|
||||
|
||||
$comment->commentable()->associate($model);
|
||||
$comment->comment = $request->message;
|
||||
$comment->approved = !Config::get('comments.approval_required');
|
||||
$comment->save();
|
||||
|
||||
return $comment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles updating the message of the comment.
|
||||
* @return mixed the configured comment-model
|
||||
*/
|
||||
public function update(Request $request, Comment $comment)
|
||||
{
|
||||
Gate::authorize('edit-comment', $comment);
|
||||
|
||||
Validator::make($request->all(), [
|
||||
'message' => 'required|string'
|
||||
])->validate();
|
||||
|
||||
$comment->update([
|
||||
'comment' => $request->message
|
||||
]);
|
||||
|
||||
return $comment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles deleting a comment.
|
||||
* @return mixed the configured comment-model
|
||||
*/
|
||||
public function destroy(Comment $comment): void
|
||||
{
|
||||
Gate::authorize('delete-comment', $comment);
|
||||
|
||||
if (Config::get('comments.soft_deletes') == true) {
|
||||
$comment->delete();
|
||||
} else {
|
||||
$comment->forceDelete();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles creating a reply "comment" to a comment.
|
||||
* @return mixed the configured comment-model
|
||||
*/
|
||||
public function reply(Request $request, Comment $comment)
|
||||
{
|
||||
Gate::authorize('reply-to-comment', $comment);
|
||||
|
||||
Validator::make($request->all(), [
|
||||
'message' => 'required|string'
|
||||
])->validate();
|
||||
|
||||
$commentClass = Config::get('comments.model');
|
||||
$reply = new $commentClass;
|
||||
$reply->commenter()->associate(Auth::user());
|
||||
$reply->commentable()->associate($comment->commentable);
|
||||
$reply->parent()->associate($comment);
|
||||
$reply->comment = $request->message;
|
||||
$reply->approved = !Config::get('comments.approval_required');
|
||||
$reply->save();
|
||||
|
||||
// Notify
|
||||
if ($comment->commentable_type == 'App\Models\Episode') {
|
||||
$episode = Episode::where('id', $comment->commentable_id)->firstOrFail();
|
||||
$url = '/hentai/' . $episode->slug . '#comment-' . $reply->id;
|
||||
|
||||
$user = Auth::user();
|
||||
$username = $user->global_name ?? $user->username;
|
||||
|
||||
$parentCommentUser = User::where('id', $comment->commenter_id)->firstOrFail();
|
||||
$parentCommentUser->notify(
|
||||
new CommentNotification(
|
||||
"{$username} replied to your comment.",
|
||||
Str::limit($reply->comment, 50),
|
||||
$url
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return $reply;
|
||||
}
|
||||
}
|
155
app/Override/Discord/DiscordController.php
Normal file
155
app/Override/Discord/DiscordController.php
Normal file
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
namespace Jakyeru\Larascord\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use App\Providers\RouteServiceProvider;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Jakyeru\Larascord\Http\Requests\StoreUserRequest;
|
||||
use Jakyeru\Larascord\Services\DiscordService;
|
||||
|
||||
use RealRashid\SweetAlert\Facades\Alert;
|
||||
|
||||
class DiscordController extends Controller
|
||||
{
|
||||
/**
|
||||
* Handles the Discord OAuth2 login.
|
||||
*/
|
||||
public function handle(StoreUserRequest $request): RedirectResponse | JsonResponse
|
||||
{
|
||||
// Making sure the "guilds" scope was added to .env if there are any guilds specified in "larascord.guilds".
|
||||
if (count(config('larascord.guilds'))) {
|
||||
if (!in_array('guilds', explode('&', config('larascord.scopes')))) {
|
||||
return $this->throwError('missing_guilds_scope');
|
||||
}
|
||||
}
|
||||
|
||||
// Getting the accessToken from the Discord API.
|
||||
try {
|
||||
$accessToken = (new DiscordService())->getAccessTokenFromCode($request->get('code'));
|
||||
} catch (\Exception $e) {
|
||||
return $this->throwError('invalid_code', $e);
|
||||
}
|
||||
|
||||
// Get the user from the Discord API.
|
||||
try {
|
||||
$user = (new DiscordService())->getCurrentUser($accessToken);
|
||||
$user->setAccessToken($accessToken);
|
||||
} catch (\Exception $e) {
|
||||
return $this->throwError('authorization_failed', $e);
|
||||
}
|
||||
|
||||
// Making sure the user has an email if the email scope is set.
|
||||
if (in_array('email', explode('&', config('larascord.scopes')))) {
|
||||
if (empty($user->email)) {
|
||||
return $this->throwError('missing_email');
|
||||
}
|
||||
}
|
||||
|
||||
if (auth()->check()) {
|
||||
// Making sure the current logged-in user's ID is matching the ID retrieved from the Discord API.
|
||||
if (auth()->id() !== (int)$user->id) {
|
||||
auth()->logout();
|
||||
return $this->throwError('invalid_user');
|
||||
}
|
||||
|
||||
// Confirming the session in case the user was redirected from the password.confirm middleware.
|
||||
$request->session()->put('auth.password_confirmed_at', time());
|
||||
}
|
||||
|
||||
// Trying to create or update the user in the database.
|
||||
// Initiating a database transaction in case something goes wrong.
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$user = (new DiscordService())->createOrUpdateUser($user);
|
||||
$user->accessToken()->updateOrCreate([], $accessToken->toArray());
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
return $this->throwError('database_error', $e);
|
||||
}
|
||||
|
||||
// Verifying if the user is soft-deleted.
|
||||
if (Schema::hasColumn('users', 'deleted_at')) {
|
||||
if ($user->trashed()) {
|
||||
DB::rollBack();
|
||||
return $this->throwError('user_deleted');
|
||||
}
|
||||
}
|
||||
|
||||
// Patreon check
|
||||
try {
|
||||
if (!$accessToken->hasScopes(['guilds', 'guilds.members.read'])) {
|
||||
DB::rollBack();
|
||||
return $this->throwError('missing_guilds_members_read_scope');
|
||||
}
|
||||
$guildMember = (new DiscordService())->getGuildMember($accessToken, config('discord.guild_id'));
|
||||
$patreonroles = config('discord.patreon_roles');
|
||||
$user->is_patreon = false;
|
||||
if ((new DiscordService())->hasRoleInGuild($guildMember, $patreonroles)) {
|
||||
$user->is_patreon = true;
|
||||
}
|
||||
$user->save();
|
||||
} catch (\Exception $e) {
|
||||
// Clearly not a patreon
|
||||
$user->is_patreon = false;
|
||||
$user->save();
|
||||
}
|
||||
|
||||
// Committing the database transaction.
|
||||
DB::commit();
|
||||
|
||||
// Authenticating the user if the user is not logged in.
|
||||
if (!auth()->check()) {
|
||||
auth()->login($user, config('larascord.remember_me', false));
|
||||
}
|
||||
|
||||
// Redirecting the user to the intended page or to the home page.
|
||||
return redirect()->intended(RouteServiceProvider::HOME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the throwing of an error.
|
||||
*/
|
||||
private function throwError(string $message, \Exception $exception = NULL): RedirectResponse | JsonResponse
|
||||
{
|
||||
if (app()->hasDebugModeEnabled()) {
|
||||
return response()->json([
|
||||
'larascord_message' => config('larascord.error_messages.' . $message),
|
||||
'message' => $exception?->getMessage(),
|
||||
'code' => $exception?->getCode()
|
||||
]);
|
||||
} else {
|
||||
if (config('larascord.error_messages.' . $message . '.redirect')) {
|
||||
Alert::error('Error', config('larascord.error_messages.' . $message . '.message', 'An error occurred while trying to log you in.'));
|
||||
return redirect(config('larascord.error_messages.' . $message . '.redirect'))->with('error', config('larascord.error_messages.' . $message . '.message', 'An error occurred while trying to log you in.'));
|
||||
} else {
|
||||
return redirect('/')->with('error', config('larascord.error_messages.' . $message, 'An error occurred while trying to log you in.'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the deletion of the user.
|
||||
*/
|
||||
public function destroy(): RedirectResponse | JsonResponse
|
||||
{
|
||||
// Revoking the OAuth2 access token.
|
||||
try {
|
||||
(new DiscordService())->revokeAccessToken(auth()->user()->accessToken()->first()->refresh_token);
|
||||
} catch (\Exception $e) {
|
||||
return $this->throwError('revoke_token_failed', $e);
|
||||
}
|
||||
|
||||
// Deleting the user from the database.
|
||||
auth()->user()->delete();
|
||||
|
||||
// Showing the success message.
|
||||
if (config('larascord.success_messages.user_deleted.redirect')) {
|
||||
return redirect(config('larascord.success_messages.user_deleted.redirect'))->with('success', config('larascord.success_messages.user_deleted.message', 'Your account has been deleted.'));
|
||||
} else {
|
||||
return redirect('/')->with('success', config('larascord.success_messages.user_deleted', 'Your account has been deleted.'));
|
||||
}
|
||||
}
|
||||
}
|
273
app/Override/Discord/Services/DiscordService.php
Normal file
273
app/Override/Discord/Services/DiscordService.php
Normal file
@@ -0,0 +1,273 @@
|
||||
<?php
|
||||
|
||||
namespace Jakyeru\Larascord\Services;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\OldUser;
|
||||
use App\Models\Playlist;
|
||||
use App\Models\PlaylistEpisode;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Http\Client\RequestException;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Jakyeru\Larascord\Types\AccessToken;
|
||||
use Jakyeru\Larascord\Types\GuildMember;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class DiscordService
|
||||
{
|
||||
/**
|
||||
* The Discord OAuth2 token URL.
|
||||
*/
|
||||
protected string $tokenURL = "https://discord.com/api/oauth2/token";
|
||||
|
||||
/**
|
||||
* The Discord API base URL.
|
||||
*/
|
||||
protected string $baseApi = "https://discord.com/api";
|
||||
/**
|
||||
* The required data for the token request.
|
||||
*/
|
||||
protected array $tokenData = [
|
||||
"client_id" => NULL,
|
||||
"client_secret" => NULL,
|
||||
"grant_type" => "authorization_code",
|
||||
"code" => NULL,
|
||||
"redirect_uri" => NULL,
|
||||
"scope" => null
|
||||
];
|
||||
|
||||
/**
|
||||
* UserService constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->tokenData['client_id'] = config('larascord.client_id');
|
||||
$this->tokenData['client_secret'] = config('larascord.client_secret');
|
||||
$this->tokenData['grant_type'] = config('larascord.grant_type');
|
||||
$this->tokenData['redirect_uri'] = config('larascord.redirect_uri');
|
||||
$this->tokenData['scope'] = config('larascord.scopes');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the Discord OAuth2 callback and returns the access token.
|
||||
*
|
||||
* @throws RequestException
|
||||
*/
|
||||
public function getAccessTokenFromCode(string $code): AccessToken
|
||||
{
|
||||
$this->tokenData['code'] = $code;
|
||||
|
||||
$response = Http::asForm()->post($this->tokenURL, $this->tokenData);
|
||||
|
||||
$response->throw();
|
||||
|
||||
return new AccessToken(json_decode($response->body()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get access token from refresh token.
|
||||
*
|
||||
* @throws RequestException
|
||||
*/
|
||||
public function refreshAccessToken(string $refreshToken): AccessToken
|
||||
{
|
||||
$response = Http::asForm()->post($this->tokenURL, [
|
||||
'client_id' => config('larascord.client_id'),
|
||||
'client_secret' => config('larascord.client_secret'),
|
||||
'grant_type' => 'refresh_token',
|
||||
'refresh_token' => $refreshToken,
|
||||
]);
|
||||
|
||||
$response->throw();
|
||||
|
||||
return new AccessToken(json_decode($response->body()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Authenticates the user with the access token and returns the user data.
|
||||
*
|
||||
* @throws RequestException
|
||||
*/
|
||||
public function getCurrentUser(AccessToken $accessToken): \Jakyeru\Larascord\Types\User
|
||||
{
|
||||
$response = Http::withToken($accessToken->access_token)->get($this->baseApi . '/users/@me');
|
||||
|
||||
$response->throw();
|
||||
|
||||
return new \Jakyeru\Larascord\Types\User(json_decode($response->body()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user's guilds.
|
||||
*
|
||||
* @throws RequestException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getCurrentUserGuilds(AccessToken $accessToken, bool $withCounts = false): array
|
||||
{
|
||||
if (!$accessToken->hasScope('guilds')) throw new Exception(config('larascord.error_messages.missing_guilds_scope.message'));
|
||||
|
||||
$endpoint = '/users/@me/guilds';
|
||||
|
||||
if ($withCounts) {
|
||||
$endpoint .= '?with_counts=true';
|
||||
}
|
||||
|
||||
$response = Http::withToken($accessToken->access_token, $accessToken->token_type)->get($this->baseApi . $endpoint);
|
||||
|
||||
$response->throw();
|
||||
|
||||
return array_map(function ($guild) {
|
||||
return new \Jakyeru\Larascord\Types\Guild($guild);
|
||||
}, json_decode($response->body()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Guild Member object for a user.
|
||||
*
|
||||
* @throws RequestException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getGuildMember(AccessToken $accessToken, string $guildId): GuildMember
|
||||
{
|
||||
if (!$accessToken->hasScopes(['guilds', 'guilds.members.read'])) throw new Exception(config('larascord.error_messages.missing_guilds_members_read_scope.message'));
|
||||
|
||||
$response = Http::withToken($accessToken->access_token, $accessToken->token_type)->get($this->baseApi . '/users/@me/guilds/' . $guildId . '/member');
|
||||
|
||||
$response->throw();
|
||||
|
||||
return new GuildMember(json_decode($response->body()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the User's connections.
|
||||
*
|
||||
* @throws RequestException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getCurrentUserConnections(AccessToken $accessToken): array
|
||||
{
|
||||
if (!$accessToken->hasScope('connections')) throw new Exception('The "connections" scope is required.');
|
||||
|
||||
$response = Http::withToken($accessToken->access_token, $accessToken->token_type)->get($this->baseApi . '/users/@me/connections');
|
||||
|
||||
$response->throw();
|
||||
|
||||
return array_map(function ($connection) {
|
||||
return new \Jakyeru\Larascord\Types\Connection($connection);
|
||||
}, json_decode($response->body()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Join a guild.
|
||||
*
|
||||
* @throws RequestException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function joinGuild(AccessToken $accessToken, User $user, string $guildId, array $options = []): GuildMember
|
||||
{
|
||||
if (!config('larascord.access_token')) throw new Exception(config('larascord.error_messages.missing_access_token.message'));
|
||||
if (!$accessToken->hasScope('guilds.join')) throw new Exception('The "guilds" and "guilds.join" scopes are required.');
|
||||
|
||||
$response = Http::withToken(config('larascord.access_token'), 'Bot')->put($this->baseApi . '/guilds/' . $guildId . '/members/' . $user->id, array_merge([
|
||||
'access_token' => $accessToken->access_token,
|
||||
], $options));
|
||||
|
||||
$response->throw();
|
||||
|
||||
if ($response->status() === 204) return throw new Exception('User is already in the guild.');
|
||||
|
||||
return new GuildMember(json_decode($response->body()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create or update a user in the database.
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function createOrUpdateUser(\Jakyeru\Larascord\Types\User $user): User
|
||||
{
|
||||
if (!$user->getAccessToken()) {
|
||||
throw new Exception('User access token is missing.');
|
||||
}
|
||||
|
||||
$forgottenUser = User::where('email', '=', $user->email)->where('id', '!=', $user->id)->first();
|
||||
if ($forgottenUser) {
|
||||
// This case should never happen (TM) - The discord id changed
|
||||
// The user probably re-created their discord account with the same email
|
||||
|
||||
// Delete Playlist
|
||||
$playlists = Playlist::where('user_id', $forgottenUser->id)->get();
|
||||
foreach($playlists as $playlist) {
|
||||
PlaylistEpisode::where('playlist_id', $playlist->id)->forceDelete();
|
||||
$playlist->forceDelete();
|
||||
}
|
||||
|
||||
// Update comments to deleted user
|
||||
DB::table('comments')->where('commenter_id', '=', $forgottenUser->id)->update(['commenter_id' => 1]);
|
||||
|
||||
$forgottenUser->forceDelete();
|
||||
}
|
||||
|
||||
return User::updateOrCreate(
|
||||
[
|
||||
'id' => $user->id,
|
||||
],
|
||||
$user->toArray(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify if the user is in the specified guild(s).
|
||||
*/
|
||||
public function isUserInGuilds(array $guilds): bool
|
||||
{
|
||||
// Verify if the user is in all the specified guilds if strict mode is enabled.
|
||||
if (config('larascord.guilds_strict')) {
|
||||
return empty(array_diff(config('larascord.guilds'), array_column($guilds, 'id')));
|
||||
}
|
||||
|
||||
// Verify if the user is in any of the specified guilds if strict mode is disabled.
|
||||
return !empty(array_intersect(config('larascord.guilds'), array_column($guilds, 'id')));
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify if the user has the specified role(s) in the specified guild.
|
||||
*/
|
||||
public function hasRoleInGuild(GuildMember $guildMember, array $roles): bool
|
||||
{
|
||||
// Verify if the user has any of the specified roles.
|
||||
return !empty(array_intersect($roles, $guildMember->roles));
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the user's roles in the database.
|
||||
*/
|
||||
public function updateUserRoles(User $user, GuildMember $guildMember, int $guildId): void
|
||||
{
|
||||
// Updating the user's roles in the database.
|
||||
$updatedRoles = $user->roles;
|
||||
$updatedRoles[$guildId] = $guildMember->roles;
|
||||
$user->roles = $updatedRoles;
|
||||
$user->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Revoke the user's access token.
|
||||
*
|
||||
* @throws RequestException
|
||||
*/
|
||||
public function revokeAccessToken(string $accessToken): object
|
||||
{
|
||||
$response = Http::asForm()->post($this->tokenURL . '/revoke', [
|
||||
'token' => $accessToken,
|
||||
'client_id' => config('larascord.client_id'),
|
||||
'client_secret' => config('larascord.client_secret'),
|
||||
]);
|
||||
|
||||
$response->throw();
|
||||
|
||||
return json_decode($response->body());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user