Skip to main content

GroupService

Struct GroupService 

Source
pub struct GroupService {
    backend: Arc<dyn GroupBackend>,
}
Expand description

Service for group management operations.

Encapsulates business logic for groups, members, and invites. Uses a GroupBackend for persistence.

Fields§

§backend: Arc<dyn GroupBackend>

Implementations§

Source§

impl GroupService

Source

pub fn new(backend: Arc<dyn GroupBackend>) -> Self

Creates a new group service with the given backend.

Source

pub fn try_default() -> Result<Self>

Creates a new group service with a default SQLite backend.

Uses the user’s data directory for storage.

§Errors

Returns an error if the backend cannot be initialized.

Source

pub fn create_group( &self, org_id: &str, name: &str, description: &str, creator_email: &str, ) -> Result<Group>

Creates a new group in the organization.

The creator is automatically added as an admin.

§Arguments
  • org_id - Organization identifier
  • name - Group name (must be unique within org)
  • description - Optional description
  • creator_email - Email of the group creator
§Errors

Returns an error if:

  • A group with the same name already exists
  • Storage cannot be accessed
Source

pub fn get_group(&self, group_id: &GroupId) -> Result<Option<Group>>

Gets a group by ID.

§Errors

Returns an error if storage cannot be accessed.

Source

pub fn get_group_by_name( &self, org_id: &str, name: &str, ) -> Result<Option<Group>>

Gets a group by name within an organization.

§Errors

Returns an error if storage cannot be accessed.

Source

pub fn list_groups(&self, org_id: &str) -> Result<Vec<Group>>

Lists all groups in an organization.

§Errors

Returns an error if storage cannot be accessed.

Source

pub fn delete_group( &self, group_id: &GroupId, requester_email: &str, ) -> Result<bool>

Deletes a group and all its members and invites.

Only admins can delete groups.

§Arguments
  • group_id - The group to delete
  • requester_email - Email of the user requesting deletion
§Errors

Returns an error if:

  • The requester is not an admin
  • Storage cannot be accessed
Source

pub fn add_member( &self, group_id: &GroupId, email: &str, role: GroupRole, requester_email: &str, ) -> Result<GroupMember>

Adds a member to a group.

If the member already exists, updates their role. Only admins can add members.

§Arguments
  • group_id - The group to add to
  • email - Email of the new member
  • role - Role to assign
  • requester_email - Email of the user adding the member
§Errors

Returns an error if:

  • The requester is not an admin
  • The group doesn’t exist
  • Storage cannot be accessed
Source

pub fn get_member( &self, group_id: &GroupId, email: &str, ) -> Result<Option<GroupMember>>

Gets a member’s record in a group.

§Errors

Returns an error if storage cannot be accessed.

Source

pub fn update_member_role( &self, group_id: &GroupId, email: &str, new_role: GroupRole, requester_email: &str, ) -> Result<bool>

Updates a member’s role in a group.

Only admins can update roles. Cannot demote the last admin.

§Arguments
  • group_id - The group
  • email - The member’s email
  • new_role - The new role to assign
  • requester_email - Email of the user updating the role
§Errors

Returns an error if:

  • The requester is not an admin
  • Demoting the last admin
  • Storage cannot be accessed
Source

pub fn remove_member( &self, group_id: &GroupId, email: &str, requester_email: &str, ) -> Result<bool>

Removes a member from a group.

Only admins can remove members. Cannot remove the last admin.

§Arguments
  • group_id - The group
  • email - The member’s email
  • requester_email - Email of the user removing the member
§Errors

Returns an error if:

  • The requester is not an admin
  • Removing the last admin
  • Storage cannot be accessed
Source

pub fn list_members(&self, group_id: &GroupId) -> Result<Vec<GroupMember>>

Lists all members of a group.

§Errors

Returns an error if storage cannot be accessed.

Source

pub fn get_user_groups( &self, org_id: &str, email: &str, ) -> Result<Vec<GroupMembership>>

Gets all groups a user is a member of.

§Errors

Returns an error if storage cannot be accessed.

Source

pub fn leave_group(&self, group_id: &GroupId, email: &str) -> Result<bool>

Allows a user to leave a group.

Cannot leave if the user is the last admin.

§Arguments
  • group_id - The group to leave
  • email - Email of the user leaving
§Errors

Returns an error if:

  • The user is the last admin
  • Storage cannot be accessed
Source

pub fn create_invite( &self, group_id: &GroupId, role: GroupRole, creator_email: &str, expires_in_secs: Option<u64>, max_uses: Option<u32>, ) -> Result<(GroupInvite, String)>

Creates an invite for a group.

Only admins can create invites.

§Arguments
  • group_id - The group to invite to
  • role - Role to assign when joined
  • creator_email - Email of the admin creating the invite
  • expires_in_secs - How long until expiration (default: 7 days)
  • max_uses - Maximum number of uses (default: unlimited)
§Returns

A tuple of (invite, plaintext_token). The token should be shared with invitees and never stored.

§Errors

Returns an error if:

  • The creator is not an admin
  • Storage cannot be accessed
Source

pub fn join_via_invite(&self, token: &str, email: &str) -> Result<GroupMember>

Joins a group using an invite token.

Validates the token and adds the user as a member with the invite’s role.

§Arguments
  • token - The plaintext invite token
  • email - Email of the user joining
§Returns

The created member record.

§Errors

Returns an error if:

  • The token is invalid or expired
  • Storage cannot be accessed
Source

pub fn list_invites( &self, group_id: &GroupId, include_expired: bool, ) -> Result<Vec<GroupInvite>>

Lists all invites for a group.

§Arguments
  • group_id - The group
  • include_expired - Whether to include expired/revoked invites
§Errors

Returns an error if storage cannot be accessed.

Source

pub fn revoke_invite( &self, invite_id: &str, requester_email: &str, ) -> Result<bool>

Revokes an invite.

Only admins can revoke invites.

§Arguments
  • invite_id - The invite to revoke
  • requester_email - Email of the admin revoking
§Errors

Returns an error if:

  • The requester is not an admin
  • Storage cannot be accessed
Source

pub fn cleanup_expired_invites(&self) -> Result<u64>

Cleans up expired invites.

§Returns

Number of invites deleted.

§Errors

Returns an error if storage cannot be accessed.

Source

fn require_admin(&self, group_id: &GroupId, email: &str) -> Result<()>

Checks if a user has admin role in a group.

Source

pub fn require_role( &self, group_id: &GroupId, email: &str, min_role: GroupRole, ) -> Result<()>

Checks if a user has at least the specified role in a group.

Role hierarchy: Admin > Write > Read

§Errors

Returns an error if:

  • The user is not a member of the group
  • The user’s role is insufficient
Source

pub fn is_member(&self, group_id: &GroupId, email: &str) -> Result<bool>

Checks if a user is a member of a group.

§Errors

Returns an error if storage cannot be accessed.

Source

pub fn get_user_role( &self, group_id: &GroupId, email: &str, ) -> Result<Option<GroupRole>>

Gets a user’s role in a group, if they are a member.

§Errors

Returns an error if storage cannot be accessed.

Auto Trait Implementations§

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FutureExt for T

§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
§

impl<T> Instrument for T

§

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

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

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> IntoRequest<T> for T

§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

impl<L> LayerExt<L> for L

§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in [Layered].
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

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>,

Source§

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.
§

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

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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