Skip to content

Commit 9696edb

Browse files
authored
Merge pull request #42 from laravelcm/add-social-sharing-posts-on-twitter
Add social sharing posts on twitter
2 parents 17d37de + 7b36899 commit 9696edb

File tree

11 files changed

+116
-74
lines changed

11 files changed

+116
-74
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ TORCHLIGHT_TOKEN=
7373
MIX_TORCHLIGHT_TOKEN="${TORCHLIGHT_TOKEN}"
7474
UNSPLASH_ACCESS_KEY=
7575
TELEGRAM_BOT_TOKEN=
76+
TELEGRAM_CHANNEL=
7677
MEDIA_DISK=media
7778
SENTRY_LARAVEL_DSN=
7879
SENTRY_TRACES_SAMPLE_RATE=

app/Console/Kernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ protected function schedule(Schedule $schedule)
2626
{
2727
$schedule->command('media-library:delete-old-temporary-uploads')->daily();
2828
$schedule->command('lcm:delete-old-unverified-users')->daily();
29-
// $schedule->command('lcm:post-article-to-twitter')->twiceDaily(12, 16);
29+
$schedule->command('lcm:post-article-to-twitter')->twiceDaily(12, 16);
3030
$schedule->command('lcm:post-article-to-telegram')->everyFourHours();
3131
$schedule->command('lcm:send-unverified-mails')->weeklyOn(1, '8:00');
3232
$schedule->command('sitemap:generate')->daily();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use App\Models\Article;
6+
use Illuminate\Queue\SerializesModels;
7+
8+
class ArticleWasSubmittedForApproval
9+
{
10+
use SerializesModels;
11+
12+
public function __construct(
13+
public Article $article
14+
) {
15+
}
16+
}

app/Http/Livewire/Articles/Create.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace App\Http\Livewire\Articles;
44

5+
use App\Events\ArticleWasSubmittedForApproval;
56
use App\Gamify\Points\PostCreated;
67
use App\Models\Article;
78
use App\Models\Tag;
89
use App\Models\User;
9-
use App\Notifications\SendSubmittedArticle;
1010
use App\Traits\WithArticleAttributes;
1111
use App\Traits\WithTagsAssociation;
1212
use Illuminate\Support\Facades\Auth;
@@ -67,10 +67,9 @@ public function store()
6767
$article->addMedia($this->file->getRealPath())->toMediaCollection('media');
6868
}
6969

