Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose main site moderator IDs #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ abstract class Endpoint
// main site info
const MAINSITE_URLS_START = 500;
const MAINSITE_USER = 501;
const MAINSITE_MODERATOR_LIST = 502;
}
1 change: 1 addition & 0 deletions src/EndpointURLResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class EndpointURLResolver
Endpoint::CHAT_USER_INFO_EXTRA => 'https://%1$s/users/thumbs/%3$d?showUsage=false',

Endpoint::MAINSITE_USER => '%1$s/users/%2$d?tab=profile',
Endpoint::MAINSITE_MODERATOR_LIST => '%1$s/users?tab=moderators',
];

private $connectedRooms;
Expand Down
6 changes: 6 additions & 0 deletions src/Room/AclDataAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ function getRoomAccess(Room $room): Promise;
*/
function getRoomOwners(Room $room): Promise;

/**
* @param Room $room
* @return Promise<string[]>
*/
function getMainSiteModerators(Room $room): Promise;

/**
* @param Room $room
* @param int $userId
Expand Down
37 changes: 34 additions & 3 deletions src/Room/ChatRoomAclDataAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
use Amp\Artax\HttpClient;
use Amp\Artax\Response as HttpResponse;
use Amp\Promise;
use function Amp\resolve;
Nessworthy marked this conversation as resolved.
Show resolved Hide resolved
use function Room11\DOMUtils\domdocument_load_html;
use Room11\DOMUtils\ElementNotFoundException;
use function Room11\DOMUtils\xpath_get_elements;
Nessworthy marked this conversation as resolved.
Show resolved Hide resolved
use Room11\StackChat\Auth\ActiveSessionTracker;
use Room11\StackChat\Endpoint;
use Room11\StackChat\EndpointURLResolver;
Expand Down Expand Up @@ -59,11 +62,10 @@ public function getRoomAccess(Room $room): Promise
{
$url = $this->urlResolver->getEndpointURL($room, Endpoint::CHATROOM_INFO_ACCESS);

return \Amp\resolve(function() use($url) {
return resolve(function() use($url) {
/** @var HttpResponse $response */
$response = yield $this->httpClient->request($url);

$doc = \Room11\DOMUtils\domdocument_load_html($response->getBody());
$doc = domdocument_load_html($response->getBody());

$result = [];

Expand All @@ -76,6 +78,35 @@ public function getRoomAccess(Room $room): Promise
});
}

public function getMainSiteModerators(Room $room): Promise
{
$url = $this->urlResolver->getEndpointURL($room, Endpoint::MAINSITE_MODERATOR_LIST);

$promise = $this->httpClient->request($url);
return resolve(function() use ($promise) {
/** @var HttpResponse $response */
$response = yield $promise;

$doc = domdocument_load_html($response->getBody());
try {
$userElements = xpath_get_elements($doc, "//div[@id='user-browser']//div[contains(concat(' ', normalize-space(@class), ' '), ' user-details ')]//a[1]");
} catch (ElementNotFoundException $e) {
return [];
}

$moderators = [];

foreach ($userElements as $userElement) {
preg_match('#/users/(?<id>\d+)/#', $userElement->getAttribute('href'), $urlParts);
$moderators[(int) $urlParts['id']] = trim($userElement->textContent);
}

return $moderators;

});

}

/**
* {@inheritdoc}
*/
Expand Down
1 change: 1 addition & 0 deletions src/Room/UserAccessType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ class UserAccessType
const READ_ONLY = 'read-only';
const READ_WRITE = 'read-write';
const OWNER = 'owner';
const SITE_MODERATOR = 'site-moderator';
}