21 public function __construct(
26 parent::__construct( $mainModule, $moduleName );
27 $this->groupSubscription = $groupSubscription;
30 public function execute():
void {
31 if ( !$this->groupSubscription->isEnabled() ) {
32 $this->dieWithError(
'apierror-translate-messagegroupsubscription-disabled' );
35 $params = $this->extractRequestParams();
37 $groupId = $params[
'groupId'];
38 $operation = $params[
'operation'];
41 if ( $group ===
null ) {
42 $this->dieWithError(
'apierror-translate-invalidgroup',
'invalidgroup' );
45 $user = $this->getUser();
46 if ( $operation ===
'subscribe' ) {
47 $status = $this->groupSubscription->subscribeToGroup( $group, $user );
48 if ( !$status->isOK() ) {
49 $this->handleSubscriptionFailure( $status );
51 } elseif ( $operation ===
'unsubscribe' ) {
52 $this->groupSubscription->unsubscribeFromGroup( $group, $user );
55 $this->getResult()->addValue(
57 $this->getModuleName(),
62 'label' => $group->getLabel( $this->getContext() )
68 protected function getAllowedParams(): array {
71 ParamValidator::PARAM_TYPE =>
'string',
72 ParamValidator::PARAM_REQUIRED =>
true,
75 ParamValidator::PARAM_TYPE => [
'subscribe',
'unsubscribe' ],
76 ParamValidator::PARAM_ISMULTI =>
false,
77 ParamValidator::PARAM_REQUIRED =>
true,
82 public function isInternal():
bool {
86 public function needsToken():
string {
90 private function handleSubscriptionFailure( StatusValue $status ):
void {
91 if ( $status->hasMessage( MessageGroupSubscription::NOT_ENABLED ) ) {
92 $this->dieWithError(
'apierror-translate-messagegroupsubscription-disabled' );
93 } elseif ( $status->hasMessage( MessageGroupSubscription::UNNAMED_USER_UNSUPPORTED ) ) {
95 [
'apierror-mustbeloggedin', $this->msg(
'action-translate-watch-message-group' ) ]
98 $this->dieWithError(
'apierror-translate-messagegroupsubscription-dynamic-group-unsupported' );