-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
336 lines (276 loc) · 11.2 KB
/
mainwindow.cpp
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include "mainwindow.h"
#include "about.h"
#include "editor.h"
#include "replier.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowIcon(QIcon(":icon/favicon.ico"));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::set_comboBox_1(QMap<QString, QMap<QString, QJsonObject>> commentsByPost)
{
extern QString gPostTitle;
ui->comboBox_1->clear();
for (auto it = commentsByPost.begin(); it != commentsByPost.end(); ++it) {
ui->comboBox_1->addItem(it.key());
}
on_comboBox_1_textActivated(ui->comboBox_1->currentText());
}
void MainWindow::set_comboBox_2(QMap<QString, QJsonObject> comments)
{
extern QString gCommentTitle;
ui->comboBox_2->clear();
if (comments.isEmpty()) {
ui->comboBox_2->setEnabled(false);
} else {
ui->comboBox_2->setEnabled(true);
}
for (auto it = comments.begin(); it != comments.end(); ++it) {
ui->comboBox_2->addItem(it.key());
}
on_comboBox_2_textActivated(ui->comboBox_2->currentText());
}
void MainWindow::set_comment(QJsonObject comment)
{
ui->plainTextEdit->clear();
if (comment.isEmpty()) {
ui->pushButton_1->setEnabled(false);
ui->pushButton_2->setEnabled(false);
ui->pushButton_3->setEnabled(false);
ui->pushButton_4->setEnabled(false);
ui->pushButton_5->setEnabled(false);
} else {
set_placeholder();
ui->plainTextEdit->setPlainText(comment.value("content").toObject().value("rendered").toString());
ui->pushButton_1->setEnabled(true);
ui->pushButton_2->setEnabled(true);
ui->pushButton_3->setEnabled(true);
ui->pushButton_4->setEnabled(true);
ui->pushButton_5->setEnabled(true);
}
}
void MainWindow::on_comboBox_1_textActivated(const QString& arg1)
{
extern QMap<QString, QMap<QString, QJsonObject>> gCommentsByPost;
extern QString gPostTitle;
gPostTitle = arg1;
set_comboBox_2(gCommentsByPost[arg1]);
}
void MainWindow::on_comboBox_2_textActivated(const QString& arg1)
{
extern QMap<QString, QMap<QString, QJsonObject>> gCommentsByPost;
extern QString gPostTitle, gCommentTitle;
gCommentTitle = arg1;
set_comment(gCommentsByPost[gPostTitle][arg1]);
}
void MainWindow::on_pushButton_1_clicked()
{
// POST /wp/v2/comments/<id> <- status: approved
extern QString gDomain, gUser, gPassword;
extern QMap<QString, QMap<QString, QJsonObject>> gCommentsByPost;
extern QString gPostTitle, gCommentTitle;
QNetworkAccessManager manager;
QNetworkRequest request;
QNetworkReply* reply;
request.setUrl(QUrl("https://" + gDomain + "/wp-json/wp/v2/comments/" + QString::number(gCommentsByPost[gPostTitle][gCommentTitle].value("id").toInt())));
request.setRawHeader("Authorization", "Basic " + QByteArray(QString("%1:%2").arg(gUser).arg(gPassword).toLatin1()).toBase64());
QJsonObject data;
data.insert("status", "approved");
QJsonDocument doc(data);
QByteArray body = doc.toJson();
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
reply = manager.post(request, body);
QEventLoop loop;
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
if (reply->error() != QNetworkReply::NoError) {
QMessageBox::critical(nullptr, QObject::tr("Error"), QObject::tr("Approval Failed"));
qDebug() << reply->errorString();
return;
}
reset();
}
void MainWindow::on_pushButton_2_clicked()
{
extern QString gDomain, gUser, gPassword;
if (gUser.indexOf('@') == -1) {
QMessageBox::critical(nullptr, QObject::tr("Error"), QObject::tr("Posting replies requires logging in with email address"));
return;
}
Replier replier(this);
if (replier.exec() == QDialog::Accepted) {
reset();
}
}
void MainWindow::on_pushButton_3_clicked()
{
extern QMap<QString, QMap<QString, QJsonObject>> gCommentsByPost;
extern QString gPostTitle, gCommentTitle;
QJsonObject now_comment = gCommentsByPost[gPostTitle][gCommentTitle];
Editor editor(this);
if (editor.exec() == QDialog::Accepted) {
reset();
QString postTitle = gPostTitle;
QString commentTitle = gCommentTitle;
set_comboBox_1(gCommentsByPost);
ui->comboBox_1->setCurrentText(postTitle);
on_comboBox_1_textActivated(postTitle);
ui->comboBox_2->setCurrentText(commentTitle);
on_comboBox_2_textActivated(commentTitle);
}
}
void MainWindow::on_pushButton_4_clicked()
{
// POST /wp/v2/comments/<id> <- status: trash
extern QString gDomain, gUser, gPassword;
extern QMap<QString, QMap<QString, QJsonObject>> gCommentsByPost;
extern QString gPostTitle, gCommentTitle;
QNetworkAccessManager manager;
QNetworkRequest request;
QNetworkReply* reply;
request.setUrl(QUrl("https://" + gDomain + "/wp-json/wp/v2/comments/" + QString::number(gCommentsByPost[gPostTitle][gCommentTitle].value("id").toInt())));
request.setRawHeader("Authorization", "Basic " + QByteArray(QString("%1:%2").arg(gUser).arg(gPassword).toLatin1()).toBase64());
QJsonObject data;
data.insert("status", "trash");
QJsonDocument doc(data);
QByteArray body = doc.toJson();
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
reply = manager.post(request, body);
QEventLoop loop;
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
if (reply->error() != QNetworkReply::NoError) {
QMessageBox::critical(nullptr, QObject::tr("Error"), QObject::tr("Deletion Failed"));
qDebug() << reply->errorString();
return;
}
reset();
}
void MainWindow::on_pushButton_5_clicked()
{
// POST /wp/v2/comments/<id> <- status: spam
extern QString gDomain, gUser, gPassword;
extern QMap<QString, QMap<QString, QJsonObject>> gCommentsByPost;
extern QString gPostTitle, gCommentTitle;
QNetworkAccessManager manager;
QNetworkRequest request;
QNetworkReply* reply;
request.setUrl(QUrl("https://" + gDomain + "/wp-json/wp/v2/comments/" + QString::number(gCommentsByPost[gPostTitle][gCommentTitle].value("id").toInt())));
request.setRawHeader("Authorization", "Basic " + QByteArray(QString("%1:%2").arg(gUser).arg(gPassword).toLatin1()).toBase64());
QJsonObject data;
data.insert("status", "spam");
QJsonDocument doc(data);
QByteArray body = doc.toJson();
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
reply = manager.post(request, body);
QEventLoop loop;
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
if (reply->error() != QNetworkReply::NoError) {
QMessageBox::critical(nullptr, QObject::tr("Error"), QObject::tr("Deletion Failed"));
qDebug() << reply->errorString();
return;
}
reset();
}
void MainWindow::on_pushButton_6_clicked()
{
reset();
}
void MainWindow::on_pushButton_7_clicked()
{
About about(this);
about.exec();
}
void MainWindow::reset()
{
extern QString gDomain, gUser, gPassword;
extern QMap<QString, QMap<QString, QJsonObject>> gCommentsByPost;
extern QString gPostTitle, gCommentTitle;
set_placeholder(QString(QObject::tr("[ Refreshing... ]")));
set_comment(QJsonObject());
QJsonArray comments = getComments(gDomain, gUser, gPassword);
if (comments.isEmpty()) {
QMessageBox::critical(nullptr, QObject::tr("Error"), QObject::tr("All comments have been approved"));
}
gCommentsByPost = getCommentsByPost(comments);
if (gCommentsByPost.contains(gPostTitle) && gCommentsByPost[gPostTitle].contains(gCommentTitle)) {
set_comment(gCommentsByPost[gPostTitle][gCommentTitle]);
} else if (gCommentsByPost.contains(gPostTitle)) {
set_comboBox_2(gCommentsByPost[gPostTitle]);
} else {
set_comboBox_1(gCommentsByPost);
}
}
void MainWindow::set_placeholder(QString placeholder)
{
ui->plainTextEdit->setPlaceholderText(placeholder);
}
QJsonArray MainWindow::getComments(QString domain, QString user, QString password)
{
set_placeholder(QString(QObject::tr("[ Refreshing... ]")));
QNetworkAccessManager manager;
QNetworkRequest request;
QNetworkReply* reply;
request.setUrl(QUrl("https://" + domain + "/wp-json/wp/v2/comments?status=hold&per_page=100"));
request.setRawHeader("Authorization", "Basic " + QByteArray(QString("%1:%2").arg(user).arg(password).toLatin1()).toBase64());
reply = manager.get(request);
QEventLoop loop;
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
if (reply->error() != QNetworkReply::NoError) {
QMessageBox::critical(nullptr, QObject::tr("Error"), QObject::tr("Failed to read comments"));
qDebug() << reply->errorString();
return QJsonArray();
}
QByteArray data = reply->readAll();
QJsonDocument doc = QJsonDocument::fromJson(data);
return doc.array();
}
QMap<QString, QMap<QString, QJsonObject>> MainWindow::getCommentsByPost(QJsonArray comments)
{
set_placeholder(QString("[ Reading post title... ]"));
QSet<int> postIds;
for (int i = 0; i < comments.size(); i++) {
postIds.insert(comments.at(i).toObject().value("post").toInt());
}
QMap<int, QString> postName;
for (int postId : postIds) {
QNetworkAccessManager manager;
QNetworkRequest request;
QNetworkReply* reply;
extern QString gDomain, gUser, gPassword;
request.setUrl(QUrl("https://" + gDomain + "/wp-json/wp/v2/posts/" + QString::number(postId)));
request.setRawHeader("Authorization", "Basic " + QByteArray(QString("%1:%2").arg(gUser).arg(gPassword).toLatin1()).toBase64());
reply = manager.get(request);
QEventLoop loop;
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
if (reply->error() != QNetworkReply::NoError) {
QMessageBox::critical(nullptr, QObject::tr("Error"), QObject::tr("Failed to read post title"));
qDebug() << reply->errorString();
return QMap<QString, QMap<QString, QJsonObject>>();
}
QByteArray data = reply->readAll();
QJsonDocument doc = QJsonDocument::fromJson(data);
QString title = doc.object().value("title").toObject().value("rendered").toString();
postName.insert(postId, QString::number(postId) + " - " + title);
set_placeholder(QString("[ Identified post ") + title + QString(" ]"));
}
QMap<QString, QMap<QString, QJsonObject>> commentsByPost;
for (int i = 0; i < comments.size(); i++) {
QJsonObject comment = comments.at(i).toObject();
QString postTitle = postName.value(comment.value("post").toInt());
QString commentTitle = QString::number(comment.value("id").toInt()) + " - " + comment.value("author_name").toString() + " - " + comment.value("content").toObject().value("rendered").toString().simplified();
commentsByPost[postTitle][commentTitle.length() > 50 ? commentTitle.left(50) : commentTitle] = comment;
}
set_placeholder(QString(QObject::tr("[ Loading complete ]")));
return commentsByPost;
}