-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmessagePublisher.ts
More file actions
135 lines (121 loc) · 3.47 KB
/
messagePublisher.ts
File metadata and controls
135 lines (121 loc) · 3.47 KB
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
import { ExtensionContext, StatusBarItem, window } from 'vscode'
import { CodeReviewSelectionItem, PresenceStatus } from '../utils'
import { log } from '../utils/logger'
import { Authorization } from '../utils/authorization'
import { MessageApi } from '../api/messageApi'
import { Presence } from '../models/presence'
const module = 'message_publisher.ts'
export const MessagePublisher = {
sendThreadMessage: async ({
codeReview,
context,
statusBar,
}: {
codeReview: CodeReviewSelectionItem
context: ExtensionContext
statusBar: StatusBarItem
}) => {
log.info(
`sending thread message. parentMessageXid: ${codeReview.parentMessageXid}`,
module
)
const session = await Authorization.currentSession(context)
const inputText = await getInputText({
prompt:
'Type a custom message or send the default message in the pull request thread.',
placeHolder: "I'm on it.",
})
if (inputText === undefined) return false // text is undefined when user cancels input box
const response = await MessageApi.sendCodeReviewThreadMessage({
body: inputText || "I'm on it.",
parentMessageXid: codeReview.parentMessageXid,
chatChannelId: codeReview.chatChannelId,
accessToken: session?.accessToken ?? '',
context,
})
if (response?.error) {
window.showInformationMessage(
`Something went wrong, failed to send message to thread for ${{
codeReview,
}}.`
)
return false
}
await Presence.set({
status: PresenceStatus.Active,
context,
statusBar,
})
return true
},
sendDirectMessage: async ({
toAccount,
context,
codeReviewId,
chatLink,
fromAuthor,
statusBar,
}: {
toAccount: { xid: string; hasUser: boolean }
context: ExtensionContext
chatLink: string
codeReviewId: string
fromAuthor: boolean
statusBar: StatusBarItem
}) => {
log.info(
`sending direct message. toCodeAccountXid: ${toAccount.xid}`,
module
)
const session = await Authorization.currentSession(context)
const messageTemplate = fromAuthor
? `Reminder: I have a pull request waiting for you.`
: `Is this PR ready for review?`
const inputText = await getInputText({
prompt: 'Type a custom message or send the default message.',
placeHolder: messageTemplate,
})
if (inputText === undefined) return false // text is undefined when user cancels input box
if (!toAccount.hasUser) {
window.showInformationMessage(
`Failed to send message to ${toAccount.xid} because they are not a PullFlow user.
Please ask them to sign up to PullFlow at: https://app.pullflow.com`
)
return null
}
const message = inputText !== '' ? inputText : messageTemplate
const response = await MessageApi.sendDirectMessage({
message,
codeAccountXid: toAccount.xid,
chatLink,
accessToken: session?.accessToken ?? '',
codeReviewId,
fromAuthor,
context,
})
if (response?.error) {
window.showInformationMessage(
`Something went wrong, failed to send message to ${toAccount.xid}.`
)
return false
}
await Presence.set({
status: PresenceStatus.Active,
context,
statusBar,
})
return true
},
}
const getInputText = ({
prompt,
placeHolder,
}: {
prompt: string
placeHolder: string
}) => {
return window.showInputBox({
prompt,
placeHolder,
})
}