forked from mirror/nheko
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
2.5 KiB
73 lines
2.5 KiB
8 years ago
|
/*
|
||
|
* nheko Copyright (C) 2017 Konstantinos Sideris <siderisk@auth.gr>
|
||
|
*
|
||
|
* This program is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU General Public License as published by
|
||
|
* the Free Software Foundation, either version 3 of the License, or
|
||
|
* (at your option) any later version.
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License
|
||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*/
|
||
|
|
||
7 years ago
|
#include <QBuffer>
|
||
7 years ago
|
#include <memory>
|
||
7 years ago
|
|
||
8 years ago
|
#include "AvatarProvider.h"
|
||
7 years ago
|
#include "Cache.h"
|
||
6 years ago
|
#include "Logging.h"
|
||
7 years ago
|
#include "MatrixClient.h"
|
||
7 years ago
|
|
||
7 years ago
|
namespace AvatarProvider {
|
||
|
|
||
7 years ago
|
void
|
||
7 years ago
|
resolve(const QString &room_id, const QString &user_id, QObject *receiver, AvatarCallback callback)
|
||
8 years ago
|
{
|
||
7 years ago
|
const auto key = QString("%1 %2").arg(room_id).arg(user_id);
|
||
|
const auto avatarUrl = Cache::avatarUrl(room_id, user_id);
|
||
7 years ago
|
|
||
7 years ago
|
if (!Cache::AvatarUrls.contains(key) || !cache::client())
|
||
7 years ago
|
return;
|
||
8 years ago
|
|
||
7 years ago
|
if (avatarUrl.isEmpty())
|
||
|
return;
|
||
8 years ago
|
|
||
7 years ago
|
auto data = cache::client()->image(avatarUrl);
|
||
7 years ago
|
if (!data.isNull()) {
|
||
|
callback(QImage::fromData(data));
|
||
|
return;
|
||
7 years ago
|
}
|
||
8 years ago
|
|
||
7 years ago
|
auto proxy = std::make_shared<AvatarProxy>();
|
||
|
QObject::connect(proxy.get(),
|
||
|
&AvatarProxy::avatarDownloaded,
|
||
|
receiver,
|
||
|
[callback](const QByteArray &data) { callback(QImage::fromData(data)); });
|
||
8 years ago
|
|
||
7 years ago
|
mtx::http::ThumbOpts opts;
|
||
|
opts.mxc_url = avatarUrl.toStdString();
|
||
8 years ago
|
|
||
6 years ago
|
http::client()->get_thumbnail(
|
||
7 years ago
|
opts,
|
||
|
[opts, proxy = std::move(proxy)](const std::string &res, mtx::http::RequestErr err) {
|
||
|
if (err) {
|
||
7 years ago
|
nhlog::net()->warn("failed to download avatar: {} - ({} {})",
|
||
|
opts.mxc_url,
|
||
|
mtx::errors::to_string(err->matrix_error.errcode),
|
||
|
err->matrix_error.error);
|
||
7 years ago
|
return;
|
||
|
}
|
||
7 years ago
|
|
||
7 years ago
|
cache::client()->saveImage(opts.mxc_url, res);
|
||
|
|
||
|
auto data = QByteArray(res.data(), res.size());
|
||
|
emit proxy->avatarDownloaded(data);
|
||
|
});
|
||
|
}
|
||
8 years ago
|
}
|