-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplier.cpp
145 lines (116 loc) · 4.12 KB
/
replier.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
#include "replier.h"
#include "ui_replier.h"
Replier::Replier(QWidget* parent)
: QDialog(parent)
, ui(new Ui::Replier)
, _comment(nullptr)
{
ui->setupUi(this);
extern QMap<QString, QMap<QString, QJsonObject>> gCommentsByPost;
extern QString gPostTitle, gCommentTitle;
_comment = &gCommentsByPost[gPostTitle][gCommentTitle];
}
Replier::~Replier()
{
delete ui;
}
void Replier::on_plainTextEdit_textChanged()
{
ui->pushButton_1->setEnabled(!ui->plainTextEdit->toPlainText().isEmpty());
}
void Replier::on_pushButton_1_clicked()
{
if (!approve()) {
return;
}
ui->pushButton_1->setEnabled(false);
if (!sendreply(getMe())) {
return;
}
accept();
}
void Replier::on_pushButton_2_clicked()
{
reject();
}
bool Replier::approve()
{
extern QString gDomain, gUser, gPassword;
extern QString gPostTitle, gCommentTitle;
QNetworkAccessManager manager;
QNetworkRequest request;
QNetworkReply* reply;
request.setUrl(QUrl("https://" + gDomain + "/wp-json/wp/v2/comments/" + QString::number(_comment->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 false;
}
return true;
}
QJsonObject Replier::getMe()
{
extern QString gDomain, gUser, gPassword;
QNetworkAccessManager manager;
QNetworkRequest request;
QNetworkReply* reply;
request.setUrl(QUrl("https://" + gDomain + "/wp-json/wp/v2/users/me"));
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 user information"));
qDebug() << reply->errorString();
return QJsonObject();
}
QByteArray data = reply->readAll();
QJsonDocument doc = QJsonDocument::fromJson(data);
return doc.object();
}
bool Replier::sendreply(QJsonObject me)
{
if (me.isEmpty()) {
return false;
}
extern QString gDomain, gUser, gPassword;
extern QString gPostTitle, gCommentTitle;
QNetworkAccessManager manager;
QNetworkRequest request;
QNetworkReply* reply;
request.setUrl(QUrl("https://" + gDomain + "/wp-json/wp/v2/comments"));
request.setRawHeader("Authorization", "Basic " + QByteArray(QString("%1:%2").arg(gUser).arg(gPassword).toLatin1()).toBase64());
QJsonObject data;
data.insert("author", me.value("id").toInt());
data.insert("author_email", gUser);
data.insert("author_name", me.value("name").toString());
data.insert("author_url", gDomain);
data.insert("content", ui->plainTextEdit->toPlainText());
data.insert("parent", _comment->value("id").toInt());
data.insert("post", _comment->value("post").toInt());
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("Failed to send reply"));
qDebug() << reply->errorString();
return false;
}
return true;
}