-
-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite Discord group sync to use v5 Nameless-Link API (#3222)
* Rewrite Discord group sync * fix param * Create new endpoint for plugin to use * wip * wip (will work after NamelessMC/Nameless-Java-API#61) * wip * Undo hardcoded version * Style * Style * Style * Fix nickname sync * UNDO UNDO UNDO
- Loading branch information
1 parent
1715723
commit 60a7cef
Showing
11 changed files
with
219 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
/** | ||
* Represents a GroupSyncInjector which can add multiple groups to a {@see User} at once | ||
* | ||
* @package NamelessMC\Group_Sync | ||
* @author Aberdeener | ||
* @version 2.0.3 | ||
* @license MIT | ||
*/ | ||
interface BatchableGroupSyncInjector { | ||
|
||
/** | ||
* Add multiple groups to a user at once. | ||
* | ||
* @param User $user The user to add groups to | ||
* @param array $group_ids Array of native group IDs to remove | ||
* @return false|array Array of group IDs and the status of the operation for each, or false if error | ||
*/ | ||
public function batchAddGroups(User $user, array $group_ids); | ||
|
||
/** | ||
* Remove multiple groups from a user at once. | ||
* | ||
* @param User $user The user to remove groups from | ||
* @param array $group_ids Array of native group IDs to remove | ||
* @return false|array Array of group IDs and the status of the operation for each, or false if error | ||
*/ | ||
public function batchRemoveGroups(User $user, array $group_ids); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
class UpdateGroupsEndpoint extends KeyAuthEndpoint { | ||
|
||
public function __construct() { | ||
$this->_route = 'minecraft/update-groups'; | ||
$this->_module = 'Core'; | ||
$this->_description = 'Update a users groups based on their groups from the Minecraft server'; | ||
$this->_method = 'POST'; | ||
} | ||
|
||
public function execute(Nameless2API $api): void { | ||
$api->validateParams($_POST, ['server_id', 'player_groups']); | ||
|
||
$server_id = $_POST['server_id']; | ||
$group_sync_log = []; | ||
|
||
if ($server_id == Util::getSetting('group_sync_mc_server')) { | ||
try { | ||
$integration = Integrations::getInstance()->getIntegration('Minecraft'); | ||
|
||
foreach ($_POST['player_groups'] as $uuid => $groups) { | ||
$integrationUser = new IntegrationUser($integration, str_replace('-', '', $uuid), 'identifier'); | ||
if ($integrationUser->exists()) { | ||
$log = $this->updateGroups($integrationUser, $groups['groups']); | ||
if (count($log)) { | ||
$group_sync_log[] = $log; | ||
} | ||
} | ||
} | ||
} catch (Exception $e) { | ||
$api->throwError(CoreApiErrors::ERROR_UNABLE_TO_UPDATE_GROUPS, $e->getMessage(), 500); | ||
} | ||
|
||
$api->returnArray(array_merge(['message' => $api->getLanguage()->get('api', 'groups_updates_successfully')], ['log' => $group_sync_log])); | ||
} | ||
|
||
$api->throwError(CoreApiErrors::ERROR_INVALID_SERVER_ID, $server_id); | ||
} | ||
|
||
private function updateGroups(IntegrationUser $integrationUser, array $groups): array { | ||
if (!$integrationUser->isVerified()) { | ||
return []; | ||
} | ||
|
||
$user = $integrationUser->getUser(); | ||
if (!$user->exists()) { | ||
return []; | ||
} | ||
|
||
if (!$user->isValidated()) { | ||
return []; | ||
} | ||
|
||
$log = GroupSyncManager::getInstance()->broadcastChange( | ||
$user, | ||
MinecraftGroupSyncInjector::class, | ||
$groups, | ||
); | ||
|
||
if (count($log)) { | ||
Log::getInstance()->log(Log::Action('mc_group_sync/role_set'), json_encode($log), $user->data()->id); | ||
} | ||
|
||
return $log; | ||
} | ||
} |
Oops, something went wrong.