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 all 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
34 changes: 32 additions & 2 deletions src/Room/ChatRoomAclDataAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Room11\StackChat\Auth\ActiveSessionTracker;
use Room11\StackChat\Endpoint;
use Room11\StackChat\EndpointURLResolver;
use function Room11\DOMUtils\domdocument_load_html;
use function Room11\DOMUtils\xpath_get_elements;

class ChatRoomAclDataAccessor implements AclDataAccessor
{
Expand Down Expand Up @@ -62,8 +64,7 @@ public function getRoomAccess(Room $room): Promise
return \Amp\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 +77,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 \Amp\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';
}