Skip to content

Commit

Permalink
Handle Discourse group stuckness.
Browse files Browse the repository at this point in the history
  • Loading branch information
edwh committed Dec 8, 2021
1 parent cc7ed7f commit ba6d75b
Showing 1 changed file with 95 additions and 81 deletions.
176 changes: 95 additions & 81 deletions app/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -561,91 +561,105 @@ public function createDiscourseGroup() {
return;
}

try {
// We want to internationalise the message. Use the languages of any networks that the group
// is in.
$text = '';
$langs = [];

foreach ($this->networks as $network) {
$lang = $network->default_language;

if (!in_array($lang, $langs)) {
$text .= Lang::get('groups.discourse_title',[
'group' => $this->name,
'link' => env('APP_URL') . '/group/view/' . $this->idgroups,
'help' => 'https://talk.restarters.net/t/how-to-communicate-with-your-repair-group/6293'
],$lang);

$langs[] = $lang;
}
}
$unique = '';

do {
$retry = false;

try {
// We want to internationalise the message. Use the languages of any networks that the group
// is in.
$text = '';
$langs = [];

// We want the host to create the group, so use their username. The API key should
// allow us to do this - see https://meta.discourse.org/t/how-can-an-api-user-create-posts-as-another-user/45968/3.
$client = app('discourse-client', [
'username' => env('DISCOURSE_APIUSER'),
]);

// Restricted characters allowed in name, and only 25 characters.
$name = str_replace(' ', '_', $this->name);
$name = preg_replace("/[^A-Za-z0-9_]/", '', $name);
$name = substr($name, 0, 25);

$params = [
'group' => [
'name' => $name,
'full_name' => $this->name,
'mentionable_level' => 4,
'messageable_level' => 99,
'visibility_level' => 0,
'members_visibility_level' => 0,
'automatic_membership_email_domains' => null,
'automatic_membership_retroactive' => false,
'primary_group' => false,
'flair_url' => $this->groupImagePath(),
'flair_bg_color' => null,
'flair_color' => null,
'bio_raw' => $text,
'public_admission' => false,
'public_exit' => false,
'default_notification_level' => 3,
'publish_read_state' => true,
'owner_usernames' => $host->username
]
];

$endpoint = '/admin/groups.json';

Log::info('Creating group : '.json_encode($params));
$response = $client->request(
'POST',
$endpoint,
[
'form_params' => $params,
]
);

Log::info('Response status: '.$response->getStatusCode());
Log::info('Response body: '.$response->getBody());

if (! $response->getStatusCode() === 200) {
Log::error('Could not create group ('.$this->idgroups.') thread: '.$response->getReasonPhrase());
} else {
// We want to save the discourse thread id in the group, so that we can invite people to it later
// when they join.
$json = json_decode($response->getBody(), true);
if (empty($json['basic_group'])) {
throw new \Exception('Group not found in create response');
foreach ($this->networks as $network) {
$lang = $network->default_language;

if (!in_array($lang, $langs)) {
$text .= Lang::get('groups.discourse_title',[
'group' => $this->name,
'link' => env('APP_URL') . '/group/view/' . $this->idgroups,
'help' => 'https://talk.restarters.net/t/how-to-communicate-with-your-repair-group/6293'
],$lang);

$langs[] = $lang;
}
}

$this->discourse_group = $name;
$this->save();
$success = true;
// We want the host to create the group, so use their username. The API key should
// allow us to do this - see https://meta.discourse.org/t/how-can-an-api-user-create-posts-as-another-user/45968/3.
$client = app('discourse-client', [
'username' => env('DISCOURSE_APIUSER'),
]);

// Restricted characters allowed in name, and only 25 characters.
$name = str_replace(' ', '_', $this->name);
$name = preg_replace("/[^A-Za-z0-9_]/", '', $name);
$name = substr($name, 0, 25);

$params = [
'group' => [
'name' => "$name$unique",
'full_name' => $this->name,
'mentionable_level' => 4,
'messageable_level' => 99,
'visibility_level' => 0,
'members_visibility_level' => 0,
'automatic_membership_email_domains' => null,
'automatic_membership_retroactive' => false,
'primary_group' => false,
'flair_url' => $this->groupImagePath(),
'flair_bg_color' => null,
'flair_color' => null,
'bio_raw' => $text,
'public_admission' => false,
'public_exit' => false,
'default_notification_level' => 3,
'publish_read_state' => true,
'owner_usernames' => $host->username
]
];

$endpoint = '/admin/groups.json';

Log::info('Creating group : '.json_encode($params));
$response = $client->request(
'POST',
$endpoint,
[
'form_params' => $params,
]
);

Log::info('Response status: '.$response->getStatusCode());
Log::info('Response body: '.$response->getBody());

if (! $response->getStatusCode() === 200) {
if ($response->getReasonPhrase() == 'Name has already been taken') {
// Discourse sometimes seems to have groups stuck in a bad state which are not accessible.
// This can result in a create failure, and a group which we cannot then locate to delete.
// So skip over it and retry creation with a different name.
$retry = true;
$unique = $unique ? ($unique + 1) : 1;
} else {
Log::error('Could not create group ('.$this->idgroups.') thread: '.$response->getReasonPhrase());
}
} else {
// We want to save the discourse thread id in the group, so that we can invite people to it later
// when they join.
$json = json_decode($response->getBody(), true);
if (empty($json['basic_group'])) {
throw new \Exception('Group not found in create response');
}

$this->discourse_group = $name;
$this->save();
$success = true;
}
} catch (\Exception $ex) {
Log::error('Could not create group ('.$this->idgroups.') thread: '.$ex->getMessage());
}
} catch (\Exception $ex) {
Log::error('Could not create group ('.$this->idgroups.') thread: '.$ex->getMessage());
}
} while ($retry);

return $success;
}
Expand Down

0 comments on commit ba6d75b

Please sign in to comment.