70-
if ($article->submitted_at) {
71-
// Envoi du mail à l'admin pour la validation de l'article.
72-
$admin = User::findByEmailAddress('monneylobe@gmail.com');
73-
Notification::send($admin, new SendSubmittedArticle($article));
70+
if ($article->isAwaitingApproval()) {
71+
// Envoi de la notification sur le channel Telegram pour la validation de l'article.
72+
event(new ArticleWasSubmittedForApproval($article));
7473

7574
session()->flash('status', 'Merci d\'avoir soumis votre article. Vous aurez des nouvelles que lorsque nous accepterons votre article.');
7675
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\Listeners;
4+
5+
use App\Events\ArticleWasSubmittedForApproval;
6+
use App\Notifications\ArticleSubmitted;
7+
use Illuminate\Notifications\AnonymousNotifiable;
8+
9+
final class SendNewArticleNotification
10+
{
11+
public function __construct(private AnonymousNotifiable $notifiable)
12+
{
13+
}
14+
15+
public function handle(ArticleWasSubmittedForApproval $event): void
16+
{
17+
$this->notifiable->notify(new ArticleSubmitted($event->article));
18+
}
19+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace App\Notifications;
4+
5+
use App\Models\Article;
6+
use Illuminate\Bus\Queueable;
7+
use Illuminate\Contracts\Queue\ShouldQueue;
8+
use Illuminate\Notifications\Notification;
9+
use NotificationChannels\Telegram\TelegramChannel;
10+
use NotificationChannels\Telegram\TelegramMessage;
11+
12+
class ArticleSubmitted extends Notification implements ShouldQueue
13+
{
14+
use Queueable;
15+
16+
public function __construct(private Article $article)
17+
{
18+
}
19+
20+
public function via($notifiable)
21+
{
22+
if (
23+
! empty(config('services.telegram-bot-api.token')) &&
24+
! empty(config('services.telegram-bot-api.channel'))
25+
) {
26+
return [TelegramChannel::class];
27+
}
28+
29+
return [];
30+
}
31+
32+
public function toTelegram($notifiable)
33+
{
34+
$url = route('articles.show', $this->article->slug());
35+
36+
return TelegramMessage::create()
37+
->to(config('services.telegram-bot-api.channel'))
38+
->content($this->content())
39+
->button('Voir l\'article', $url);
40+
}
41+
42+
private function content(): string
43+
{
44+
$content = "*Nouvel Article Soumis!*\n\n";
45+
$content .= 'Titre: ' . $this->article->title . "\n";
46+
$content .= 'Par: [@' . $this->article->author->username . '](' . route('profile', $this->article->author->username) . ')';
47+
48+
return $content;
49+
}
50+
}

app/Notifications/SendSubmittedArticle.php

Lines changed: 0 additions & 38 deletions
This file was deleted.

app/Providers/EventServiceProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace App\Providers;
44

5+
use App\Events\ArticleWasSubmittedForApproval;
56
use App\Events\CommentWasAdded;
67
use App\Events\ReplyWasCreated;
78
use App\Events\ThreadWasCreated;
89
use App\Listeners\NotifyMentionedUsers;
910
use App\Listeners\PostNewThreadNotification;
11+
use App\Listeners\SendNewArticleNotification;
1012
use App\Listeners\SendNewCommentNotification;
1113
use App\Listeners\SendNewReplyNotification;
1214
use App\Listeners\SendNewThreadNotification;
@@ -33,6 +35,9 @@ class EventServiceProvider extends ServiceProvider
3335
SendNewThreadNotification::class,
3436
PostNewThreadNotification::class,
3537
],
38+
ArticleWasSubmittedForApproval::class => [
39+
SendNewArticleNotification::class,
40+
],
3641
CommentWasAdded::class => [
3742
SendNewCommentNotification::class,
3843
],

auth.json.example

Lines changed: 0 additions & 8 deletions
This file was deleted.

config/services.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
'client_id' => env('TWITTER_CLIENT_ID'),
4343
'client_secret' => env('TWITTER_CLIENT_SECRET'),
4444
'redirect' => env('TWITTER_REDIRECT'),
45-
'consumer_key' => env('TWITTER_CONSUMER_KEY'),
46-
'consumer_secret' => env('TWITTER_CONSUMER_SECRET'),
45+
'consumer_key' => env('TWITTER_CLIENT_ID'),
46+
'consumer_secret' => env('TWITTER_CLIENT_SECRET'),
4747
'access_token' => env('TWITTER_ACCESS_TOKEN'),
4848
'access_secret' => env('TWITTER_ACCESS_SECRET'),
4949
'scopes' => [],
@@ -56,6 +56,7 @@
5656

5757
'telegram-bot-api' => [
5858
'token' => env('TELEGRAM_BOT_TOKEN'),
59+
'channel' => env('TELEGRAM_CHANNEL'),
5960
],
6061

6162
];

resources/views/vendor/wireui/components/notifications.blade.php

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@
44
x-on:wireui:notification.window="addNotification($event.detail)"
55
x-on:wireui:confirm-notification.window="addConfirmNotification($event.detail)"
66
wire:ignore>
7-
<div class="max-w-sm w-full space-y-2 pointer-events-auto flex flex-col-reverse">
7+
<div class="flex flex-col-reverse w-full max-w-sm space-y-2 pointer-events-auto">
88
<template x-for="notification in notifications" :key="`notification-${notification.id}`">
9-
<div class="max-w-sm w-full bg-skin-card shadow-lg rounded-lg ring-1 ring-black
10-
ring-opacity-5 relative overflow-hidden pointer-events-auto"
9+
<div class="relative w-full max-w-sm overflow-hidden rounded-lg shadow-lg pointer-events-auto bg-skin-card ring-1 ring-black ring-opacity-5"
1110
:class="{ 'flex': notification.rightButtons }"
1211
:id="`notification.${notification.id}`"
1312
x-transition:enter="transform ease-out duration-300 transition"
1413
x-transition:enter-start="translate-y-2 opacity-0 sm:translate-y-0 sm:translate-x-2"
1514
x-transition:enter-end="translate-y-0 opacity-100 sm:translate-x-0"
1615
x-on:mouseenter="pauseNotification(notification)"
1716
x-on:mouseleave="resumeNotification(notification)">
18-
<div class="bg-secondary-300 rounded-full transition-all duration-150 ease-linear absolute top-0 left-0"
17+
<div class="absolute top-0 left-0 transition-all duration-150 ease-linear rounded-full bg-secondary-300"
1918
style="height: 2px; width: 100%;"
2019
:id="`timeout.bar.${notification.id}`"
2120
x-show="Boolean(notification.timer) && notification.progressbar !== false">
@@ -40,7 +39,7 @@
4039
</template>
4140

4241
<template x-if="notification.img">
43-
<img class="h-10 w-10 rounded-full" :src="notification.img" />
42+
<img class="w-10 h-10 rounded-full" :src="notification.img" />
4443
</template>
4544
</div>
4645
</template>
@@ -59,8 +58,8 @@
5958

6059
<!-- actions buttons -->
6160
<template x-if="!notification.dense && !notification.rightButtons && (notification.accept || notification.reject)">
62-
<div class="mt-3 flex gap-x-3">
63-
<button class="rounded-md text-sm font-medium focus:outline-none"
61+
<div class="flex mt-3 gap-x-3">
62+
<button class="text-sm font-medium rounded-md focus:outline-none"
6463
:class="{
6564
'bg-skin-card text-primary-600 hover:text-primary-500': !Boolean($wireui.dataGet(notification, 'accept.style')),
6665
[$wireui.dataGet(notification, 'accept.style')]: Boolean($wireui.dataGet(notification, 'accept.style')),
@@ -71,7 +70,7 @@
7170
x-text="$wireui.dataGet(notification, 'accept.label', '')">
7271
</button>
7372

74-
<button class="rounded-md text-sm font-medium focus:outline-none"
73+
<button class="text-sm font-medium rounded-md focus:outline-none"
7574
:class="{
7675
'bg-skin-card text-skin-inverted-muted hover:text-skin-base': !Boolean($wireui.dataGet(notification, 'reject.style')),
7776
[$wireui.dataGet(notification, 'reject.style')]: Boolean($wireui.dataGet(notification, 'reject.style')),
@@ -85,9 +84,9 @@
8584
</template>
8685
</div>
8786

88-
<div class="ml-4 shrink-0 flex">
87+
<div class="flex ml-4 shrink-0">
8988
<!-- accept button -->
90-
<button class="mr-4 shrink-0 rounded-md text-sm font-medium focus:outline-none"
89+
<button class="mr-4 text-sm font-medium rounded-md shrink-0 focus:outline-none"
9190
:class="{
9291
'text-primary-600 hover:text-primary-500': !Boolean($wireui.dataGet(notification, 'accept.style')),
9392
[$wireui.dataGet(notification, 'accept.style')]: Boolean($wireui.dataGet(notification, 'accept.style'))
@@ -98,14 +97,14 @@
9897
</button>
9998

10099
<!-- close button -->
101-
<button class="rounded-md inline-flex text-skin-muted hover:text-skin-base focus:outline-none"
100+
<button class="inline-flex rounded-md text-skin-muted hover:text-skin-base focus:outline-none"
102101
x-show="notification.closeButton"
103102
x-on:click="closeNotification(notification)">
104103
<span class="sr-only">Close</span>
105104
<x-dynamic-component
106-
:component="WireUiComponent::resolve('icon')"
107-
class="h-5 w-5"
108-
name="x"
105+
:component="WireUi::component('icon')"
106+
class="w-5 h-5"
107+
name="x"
109108
/>
110109
</button>
111110
</div>
@@ -116,11 +115,10 @@ class="h-5 w-5"
116115
<template x-if="notification.rightButtons">
117116
<div class="flex flex-col border-l border-secondary-200">
118117
<template x-if="notification.accept">
119-
<div class="h-0 flex-1 flex" :class="{
118+
<div class="flex flex-1 h-0" :class="{
120119
'border-b border-secondary-200': notification.reject
121120
}">
122-
<button class="w-full rounded-none rounded-tr-lg px-4 py-3 flex items-center
123-
justify-center text-sm font-medium focus:outline-none"
121+
<button class="flex items-center justify-center w-full px-4 py-3 text-sm font-medium rounded-none rounded-tr-lg focus:outline-none"
124122
:class="{
125123
'text-primary-600 hover:text-primary-500 hover:bg-secondary-50': !Boolean(notification.accept.style),
126124
[notification.accept.style]: Boolean(notification.accept.style),
@@ -133,9 +131,8 @@ class="h-5 w-5"
133131
</template>
134132

135133
<template x-if="notification.reject">
136-
<div class="h-0 flex-1 flex">
137-
<button class="w-full rounded-none rounded-br-lg px-4 py-3 flex items-center
138-
justify-center text-sm font-medium focus:outline-none"
134+
<div class="flex flex-1 h-0">
135+
<button class="flex items-center justify-center w-full px-4 py-3 text-sm font-medium rounded-none rounded-br-lg focus:outline-none"
139136
:class="{
140137
'text-skin-inverted-muted hover:text-skin-base': !Boolean(notification.reject.style),
141138
[notification.reject.style]: Boolean(notification.reject.style),

0 commit comments

Comments
 (0)