forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhabricatorTokenGivenEditor.php
216 lines (170 loc) · 5.59 KB
/
PhabricatorTokenGivenEditor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
final class PhabricatorTokenGivenEditor
extends PhabricatorEditor {
private $contentSource;
private $request;
private $cancelURI;
public function setContentSource(PhabricatorContentSource $content_source) {
$this->contentSource = $content_source;
return $this;
}
public function getContentSource() {
return $this->contentSource;
}
public function setRequest(AphrontRequest $request) {
$this->request = $request;
return $this;
}
public function getRequest() {
return $this->request;
}
public function setCancelURI($cancel_uri) {
$this->cancelURI = $cancel_uri;
return $this;
}
public function getCancelURI() {
return $this->cancelURI;
}
public function addToken($object_phid, $token_phid) {
$token = $this->validateToken($token_phid);
$object = $this->validateObject($object_phid);
$current_token = $this->loadCurrentToken($object);
$actor = $this->requireActor();
$token_given = id(new PhabricatorTokenGiven())
->setAuthorPHID($actor->getPHID())
->setObjectPHID($object->getPHID())
->setTokenPHID($token->getPHID());
$token_given->openTransaction();
if ($current_token) {
$this->executeDeleteToken($object, $current_token);
}
$token_given->save();
queryfx(
$token_given->establishConnection('w'),
'INSERT INTO %T (objectPHID, tokenCount) VALUES (%s, 1)
ON DUPLICATE KEY UPDATE tokenCount = tokenCount + 1',
id(new PhabricatorTokenCount())->getTableName(),
$object->getPHID());
$current_token_phid = null;
if ($current_token) {
$current_token_phid = $current_token->getTokenPHID();
}
try {
$this->publishTransaction(
$object,
$current_token_phid,
$token->getPHID());
} catch (Exception $ex) {
$token_given->killTransaction();
throw $ex;
}
$token_given->saveTransaction();
$subscribed_phids = $object->getUsersToNotifyOfTokenGiven();
if ($subscribed_phids) {
$related_phids = $subscribed_phids;
$related_phids[] = $actor->getPHID();
$story_type = 'PhabricatorTokenGivenFeedStory';
$story_data = array(
'authorPHID' => $actor->getPHID(),
'tokenPHID' => $token->getPHID(),
'objectPHID' => $object->getPHID(),
);
id(new PhabricatorFeedStoryPublisher())
->setStoryType($story_type)
->setStoryData($story_data)
->setStoryTime(time())
->setStoryAuthorPHID($actor->getPHID())
->setRelatedPHIDs($related_phids)
->setPrimaryObjectPHID($object->getPHID())
->setSubscribedPHIDs($subscribed_phids)
->publish();
}
return $token_given;
}
public function deleteToken($object_phid) {
$object = $this->validateObject($object_phid);
$token_given = $this->loadCurrentToken($object);
if (!$token_given) {
return;
}
$token_given->openTransaction();
$this->executeDeleteToken($object, $token_given);
try {
$this->publishTransaction(
$object,
$token_given->getTokenPHID(),
null);
} catch (Exception $ex) {
$token_given->killTransaction();
throw $ex;
}
$token_given->saveTransaction();
}
private function executeDeleteToken(
PhabricatorTokenReceiverInterface $object,
PhabricatorTokenGiven $token_given) {
$token_given->openTransaction();
$token_given->delete();
queryfx(
$token_given->establishConnection('w'),
'INSERT INTO %T (objectPHID, tokenCount) VALUES (%s, 0)
ON DUPLICATE KEY UPDATE tokenCount = tokenCount - 1',
id(new PhabricatorTokenCount())->getTableName(),
$object->getPHID());
$token_given->saveTransaction();
}
private function validateToken($token_phid) {
$token = id(new PhabricatorTokenQuery())
->setViewer($this->requireActor())
->withPHIDs(array($token_phid))
->executeOne();
if (!$token) {
throw new Exception(pht('No such token "%s"!', $token_phid));
}
return $token;
}
private function validateObject($object_phid) {
$object = id(new PhabricatorObjectQuery())
->setViewer($this->requireActor())
->withPHIDs(array($object_phid))
->executeOne();
if (!$object) {
throw new Exception(pht('No such object "%s"!', $object_phid));
}
return $object;
}
private function loadCurrentToken(PhabricatorTokenReceiverInterface $object) {
return id(new PhabricatorTokenGiven())->loadOneWhere(
'authorPHID = %s AND objectPHID = %s',
$this->requireActor()->getPHID(),
$object->getPHID());
}
private function publishTransaction(
PhabricatorTokenReceiverInterface $object,
$old_token_phid,
$new_token_phid) {
if (!($object instanceof PhabricatorApplicationTransactionInterface)) {
return;
}
$actor = $this->requireActor();
$xactions = array();
$xactions[] = id($object->getApplicationTransactionTemplate())
->setTransactionType(PhabricatorTransactions::TYPE_TOKEN)
->setOldValue($old_token_phid)
->setNewValue($new_token_phid);
$editor = $object->getApplicationTransactionEditor()
->setActor($actor)
->setContentSource($this->getContentSource())
->setContinueOnNoEffect(true)
->setContinueOnMissingFields(true);
$request = $this->getRequest();
if ($request) {
$editor->setRequest($request);
}
$cancel_uri = $this->getCancelURI();
if ($cancel_uri) {
$editor->setCancelURI($cancel_uri);
}
$editor->applyTransactions($object, $xactions);
}
}