Skip to content

Instantly share code, notes, and snippets.

@Bluebie
Created March 1, 2009 04:38
Show Gist options
  • Save Bluebie/72211 to your computer and use it in GitHub Desktop.
Save Bluebie/72211 to your computer and use it in GitHub Desktop.
class CreateTempRoom < R '/rooms/create-temporary'
def get
require 'json'
room_id = "--#{rand(2 ** 16 - 1).to_s(36)}" until !File.exist?("rooms/#{room_id}")
default_settings = {
:title => input.name || "Temporary Talkie Room",
:desc => input.desc || "",
:owners => [],
:type => 'democracy',
:banned => []
}
Dir.mkdir("rooms/#{room_id}")
Dir.mkdir("rooms/#{room_id}/active-members")
File.open("rooms/#{room_id}/active-users", 'w') { |f| f.write('[]') }
File.open("rooms/#{room_id}/message-counter", 'w') { |f| f.write('0') }
File.open("rooms/#{room_id}/settings", 'w') { |f| f.write(JSON.generate(default_settings)) }
File.chmod(0777, "rooms/#{room_id}", "rooms/#{room_id}/active-members")
File.chmod(0666, "rooms/#{room_id}/active-users",
"rooms/#{room_id}/message-counter",
"rooms/#{room_id}/settings")
send_message(room_id,
:type => 'text/x-talkie-action',
:from => talkie_user,
:body => "created this temporary room for #{input.for || 'unknown user'}."
)
# clear out old disused temp rooms
Thread.new do
require 'timeout'
Timeout.timeout(5) do
cutoff = Time.now - 60*60*24
Dir.foreach("rooms") do |room|
next unless room.match(/^--/)
next unless File.mtime("rooms/#{room}") < cutoff
FileUtils.rm_rf("rooms/#{room}")
end
end
Thread.exit
end
headers['Content-Type'] = 'text/plain'
URL(ShortURL, room_id).to_s
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment