Skip to content

Commit 23094b4

Browse files
author
epriestley
committedMay 26, 2022
Move Slowvote response visibility to a separate object
Summary: Ref T13682. This change supports modifying these constants to be sensible strings instead of opaque integers. Test Plan: Created and edited polls. Maniphest Tasks: T13682 Differential Revision: https://secure.phabricator.com/D21843
1 parent b1533e5 commit 23094b4

6 files changed

+126
-32
lines changed
 

‎src/__phutil_library_map__.php

+2
Original file line numberDiff line numberDiff line change
@@ -5872,6 +5872,7 @@
58725872
'SlowvoteConduitAPIMethod' => 'applications/slowvote/conduit/SlowvoteConduitAPIMethod.php',
58735873
'SlowvoteEmbedView' => 'applications/slowvote/view/SlowvoteEmbedView.php',
58745874
'SlowvoteInfoConduitAPIMethod' => 'applications/slowvote/conduit/SlowvoteInfoConduitAPIMethod.php',
5875+
'SlowvotePollResponseVisibility' => 'applications/slowvote/constants/SlowvotePollResponseVisibility.php',
58755876
'SlowvoteRemarkupRule' => 'applications/slowvote/remarkup/SlowvoteRemarkupRule.php',
58765877
'SlowvoteSearchConduitAPIMethod' => 'applications/slowvote/conduit/SlowvoteSearchConduitAPIMethod.php',
58775878
'SubscriptionListDialogBuilder' => 'applications/subscriptions/view/SubscriptionListDialogBuilder.php',
@@ -12773,6 +12774,7 @@
1277312774
'SlowvoteConduitAPIMethod' => 'ConduitAPIMethod',
1277412775
'SlowvoteEmbedView' => 'AphrontView',
1277512776
'SlowvoteInfoConduitAPIMethod' => 'SlowvoteConduitAPIMethod',
12777+
'SlowvotePollResponseVisibility' => 'Phobject',
1277612778
'SlowvoteRemarkupRule' => 'PhabricatorObjectRemarkupRule',
1277712779
'SlowvoteSearchConduitAPIMethod' => 'PhabricatorSearchEngineAPIMethod',
1277812780
'SubscriptionListDialogBuilder' => 'Phobject',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
final class SlowvotePollResponseVisibility
4+
extends Phobject {
5+
6+
const RESPONSES_VISIBLE = 0;
7+
const RESPONSES_VOTERS = 1;
8+
const RESPONSES_OWNER = 2;
9+
10+
private $key;
11+
12+
public static function newResponseVisibilityObject($key) {
13+
$object = new self();
14+
$object->key = $key;
15+
return $object;
16+
}
17+
18+
public function getKey() {
19+
return $this->key;
20+
}
21+
22+
public static function getAll() {
23+
$map = self::getMap();
24+
25+
$result = array();
26+
foreach ($map as $key => $spec) {
27+
$result[$key] = self::newResponseVisibilityObject($key);
28+
}
29+
30+
return $result;
31+
}
32+
33+
public function getName() {
34+
$name = $this->getProperty('name');
35+
36+
if ($name === null) {
37+
$name = pht('Unknown ("%s")', $this->getKey());
38+
}
39+
40+
return $name;
41+
}
42+
43+
public function getNameForEdit() {
44+
$name = $this->getProperty('name.edit');
45+
46+
if ($name === null) {
47+
$name = pht('Unknown ("%s")', $this->getKey());
48+
}
49+
50+
return $name;
51+
}
52+
53+
private function getProperty($key, $default = null) {
54+
$spec = idx(self::getMap(), $this->getKey());
55+
return idx($spec, $key, $default);
56+
}
57+
58+
private static function getMap() {
59+
return array(
60+
self::RESPONSES_VISIBLE => array(
61+
'name' => pht('Always Visible'),
62+
'name.edit' => pht('Anyone can see the responses'),
63+
),
64+
self::RESPONSES_VOTERS => array(
65+
'name' => pht('Voters'),
66+
'name.edit' => pht('Require a vote to see the responses'),
67+
),
68+
self::RESPONSES_OWNER => array(
69+
'name' => pht('Owner'),
70+
'name.edit' => pht('Only the poll owner can see the responses'),
71+
),
72+
);
73+
}
74+
75+
}

