mirror of
https://github.com/twonlyapp/twonly-app.git
synced 2026-09-01 08:04:07 +00:00
101 lines
3.3 KiB
Rust
101 lines
3.3 KiB
Rust
/*
|
|
* Copyright (c) 2026, Tobias Müller git@tsmr.eu
|
|
*
|
|
*/
|
|
|
|
use super::Server;
|
|
use crate::api::{proto::client_to_server, ApiRuntime};
|
|
use crate::context::Context;
|
|
use crate::error::Result;
|
|
use prost::Message as _;
|
|
use std::sync::Arc;
|
|
|
|
impl Server {
|
|
fn application_request(
|
|
application: client_to_server::application_data::ApplicationData,
|
|
) -> Vec<u8> {
|
|
client_to_server::ClientToServer {
|
|
v: Some(client_to_server::client_to_server::V::V0(
|
|
client_to_server::V0 {
|
|
seq: 0,
|
|
kind: Some(client_to_server::v0::Kind::Applicationdata(
|
|
client_to_server::ApplicationData {
|
|
application_data: Some(application),
|
|
},
|
|
)),
|
|
},
|
|
)),
|
|
}
|
|
.encode_to_vec()
|
|
}
|
|
|
|
fn handshake_request(handshake: client_to_server::handshake::Handshake) -> Vec<u8> {
|
|
client_to_server::ClientToServer {
|
|
v: Some(client_to_server::client_to_server::V::V0(
|
|
client_to_server::V0 {
|
|
seq: 0,
|
|
kind: Some(client_to_server::v0::Kind::Handshake(
|
|
client_to_server::Handshake {
|
|
handshake: Some(handshake),
|
|
},
|
|
)),
|
|
},
|
|
)),
|
|
}
|
|
.encode_to_vec()
|
|
}
|
|
|
|
pub(super) async fn application(
|
|
ctx: &Arc<Context>,
|
|
value: client_to_server::application_data::ApplicationData,
|
|
) -> Result<Vec<u8>> {
|
|
ApiRuntime::request_authenticated(ctx, Self::application_request(value), None).await
|
|
}
|
|
|
|
pub(super) async fn application_for_contact(
|
|
ctx: &Arc<Context>,
|
|
value: client_to_server::application_data::ApplicationData,
|
|
contact_id: i64,
|
|
) -> Result<Vec<u8>> {
|
|
ApiRuntime::request_authenticated(ctx, Self::application_request(value), Some(contact_id))
|
|
.await
|
|
}
|
|
|
|
pub(super) async fn durable_application(
|
|
ctx: &Arc<Context>,
|
|
value: client_to_server::application_data::ApplicationData,
|
|
kind: &'static str,
|
|
) -> Result<Vec<u8>> {
|
|
ApiRuntime::request_durable_authenticated(ctx, Self::application_request(value), kind).await
|
|
}
|
|
|
|
pub(super) async fn handshake(
|
|
ctx: &Arc<Context>,
|
|
value: client_to_server::handshake::Handshake,
|
|
) -> Result<Vec<u8>> {
|
|
let client = ApiRuntime::client(ctx).await?;
|
|
if client
|
|
.is_authenticated
|
|
.load(std::sync::atomic::Ordering::Acquire)
|
|
{
|
|
client.close().await;
|
|
}
|
|
ApiRuntime::request_binary(ctx, Self::handshake_request(value)).await
|
|
}
|
|
}
|
|
|
|
impl Server {
|
|
/// Asks the server to (re)start a mailbox drain. It answers immediately;
|
|
/// the messages themselves arrive as unsolicited `PendingMessagesV2`
|
|
/// batches, so this must not be treated as a delivery round trip.
|
|
pub(crate) async fn request_pending_messages(ctx: &Arc<Context>) -> Result<()> {
|
|
Self::application(
|
|
ctx,
|
|
client_to_server::application_data::ApplicationData::RequestPendingMessages(
|
|
client_to_server::application_data::RequestPendingMessages {},
|
|
),
|
|
)
|
|
.await
|
|
.map(|_| ())
|
|
}
|
|
}
|