Skip to content

Commit

Permalink
Merge pull request #426 from hellostealth/feature/3.0-mountable-sessi…
Browse files Browse the repository at this point in the history
…on-locals

Added ability to pass locals to step_to and update_session_to
  • Loading branch information
matthewblack authored Oct 18, 2024
2 parents 78fa4ad + e350a6b commit 3b96aed
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
15 changes: 8 additions & 7 deletions app/controllers/stealth/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def step_to_at(timestamp, session: nil, flow: nil, state: nil, slug: nil)
Stealth::Logger.l(topic: "session", message: "User #{current_session_id}: scheduled session step to #{flow}->#{state} at #{timestamp.iso8601}")
end

def step_to(session: nil, flow: nil, state: nil, slug: nil, pos: nil)
def step_to(session: nil, flow: nil, state: nil, slug: nil, pos: nil, locals: nil)
if interrupt_detected?
run_interrupt_action
return :interrupted
Expand All @@ -150,10 +150,10 @@ def step_to(session: nil, flow: nil, state: nil, slug: nil, pos: nil)
state: state,
slug: slug
)
step(flow: flow, state: state, pos: pos)
step(flow: flow, state: state, pos: pos, locals: locals)
end

def update_session_to(session: nil, flow: nil, state: nil, slug: nil)
def update_session_to(session: nil, flow: nil, state: nil, slug: nil, locals: nil)
if interrupt_detected?
run_interrupt_action
return :interrupted
Expand All @@ -166,7 +166,7 @@ def update_session_to(session: nil, flow: nil, state: nil, slug: nil)
slug: slug
)

update_session(flow: flow, state: state)
update_session(flow: flow, state: state, locals: locals)
end

def set_back_to(session: nil, flow: nil, state: nil, slug: nil)
Expand Down Expand Up @@ -211,11 +211,12 @@ def halt!

private

def update_session(flow:, state:)
def update_session(flow:, state:, locals: nil)
@progressed = :updated_session
@current_session = Session.new(id: current_session_id)

unless current_session.flow_string == flow.to_s && current_session.state_string == state.to_s
@current_session.locals = locals
@current_session.set_session(new_flow: flow, new_state: state)
end

Expand All @@ -229,8 +230,8 @@ def store_back_to_session(flow:, state:)
back_to_session.set_session(new_flow: flow, new_state: state)
end

def step(flow:, state:, pos: nil)
update_session(flow: flow, state: state)
def step(flow:, state:, pos: nil, locals: nil)
update_session(flow: flow, state: state, locals: locals)
Stealth.trigger_flow(flow, state, @current_message)

@progressed = :stepped
Expand Down
30 changes: 25 additions & 5 deletions lib/stealth/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Session
SLUG_SEPARATOR = '->'

attr_reader :flow, :state, :id, :type
attr_accessor :session
attr_accessor :session, :locals

# Session types:
# - :primary
Expand All @@ -27,6 +27,7 @@ def initialize(id: nil, type: :primary)
)
end

load_previous_locals
get_session
end

Expand Down Expand Up @@ -66,6 +67,23 @@ def state_string
session&.split(SLUG_SEPARATOR)&.last
end

def load_previous_locals
return if primary_session?

@locals = get_key(previous_locals_key)

if @locals.present? && @locals.is_a?(String)
begin
@locals = JSON.parse(@locals)
rescue JSON::ParserError => e
Stealth::Logger.l(
topic: "session",
message: "User #{id}: failed to parse locals from Redis -> #{@locals}, error: #{e.message}"
)
end
end
end

def get_session
@session ||= get_key(session_key)
end
Expand Down Expand Up @@ -175,6 +193,10 @@ def previous_session_key
[id, 'previous'].join('-')
end

def previous_locals_key
[id, 'previous', 'locals'].join('-')
end

def back_to_key
[id, 'back_to'].join('-')
end
Expand All @@ -193,10 +215,8 @@ def store_current_to_previous(existing_session:)
message: "User #{id}: setting to #{existing_session}"
)

persist_key(
key: previous_session_key,
value: existing_session
)
persist_key(key: previous_session_key, value: existing_session)
persist_key(key: previous_locals_key, value: @locals.to_json)
end
end

Expand Down

0 comments on commit 3b96aed

Please sign in to comment.