mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 09:36:19 -05:00
FEATURE: rudimentary view tracking wired in
This commit is contained in:
parent
e105f0965c
commit
c150c55e2d
3 changed files with 65 additions and 8 deletions
|
@ -60,8 +60,12 @@ module Middleware
|
||||||
@env["REQUEST_METHOD"] == "GET"
|
@env["REQUEST_METHOD"] == "GET"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def has_auth_cookie?
|
||||||
|
CurrentUser.has_auth_cookie?(@env)
|
||||||
|
end
|
||||||
|
|
||||||
def cacheable?
|
def cacheable?
|
||||||
!!(!CurrentUser.has_auth_cookie?(@env) && get?)
|
!!(!has_auth_cookie? && get?)
|
||||||
end
|
end
|
||||||
|
|
||||||
def cached
|
def cached
|
||||||
|
@ -110,9 +114,36 @@ module Middleware
|
||||||
@app = app
|
@app = app
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.log_request_on_site(env, helper=nil)
|
||||||
|
host = RailsMultisite::ConnectionManagement.host(env)
|
||||||
|
RailsMultisite::ConnectionManagement.with_hostname(host) do
|
||||||
|
log_request(env,helper)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.log_request(env,helper=nil)
|
||||||
|
|
||||||
|
helper ||= Helper.new(env)
|
||||||
|
|
||||||
|
type =
|
||||||
|
if helper.is_crawler?
|
||||||
|
:crawler
|
||||||
|
elsif helper.has_auth_cookie?
|
||||||
|
:logged_in
|
||||||
|
else
|
||||||
|
:anon
|
||||||
|
end
|
||||||
|
|
||||||
|
ApplicationRequest.increment!(type)
|
||||||
|
end
|
||||||
|
|
||||||
def call(env)
|
def call(env)
|
||||||
helper = Helper.new(env)
|
helper = Helper.new(env)
|
||||||
|
|
||||||
|
Scheduler::Defer.later "Track view" do
|
||||||
|
self.class.log_request_on_site(env,helper)
|
||||||
|
end
|
||||||
|
|
||||||
if helper.cacheable?
|
if helper.cacheable?
|
||||||
helper.cached or helper.cache(@app.call(env))
|
helper.cached or helper.cache(@app.call(env))
|
||||||
else
|
else
|
||||||
|
|
|
@ -3,12 +3,33 @@ require_dependency "middleware/anonymous_cache"
|
||||||
|
|
||||||
describe Middleware::AnonymousCache::Helper do
|
describe Middleware::AnonymousCache::Helper do
|
||||||
|
|
||||||
def new_helper(env={})
|
def env(opts={})
|
||||||
Middleware::AnonymousCache::Helper.new({
|
{
|
||||||
"HTTP_HOST" => "http://test.com",
|
"HTTP_HOST" => "http://test.com",
|
||||||
"REQUEST_URI" => "/path?bla=1",
|
"REQUEST_URI" => "/path?bla=1",
|
||||||
"REQUEST_METHOD" => "GET"
|
"REQUEST_METHOD" => "GET",
|
||||||
}.merge(env))
|
"rack.input" => ""
|
||||||
|
}.merge(opts)
|
||||||
|
end
|
||||||
|
|
||||||
|
def new_helper(opts={})
|
||||||
|
Middleware::AnonymousCache::Helper.new(env(opts))
|
||||||
|
end
|
||||||
|
|
||||||
|
context "log_request" do
|
||||||
|
it "can log requests correctly" do
|
||||||
|
freeze_time Time.now
|
||||||
|
|
||||||
|
ApplicationRequest.clear_cache!
|
||||||
|
|
||||||
|
Middleware::AnonymousCache.log_request(env "HTTP_USER_AGENT" => "AdsBot-Google (+http://www.google.com/adsbot.html)")
|
||||||
|
Middleware::AnonymousCache.log_request(env)
|
||||||
|
|
||||||
|
ApplicationRequest.write_cache!
|
||||||
|
|
||||||
|
ApplicationRequest.crawler.first.count.should == 1
|
||||||
|
ApplicationRequest.anon.first.count.should == 1
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "cachable?" do
|
context "cachable?" do
|
||||||
|
|
|
@ -158,15 +158,20 @@ module RailsMultisite
|
||||||
@app = app
|
@app = app
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(env)
|
def self.host(env)
|
||||||
request = Rack::Request.new(env)
|
request = Rack::Request.new(env)
|
||||||
|
request['__ws'] || request.host
|
||||||
|
end
|
||||||
|
|
||||||
|
def call(env)
|
||||||
|
host = self.class.host(env)
|
||||||
begin
|
begin
|
||||||
|
|
||||||
#TODO: add a callback so users can simply go to a domain to register it, or something
|
#TODO: add a callback so users can simply go to a domain to register it, or something
|
||||||
return [404, {}, ["not found"]] unless @@host_spec_cache[request.host]
|
return [404, {}, ["not found"]] unless @@host_spec_cache[host]
|
||||||
|
|
||||||
ActiveRecord::Base.connection_handler.clear_active_connections!
|
ActiveRecord::Base.connection_handler.clear_active_connections!
|
||||||
self.class.establish_connection(:host => request['__ws'] || request.host)
|
self.class.establish_connection(:host => host)
|
||||||
@app.call(env)
|
@app.call(env)
|
||||||
ensure
|
ensure
|
||||||
ActiveRecord::Base.connection_handler.clear_active_connections!
|
ActiveRecord::Base.connection_handler.clear_active_connections!
|
||||||
|
|
Loading…
Reference in a new issue