Skip to content

Commit

Permalink
Add journey sessions and participations
Browse files Browse the repository at this point in the history
  • Loading branch information
hulloitskai committed Dec 12, 2023
1 parent f2f48e5 commit 808d7db
Show file tree
Hide file tree
Showing 41 changed files with 3,276 additions and 149 deletions.
2 changes: 1 addition & 1 deletion app/channels/graphql_channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ def execute(data)
extensions = prepare_extensions(params["extensions"])
context = {
channel: self,
cookies:,
extensions:,
current_user:,
actor_id: cookies.signed[:actor_id],
}
result = Schema.execute(query, variables:, operation_name:, context:)

Expand Down
2 changes: 1 addition & 1 deletion app/concerns/queryable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def query(name, variables = {})
context = { current_user: }
if is_a?(ActionController::Base)
context[:controller] = self
context[:actor_id] = cookies.signed[:actor_id]
context[:cookies] = cookies
end
variables = variables.transform_keys do |key|
if key.is_a?(Symbol)
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ def error_context
end
end

sig { returns(String) }
def actor_id
cookies.signed[:actor_id] or raise "Missing actor ID"
end

# == Filter Handlers
# sig { void }
# def debug_action
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/graphql_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def execute
extensions = prepare_extensions(params[:extensions])
context = {
controller: self,
cookies:,
extensions:,
current_user:,
actor_id: cookies.signed[:actor_id],
}
result = Schema.execute(query, variables:, operation_name:, context:)

Expand Down
25 changes: 25 additions & 0 deletions app/controllers/journey/application_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# typed: strict
# frozen_string_literal: true

module Journey
class ApplicationController < ::ApplicationController
# == Filters
before_action :set_participant_id

private

# == Helpers
sig { returns(String) }
def participant_id
cookies.signed[:journey_participant_id] or
raise "Missing journey participant ID"
end

# == Filter Callbacks
sig { void }
def set_participant_id
return if cookies.key?(:journey_participant_id)
cookies.permanent.signed[:journey_participant_id] = SecureRandom.uuid
end
end
end
24 changes: 21 additions & 3 deletions app/controllers/journey/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,29 @@

module Journey
class HomeController < ApplicationController
# == Filters
before_action :set_active_session

# == Actions
def show
render(inertia: "JourneyHomePage", props: {
transcription_url: journey_transcriptions_path,
})
if (session = @active_session)
redirect_to(journey_session_path(session))
else
render(inertia: "JourneyHomePage")
end
end

private

# == Filter Handlers
sig { void }
def set_active_session
@active_session = T.let(
Session.active.find_by(
participations: SessionParticipation.where(participant_id:),
),
T.nilable(Session),
)
end
end
end
94 changes: 94 additions & 0 deletions app/controllers/journey/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# typed: true
# frozen_string_literal: true

module Journey
class SessionsController < ApplicationController
# == Filters
before_action :set_session_and_participation, only: :show

# == Actions
def show
session = @session or raise "Missing session"
if @participation
data = query!("JourneySessionPageQuery", {
session_id: session.to_gid.to_s,
})
render(inertia: "JourneySessionPage", props: {
data:,
})
else
redirect_to(
journey_root_path,
alert: "You are not a participant in this session.",
)
end
end

def create
session = T.let(Session.joinable.first || Session.new, Session)
goal = transcribe_goal_recording
session.participations.build(
participant_id:,
goal:,
**participation_params,
)
session.save!
redirect_to(journey_session_path(session))
rescue => error
redirect_to(
journey_root_path,
alert: "Failed to start session: #{error.message}",
)
end

private

# == Helpers
sig { returns(ActionController::Parameters) }
def participation_params
params.require(:participation).permit(:participant_name)
end

sig { returns(OpenAI::Client) }
def client
@client ||= T.let(OpenAI::Client.new, T.nilable(OpenAI::Client))
end

sig { returns(ActionDispatch::Http::UploadedFile) }
def goal_recording
params.fetch(:goal_recording)
end

sig { returns(String) }
def transcribe_goal_recording
recording = goal_recording
Tempfile.open(["recording", ".webm"], binmode: true) do |file|
file.write(recording.read)
file.flush
file.seek(0)
movie = FFMPEG::Movie.new(file.path)
movie.transcode(file.path, { audio_codec: "webm" })
response = client.audio.translate(
parameters: {
model: "whisper-1",
file:,
},
)
response.fetch("text")
end
end

# == Filter Handlers
sig { void }
def set_session_and_participation
@session = T.let(
Session.friendly.find(params.fetch(:id)),
T.nilable(Session),
)
@participation = T.let(
SessionParticipation.find_by(session: @session, participant_id:),
T.nilable(SessionParticipation),
)
end
end
end
36 changes: 0 additions & 36 deletions app/controllers/journey/transcriptions_controller.rb