‎src/applications/slowvote/controller/PhabricatorSlowvoteEditController.php

+3-9
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function handleRequest(AphrontRequest $request) {
4949
if ($request->isFormPost()) {
5050
$v_question = $request->getStr('question');
5151
$v_description = $request->getStr('description');
52-
$v_responses = (int)$request->getInt('responses');
52+
$v_responses = $request->getStr('responses');
5353
$v_shuffle = (int)$request->getBool('shuffle');
5454
$v_view_policy = $request->getStr('viewPolicy');
5555
$v_projects = $request->getArr('projects');
@@ -196,14 +196,8 @@ public function handleRequest(AphrontRequest $request) {
196196
pht('Approval (Multiple Choice)'),
197197
);
198198

199-
$response_type_options = array(
200-
PhabricatorSlowvotePoll::RESPONSES_VISIBLE
201-
=> pht('Allow anyone to see the responses'),
202-
PhabricatorSlowvotePoll::RESPONSES_VOTERS
203-
=> pht('Require a vote to see the responses'),
204-
PhabricatorSlowvotePoll::RESPONSES_OWNER
205-
=> pht('Only I can see the responses'),
206-
);
199+
$response_type_map = SlowvotePollResponseVisibility::getAll();
200+
$response_type_options = mpull($response_type_map, 'getNameForEdit');
207201

208202
if ($is_new) {
209203
$form->appendChild(

‎src/applications/slowvote/storage/PhabricatorSlowvotePoll.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,13 @@ final class PhabricatorSlowvotePoll
1313
PhabricatorSpacesInterface,
1414
PhabricatorConduitResultInterface {
1515

16-
const RESPONSES_VISIBLE = 0;
17-
const RESPONSES_VOTERS = 1;
18-
const RESPONSES_OWNER = 2;
19-
2016
const METHOD_PLURALITY = 0;
2117
const METHOD_APPROVAL = 1;
2218

2319
protected $question;
2420
protected $description;
2521
protected $authorPHID;
26-
protected $responseVisibility = 0;
22+
protected $responseVisibility;
2723
protected $shuffle = 0;
2824
protected $method;
2925
protected $viewPolicy;
@@ -43,10 +39,13 @@ public static function initializeNewPoll(PhabricatorUser $actor) {
4339
$view_policy = $app->getPolicy(
4440
PhabricatorSlowvoteDefaultViewCapability::CAPABILITY);
4541

42+
$default_responses = SlowvotePollResponseVisibility::RESPONSES_VISIBLE;
43+
4644
return id(new PhabricatorSlowvotePoll())
4745
->setAuthorPHID($actor->getPHID())
4846
->setViewPolicy($view_policy)
49-
->setSpacePHID($actor->getDefaultSpacePHID());
47+
->setSpacePHID($actor->getDefaultSpacePHID())
48+
->setResponseVisibility($default_responses);
5049
}
5150

5251
protected function getConfiguration() {

‎src/applications/slowvote/view/SlowvoteEmbedView.php

+14-10
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ public function render() {
7777

7878
$vis = $poll->getResponseVisibility();
7979
if ($this->areResultsVisible()) {
80-
if ($vis == PhabricatorSlowvotePoll::RESPONSES_OWNER) {
80+
if ($vis == SlowvotePollResponseVisibility::RESPONSES_OWNER) {
8181
$quip = pht('Only you can see the results.');
8282
} else {
8383
$quip = pht('Voting improves cardiovascular endurance.');
8484
}
85-
} else if ($vis == PhabricatorSlowvotePoll::RESPONSES_VOTERS) {
85+
} else if ($vis == SlowvotePollResponseVisibility::RESPONSES_VOTERS) {
8686
$quip = pht('You must vote to see the results.');
87-
} else if ($vis == PhabricatorSlowvotePoll::RESPONSES_OWNER) {
87+
} else if ($vis == SlowvotePollResponseVisibility::RESPONSES_OWNER) {
8888
$quip = pht('Only the author can see the results.');
8989
}
9090

@@ -321,15 +321,19 @@ private function renderStatus(PhabricatorSlowvoteOption $option) {
321321
private function areResultsVisible() {
322322
$poll = $this->getPoll();
323323

324-
$vis = $poll->getResponseVisibility();
325-
if ($vis == PhabricatorSlowvotePoll::RESPONSES_VISIBLE) {
324+
$visibility = $poll->getResponseVisibility();
325+
if ($visibility == SlowvotePollResponseVisibility::RESPONSES_VISIBLE) {
326326
return true;
327-
} else if ($vis == PhabricatorSlowvotePoll::RESPONSES_OWNER) {
328-
return ($poll->getAuthorPHID() == $this->getUser()->getPHID());
329-
} else {
330-
$choices = mgroup($poll->getChoices(), 'getAuthorPHID');
331-
return (bool)idx($choices, $this->getUser()->getPHID());
332327
}
328+
329+
$viewer = $this->getViewer();
330+
331+
if ($visibility == SlowvotePollResponseVisibility::RESPONSES_OWNER) {
332+
return ($poll->getAuthorPHID() === $viewer->getPHID());
333+
}
334+
335+
$choices = mgroup($poll->getChoices(), 'getAuthorPHID');
336+
return (bool)idx($choices, $viewer->getPHID());
333337
}
334338

335339
}

‎src/applications/slowvote/xaction/PhabricatorSlowvoteResponsesTransaction.php

+27-7
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,46 @@ final class PhabricatorSlowvoteResponsesTransaction
66
const TRANSACTIONTYPE = 'vote:responses';
77

88
public function generateOldValue($object) {
9-
return (int)$object->getResponseVisibility();
9+
return $object->getResponseVisibility();
1010
}
1111

1212
public function applyInternalEffects($object, $value) {
1313
$object->setResponseVisibility($value);
1414
}
1515

1616
public function getTitle() {
17-
// TODO: This could be more detailed
17+
$old_name = $this->getOldResponseVisibilityObject()->getName();
18+
$new_name = $this->getNewResponseVisibilityObject()->getName();
19+
1820
return pht(
19-
'%s changed who can see the responses.',
20-
$this->renderAuthor());
21+
'%s changed who can see the responses from %s to %s.',
22+
$this->renderAuthor(),
23+
$this->renderValue($old_name),
24+
$this->renderValue($new_name));
2125
}
2226

2327
public function getTitleForFeed() {
24-
// TODO: This could be more detailed
28+
$old_name = $this->getOldResponseVisibilityObject()->getName();
29+
$new_name = $this->getNewResponseVisibilityObject()->getName();
30+
2531
return pht(
26-
'%s changed who can see the responses of %s.',
32+
'%s changed who can see the responses of %s from %s to %s.',
2733
$this->renderAuthor(),
28-
$this->renderObject());
34+
$this->renderObject(),
35+
$this->renderValue($old_name),
36+
$this->renderValue($new_name));
37+
}
38+
39+
private function getOldResponseVisibilityObject() {
40+
return $this->newResponseVisibilityObject($this->getOldValue());
41+
}
42+
43+
private function getNewResponseVisibilityObject() {
44+
return $this->newResponseVisibilityObject($this->getNewValue());
45+
}
46+
47+
private function newResponseVisibilityObject($value) {
48+
return SlowvotePollResponseVisibility::newResponseVisibilityObject($value);
2949
}
3050

3151
}

0 commit comments

Comments
 (0)
Failed to load comments.