Commit 7f215fdb authored by kaniini's avatar kaniini

Merge branch 'content-type' into 'master'

Add content-type support

See merge request !10
parents 1ccd4f6d 2bf6daa3
......@@ -261,7 +261,6 @@ void Account::buildFromSettings(QSettings &settings)
m_client_secret = settings.value("client_secret").toString();
m_name = settings.value("name").toString();
m_instance_uri = settings.value("instance_uri").toString();
validateToken();
}
......@@ -305,6 +304,7 @@ void Account::fetchTimeline(QString &original_name, QString &from_id)
});
}
void Account::postStatus(std::shared_ptr<Post> p)
{
QUrl post_status_url = apiUrl("/api/v1/statuses");
......@@ -367,11 +367,37 @@ 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){
using ContentType = Account::AllowedContentType;
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){
using ContentType = Account::AllowedContentType;
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;
}
}
}
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) {
auto data = reply->readAll();
auto doc = QJsonDocument::fromJson(data);
......@@ -383,10 +409,20 @@ void Account::fetchInstanceMetadata()
if (obj.contains("max_toot_chars"))
m_max_post_length = (unsigned) obj["max_toot_chars"].toInt();
//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());
}
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()));
}
});
}
void Account::invalidate()
......
......@@ -102,6 +102,19 @@ public:
// post refresh
void invalidatePost(Post *p);
// 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,
Markdown = 1 << 1,
Html = 1 << 2,
BBCode = 1 << 3
};
AllowedContentType allowedContentTypes () const{
return m_allowed_content_types;
}
signals:
void authenticated();
void registered();
......@@ -121,6 +134,7 @@ private:
size_t m_max_post_length;
QString m_instance_name;
Identity m_identity;
AllowedContentType m_allowed_content_types;
QMap<QString, std::shared_ptr<Identity>> m_identity_cache;
......
......@@ -12,6 +12,7 @@ ComposeForm::ComposeForm(Michabo::AccountManager *manager, QWidget *parent)
m_post_length_counter(new QLabel(this)),
m_bottom_box(new QHBoxLayout()),
m_layout(new QVBoxLayout(this)),
m_content_type(new QComboBox(this)),
m_reply_to(nullptr),
m_scratch(std::make_shared<Michabo::Post>(m_account))
{
......@@ -44,13 +45,29 @@ ComposeForm::ComposeForm(Michabo::AccountManager *manager, QWidget *parent)
m_button_box->addButton(m_send_button, QDialogButtonBox::AcceptRole);
m_button_box->addButton(m_cancel_button, QDialogButtonBox::RejectRole);
// My apologies to those who prefer bitsets or a number of booleans to represent
// this logic.
using ContentType = Michabo::Account::AllowedContentType;
auto allowed_content_types = m_account->allowedContentTypes();
if (allowed_content_types & ContentType::PlainText)
m_content_type->addItem (tr("Plain text"), "text/plain");
if (allowed_content_types & ContentType::BBCode)
m_content_type->addItem (tr("BBCode"), "text/bbcode");
if (allowed_content_types & ContentType::Html)
m_content_type->addItem (tr("HTML"), "text/html");
if (allowed_content_types & ContentType::Markdown)
m_content_type->addItem (tr("Markdown"), "text/markdown");
m_content_type->setCurrentIndex (0);
m_post_length_counter->setText(QString::number(m_account->max_post_length()));
QObject::connect(m_subject, &QLineEdit::textChanged, this, &ComposeForm::updateScratch);
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_layout->addLayout(m_bottom_box);
}
......@@ -66,6 +83,10 @@ void ComposeForm::updateScratch()
m_scratch->m_content = m_content->toPlainText();
m_scratch->m_subject = m_subject->text();
m_scratch->m_content_type = m_content_type->currentData().toString();
}
void ComposeForm::updateContentType(){
m_scratch->m_content_type = m_content_type->currentData().toString();
}
void ComposeForm::setReplyTo(std::shared_ptr<Michabo::Post> p)
......
......@@ -10,6 +10,7 @@
#include <QHBoxLayout>
#include <QLabel>
#include <QTextBlock>
#include <QComboBox>
#include <memory>
#include "accountmanager.h"
......@@ -26,7 +27,7 @@ signals:
public slots:
void updateScratch();
void updateContentType();
private:
Michabo::Account *m_account;
QLineEdit *m_subject;
......@@ -37,6 +38,8 @@ private:
QLabel *m_post_length_counter;
QHBoxLayout *m_bottom_box;
QVBoxLayout *m_layout;
QString m_visibility;
QComboBox *m_content_type;
std::shared_ptr<Michabo::Post> m_reply_to;
std::shared_ptr<Michabo::Post> m_scratch;
};
......
......@@ -173,6 +173,7 @@ QJsonDocument Post::toJsonDocument() const
obj["spoiler_text"] = m_subject;
obj["status"] = m_content;
obj["content_type"] = m_content_type;
obj["sensitive"] = m_is_sensitive;
obj["visibility"] = vis_to_str[m_visibility];
......
......@@ -79,6 +79,7 @@ struct Post
QString m_subject;
QString m_author;
QString m_reply_to_author;
QString m_content_type;
QList<std::shared_ptr<Attachment>> m_attachments;
Visibility m_visibility;
QStringList m_mentions;
......
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