Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

savedFeedsPrefV2に対応 #296

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 48 additions & 13 deletions app/qtquick/feedgenerator/feedgeneratorlistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "atprotocol/app/bsky/actor/appbskyactorgetpreferences.h"
#include "atprotocol/app/bsky/unspecced/appbskyunspeccedgetpopularfeedgenerators.h"
#include "atprotocol/app/bsky/actor/appbskyactorputpreferences.h"
#include "tools/tid.h"

#include <QJsonDocument>
#include <QJsonObject>
Expand Down Expand Up @@ -229,13 +230,22 @@ void FeedGeneratorListModel::getSavedGenerators()
AppBskyActorGetPreferences *pref = new AppBskyActorGetPreferences(this);
connect(pref, &AppBskyActorGetPreferences::finished, [=](bool success) {
if (success) {
for (const auto &feed : pref->preferences().savedFeedsPref) {
m_savedUriList.append(feed.saved);
for (const auto &prefs : pref->preferences().savedFeedsPrefV2) {
for (const auto &item : prefs.items) {
if (item.type == "feed") {
m_savedUriList.append(item.value);
}
}
}
for (int i = 0; i < m_cidList.count(); i++) {
emit dataChanged(index(i), index(i),
QVector<int>() << static_cast<int>(SavingRole));
for (const auto &feed : pref->preferences().savedFeedsPref) {
for (const auto &uri : feed.saved) {
if (!m_savedUriList.contains(uri)) {
m_savedUriList.append(uri);
}
}
}
emit dataChanged(index(0), index(m_cidList.count() - 1),
QVector<int>() << static_cast<int>(SavingRole));
} else {
emit errorOccured(pref->errorCode(), pref->errorMessage());
}
Expand Down Expand Up @@ -281,14 +291,27 @@ QJsonArray FeedGeneratorListModel::appendGeneratorToPreference(const QString &sr
if (!preferences.toArray().at(i).isObject())
continue;
QJsonObject value = preferences.toArray().takeAt(i).toObject();
if (value.value("$type") == QStringLiteral("app.bsky.actor.defs#savedFeedsPref")
&& value.value("saved").isArray()) {
QJsonArray json_saved = value.value("saved").toArray();
if (!json_saved.contains(QJsonValue(uri))) {
if (value.value("$type") == QStringLiteral("app.bsky.actor.defs#savedFeedsPrefV2")
&& value.value("items").isArray()) {
QJsonArray json_items = value.value("items").toArray();
QJsonArray json_items_dest;
bool exist = false;
for (const auto &v : qAsConst(json_items)) {
if (v.toObject().value("value").toString() == uri) {
exist = true;
}
json_items_dest.append(v);
}
if (!exist) {
// 含まれていなければ追加
json_saved.append(QJsonValue(uri));
value.insert("saved", json_saved);
QJsonObject json_item;
json_item.insert("id", Tid::next());
json_item.insert("pinned", false);
json_item.insert("type", "feed");
json_item.insert("value", uri);
json_items_dest.append(json_item);
}
value.insert("items", json_items_dest);
}
dest_preferences.append(value);
}
Expand All @@ -312,8 +335,20 @@ QJsonArray FeedGeneratorListModel::removeGeneratorToPreference(const QString &sr
if (!preferences.toArray().at(i).isObject())
continue;
QJsonObject value = preferences.toArray().takeAt(i).toObject();
if (value.value("$type") == QStringLiteral("app.bsky.actor.defs#savedFeedsPref")
&& value.value("saved").isArray()) {
if (value.value("$type") == QStringLiteral("app.bsky.actor.defs#savedFeedsPrefV2")
&& value.value("items").isArray()) {
QJsonArray json_items = value.value("items").toArray();
QJsonArray json_items_dest;
for (const auto &v : qAsConst(json_items)) {
if (v.toObject().value("value").toString() != uri) {
json_items_dest.append(v);
}
}
value.insert("items", json_items_dest);

} else if (value.value("$type") == QStringLiteral("app.bsky.actor.defs#savedFeedsPref")
&& value.value("saved").isArray()) {
// 互換性維持で読み込み側はv1とv2のORなので削除も両方から実施
QJsonArray json_saved;
for (const auto &value : value.value("saved").toArray()) {
if (value.toString() != uri) {
Expand Down
6 changes: 3 additions & 3 deletions app/qtquick/timeline/customfeedlistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ void CustomFeedListModel::updateFeedSaveStatus()
connect(pref, &AppBskyActorGetPreferences::finished, [=](bool success) {
if (success) {
bool exist = false;
for (const auto &feed : pref->preferences().savedFeedsPref) {
for (const auto &saved : feed.saved) {
if (saved == uri()) {
for (const auto &prefs : pref->preferences().savedFeedsPrefV2) {
for (const auto &item : prefs.items) {
if (item.type == "feed" && item.value == uri()) {
exist = true;
break;
}
Expand Down
6 changes: 4 additions & 2 deletions lib/lib.pro
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ SOURCES += \
$$PWD/tools/leb128.cpp \
$$PWD/tools/listitemscache.cpp \
$$PWD/tools/opengraphprotocol.cpp \
$$PWD/tools/pinnedpostcache.cpp
$$PWD/tools/pinnedpostcache.cpp \
tools/tid.cpp

HEADERS += \
$$PWD/atprotocol/accessatprotocol.h \
Expand Down Expand Up @@ -259,7 +260,8 @@ HEADERS += \
$$PWD/tools/opengraphprotocol.h \
$$PWD/tools/pinnedpostcache.h \
$$PWD/tools/qstringex.h \
common.h
common.h \
tools/tid.h

RESOURCES += \
$$PWD/lib.qrc
12 changes: 12 additions & 0 deletions lib/tools/base32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,15 @@ QString Base32::encode(const QByteArray &data, const bool with_padding)

return result;
}

QString Base32::encode_s(qint64 num)
{
static const char alphabet[] = "234567abcdefghijklmnopqrstuvwxyz";
QString result;

for (int i = 0; i < 13; i++) {
result.insert(0, QChar(alphabet[num & 0x1F]));
num >>= 5;
}
return result;
}
1 change: 1 addition & 0 deletions lib/tools/base32.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Base32
{
public:
static QString encode(const QByteArray &data, const bool with_padding = false);
static QString encode_s(qint64 num);
};

#endif // BASE32_H
26 changes: 26 additions & 0 deletions lib/tools/tid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "tid.h"
#include "base32.h"
#include <QDateTime>
#include <QRandomGenerator>
#include <QDebug>

QString Tid::next()
{
static qint64 prev_time = 0;
static qint64 prev = 0;
qint64 current_time = QDateTime::currentMSecsSinceEpoch();
if (current_time <= prev_time) {
current_time = prev_time + 1;
}
prev_time = current_time;

qint64 clock_id = QRandomGenerator::global()->bounded(0, 1024);
qint64 id = (((current_time * 1000) << 10) & 0x7FFFFFFFFFFFFC00) | (clock_id & 0x3FF);

if (prev == id) {
prev_time = current_time++;
id = (((current_time * 1000) << 10) & 0x7FFFFFFFFFFFFC00) | (clock_id & 0x3FF);
}
prev = id;
return Base32::encode_s(id);
}
12 changes: 12 additions & 0 deletions lib/tools/tid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef TID_H
#define TID_H

#include <QString>

class Tid
{
public:
static QString next();
};

#endif // TID_H
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,35 @@
"at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.generator/bsky-team"
]
},
{
"$type": "app.bsky.actor.defs#savedFeedsPrefV2",
"items": [
{
"id": "3ksooxaaknk27",
"pinned": true,
"type": "timeline",
"value": "following"
},
{
"id": "3ksooxaaknm27",
"pinned": true,
"type": "feed",
"value": "at://did:plc:q6gjnaw2blty4crticxkmujt/app.bsky.feed.generator/cv:cat"
},
{
"id": "3ksooxaaknn27",
"pinned": true,
"type": "list",
"value": "at://did:plc:mqxsuw5b5rhpwo4lw6iwlid5/app.bsky.graph.list/3kflf2r3lwg2x"
},
{
"id": "3ksooaaaaaaaa",
"pinned": false,
"type": "feed",
"value": "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.generator/hot-classic"
}
]
},
{
"$type": "app.bsky.actor.defs#savedFeedsPref",
"pinned": [
Expand All @@ -20,7 +49,7 @@
],
"saved": [
"at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.generator/bsky-team",
"at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.generator/whats-hot"
"at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.generator/with-friends"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,41 @@
"at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.generator/bsky-team"
]
},
{
"$type": "app.bsky.actor.defs#savedFeedsPrefV2",
"items": [
{
"id": "3ksooxaaknk27",
"pinned": true,
"type": "timeline",
"value": "following"
},
{
"id": "3ksooxaaknm27",
"pinned": true,
"type": "feed",
"value": "at://did:plc:q6gjnaw2blty4crticxkmujt/app.bsky.feed.generator/cv:cat"
},
{
"id": "3ksooxaaknu27",
"pinned": false,
"type": "feed",
"value": "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.generator/whats-hot"
},
{
"id": "3ksooxaaknn27",
"pinned": true,
"type": "list",
"value": "at://did:plc:mqxsuw5b5rhpwo4lw6iwlid5/app.bsky.graph.list/3kflf2r3lwg2x"
},
{
"id": "3ksooaaaaaaaa",
"pinned": false,
"type": "feed",
"value": "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.generator/hot-classic"
}
]
},
{
"$type": "app.bsky.actor.defs#savedFeedsPref",
"pinned": [
Expand All @@ -21,8 +56,7 @@
"saved": [
"at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.generator/bsky-team",
"at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.generator/with-friends",
"at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.generator/whats-hot",
"at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.generator/hot-classic"
"at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.generator/whats-hot"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,41 @@
"at://did:plc:hiptcrt4k63szzz4ty3dhwcp/app.bsky.feed.generator/ja-images"
]
},
{
"$type": "app.bsky.actor.defs#savedFeedsPrefV2",
"items": [
{
"id": "3ksooxaaknk27",
"pinned": true,
"type": "timeline",
"value": "following"
},
{
"id": "3ksooxaaknm27",
"pinned": true,
"type": "feed",
"value": "at://did:plc:q6gjnaw2blty4crticxkmujt/app.bsky.feed.generator/cv:cat"
},
{
"id": "3ksooxaaknu27",
"pinned": false,
"type": "feed",
"value": "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.generator/whats-hot"
},
{
"id": "3ksooxaaknn27",
"pinned": true,
"type": "list",
"value": "at://did:plc:mqxsuw5b5rhpwo4lw6iwlid5/app.bsky.graph.list/3kflf2r3lwg2x"
},
{
"id": "3ksooaaaaaaaa",
"pinned": false,
"type": "feed",
"value": "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.generator/hot-classic"
}
]
},
{
"$type": "app.bsky.actor.defs#savedFeedsPref",
"saved": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,35 @@
"at://did:plc:hiptcrt4k63szzz4ty3dhwcp/app.bsky.feed.generator/ja-images"
]
},
{
"$type": "app.bsky.actor.defs#savedFeedsPrefV2",
"items": [
{
"id": "3ksooxaaknk27",
"pinned": true,
"type": "timeline",
"value": "following"
},
{
"id": "3ksooxaaknm27",
"pinned": true,
"type": "feed",
"value": "at://did:plc:q6gjnaw2blty4crticxkmujt/app.bsky.feed.generator/cv:cat"
},
{
"id": "3ksooxaaknu27",
"pinned": false,
"type": "feed",
"value": "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.generator/whats-hot"
},
{
"id": "3ksooxaaknn27",
"pinned": true,
"type": "list",
"value": "at://did:plc:mqxsuw5b5rhpwo4lw6iwlid5/app.bsky.graph.list/3kflf2r3lwg2x"
}
]
},
{
"$type": "app.bsky.actor.defs#savedFeedsPref",
"saved": [
Expand Down
Loading