-
-
Notifications
You must be signed in to change notification settings - Fork 548
Expand file tree
/
Copy pathSendWhisper.cpp
More file actions
250 lines (220 loc) · 8.08 KB
/
SendWhisper.cpp
File metadata and controls
250 lines (220 loc) · 8.08 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "controllers/commands/builtin/twitch/SendWhisper.hpp"
#include "Application.hpp"
#include "common/LinkParser.hpp"
#include "controllers/accounts/AccountController.hpp"
#include "controllers/commands/CommandContext.hpp"
#include "controllers/emotes/EmoteController.hpp"
#include "messages/Message.hpp"
#include "messages/MessageBuilder.hpp"
#include "messages/MessageElement.hpp"
#include "providers/bttv/BttvEmotes.hpp"
#include "providers/emoji/Emojis.hpp"
#include "providers/ffz/FfzEmotes.hpp"
#include "providers/twitch/api/Helix.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "providers/twitch/TwitchIrcServer.hpp"
#include "singletons/Settings.hpp"
#include "singletons/StreamerMode.hpp"
#include "singletons/Theme.hpp"
#include "util/Twitch.hpp"
namespace {
using namespace chatterino;
QString formatWhisperError(HelixWhisperError error, const QString &message)
{
using Error = HelixWhisperError;
QString errorMessage = "Failed to send whisper - ";
switch (error)
{
case Error::NoVerifiedPhone: {
errorMessage += "Due to Twitch restrictions, you are now "
"required to have a verified phone number "
"to send whispers. You can add a phone "
"number in Twitch settings. "
"https://www.twitch.tv/settings/security";
};
break;
case Error::RecipientBlockedUser: {
errorMessage += "The recipient doesn't allow whispers "
"from strangers or you directly.";
};
break;
case Error::WhisperSelf: {
errorMessage += "You cannot whisper yourself.";
};
break;
case Error::Forwarded: {
errorMessage += message;
}
break;
case Error::Ratelimited: {
errorMessage += "You may only whisper a maximum of 40 "
"unique recipients per day. Within the "
"per day limit, you may whisper a "
"maximum of 3 whispers per second and "
"a maximum of 100 whispers per minute.";
}
break;
case Error::UserMissingScope: {
// TODO(pajlada): Phrase MISSING_REQUIRED_SCOPE
errorMessage += "Missing required scope. "
"Re-login with your "
"account and try again.";
}
break;
case Error::UserNotAuthorized: {
// TODO(pajlada): Phrase MISSING_PERMISSION
errorMessage += "You don't have permission to "
"perform that action.";
}
break;
case Error::Unknown: {
errorMessage += "An unknown error has occurred.";
}
break;
}
return errorMessage;
}
bool appendWhisperMessageWordsLocally(const QStringList &words)
{
auto *app = getApp();
MessageBuilder b;
b.emplace<TimestampElement>();
b.emplace<TextElement>(
app->getAccounts()->twitch.getCurrent()->getUserName(),
MessageElementFlag::Text, MessageColor::Text,
FontStyle::ChatMediumBold);
b.emplace<TextElement>("->", MessageElementFlag::Text,
getApp()->getThemes()->messages.textColors.system);
b.emplace<TextElement>(words[1] + ":", MessageElementFlag::Text,
MessageColor::Text, FontStyle::ChatMediumBold);
const auto &acc = app->getAccounts()->twitch.getCurrent();
const auto &accemotes = *acc->accessEmotes();
const auto *bttvemotes = app->getBttvEmotes();
const auto *ffzemotes = app->getFfzEmotes();
const auto *emoteController = app->getEmotes();
auto emote = std::optional<EmotePtr>{};
for (int i = 2; i < words.length(); i++)
{
{ // Twitch emote
auto it = accemotes->find({words[i]});
if (it != accemotes->end())
{
b.emplace<EmoteElement>(it->second, MessageElementFlag::Emote);
continue;
}
} // Twitch emote
{ // bttv/ffz emote
emote = bttvemotes->emote({words[i]});
if (!emote)
{
emote = ffzemotes->emote({words[i]});
}
// TODO: Load 7tv global emotes
if (emote)
{
b.emplace<EmoteElement>(*emote, MessageElementFlag::Emote);
continue;
}
} // bttv/ffz emote
{ // third party emotes
emote = emoteController->resolveGlobal(EmoteName{words[i]});
if (emote)
{
b.emplace<EmoteElement>(*emote, MessageElementFlag::Emote);
continue;
}
} // third party emotes
{ // emoji/text
for (auto &variant : emoteController->getEmojis()->parse(words[i]))
{
constexpr const static struct {
void operator()(EmotePtr emote, MessageBuilder &b) const
{
b.emplace<EmoteElement>(emote,
MessageElementFlag::EmojiAll);
}
void operator()(const QString &string,
MessageBuilder &b) const
{
auto link = linkparser::parse(string);
if (link)
{
b.addLink(*link, string);
}
else
{
b.emplace<TextElement>(string,
MessageElementFlag::Text);
}
}
} visitor;
boost::apply_visitor(
[&b](auto &&arg) {
visitor(arg, b);
},
variant);
} // emoji/text
}
}
b->flags.set(MessageFlag::DoNotTriggerNotification);
b->flags.set(MessageFlag::Whisper);
auto messagexD = b.release();
getApp()->getTwitch()->getWhispersChannel()->addMessage(
messagexD, MessageContext::Original);
if (getSettings()->inlineWhispers &&
!(getSettings()->streamerModeSuppressInlineWhispers &&
getApp()->getStreamerMode()->isEnabled()))
{
app->getTwitch()->forEachChannel([&messagexD](ChannelPtr _channel) {
_channel->addMessage(messagexD, MessageContext::Repost);
});
}
return true;
}
} // namespace
namespace chatterino::commands {
QString sendWhisper(const CommandContext &ctx)
{
if (ctx.channel == nullptr)
{
return "";
}
if (ctx.words.size() < 3)
{
ctx.channel->addSystemMessage("Usage: /w <username> <message>");
return "";
}
auto currentUser = getApp()->getAccounts()->twitch.getCurrent();
if (currentUser->isAnon())
{
ctx.channel->addSystemMessage(
"You must be logged in to send a whisper!");
return "";
}
auto target = ctx.words.at(1);
stripChannelName(target);
auto message = ctx.words.mid(2).join(' ');
if (ctx.channel->isTwitchChannel())
{
getHelix()->getUserByName(
target,
[channel{ctx.channel}, currentUser, target, message,
words{ctx.words}](const auto &targetUser) {
getHelix()->sendWhisper(
currentUser->getUserId(), targetUser.id, message,
[words] {
appendWhisperMessageWordsLocally(words);
},
[channel, target, targetUser](auto error, auto message) {
auto errorMessage = formatWhisperError(error, message);
channel->addSystemMessage(errorMessage);
});
},
[channel{ctx.channel}] {
channel->addSystemMessage("No user matching that username.");
});
return "";
}
return "";
}
} // namespace chatterino::commands