Skip to content

Commit

Permalink
FIX: last seen date erroneously updated when browser in background
Browse files Browse the repository at this point in the history
In some cases user may be "last seen" even though browser tab is in
the background or computer is locked
  • Loading branch information
SamSaffron committed Feb 28, 2017
1 parent 352f98d commit 122fb80
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Expand Up @@ -9,6 +9,7 @@

// Stuff we need to load first
//= require ./discourse/lib/utilities
//= require ./discourse/lib/page-visible
//= require ./discourse/lib/ajax
//= require ./discourse/lib/text
//= require ./discourse/lib/hash
Expand Down
14 changes: 14 additions & 0 deletions app/assets/javascripts/discourse/initializers/message-bus.js.es6
@@ -1,4 +1,6 @@
// Initialize the message bus to receive messages.
import pageVisible from 'discourse/lib/page-visible';

export default {
name: "message-bus",
after: 'inject-objects',
Expand Down Expand Up @@ -36,9 +38,21 @@ export default {
messageBus.ajax = function(opts) {
opts.headers = opts.headers || {};
opts.headers['X-Shared-Session-Key'] = $('meta[name=shared_session_key]').attr('content');
if (pageVisible()) {
opts.headers['Discourse-Visible'] = "true";
}
return $.ajax(opts);
};
} else {

messageBus.ajax = function(opts) {
opts.headers = opts.headers || {};
if (pageVisible()) {
opts.headers['Discourse-Visible'] = "true";
}
return $.ajax(opts);
};

messageBus.baseUrl = Discourse.getURL('/');
}

Expand Down
7 changes: 7 additions & 0 deletions app/assets/javascripts/discourse/lib/ajax.js.es6
@@ -1,3 +1,5 @@
import pageVisible from 'discourse/lib/page-visible';

let _trackView = false;
let _transientHeader = null;

Expand All @@ -14,6 +16,7 @@ export function viewTrackingRequired() {
for performance reasons. Also automatically adjusts the URL to support installs
in subfolders.
**/

export function ajax() {
let url, args;
let ajaxObj;
Expand Down Expand Up @@ -47,6 +50,10 @@ export function ajax() {
args.headers['Discourse-Track-View'] = "true";
}

if (pageVisible()) {
args.headers['Discourse-Visible'] = "true";
}

args.success = (data, textStatus, xhr) => {
if (xhr.getResponseHeader('Discourse-Readonly')) {
Ember.run(() => Discourse.Site.currentProp('isReadOnly', true));
Expand Down
12 changes: 12 additions & 0 deletions app/assets/javascripts/discourse/lib/page-visible.js.es6
@@ -0,0 +1,12 @@
// for android we test webkit
var hiddenProperty = document.hidden !== undefined ? "hidden" : (
document.webkitHidden !== undefined ? "webkitHidden" : undefined
);

export default function() {
if (hiddenProperty !== undefined){
return !document[hiddenProperty];
} else {
return document && document.hasFocus;
}
};
6 changes: 5 additions & 1 deletion lib/auth/default_current_user_provider.rb
Expand Up @@ -225,7 +225,11 @@ def has_auth_cookie?
end

def should_update_last_seen?
!(@request.path =~ /^\/message-bus\//)
if @request.xhr?
@env["HTTP_DISCOURSE_VISIBLE".freeze] == "true".freeze
else
true
end
end

protected
Expand Down
18 changes: 14 additions & 4 deletions spec/components/auth/default_current_user_provider_spec.rb
Expand Up @@ -82,12 +82,22 @@ def provider(url, opts=nil)
expect(provider("/?api_key=hello&api_username=#{user.username.downcase}").current_user.id).to eq(user.id)
end

it "should not update last seen for message bus" do
expect(provider("/message-bus/anything/goes", method: "POST").should_update_last_seen?).to eq(false)
expect(provider("/message-bus/anything/goes", method: "GET").should_update_last_seen?).to eq(false)
it "should not update last seen for ajax calls without Discourse-Visible header" do
expect(provider("/topic/anything/goes",
:method => "POST",
"HTTP_X_REQUESTED_WITH" => "XMLHttpRequest"
).should_update_last_seen?).to eq(false)
end

it "should update last seen for others" do
it "should update ajax reqs with discourse visible" do
expect(provider("/topic/anything/goes",
:method => "POST",
"HTTP_X_REQUESTED_WITH" => "XMLHttpRequest",
"HTTP_DISCOURSE_VISIBLE" => "true"
).should_update_last_seen?).to eq(true)
end

it "should update last seen for non ajax" do
expect(provider("/topic/anything/goes", method: "POST").should_update_last_seen?).to eq(true)
expect(provider("/topic/anything/goes", method: "GET").should_update_last_seen?).to eq(true)
end
Expand Down

0 comments on commit 122fb80

Please sign in to comment.