Commit e265795e authored by kaniini's avatar kaniini

clean up content type patch

parent 7f215fdb
......@@ -93,7 +93,12 @@ void Account::get(QUrl url, bool authenticated, std::function<void(QNetworkReply
if (reply_cb != nullptr)
{
QObject::connect(reply, &QNetworkReply::finished, [reply, reply_cb] () { reply_cb(reply); });
QObject::connect(reply, &QNetworkReply::finished, [reply, reply_cb] () {
if (200 != reply->attribute (QNetworkRequest::HttpStatusCodeAttribute))
return;
reply_cb(reply);
});
}
}
......@@ -116,7 +121,12 @@ void Account::post(QUrl url, QJsonDocument &doc, bool authenticated, std::functi
if (reply_cb != nullptr)
{
QObject::connect(reply, &QNetworkReply::finished, [reply, reply_cb] () { reply_cb(reply); });
QObject::connect(reply, &QNetworkReply::finished, [reply, reply_cb] () {
if (200 != reply->attribute (QNetworkRequest::HttpStatusCodeAttribute))
return;
reply_cb(reply);
});
}
}
......@@ -139,7 +149,12 @@ void Account::post(QUrl url, QUrlQuery &formdata, bool authenticated, std::funct
if (reply_cb != nullptr)
{
QObject::connect(reply, &QNetworkReply::finished, [reply, reply_cb] () { reply_cb(reply); });
QObject::connect(reply, &QNetworkReply::finished, [reply, reply_cb] () {
if (200 != reply->attribute (QNetworkRequest::HttpStatusCodeAttribute))
return;
reply_cb(reply);
});
}
}
......@@ -367,61 +382,89 @@ void Account::unrepeat(std::shared_ptr<Post> p)
{
mutatePost(p, "unreblog");
}
// It seemed clearer to keep this logic separate from the general instance metadata collection, on the off chance
// that it might need to be extended later on.
static Account::AllowedContentType parse_version(QString instanceVer){
static Account::AllowedContentType parse_version(QString instanceVer)
{
using ContentType = Account::AllowedContentType;
int result = ContentType::PlainText;
unsigned int result = ContentType::PlainText;
if (instanceVer.contains ("glitch"))
result |= ContentType::Markdown | ContentType::Html;
return static_cast<ContentType>(result);
}
static Account::AllowedContentType parse_pleroma_info(QJsonDocument doc){
static QMap<QString, Account::AllowedContentType> str_to_content_type = {
{"text/plain", Account::AllowedContentType::PlainText},
{"text/bbcode", Account::AllowedContentType::BBCode},
{"text/html", Account::AllowedContentType::Html},
{"text/markdown", Account::AllowedContentType::Markdown}
};
static Account::AllowedContentType parse_pleroma_info(QJsonDocument doc)
{
using ContentType = Account::AllowedContentType;
int result = ContentType::PlainText;
unsigned int result = ContentType::PlainText;
auto obj = doc.object();
if(obj.contains("metadata")){
for(auto c : obj["metadata"].toObject()["postFormats"].toArray()){
if ("text/bbcode"==c.toString()) {
result|=ContentType::BBCode;
} else if ("text/html"==c.toString()) {
result |= ContentType::Html;
} else if ("text/markdown" == c.toString()){
result |= ContentType::Markdown;
}
if(obj.contains ("metadata"))
{
auto metadata = obj["metadata"].toObject ();
if (! metadata.contains ("postFormats"))
return static_cast<ContentType> (result);
auto formats = metadata["postFormats"].toArray ();
for(auto c : formats)
{
auto fmt = c.toString ();
if (! str_to_content_type.contains (fmt))
continue;
result |= (unsigned int) str_to_content_type[fmt];
}
}
return static_cast<ContentType>(result);
return static_cast<ContentType> (result);
}
void Account::fetchInstanceMetadata()
{
QUrl instance_url = apiUrl("/api/v1/instance");
QUrl pleroma_info = apiUrl("/nodeinfo/2.1.json");
get(instance_url, false, [=] (QNetworkReply *reply) {
if (200 != reply->attribute(QNetworkRequest::HttpStatusCodeAttribute))
return;
auto data = reply->readAll();
auto doc = QJsonDocument::fromJson(data);
auto obj = doc.object();
if (! doc.isObject())
if (! doc.isObject ())
return;
if (obj.contains("max_toot_chars"))
m_max_post_length = (unsigned) obj["max_toot_chars"].toInt();
auto obj = doc.object();
//One can only hope that there will always be a version attached
if (obj.contains("version")) {
m_allowed_content_types = parse_version(obj["version"].toString());
}
if (obj.contains ("max_toot_chars"))
m_max_post_length = (unsigned) obj["max_toot_chars"].toInt ();
m_instance_name = obj["title"].toString();
// One can only hope that there will always be a version attached
if (obj.contains ("version"))
m_allowed_content_types = parse_version(obj["version"].toString ());
emit fetchedInstanceMetadata();
m_instance_name = obj["title"].toString ();
emit fetchedInstanceMetadata ();
});
get(pleroma_info, false, [=] (QNetworkReply *reply){
if (200 == reply->attribute( QNetworkRequest::HttpStatusCodeAttribute )) {
m_allowed_content_types = parse_pleroma_info (QJsonDocument::fromJson (reply->readAll()));
}
get(pleroma_info, false, [=] (QNetworkReply *reply) {
auto data = reply->readAll ();
auto doc = QJsonDocument::fromJson (data);
m_allowed_content_types = parse_pleroma_info (doc);
emit fetchedInstanceMetadata ();
});
}
......
......@@ -104,14 +104,14 @@ public:
// Types of formatting that we may use is determined primarily by the server metadata, this is a simple enough
// way to determine what formats are accepted.
enum AllowedContentType{
PlainText= 1 << 0,
enum AllowedContentType {
PlainText = 1 << 0,
Markdown = 1 << 1,
Html = 1 << 2,
BBCode = 1 << 3
};
AllowedContentType allowedContentTypes () const{
AllowedContentType allowedContentTypes () const {
return m_allowed_content_types;
}
......
......@@ -64,10 +64,11 @@ ComposeForm::ComposeForm(Michabo::AccountManager *manager, QWidget *parent)
QObject::connect(m_content, &QTextEdit::textChanged, this, &ComposeForm::updateScratch);
QObject::connect(m_content_type, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &ComposeForm::updateContentType);
m_bottom_box->addWidget(m_post_length_counter, 1, Qt::AlignRight);
m_bottom_box->addWidget(m_button_box);
if (m_content_type->count() > 1)
m_bottom_box->addWidget(m_content_type);
m_bottom_box->addWidget(m_post_length_counter, 1, Qt::AlignRight);
m_bottom_box->addWidget(m_button_box);
m_layout->addLayout(m_bottom_box);
}
......@@ -85,7 +86,9 @@ void ComposeForm::updateScratch()
m_scratch->m_subject = m_subject->text();
m_scratch->m_content_type = m_content_type->currentData().toString();
}
void ComposeForm::updateContentType(){
void ComposeForm::updateContentType()
{
m_scratch->m_content_type = m_content_type->currentData().toString();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment