Skip to content

Instantly share code, notes, and snippets.

@LukeClancy
Last active September 12, 2024 00:19
Show Gist options
  • Select an option

  • Save LukeClancy/e583cbc40d89fa5ea8af5402fe0bc922 to your computer and use it in GitHub Desktop.

Select an option

Save LukeClancy/e583cbc40d89fa5ea8af5402fe0bc922 to your computer and use it in GitHub Desktop.
#Bear with me since I haven't touched this in forever. Partial implementation of the otterly polling backend.
#PollPush is a data table which stores the queue string along with the push data.
#The queue string is retrieved via stream_string_for (which also validates they are allowed) and looks
#like: "conversation:9384". This is then matched along with the created_at time to decide what updates they push to
#the user. trip pushes to a JSON list that will be returned, tripped returns the list. The poll action is
#extremely simple, authenticates through the decryption. It returns items from the pull_push table with the right
#queue string, that are in the store's time frame. once the time frame is up, it asks to re-authorize.
class PollsController < ApplicationController
include TripwireApi
skip_before_action :set_current_common_static_information, only: [:poll]
skip_before_action :set_device_variant, only: [:poll]
skip_before_action :grab_metrics, only: [:poll]
skip_after_action :shutdown, only: [:poll]
def bad_store?(store)
if store['created_at'] <= 2.days.ago
return true
else
return false
end
end
def dec(stuff)
return nil if stuff.nil?
JSON.parse(Record.decrypt(stuff))
end
def enc(stuff)
Record.encrypt(stuff.to_json)
end
#to unsubscribe, just resubscribe but without the channeles you are subscribing from.
def subscribe
p = params.to_unsafe_h.slice("otty-store", "tab-id", "conversation-id", 'queues')
store = dec(p['otty-store'])
store ||= {}
store['queues'] = []
store['attempted_queues'] = p['queues']
ca = channel_attributes
for q in p['queues']
klz = q.constantize
ss = stream_string_for(klz, ca: ca)
store['queues'] << ss unless ss.nil?
end
store['last_update'] = DateTime.now.to_datetime
store['created_at'] = store['last_update']
#update the tripwire-enc-store with the appropriate information
if store['queues'].count == 0
trip returning: 'no_queues'
else
trip returning: enc(store)
end
tripped
end
def poll
p = params.to_unsafe_h.slice("otty-store")
store = dec(p['otty-store'])
#check the store is not defective
if store.nil? or bad_store?(store)
trip returning: 'should_resub'
if Rails.env.development?
if store.nil?
m = 'nil store'
else
m = 'bad store'
end
trip(log: m)
end
tripped
return
end
updates = PollPush.get_updates(store['queues'], store['last_update'].to_datetime)
if updates.any?
store['last_update'] = updates.last.created_at.to_datetime #postgresql ordered it
Rails.logger.info(store['last_update'])
@tripwires = updates.map{|a|a.data} #queue should still be a string,
z = enc(store)
trip returning: z
else
Rails.logger.info(store['last_update'])
trip returning: 'no_updates'
end
tripped
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment