pub struct ObjectId<Kind>(/* private fields */)
where
    Kind: Object,
    for<'de2> <Kind as Object>::Kind: Deserialize<'de2>;
Expand description

Typed wrapper for Activitypub Object ID which helps with dereferencing and caching.

It provides convenient methods for fetching the object from remote server or local database. Objects are automatically cached locally, so they don’t have to be fetched every time. Much of the crate functionality relies on this wrapper.

Every time an object is fetched via HTTP, [RequestData.request_counter] is incremented by one. If the value exceeds [FederationSettings.http_fetch_limit], the request is aborted with Error::RequestLimit. This prevents denial of service attacks where an attack triggers infinite, recursive fetching of data.

let config = FederationConfig::builder()
    .domain("example.com")
    .app_data(db_connection)
    .build().await?;
let request_data = config.to_request_data();
let object_id = ObjectId::<DbUser>::parse("https://lemmy.ml/u/nutomic")?;
// Attempt to fetch object from local database or fall back to remote server
let user = object_id.dereference(&request_data).await;
assert!(user.is_ok());
// Now you can also read the object from local database without network requests
let user = object_id.dereference_local(&request_data).await;
assert!(user.is_ok());

Implementations§

source§

impl<Kind> ObjectId<Kind>
where Kind: Object + Send + Debug + 'static, for<'de2> <Kind as Object>::Kind: Deserialize<'de2>,

source

pub fn parse(url: &str) -> Result<Self, ParseError>

Construct a new objectid instance

source

pub fn inner(&self) -> &Url

Returns a reference to the wrapped URL value

source

pub fn into_inner(self) -> Url

Returns the wrapped URL value

source

pub async fn dereference( &self, data: &Data<<Kind as Object>::DataType> ) -> Result<Kind, <Kind as Object>::Error>
where <Kind as Object>::Error: From<Error>,

Fetches an activitypub object, either from local database (if possible), or over http.

source

pub async fn dereference_forced( &self, data: &Data<<Kind as Object>::DataType> ) -> Result<Kind, <Kind as Object>::Error>
where <Kind as Object>::Error: From<Error>,

If this is a remote object, fetch it from origin instance unconditionally to get the latest version, regardless of refresh interval.

source

pub async fn dereference_local( &self, data: &Data<<Kind as Object>::DataType> ) -> Result<Kind, <Kind as Object>::Error>
where <Kind as Object>::Error: From<Error>,

Fetch an object from the local db. Instead of falling back to http, this throws an error if the object is not found in the database.

source

pub fn is_local(&self, data: &Data<<Kind as Object>::DataType>) -> bool

Returns true if the object’s domain matches the one defined in [[FederationConfig.domain]].

Trait Implementations§

source§

impl<Kind> Clone for ObjectId<Kind>
where Kind: Object, for<'de2> <Kind as Object>::Kind: Deserialize<'de2>,

Need to implement clone manually, to avoid requiring Kind to be Clone

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<Kind> Debug for ObjectId<Kind>
where Kind: Object, for<'de2> <Kind as Object>::Kind: Deserialize<'de2>,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de, Kind> Deserialize<'de> for ObjectId<Kind>
where Kind: Object, for<'de2> <Kind as Object>::Kind: Deserialize<'de2>,

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<Kind> Display for ObjectId<Kind>
where Kind: Object, for<'de2> <Kind as Object>::Kind: Deserialize<'de2>,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<Kind> From<ObjectId<Kind>> for Url
where Kind: Object, for<'de2> <Kind as Object>::Kind: Deserialize<'de2>,

source§

fn from(id: ObjectId<Kind>) -> Self

Converts to this type from the input type.
source§

impl<Kind> From<Url> for ObjectId<Kind>
where Kind: Object + Send + 'static, for<'de2> <Kind as Object>::Kind: Deserialize<'de2>,

source§

fn from(url: Url) -> Self

Converts to this type from the input type.
source§

impl<T> FromStr for ObjectId<T>
where T: Object + Send + Debug + 'static, for<'de2> <T as Object>::Kind: Deserialize<'de2>,

§

type Err = ParseError

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl<Kind> PartialEq for ObjectId<Kind>
where Kind: Object, for<'de2> <Kind as Object>::Kind: Deserialize<'de2>,

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<Kind> Serialize for ObjectId<Kind>
where Kind: Object, for<'de2> <Kind as Object>::Kind: Deserialize<'de2>,

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<Kind> Freeze for ObjectId<Kind>
where <Kind as Object>::Kind: for<'de2> Sized,

§

impl<Kind> RefUnwindSafe for ObjectId<Kind>
where <Kind as Object>::Kind: for<'de2> Sized, Kind: RefUnwindSafe,

§

impl<Kind> Send for ObjectId<Kind>
where <Kind as Object>::Kind: for<'de2> Sized, Kind: Send,

§

impl<Kind> Sync for ObjectId<Kind>
where <Kind as Object>::Kind: for<'de2> Sized, Kind: Sync,

§

impl<Kind> Unpin for ObjectId<Kind>
where <Kind as Object>::Kind: for<'de2> Sized, Kind: Unpin,

§

impl<Kind> UnwindSafe for ObjectId<Kind>
where <Kind as Object>::Kind: for<'de2> Sized, Kind: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> DynClone for T
where T: Clone,

source§

fn __clone_box(&self, _: Private) -> *mut ()

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> FromRef<T> for T
where T: Clone,

source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,