Skip to content

Commit a722647

Browse files
committed
Implement threads and reports
1 parent 94f9ea0 commit a722647

File tree

6 files changed

+448
-0
lines changed

6 files changed

+448
-0
lines changed

lib/Client/ModrinthAPIClient.php

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Aternos\ModrinthApi\Api\ProjectsApi;
88
use Aternos\ModrinthApi\Api\TagsApi;
99
use Aternos\ModrinthApi\Api\TeamsApi;
10+
use Aternos\ModrinthApi\Api\ThreadsApi;
1011
use Aternos\ModrinthApi\Api\UsersApi;
1112
use Aternos\ModrinthApi\Api\VersionFilesApi;
1213
use Aternos\ModrinthApi\Api\VersionsApi;
@@ -19,8 +20,13 @@
1920
use Aternos\ModrinthApi\Client\Tags\License;
2021
use Aternos\ModrinthApi\Client\Tags\Loader;
2122
use Aternos\ModrinthApi\Client\Tags\ProjectType;
23+
use Aternos\ModrinthApi\Client\Threads\Report;
24+
use Aternos\ModrinthApi\Client\Threads\ReportItemType;
25+
use Aternos\ModrinthApi\Client\Threads\Thread;
26+
use Aternos\ModrinthApi\Client\Threads\ThreadMessageType;
2227
use Aternos\ModrinthApi\Configuration;
2328
use Aternos\ModrinthApi\Model\CategoryTag;
29+
use Aternos\ModrinthApi\Model\CreatableReport;
2430
use Aternos\ModrinthApi\Model\DonationPlatformTag;
2531
use Aternos\ModrinthApi\Model\ForgeUpdates;
2632
use Aternos\ModrinthApi\Model\GameVersionTag;
@@ -30,13 +36,18 @@
3036
use Aternos\ModrinthApi\Model\InvalidInputError;
3137
use Aternos\ModrinthApi\Model\LicenseTag;
3238
use Aternos\ModrinthApi\Model\LoaderTag;
39+
use Aternos\ModrinthApi\Model\ModifyReportRequest;
3340
use Aternos\ModrinthApi\Model\Notification as NotificationModel;
3441
use Aternos\ModrinthApi\Model\Project as ProjectModel;
42+
use Aternos\ModrinthApi\Model\Report as ReportModel;
3543
use Aternos\ModrinthApi\Model\Statistics;
3644
use Aternos\ModrinthApi\Model\TeamMember as TeamMemberModel;
45+
use Aternos\ModrinthApi\Model\ThreadMessageBody;
3746
use Aternos\ModrinthApi\Model\User as UserModel;
3847
use Aternos\ModrinthApi\Model\UserPayoutHistory;
3948
use Aternos\ModrinthApi\Model\Version as VersionModel;
49+
use Aternos\ModrinthApi\Model\Thread as ThreadModel;
50+
4051

4152
/**
4253
* Class ModrinthAPIClient
@@ -66,6 +77,8 @@ class ModrinthAPIClient
6677

6778
protected NotificationsApi $notifications;
6879

80+
protected ThreadsApi $threads;
81+
6982
/**
7083
* ModrinthAPIClient constructor.
7184
* @param string|null $apiToken API token used for authentication
@@ -95,6 +108,7 @@ public function setConfiguration(Configuration $configuration): static
95108
$this->tags = new TagsApi(null, $this->configuration);
96109
$this->misc = new MiscApi(null, $this->configuration);
97110
$this->notifications = new NotificationsApi(null, $this->configuration);
111+
$this->threads = new ThreadsApi(null, $this->configuration);
98112

99113
return $this;
100114
}
@@ -673,4 +687,144 @@ public function readNotifications(array $ids): void
673687
{
674688
$this->notifications->readNotifications(json_encode($ids));
675689
}
690+
691+
/**
692+
* Report a project, user, or version (requires authentication)
693+
* @param string $reportType The type of the report being sent
694+
* @param string $itemId The ID of the item (project, version, or user) being reported
695+
* @param ReportItemType $itemType The type of the item being reported
696+
* @param string $body
697+
* @return Report
698+
* @throws ApiException
699+
*/
700+
public function submitReport(string $reportType, string $itemId, ReportItemType $itemType, string $body): Report
701+
{
702+
$report = new CreatableReport();
703+
$report->setReportType($reportType);
704+
$report->setItemId($itemId);
705+
$report->setItemType($itemType->value);
706+
$report->setBody($body);
707+
708+
return new Report($this, $this->threads->submitReport($report));
709+
}
710+
711+
/**
712+
* Get your open reports (requires authentication)
713+
* @return Report[]
714+
* @throws ApiException
715+
*/
716+
public function getOpenReports(): array
717+
{
718+
return array_map(function (ReportModel $report): Report {
719+
return new Report($this, $report);
720+
}, $this->threads->getOpenReports());
721+
}
722+
723+
/**
724+
* Get a report by ID (requires authentication)
725+
* @param string $id report ID
726+
* @return Report
727+
* @throws ApiException
728+
*/
729+
public function getReport(string $id): Report
730+
{
731+
return new Report($this, $this->threads->getReport($id));
732+
}
733+
734+
/**
735+
* @param string $id
736+
* @param string|null $body
737+
* @param boolean|null $closed
738+
* @return void
739+
* @throws ApiException
740+
*/
741+
public function modifyReport(string $id, ?string $body, ?bool $closed): void
742+
{
743+
$report = new ModifyReportRequest();
744+
$report->setBody($body);
745+
$report->setClosed($closed);
746+
747+
$this->threads->modifyReport($id, $report);
748+
}
749+
750+
/**
751+
* Get multiple reports by ID (requires authentication)
752+
* @param string[] $ids
753+
* @return Report[]
754+
* @throws ApiException
755+
*/
756+
public function getReports(array $ids): array
757+
{
758+
return array_map(function (ReportModel $report): Report {
759+
return new Report($this, $report);
760+
}, $this->threads->getReports(json_encode($ids)));
761+
}
762+
763+
/**
764+
* Get a thread (requires authentication)
765+
* @param string $id
766+
* @return Thread
767+
* @throws ApiException
768+
*/
769+
public function getThread(string $id): Thread
770+
{
771+
return new Thread($this, $this->threads->getThread($id));
772+
}
773+
774+
/**
775+
* Get multiple threads by ID (requires authentication)
776+
* @param string[] $ids
777+
* @return Thread[]
778+
* @throws ApiException
779+
*/
780+
public function getThreads(array $ids): array
781+
{
782+
return array_map(function (ThreadModel $thread): Thread {
783+
return new Thread($this, $thread);
784+
}, $this->threads->getThreads(json_encode($ids)));
785+
}
786+
787+
/**
788+
* Send a message to a thread (requires authentication)
789+
* @param string $threadId
790+
* @param ThreadMessageType $messageType
791+
* @param string|null $body
792+
* @param bool|null $private
793+
* @param string|null $replyingTo
794+
* @param string|null $oldStatus
795+
* @param string|null $newStatus
796+
* @return Thread
797+
* @throws ApiException
798+
*/
799+
public function sendThreadMessage(
800+
string $threadId,
801+
ThreadMessageType $messageType,
802+
?string $body = null,
803+
?bool $private = null,
804+
?string $replyingTo = null,
805+
?string $oldStatus = null,
806+
?string $newStatus = null,
807+
): Thread
808+
{
809+
$message = new ThreadMessageBody();
810+
$message->setType($messageType->value);
811+
$message->setBody($body);
812+
$message->setPrivate($private);
813+
$message->setReplyingTo($replyingTo);
814+
$message->setOldStatus($oldStatus);
815+
$message->setNewStatus($newStatus);
816+
return new Thread($this, $this->threads->sendThreadMessage($threadId, $message));
817+
}
818+
819+
/**
820+
* Delete a thread message (requires authentication)
821+
* @param string $threadId
822+
* @param string $messageId
823+
* @return void
824+
* @throws ApiException
825+
*/
826+
public function deleteThreadMessage(string $threadId, string $messageId): void
827+
{
828+
$this->threads->deleteThreadMessage($threadId, $messageId);
829+
}
676830
}

