Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
QString extractUserName(const QString& path) {
const auto parts = path.split("/", QString::SkipEmptyParts);
if (path.startsWith("/@")) {
return parts.first();
}
if (path.startsWith("/users")) {
return parts.last();
}
return QString();
}
QUrl generateWebFingerUrlFromUrl(const QUrl& url) {
const auto domain = QString("%1://%2").arg(url.scheme()).arg(url.host());
const auto userName = extractUserName(url.path());
if (!userName.isEmpty()) {
return QUrl(QString("%1/.well-known/webfinger?resource=acct:%2@%3").arg(domain).arg(userName).arg(url.host()));
}
return QUrl();
}
WebFinger extractWebFingerFromData(const QByteArray& data) {
const QJsonDocument respJson(QJsonDocument::fromBinaryData(data));
WebFinger webFinger;
webFinger.subject = respJson.object().find("subject").value().toString();
webFinger.aliases = respJson.object().find("aliases").value().toVariant().toStringList();
return webFinger;
}
// TODO: Fetch JSON from server.
// TODO: Extract into body.
void WebFinger::discoverByUrl(const QUrl& url, function<void(WebFinger)> func) {
const QUrl webFingerUrl = generateWebFingerUrlFromUrl(url);
if (!url.isEmpty()) {
QSharedPointer<QNetworkAccessManager> manager(new QNetworkAccessManager());
QObject::connect(manager.get(), &QNetworkAccessManager::finished, QApplication::instance(), [=](QNetworkReply* reply){
const WebFinger webFinger = extractWebFingerFromData(reply->readAll());
func(webFinger);
});
QNetworkRequest request(webFingerUrl);
request.setRawHeader("Accept", "application/ld+json");
request.setHeader(QNetworkRequest::UserAgentHeader, "ActivityDesk/0.0.1 (https://activitydesk.black.af)");
manager->get(request);
} else {
WebFinger webFinger;
func(webFinger);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.