Skip to main content

Overview

The Ctx (Context) model is the central state management structure in Stremio Core. It maintains the user’s profile, library, streams, notifications, and other essential application data. All state updates flow through this model using the Update pattern.

Structure

Fields

profile
Profile
required
User profile containing authentication, settings, and installed addons
library
LibraryBucket
User’s library of saved content items (movies, series, etc.)
notifications
NotificationsBucket
required
Notifications for new episodes and content updates
streams
StreamsBucket
Cached stream data and playback history
streaming_server_urls
ServerUrlsBucket
URLs for streaming server endpoints
search_history
SearchHistoryBucket
User’s search history data
dismissed_events
DismissedEventsBucket
Events that have been dismissed by the user
status
CtxStatus
required
Current status of the context (Loading or Ready)
trakt_addon
Option<DescriptorLoadable>
Optional Trakt addon descriptor for syncing watch history
notification_catalogs
Vec<ResourceLoadable<Vec<MetaItem>>>
Catalogs from addons that support the lastVideosIds resource for notifications
events
Events
required
Modal and notification events

CtxStatus

The context can be in one of two states:
pub enum CtxStatus {
    Loading(AuthRequest),
    Ready,
}
  • Loading: Context is authenticating with the provided AuthRequest
  • Ready: Context is ready for operations

Constructor

new()

Creates a new Ctx instance with the provided buckets.
pub fn new(
    profile: Profile,
    library: LibraryBucket,
    streams: StreamsBucket,
    streaming_server_urls: ServerUrlsBucket,
    notifications: NotificationsBucket,
    search_history: SearchHistoryBucket,
    dismissed_events: DismissedEventsBucket,
) -> Self

Update Implementation

The Ctx implements the Update<E> trait to handle state changes through messages.

Supported Messages

Initiates authentication with the provided AuthRequest. Sets status to Loading and triggers authentication flow.Source: src/models/ctx/ctx.rs:113
Logs out the current user. Triggers Internal::Logout message.Source: src/models/ctx/ctx.rs:117
Handles logout process:
  • Deletes remote session if not already deleted
  • Resets profile, library, streams, and all buckets
  • Updates status to Ready
  • Emits Event::UserLoggedOut
Source: src/models/ctx/ctx.rs:120
Processes authentication result:
  • Updates profile with auth credentials
  • Loads user’s addons and library items
  • Handles locked addons or missing library errors
  • Emits Event::UserAuthenticated on success
Source: src/models/ctx/ctx.rs:169
Delegates to sub-update functions:
  • update_profile - Profile changes
  • update_library - Library changes
  • update_streams - Stream changes
  • update_streaming_server_urls - Server URL changes
  • update_trakt_addon - Trakt addon changes
  • update_notifications - Notification changes
  • update_search_history - Search history changes
  • update_events - Event changes
Source: src/models/ctx/ctx.rs:269

Authentication Flow

1

Initiate Authentication

User calls Action::Ctx::Authenticate(auth_request) with credentials
2

Loading State

Context status changes to CtxStatus::Loading(auth_request)
3

API Requests

Concurrent requests are made to:
  • Authenticate user and get auth key
  • Fetch user’s addon collection
  • Fetch user’s library items from datastore
4

Process Results

On success:
  • Profile is updated with auth credentials
  • Addons are installed
  • Library items are loaded
  • Status changes to Ready
  • Event::UserAuthenticated is emitted

Events

The Ctx model emits the following events:
  • Event::UserLoggedOut { uid } - User has logged out
  • Event::UserAuthenticated { auth_request } - User successfully authenticated
  • Event::UserAddonsLocked { addons_locked } - User’s addons are locked/unlocked
  • Event::UserLibraryMissing { library_missing } - User’s library is missing
  • Event::SessionDeleted { auth_key } - Remote session was deleted
  • Event::Error - Various error events for locked addons or missing library

Error Handling

Authentication can fail with:
  • Invalid credentials
  • Network errors
  • Locked addon collection
  • Missing library data
The model emits appropriate error events but continues operating. Check Event::Error for authentication failures.

Usage Example

use stremio_core::models::ctx::Ctx;
use stremio_core::runtime::Update;
use stremio_core::runtime::msg::{Msg, Action, ActionCtx};
use stremio_core::types::api::AuthRequest;

// Create a new context
let mut ctx = Ctx::new(
    profile,
    library,
    streams,
    streaming_server_urls,
    notifications,
    search_history,
    dismissed_events,
);

// Authenticate
let auth_request = AuthRequest::Login {
    email: "[email protected]".to_string(),
    password: "password".to_string(),
};

let effects = ctx.update(&Msg::Action(Action::Ctx(ActionCtx::Authenticate(auth_request))));

See Also

Build docs developers (and LLMs) love