This file was deleted.

21 changes: 16 additions & 5 deletions app/graphql/concerns/resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ def flash
controller&.flash || ActionDispatch::Flash::FlashHash.new
end

sig { returns(String) }
def actor_id
context[:actor_id] or raise "Missing actor ID"
end

sig { returns(T.nilable(T.any(User, Symbol))) }
def current_user
context[:current_user]
Expand All @@ -70,4 +65,20 @@ def system_user?
def current_user!
active_user or raise GraphQL::ExecutionError, "Not authenticated."
end

sig { returns(ActionDispatch::Cookies::CookieJar) }
def cookies
context.fetch(:cookies)
end

sig { returns(String) }
def actor_id
cookies.signed[:actor_id] or raise "Missing actor ID"
end

sig { returns(String) }
def journey_participant_id
cookies.signed[:journey_participant_id] or
raise "Missing journey participant ID"
end
end
24 changes: 24 additions & 0 deletions app/graphql/queries/journey_session.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# typed: strict
# frozen_string_literal: true

module Queries
class JourneySession < BaseQuery
include AllowsFailedLoads

# == Type
type Types::JourneySessionType, null: true

# == Arguments
argument :id, ID, loads: Types::JourneySessionType, as: :session

# == Resolver
sig do
params(session: T.nilable(::Journey::Session))
.returns(T.nilable(::Journey::Session))
end
def resolve(session:)
return unless session
session if allowed_to?(:show?, session)
end
end
end
2 changes: 1 addition & 1 deletion app/graphql/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Schema < GraphQL::Schema
end
def self.resolve_type(abstract_type, object, context)
if object.is_a?(ApplicationRecord)
name = object.model_name.to_s
name = object.model_name.to_s.split("::").join("")
type = "::Types::#{name}Type".safe_constantize
type or raise "Unexpected record type: #{name}"
end
Expand Down
22 changes: 22 additions & 0 deletions app/graphql/subscriptions/journey_session_participation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# typed: strict
# frozen_string_literal: true

module Subscriptions
class JourneySessionParticipation < BaseSubscription
# == Configuration
broadcastable false

# == Type
type Types::JourneySessionParticipationType, null: true

# == Arguments
argument :session_id, ID, loads: Types::JourneySessionType

# == Callback Handlers
sig do
params(session: ::Journey::Session)
.returns(T.nilable(::Journey::SessionParticipation))
end
def subscribe(session:) = nil
end
end
25 changes: 25 additions & 0 deletions app/graphql/types/journey_session_participation_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# typed: strict
# frozen_string_literal: true

module Types
class JourneySessionParticipationType < BaseObject
# == Interfaces
implements NodeType

# == Fields
field :goal, String, null: false
field :participant_is_viewer, Boolean, null: false
field :participant_name, String, null: false
field :session, [JourneySessionType], null: false

# == Resolvers
sig { returns(T::Boolean) }
def participant_is_viewer
journey_participant_id == object.participant_id
end

# == Helpers
sig { override.returns(::Journey::SessionParticipation) }
def object = super
end
end
24 changes: 24 additions & 0 deletions app/graphql/types/journey_session_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# typed: strict
# frozen_string_literal: true

module Types
class JourneySessionType < BaseObject
# == Interfaces
implements NodeType

# == Fields
field :participations, [JourneySessionParticipationType], null: false
field :started_at, DateTimeType, null: false, method: :created_at
field :url, String, null: false

# == Resolvers
sig { returns(String) }
def url
journey_session_url(object)
end

# == Helpers
sig { override.returns(::Journey::Session) }
def object = super
end
end
1 change: 1 addition & 0 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class QueryType < BaseObject
field :journal_entry, resolver: Queries::JournalEntry
field :journal_entry_comments, resolver: Queries::JournalEntryComments

field :journey_session, resolver: Queries::JourneySession
field :location_access_grants, resolver: Queries::LocationAccessGrants
field :pensieve_messages, resolver: Queries::PensieveMessages
end
Expand Down
2 changes: 2 additions & 0 deletions app/graphql/types/subscription_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class SubscriptionType < BaseObject
# == Subscriptions
field :activity_status, subscription: Subscriptions::ActivityStatus
field :currently_playing, subscription: Subscriptions::CurrentlyPlaying
field :journey_session_participation,
subscription: Subscriptions::JourneySessionParticipation
field :location, subscription: Subscriptions::Location
field :pensieve_message, subscription: Subscriptions::PensieveMessage
field :test_subscription, subscription: Subscriptions::TestSubscription
Expand Down
Loading

0 comments on commit 808d7db

Please sign in to comment.