lib/Client/Threads/Report.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace Aternos\ModrinthApi\Client\Threads;
4+
5+
use Aternos\ModrinthApi\ApiException;
6+
use Aternos\ModrinthApi\Client\ModrinthAPIClient;
7+
use Aternos\ModrinthApi\Client\Project;
8+
use Aternos\ModrinthApi\Client\User;
9+
use Aternos\ModrinthApi\Client\Version;
10+
use Aternos\ModrinthApi\Model\Report as ReportModel;
11+
use Exception;
12+
13+
class Report
14+
{
15+
public function __construct(
16+
protected ModrinthAPIClient $client,
17+
protected ReportModel $report,
18+
)
19+
{
20+
}
21+
22+
/**
23+
* @return ReportModel
24+
*/
25+
public function getData(): ReportModel
26+
{
27+
return $this->report;
28+
}
29+
30+
/**
31+
* @return ReportItemType
32+
*/
33+
public function getItemType(): ReportItemType
34+
{
35+
return ReportItemType::from($this->report->getItemType());
36+
}
37+
38+
/**
39+
* Fetch the full project from the API if the report is a project report
40+
* @return Project
41+
* @throws ApiException
42+
* @throws Exception
43+
*/
44+
public function getProject(): Project
45+
{
46+
if ($this->getItemType() !== ReportItemType::PROJECT) {
47+
throw new Exception("Report is not a project report");
48+
}
49+
50+
return $this->client->getProject($this->report->getItemId());
51+
}
52+
53+
/**
54+
* Fetch the full user from the API if the report is a user report
55+
* @return User
56+
* @throws ApiException
57+
* @throws Exception
58+
*/
59+
public function getUser(): User
60+
{
61+
if ($this->getItemType() !== ReportItemType::USER) {
62+
throw new Exception("Report is not a user report");
63+
}
64+
65+
return $this->client->getUser($this->report->getItemId());
66+
}
67+
68+
/**
69+
* Fetch the full version from the API if the report is a version report
70+
* @return Version
71+
* @throws ApiException
72+
* @throws Exception
73+
*/
74+
public function getVersion(): Version
75+
{
76+
if ($this->getItemType() !== ReportItemType::VERSION) {
77+
throw new Exception("Report is not a version report");
78+
}
79+
80+
return $this->client->getVersion($this->report->getItemId());
81+
}
82+
83+
/**
84+
* Modify the report
85+
* @param string|null $body
86+
* @param bool|null $closed
87+
* @return $this
88+
* @throws ApiException
89+
*/
90+
public function modify(?string $body, ?bool $closed): static
91+
{
92+
$this->client->modifyReport($this->report->getId(), $body, $closed);
93+
return $this;
94+
}
95+
96+
97+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Aternos\ModrinthApi\Client\Threads;
4+
5+
enum ReportItemType: string
6+
{
7+
case PROJECT = "project";
8+
case USER = "user";
9+
case VERSION = "version";
10+
}

0 commit comments

Comments
 (0)