Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nodeloc committed Jan 28, 2024
1 parent fcb906b commit 584d662
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 18 deletions.
4 changes: 2 additions & 2 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
(new Extend\Notification())
->driver('telegram', TelegramNotificationDriver::class),
(new Extend\Settings)
->serializeToForum('nodeloc-telegram.botUsername', 'nodeloc-telegram.botUsername', 'boolval')
->serializeToForum('nodeloc-telegram.botToken', 'nodeloc-telegram.botToken', 'boolval')
->serializeToForum('nodeloc-telegram.botUsername', 'nodeloc-telegram.botUsername', )
->serializeToForum('nodeloc-telegram.botToken', 'nodeloc-telegram.botToken', )
->serializeToForum('nodeloc-telegram.enableNotifications', 'nodeloc-telegram.enableNotifications', 'boolval'),
(new Extend\Frontend('admin'))
->js(__DIR__.'/js/dist/admin.js'),
Expand Down
2 changes: 1 addition & 1 deletion js/dist/forum.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion js/dist/forum.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion js/src/forum/addLoginButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import app from 'flarum/app';
export default function () {
extend(LogInButtons.prototype, 'items', function (items) {
const authUrl = app.forum.attribute('baseUrl') + '/auth/telegram';
const botUsername = app.forum.attribute('nodeloc-telegram.botUsername');
// Replace the TelegramProvide widget script
items.add('nodeloc-telegram',
m('script', {
async: true, src: 'https://telegram.org/js/telegram-widget.js?22',
'data-telegram-login': 'nodeloc_bot',
'data-telegram-login': botUsername,
'data-size': 'large',
'data-radius': '10',
'data-auth-url': authUrl,
Expand Down
4 changes: 3 additions & 1 deletion js/src/forum/addNotificationMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ export default function () {
if (user && !user.canReceiveTelegramNotifications()) {
// add button to link current account with telegram
const authUrl = app.forum.attribute('baseUrl') + '/auth/telegram';
const botUsername = app.forum.attribute('nodeloc-telegram.botUsername');

// Replace the TelegramProvide widget script
items.add('nodeloc-telegram',
m('script', {
async: true, src: 'https://telegram.org/js/telegram-widget.js?22',
'data-telegram-login': 'nodeloc_bot',
'data-telegram-login': botUsername,
'data-size': 'large',
'data-radius': '10',
'data-auth-url': authUrl,
Expand Down
4 changes: 2 additions & 2 deletions src/Controllers/TelegramAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function handle(Request $request): ResponseInterface
}

$user->loginProviders()->create(compact('provider', 'identifier'));
$content = '<script>window.opener.document.location.reload(true);</script>';
$content = '<script>window.close();window.opener.document.location.reload(true);</script>';

return new HtmlResponse($content);
}
Expand All @@ -66,7 +66,7 @@ public function handle(Request $request): ResponseInterface
$registration->provide('username', $suggestions['username']);
$registration->provide('avatar_url', $suggestions['avatar_url']);
$registration->setPayload($suggestions);
}
}
);
} catch (Exception $e) {
// 在异常情况下返回错误响应
Expand Down
18 changes: 12 additions & 6 deletions src/Driver/TelegramNotificationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
namespace Nodeloc\Telegram\Driver;

use Flarum\Notification\Driver\NotificationDriverInterface;
use Flarum\Notification\MailableInterface;
use Flarum\User\User;
use Illuminate\Contracts\Queue\Queue;
use Nodeloc\Telegram\Job\SendTelegramNotificationJob;
use Flarum\Notification\Blueprint\BlueprintInterface;
class TelegramNotificationDriver implements NotificationDriverInterface
use ReflectionClass;

class TelegramNotificationDriver implements NotificationDriverInterface
{
/**
* @var Queue
Expand Down Expand Up @@ -52,10 +55,13 @@ protected function getTelegramId($actor)

public function registerType(string $blueprintClass, array $driversEnabledByDefault): void
{
User::registerPreference(
User::getNotificationPreferenceKey($blueprintClass::getType(), 'telegram'),
'boolval',
in_array('telegram', $driversEnabledByDefault)
);
if ((new ReflectionClass($blueprintClass))->implementsInterface(MailableInterface::class)) {

User::registerPreference(
User::getNotificationPreferenceKey($blueprintClass::getType(), 'telegram'),
'boolval',
in_array('telegram', $driversEnabledByDefault)
);
}
}
}
2 changes: 1 addition & 1 deletion src/Listeners/InjectSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Nodeloc\Telegram\Listeners;

use Flarum\Api\Serializer\ForumSerializer;
use Flarum\Api\Event\Serializing;
use Flarum\Settings\Event\Serializing;
use Flarum\Settings\SettingsRepositoryInterface;

class InjectSettings
Expand Down
32 changes: 29 additions & 3 deletions src/Notifications/TelegramMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
use Flarum\Notification\Blueprint\BlueprintInterface;
use Flarum\Notification\MailableInterface;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\LoginProvider;
use Flarum\User\User;
use Illuminate\Contracts\View\Factory;
use GuzzleHttp\Exception\ClientException;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Contracts\Translation\Translator;
use Nodeloc\Telegram\Listeners\AddUserAttributes;
use Symfony\Contracts\Translation\TranslatorInterface;
use Telegram\Bot\Api;
use Telegram\Bot\Exceptions\TelegramSDKException;
Expand Down Expand Up @@ -52,19 +55,42 @@ public function __construct(Mailer $mailer, TranslatorInterface $translator, Set
$this->settings = $settings;
$this->view = $view;
}

/**
* @param User $actor
* @return int
*/
protected function getTelegramId(User $actor)
{
$query = LoginProvider::where('user_id', '=', $actor->id);
$query->where('provider', '=', 'telegram');
$provider = $query->first();
return $provider->identifier;
}
public function send(BlueprintInterface $blueprint, array $users): void
{
foreach ($users as $user) {
if ($blueprint instanceof MailableInterface) {
$view = $this->pickBestView($blueprint->getEmailView());
$text = $this->view->make($view, compact('blueprint', 'user'))->render();
} else {
}else if ($blueprint instanceof BlueprintInterface) {
return;
}else{
throw new Exception('Notification not compatible with Telegram Mailer');
}
$telegram_id = $user->flagrow_telegram_id; // Assuming 'telegram_id' is the key for the user's Telegram ID in the array
if(!$telegram_id){
$telegram_id = $this->getTelegramId($user);
if($telegram_id){
$user->flagrow_telegram_id = $telegram_id;
$user->save();
}else{
$user->flagrow_telegram_error = 'User telegram id is incorrect.';
$user->save();
return;
}
}
try {
$response = $this->telegramclient->sendMessage([
$this->telegramclient->sendMessage([
'chat_id' => $telegram_id,
'text' => $text
]);
Expand Down

0 comments on commit 584d662

Please sign in to comment.