Skip to content

Commit

Permalink
Turn off Discourse mails for MRES users.
Browse files Browse the repository at this point in the history
  • Loading branch information
edwh committed Oct 10, 2022
1 parent 0f7e60c commit 8cc7230
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
7 changes: 6 additions & 1 deletion app/Console/Commands/UserCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Console\Commands;

use App\Role;
use App\Services\DiscourseService;
use App\User;
use App\WikiSyncStatus;
use Illuminate\Console\Command;
Expand Down Expand Up @@ -41,7 +42,7 @@ public function __construct()
*
* @return mixed
*/
public function handle()
public function handle(DiscourseService $discourseService)
{
$name = $this->argument('name');
$email = $this->argument('email');
Expand Down Expand Up @@ -89,6 +90,10 @@ public function handle()
if ($user)
{
$this->info("User created #" . $user->id);

if (config('restarters.features.discourse_integration')) {
$discourseService->syncSso($user);
}
} else
{
$this->error("User creation failed");
Expand Down
54 changes: 30 additions & 24 deletions app/Services/DiscourseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,10 @@ public function syncGroups($idgroups = NULL) {

public function syncSso($user)
{
if (! config('restarters.features.discourse_integration')) {
return;
}

$endpoint = '/admin/users/sync_sso';

// see https://meta.discourse.org/t/sync-sso-user-data-with-the-sync-sso-route/84398 for details on the sync_sso route.
Expand Down Expand Up @@ -544,33 +548,35 @@ public function syncSso($user)
Log::debug('Sync '.$user->id.' to Discourse OK');
}

if ($user->network->name === 'MRES') {
// Special case - ensure that emails are turned off. This is primarily turning off email_digests.
// Nothing else should ever be happening on Discourse for these users.
$response = $this->discourseClient->request(
'PUT',
"/u/{$user->username}.json",
[
'form_params' => [
'mailing_list_mode' => false,
'mailing_list_mode_frequency' => 1,
'email_digests' => false,
'email_in_reply_to' => true,
'email_messages_level' => 2,
'email_level' => 2,
'email_previous_replies' => 1,
'digest_after_minutes' => 10080,
'include_tl0_in_digests' => true
foreach ($user->networks as $network) {
if ($network->name === 'MRES') {
// Special case - ensure that emails are turned off. This is primarily turning off email_digests.
// Nothing else should ever be happening on Discourse for these users.
$response = $this->discourseClient->request(
'PUT',
"/u/{$user->username}.json",
[
'form_params' => [
'mailing_list_mode' => false,
'mailing_list_mode_frequency' => 1,
'email_digests' => false,
'email_in_reply_to' => true,
'email_messages_level' => 2,
'email_level' => 2,
'email_previous_replies' => 1,
'digest_after_minutes' => 10080,
'include_tl0_in_digests' => true
]
]
]
);
);

if ($response->getStatusCode() !== 200) {
Log::error('Could not turn off Discourse mails for user '.$user->id.': '.$response->getReasonPhrase());
} else {
Log::debug('Turned off Discourse mails for'.$user->id);
}
if ($response->getStatusCode() !== 200) {
Log::error('Could not turn off Discourse mails for user '.$user->id.': '.$response->getReasonPhrase());
} else {
Log::debug('Turned off Discourse mails for'.$user->id);
}

}
}
}
}

0 comments on commit 8cc7230

Please sign in to comment.