import { listenToCkeditor } from "./Event";
import { EditorConfig } from "./Types";
-type SearchResultItem = {
- avatarTag: string;
- username: string;
- userID: number;
- type: "user";
-};
+type SearchResultItem =
+ | {
+ avatarTag: string;
+ username: string;
+ userID: number;
+ type: "user";
+ }
+ | {
+ name: string;
+ groupID: string;
+ type: "group";
+ };
type ResultGetSearchResultList = SearchResultItem[];
-type UserMention = {
+type Mention = {
id: string;
text: string;
icon: string;
};
-async function getPossibleMentions(query: string): Promise<UserMention[]> {
+async function getPossibleMentions(query: string): Promise<Mention[]> {
// TODO: Provide the URL as a parameter.
const url = new URL(window.WSC_API_URL + "index.php?editor-get-mention-suggestions/");
url.searchParams.set("query", query);
.fetchAsJson()) as ResultGetSearchResultList;
return result.map((item) => {
- return {
- id: `@${item.username}`,
- text: item.username,
- icon: item.avatarTag,
- objectId: item.userID,
- type: item.type,
- };
+ if (item.type === "user") {
+ return {
+ id: `@${item.username}`,
+ text: item.username,
+ icon: item.avatarTag,
+ objectId: item.userID,
+ type: item.type,
+ };
+ } else {
+ return {
+ id: `@${item.name}`,
+ text: item.name,
+ icon: '<fa-icon name="users"></fa-icon>',
+ objectId: item.groupID,
+ type: item.type,
+ };
+ }
});
}
.disableLoadingIndicator()
.fetchAsJson());
return result.map((item) => {
- return {
- id: `@${item.username}`,
- text: item.username,
- icon: item.avatarTag,
- objectId: item.userID,
- type: item.type,
- };
+ if (item.type === "user") {
+ return {
+ id: `@${item.username}`,
+ text: item.username,
+ icon: item.avatarTag,
+ objectId: item.userID,
+ type: item.type,
+ };
+ }
+ else {
+ return {
+ id: `@${item.name}`,
+ text: item.name,
+ icon: '<fa-icon name="users"></fa-icon>',
+ objectId: item.groupID,
+ type: item.type,
+ };
+ }
});
}
function getMentionConfiguration() {
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
+use wcf\data\user\group\UserGroup;
use wcf\data\user\UserProfile;
use wcf\data\user\UserProfileList;
use wcf\http\Helper;
EOT,
);
- $users = $this->getUsers($parameters);
+ $query = \mb_strtolower($parameters['query']);
+ $matches = [];
- // TODO: Groups
+ foreach ($this->getGroups($query) as $userGroup) {
+ $matches[] = [
+ 'name' => $userGroup->getName(),
+ 'groupID' => $userGroup->groupID,
+ 'type' => 'group',
+ ];
+ }
+
+ foreach ($this->getUsers($query) as $userProfile) {
+ $matches[] = [
+ 'avatarTag' => $userProfile->getAvatar()->getImageTag(16),
+ 'username' => $userProfile->getUsername(),
+ 'userID' => $userProfile->getObjectID(),
+ 'type' => 'user',
+ ];
+ }
return new JsonResponse(
- \array_map(
- static fn (UserProfile $userProfile) => [
- 'avatarTag' => $userProfile->getAvatar()->getImageTag(16),
- 'username' => $userProfile->getUsername(),
- 'userID' => $userProfile->getObjectID(),
- 'type' => 'user',
- ],
- $users
- ),
+ $matches,
200,
[
'cache-control' => [
/**
* @return list<UserProfile>
*/
- private function getUsers(array $parameters): array
+ private function getUsers(string $query): array
{
$userProfileList = new UserProfileList();
- $userProfileList->getConditionBuilder()->add("username LIKE ?", [$parameters['query'] . '%']);
+ $userProfileList->getConditionBuilder()->add("username LIKE ?", [$query . '%']);
$userProfileList->sqlLimit = 10;
$userProfileList->readObjects();
return \array_values($userProfileList->getObjects());
}
+
+ /**
+ * @return list<UserGroup>
+ */
+ private function getGroups(string $query): array
+ {
+ $userGroups = UserGroup::getMentionableGroups();
+ if ($userGroups === []) {
+ return [];
+ }
+
+ $userGroups = \array_filter($userGroups, static function (UserGroup $userGroup) use ($query) {
+ return \str_starts_with(\mb_strtolower($userGroup->getName()), $query);
+ });
+
+ \usort($userGroups, static function (UserGroup $a, UserGroup $b) {
+ return \mb_strtolower($a->getName()) <=> \mb_strtolower($b->getName());
+ });
+
+ return $userGroups;
+ }
}