2026-11-01 18:35:23 +01:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0-only
|
|
|
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
2026-10-31 20:50:57 +01:00
|
|
|
|
|
|
|
|
module Nostro:Account_impl;
|
|
|
|
|
import :Account;
|
|
|
|
|
import :File;
|
|
|
|
|
import std;
|
2026-11-01 18:35:23 +01:00
|
|
|
import Crafter.DB;
|
2026-10-31 20:50:57 +01:00
|
|
|
|
|
|
|
|
using namespace Nostro;
|
2026-11-01 18:35:23 +01:00
|
|
|
using namespace Crafter;
|
2026-10-31 20:50:57 +01:00
|
|
|
namespace fs = std::filesystem;
|
|
|
|
|
|
2026-11-01 18:35:23 +01:00
|
|
|
// Transaction is the on-disk schema of the tables below. These fail when it changes, as a reminder to bump
|
|
|
|
|
// Transaction::version; update them together with it.
|
|
|
|
|
static_assert(DB::Layout<Transaction>::rowSize == 120);
|
|
|
|
|
static_assert(DB::Layout<Transaction>::fingerprint == 0x1ceea246dfcdf4f0);
|
|
|
|
|
// Version 1, which existing tables migrate from. It must never change.
|
|
|
|
|
static_assert(DB::Layout<Transaction::Previous>::rowSize == 88);
|
|
|
|
|
static_assert(DB::Layout<Transaction::Previous>::fingerprint == 0x6b478e471876761f);
|
2026-10-31 20:50:57 +01:00
|
|
|
|
2026-11-01 18:35:23 +01:00
|
|
|
namespace {
|
|
|
|
|
// An account name is the file name of its table: one with a '/' in it would be a path.
|
|
|
|
|
std::string FileName(std::string_view account) {
|
|
|
|
|
std::string name(account);
|
|
|
|
|
std::ranges::replace(name, '/', '_');
|
|
|
|
|
return std::format("{}.cdb", name);
|
2026-10-31 20:50:57 +01:00
|
|
|
}
|
|
|
|
|
|
2026-11-01 18:35:23 +01:00
|
|
|
// Each table is opened on first use and kept open for the rest of the process, or until its connector is
|
|
|
|
|
// forgotten: an open maps the whole file, and a second writable Table on the same file would throw DB::LockedError.
|
|
|
|
|
std::map<fs::path, DB::Table<Transaction>>& Tables() {
|
|
|
|
|
static std::map<fs::path, DB::Table<Transaction>> tables;
|
|
|
|
|
return tables;
|
2026-10-31 20:50:57 +01:00
|
|
|
}
|
|
|
|
|
|
2026-11-01 18:35:23 +01:00
|
|
|
DB::Table<Transaction>& GetTable(std::string_view connector, std::string_view account) {
|
|
|
|
|
const fs::path path = cachePath / "transactions" / connector / FileName(account);
|
|
|
|
|
return Tables().try_emplace(path, path).first->second;
|
2026-10-31 20:50:57 +01:00
|
|
|
}
|
|
|
|
|
|
2026-11-01 18:35:23 +01:00
|
|
|
// ASCII case folding: enough for names and descriptions, IBANs are upper case anyway.
|
|
|
|
|
char Lower(char c) {
|
|
|
|
|
return c >= 'A' && c <= 'Z' ? static_cast<char>(c + ('a' - 'A')) : c;
|
2026-10-31 20:50:57 +01:00
|
|
|
}
|
|
|
|
|
|
2026-11-01 18:35:23 +01:00
|
|
|
// `search` is lower case already.
|
|
|
|
|
bool Contains(std::string_view text, std::string_view search) {
|
|
|
|
|
return !std::ranges::search(text, search, {}, Lower).empty();
|
2026-10-31 20:50:57 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-01 18:35:23 +01:00
|
|
|
std::chrono::system_clock::time_point Account::GetLastTransactionTime(std::string_view connector) const {
|
|
|
|
|
const DB::Table<Transaction>& table = GetTable(connector, name);
|
|
|
|
|
if(table.Empty()) {
|
|
|
|
|
return std::chrono::system_clock::time_point::min();
|
2026-10-31 20:50:57 +01:00
|
|
|
}
|
2026-11-01 18:35:23 +01:00
|
|
|
return table.Back()[&Transaction::timePoint];
|
|
|
|
|
}
|
2026-10-31 20:50:57 +01:00
|
|
|
|
2026-11-01 18:35:23 +01:00
|
|
|
void Account::SaveTransactions(std::string_view connector, std::vector<Transaction> transactions) {
|
|
|
|
|
DB::Table<Transaction>& table = GetTable(connector, name);
|
|
|
|
|
const std::chrono::system_clock::time_point last = GetLastTransactionTime(connector);
|
2026-10-31 20:50:57 +01:00
|
|
|
|
2026-11-01 18:35:23 +01:00
|
|
|
// The ids stored at the last timestamp: a fetch that includes that timestamp again brings them back. A row there
|
|
|
|
|
// without an id (stored by version 1) cannot be matched, so then nothing at that timestamp is taken as new.
|
|
|
|
|
std::vector<std::string> known;
|
|
|
|
|
bool unmatchable = false;
|
|
|
|
|
if(!table.Empty()) {
|
|
|
|
|
for(DB::RowId id = table.LowerBound<&Transaction::timePoint>(last); id < table.SlotCount(); id++) {
|
|
|
|
|
const DB::Row<Transaction> row = table[id];
|
|
|
|
|
if(!row.Erased()) {
|
|
|
|
|
const std::string_view stored = row[&Transaction::id];
|
|
|
|
|
unmatchable = unmatchable || stored.empty();
|
|
|
|
|
known.emplace_back(stored);
|
|
|
|
|
}
|
2026-10-31 20:50:57 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-01 18:35:23 +01:00
|
|
|
// The table is ordered by timePoint and refuses a row older than its last one. A row at the last timestamp is
|
|
|
|
|
// new when its id is.
|
|
|
|
|
std::ranges::stable_sort(transactions, {}, &Transaction::timePoint);
|
|
|
|
|
std::erase_if(transactions, [last, unmatchable, &known](const Transaction& transaction) {
|
|
|
|
|
return transaction.timePoint < last || (transaction.timePoint == last && (unmatchable || transaction.id.empty() || std::ranges::contains(known, transaction.id)));
|
|
|
|
|
});
|
2026-10-31 20:50:57 +01:00
|
|
|
|
feat(app): add the Nostro app, a window for phones and desktops
Linux phones have no banking apps, and a command line is no answer on a
phone. The app does everything the nostro command does and, like it, only
talks to nostrod: it holds no key and no token.
- Nostro.Ui, a toolkit whose screens are functions that fill a frame, ported
from imsd-messages ca5cd72d; Nostro.App, the app's state, forms and screens;
Nostro.Ui.Shell, which puts them on a Wayland window with Crafter.Graphics;
nostro-app; and Nostro.DebugCommands for the --debug-api build.
- Client: every method also in an asynchronous form for an sd-event loop,
with DaemonMissing and OutcomeUnknown.
- nostrod's Flatpak is net.catcrafts.Nostrod now and the app takes
net.catcrafts.Nostro; install.sh moves the state. The app's Flatpak, both
Flatpaks cross-built for aarch64, and the licence texts they carry.
- nostro-fake-system, a made-up bus and bank with a real nostrod on them, to
look at the app against. The README's screenshots are taken on it.
- The nostro command: an API key's environment is the key's own to say, so
--sandbox is for bunq-oauth alone, and a virtual card is listed as VIRTUAL.
The published contract changes in three places, before any release (design
rule 10):
- Transactions' query takes `stored`: answered from what nostrod has stored,
without asking the bank. The app searches with it; before, every search was
a call to bunq.
- AddBunq's `environment` no longer chooses over what the key says of itself:
a key of the other environment is refused.
- SetCardStatus refuses to freeze or unfreeze a virtual card. bunq reports one
as DEACTIVATED however well it pays, and leaves it as it is when asked,
without an error.
The app is GPL-3.0-only, the library stays LGPL-3.0-only, and no code moved
from one to the other (design rule 13). Transaction's schema is unchanged.
Verified: the 17 tests pass with every line, function and region of what they
link run, and the five lint runs are clean. Every screen was looked at on the
made-up system, at a phone's and a desktop's sizes in the light and the dark
look, on Plasma under Wayland. What is said of bunq's cards comes from its
sandbox, with throwaway users, and from one real virtual card. Known to have
been used against production: connecting, and reading accounts, payments and
cards. Not known to have been tried: a payment from the app against bunq,
OAuth against bunq, and anything on a phone.
Building the app takes --graphics=, a Crafter.Graphics checkout on its branch
claude/nostro-app, until that branch is merged and pinned here.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 12:27:14 +01:00
|
|
|
// Nothing new is what most syncs bring, and a flush syncs the file to disk whether anything was written or not.
|
|
|
|
|
if(transactions.empty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-11-01 18:35:23 +01:00
|
|
|
table.Append(std::span<const Transaction>(transactions));
|
|
|
|
|
table.Flush();
|
2026-10-31 20:50:57 +01:00
|
|
|
}
|
|
|
|
|
|
2026-11-01 18:35:23 +01:00
|
|
|
void Account::ForgetTransactions(std::string_view connector) {
|
|
|
|
|
const fs::path directory = cachePath / "transactions" / connector;
|
|
|
|
|
std::erase_if(Tables(), [&directory](const std::pair<const fs::path, DB::Table<Transaction>>& table) {
|
|
|
|
|
return std::mismatch(directory.begin(), directory.end(), table.first.begin(), table.first.end()).first == directory.end();
|
|
|
|
|
});
|
|
|
|
|
fs::remove_all(directory);
|
2026-10-31 20:50:57 +01:00
|
|
|
}
|
|
|
|
|
|
2026-11-01 18:35:23 +01:00
|
|
|
std::vector<Transaction> Account::FindTransactions(std::string_view connector, const TransactionQuery& query) const {
|
|
|
|
|
const DB::Table<Transaction>& table = GetTable(connector, name);
|
|
|
|
|
const std::string search = query.search | std::views::transform(Lower) | std::ranges::to<std::string>();
|
|
|
|
|
|
|
|
|
|
// Walk the time range backwards from the newest row and stop at the limit, so a search that matches often
|
|
|
|
|
// returns early; only a rare or failed one reads the whole range.
|
|
|
|
|
const DB::RowId first = table.LowerBound<&Transaction::timePoint>(query.from);
|
|
|
|
|
std::vector<Transaction> rows;
|
|
|
|
|
for(DB::RowId id = table.UpperBound<&Transaction::timePoint>(query.to); id > first && rows.size() < query.limit; id--) {
|
|
|
|
|
const DB::Row<Transaction> row = table[id - 1];
|
|
|
|
|
if(row.Erased() || (query.amount && row[&Transaction::amount] != *query.amount)) {
|
|
|
|
|
continue;
|
2026-10-31 20:50:57 +01:00
|
|
|
}
|
2026-11-01 18:35:23 +01:00
|
|
|
if(search.empty() || Contains(row[&Transaction::name], search) || Contains(row[&Transaction::description], search) || Contains(row[&Transaction::iban], search)) {
|
|
|
|
|
rows.push_back(row.Materialize());
|
2026-10-31 20:50:57 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-11-01 18:35:23 +01:00
|
|
|
return rows;
|
|
|
|
|
}
|