Archived
1
Fork 0
This repository has been archived on 2025-04-27. You can view files and clone it, but cannot push or open issues or pull requests.
sukai/src/entities.rs

202 lines
5 KiB
Rust
Raw Normal View History

2024-12-08 13:15:54 -05:00
// SPDX-FileCopyrightText: 2024 Joshua Goins <josh@redstrate.com>
// SPDX-License-Identifier: GPL-3.0-or-later
use crate::serde_utilities::parse_datetime;
use crate::serde_utilities::parse_url;
use cxx_qt_lib::{QDateTime, QString, QUrl};
use serde::{Deserialize, Serialize};
#[cxx_qt::bridge]
pub mod entities {
#[namespace = "rust::cxxqtlib1"]
unsafe extern "C++" {
include!("cxx-qt-lib/common.h");
#[rust_name = "make_author_qml"]
fn make_unique() -> UniquePtr<AuthorQML>; // TODO: lol
#[rust_name = "make_account_qml"]
fn make_unique() -> UniquePtr<AccountQML>; // TODO: lol
}
unsafe extern "C++" {
include!("cxx-qt-lib/qstring.h");
type QString = cxx_qt_lib::QString;
include!("cxx-qt-lib/qurl.h");
type QUrl = cxx_qt_lib::QUrl;
include!("cxx-qt-lib/qvariant.h");
type QVariant = cxx_qt_lib::QVariant;
include!("entities.h");
#[rust_name = "qvariant_can_convert_account"]
fn qvariantCanConvertAccount(value: &QVariant) -> bool;
}
// TODO: I don't think this needs to be a qml_element
unsafe extern "RustQt" {
#[qobject]
#[qml_element = "Author"]
#[qml_uncreatable]
#[qproperty(QString, did)]
#[qproperty(QString, handle)]
#[qproperty(QString, display_name, cxx_name = "displayName")]
#[qproperty(QUrl, avatar)]
type AuthorQML = super::Author;
#[qobject]
#[qml_element = "Account"]
#[qml_uncreatable]
type AccountQML = super::Account;
}
#[namespace = "rust::cxxqtlib1::qvariant"]
unsafe extern "C++" {
include!("cxx-qt-lib/qvariant.h");
#[rust_name = "qvariant_construct_account"]
unsafe fn qvariantConstruct(value: &*mut AuthorQML) -> QVariant;
#[rust_name = "qvariant_value_or_default_account"]
fn qvariantValueOrDefault(value: &QVariant) -> *mut AuthorQML;
}
}
#[derive(Debug, Clone, Default)]
pub enum AccountStatus {
#[default]
NotLoaded,
Loaded,
}
#[derive(Debug, Default, Clone)] // TODO: remove default?
pub struct Account {
pub status: AccountStatus,
pub identifier: QString,
pub access_jwt: QString,
pub refresh_jwt: QString,
}
#[derive(Deserialize, Debug, Default, Clone)] // TODO: remove default?
pub struct Author {
pub did: QString,
handle: QString,
#[serde(rename = "displayName")]
display_name: QString,
#[serde(default)]
#[serde(deserialize_with = "parse_url")]
avatar: QUrl,
labels: Vec<Label>,
#[serde(rename = "createdAt")]
#[serde(deserialize_with = "parse_datetime")]
created_at: QDateTime,
}
#[derive(Deserialize, Debug)]
pub struct Record {
#[serde(rename = "$type")]
r#type: QString,
#[serde(rename = "createdAt")]
#[serde(deserialize_with = "parse_datetime")]
pub(crate) created_at: QDateTime,
langs: Vec<QString>,
pub text: QString,
}
#[derive(Deserialize, Debug, Clone)]
pub struct Label {
src: QString,
#[serde(deserialize_with = "parse_url")]
uri: QUrl,
cid: QString,
val: QString,
#[serde(deserialize_with = "parse_datetime")]
cts: QDateTime,
}
#[derive(Deserialize, Debug, Clone)]
pub struct EmbedImage {
#[serde(deserialize_with = "parse_url")]
pub(crate) thumb: QUrl,
#[serde(deserialize_with = "parse_url")]
#[serde(rename = "fullsize")]
full_size: QUrl,
alt: QString,
}
#[derive(Deserialize, Debug, Clone)]
#[serde(tag = "$type")]
pub enum Embed {
#[serde(rename = "app.bsky.embed.images#view")]
Image { images: Vec<EmbedImage> },
#[serde(other)]
Unknown,
}
#[derive(Deserialize, Debug)]
pub struct Post {
pub uri: QString,
cid: QString,
#[serde(rename = "indexedAt")]
#[serde(deserialize_with = "parse_datetime")]
indexed_at: QDateTime,
pub author: Author,
pub record: Record,
#[serde(rename = "replyCount")]
pub reply_count: i32,
#[serde(rename = "repostCount")]
pub repost_count: i32,
#[serde(rename = "likeCount")]
pub like_count: i32,
#[serde(rename = "quoteCount")]
pub quote_count: i32,
pub labels: Vec<Label>,
pub embed: Option<Embed>,
}
#[derive(Deserialize, Debug)]
pub struct FeedItem {
pub post: Post,
}
#[derive(Deserialize, Debug)]
pub struct Feed {
pub feed: Vec<FeedItem>,
}
#[derive(Deserialize, Debug)]
pub struct Thread {
pub post: Post,
//pub parent: Option<Thread>,
pub replies: Vec<Post>,
}
#[derive(Deserialize, Debug)]
pub struct ThreadResponse {
pub thread: Thread,
}
#[derive(Serialize, Debug)]
pub struct CreateSessionRequest {
pub identifier: QString,
pub password: QString,
}
#[derive(Deserialize, Debug)]
pub struct ErrorResponse {
pub error: QString,
pub message: QString,
}
#[derive(Deserialize, Debug)]
pub struct CreateSessionResponse {
#[serde(rename = "accessJwt")]
pub access_jwt: QString,
#[serde(rename = "refreshJwt")]
pub refresh_jwt: QString,
pub handle: QString,
pub did: QString,
}