Skip to content

Commit 76b0368

Browse files
committed
Fix fetching users for ChatCommandDelegate
1 parent 1f7251d commit 76b0368

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/main/groovy/net/zomis/duga/tasks/ChatCommandDelegate.groovy

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import net.zomis.duga.HookStringification
55
import net.zomis.duga.User
66
import net.zomis.duga.chat.listen.ChatMessageIncoming
77

8+
import java.util.concurrent.atomic.AtomicReference
9+
810
/**
911
* Delegate for running chat commands
1012
*/
@@ -31,9 +33,17 @@ abstract class ChatCommandDelegate extends Script {
3133
message.reply('pong!')
3234
}
3335

36+
private User fetchUser() {
37+
AtomicReference<User> user = new AtomicReference<>();
38+
User.withNewSession {status ->
39+
user.set(User.findByChatId(message.getUserId()))
40+
}
41+
return user.get()
42+
}
43+
3444
Closure webhooks = {
3545
requireUser()
36-
User user = message.fetchUser()
46+
User user = fetchUser()
3747

3848
List<String> reposHooked = new ArrayList<>()
3949
List<String> unhooked = new ArrayList<>()
@@ -62,7 +72,7 @@ abstract class ChatCommandDelegate extends Script {
6272

6373
void addWebhook(String repo, int roomId) {
6474
requireUser()
65-
User user = message.fetchUser()
75+
User user = fetchUser()
6676
def request = [name: 'web', active: true, 'events': ['*'],
6777
config: [
6878
url: "http://stats.zomis.net/GithubHookSEChatService/hook?roomId=$roomId",
@@ -87,18 +97,18 @@ abstract class ChatCommandDelegate extends Script {
8797
}
8898

8999
void requireUser() {
90-
assert message.fetchUser()
100+
assert fetchUser()
91101
}
92102

93103
void requireRole(String role) {
94-
User user = message.fetchUser()
104+
User user = fetchUser()
95105
if (!user) {
96106
message.reply('You are not registered with Duga. Please go to http://stats.zomis.net/GithubHookSEChatService/registration/index for registration instructions')
97107
assert false
98108
}
99109
boolean userHasRole = false
100110
User.withNewSession {status ->
101-
userHasRole = message.fetchUser().getAuthorities().stream().anyMatch({auth ->
111+
userHasRole = fetchUser().getAuthorities().stream().anyMatch({auth ->
102112
auth.authority.equals(role)
103113
})
104114
if (!userHasRole) {
@@ -125,7 +135,7 @@ abstract class ChatCommandDelegate extends Script {
125135
[search: {String query ->
126136
// https://api.github.com/search/issues?q=systems%20repo:Cardshifter/Cardshifter%20is:open
127137
String q = query.replaceAll(' ', '+')
128-
def issues = message.fetchUser().github("search/issues?q=repo:$repo+is:open+$q")
138+
def issues = fetchUser().github("search/issues?q=repo:$repo+is:open+$q")
129139
StringBuilder results = new StringBuilder(issues.total_count + ' results found. ' as String)
130140
int count = 0
131141
for (def json in issues.items) {
@@ -142,7 +152,7 @@ abstract class ChatCommandDelegate extends Script {
142152
}
143153
message.reply(results.toString())
144154
}, id: {int id ->
145-
def issue = message.fetchUser().github("repos/$repo/issues/$id")
155+
def issue = fetchUser().github("repos/$repo/issues/$id")
146156
message.reply(HookStringification.issue(issue))
147157
}]
148158
}

0 commit comments

Comments
 (0)