Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions app/Http/Controllers/Community.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Http\Requests\CreateNewPost;
use App\Http\Requests\UpdateUserComment;
use App\Http\Requests\UpdateUserPost;
use App\Jobs\SendPostWebhook;
use App\Models\Comment;
use App\Models\Like;
use App\Models\Post;
Expand Down Expand Up @@ -99,17 +100,7 @@ public function createNewPost(CreateNewPost $request)

$post->save();

Http::post('https://discord.com/api/webhooks/914187384835420211/aUjMOW2HNugOC163Rf3ziggluhvTtzROxAoku9AWR258sGTf6Ec6u2DaOKTzx-G6hhTC', [
'content' => "New post!",
'embeds' => [
[
'title' => $request->input('title'),
'description' => $request->input('text'),
'color' => '7506394',
'url' => route('community.post', $post->id),
]
],
]);
SendPostWebhook::dispatch($post);

return redirect(route('community.post', $post->id));
}
Expand Down
141 changes: 141 additions & 0 deletions app/Http/Controllers/Webhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class Webhook extends Controller
{
public function all()
{
return view('webhooks', [
'hooks' => \App\Models\Webhook::query()->where('user_id', auth()->id())->orderByDesc('name')->paginate(10)
]);
}

public function new()
{
return view('webhooknew');
}

public function createWebhook(Request $request)
{
\App\Models\Webhook::query()->create([
'user_id' => auth()->id(),
'url' => $request->input('url'),
'name' => $request->input('name'),
'posts' => 0,
'comments' => 0,
'assignments' => 0,
'blog' => 0,
'active' => 0
])->save();
return redirect(route('webhook.all'));
}

public function deleteWebhook($id)
{
\App\Models\Webhook::query()->find($id)->delete();
return redirect(route('webhook.all'));
}

public function enablePost($id)
{
\App\Models\Webhook::query()->find($id)->update(
[
'posts' => 1
]
);
return redirect(route('webhook.all'));
}

public function disablePost($id)
{
\App\Models\Webhook::query()->find($id)->update(
[
'posts' => 0
]
);
return redirect(route('webhook.all'));
}

public function enableComments($id)
{
\App\Models\Webhook::query()->find($id)->update(
[
'comments' => 1
]
);
return redirect(route('webhook.all'));
}

public function disableComments($id)
{
\App\Models\Webhook::query()->find($id)->update(
[
'comments' => 0
]
);
return redirect(route('webhook.all'));
}

public function enableAssignments($id)
{
\App\Models\Webhook::query()->find($id)->update(
[
'assignments' => 1
]
);
return redirect(route('webhook.all'));
}

public function disableAssignments($id)
{
\App\Models\Webhook::query()->find($id)->update(
[
'assignments' => 0
]
);
return redirect(route('webhook.all'));
}

public function enableBlog($id)
{
\App\Models\Webhook::query()->find($id)->update(
[
'blog' => 1
]
);
return redirect(route('webhook.all'));
}

public function disableBlog($id)
{
\App\Models\Webhook::query()->find($id)->update(
[
'blog' => 0
]
);
return redirect(route('webhook.all'));
}

public function enableWebhook($id)
{
\App\Models\Webhook::query()->find($id)->update(
[
'active' => 1
]
);
return redirect(route('webhook.all'));
}

public function disableWebhook($id)
{
\App\Models\Webhook::query()->find($id)->update(
[
'active' => 0
]
);
return redirect(route('webhook.all'));
}
}
56 changes: 56 additions & 0 deletions app/Jobs/SendPostWebhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace App\Jobs;

use App\Models\Webhook;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Http\Request;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;

class SendPostWebhook implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* Create a new job instance.
*
* @return void
*/

protected $webhook;

protected $post;

public function __construct()
{
//
}

/**
* Execute the job.
*
* @return void
*/
public function handle($post)
{
foreach (\Auth::user()->Webhook()->get() as $w)
{
Http::post($w->url, [
'content' => "New post!",
'embeds' => [
[
'title' => $post->title,
'description' => $post->body,
'color' => '7506394',
'url' => route('community.post', $post->id),
]
],
]);
}
}
}
5 changes: 5 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public function Report()
return $this->hasMany(Report::class);
}

public function Webhook()
{
return $this->hasMany(Webhook::class);
}

/**
* The attributes that are mass assignable.
*
Expand Down
35 changes: 35 additions & 0 deletions app/Models/Webhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Webhook extends Model
{
use HasFactory;

protected $fillable = [
'user_id',
'url',
'name',
'posts',
'comments',
'assignments',
'blog',
'active'
];

protected $casts = [
'posts' => 'boolean',
'comments' => 'boolean',
'assignments' => 'boolean',
'blog' => 'boolean',
'active' => 'boolean',
];

public function User()
{
return $this->belongsTo(User::class);
}
}
Loading