1

I'm creating rails app. I add to devise user colums: total_online and last_seen to track their online total time.

app/models/user.rb

class User < ApplicationRecord
  devise :session_limitable, :database_authenticatable, :registerable, :confirmable,
    :recoverable, :rememberable, :trackable, :validatable

  validates :full_name, presence: true
  def active_now!
    if (Time.now - self.last_seen) <= 3.minutes
      self.total_online += (Time.now - self.last_seen)
    end

    self.last_seen = Time.now
    save!
  end
end

app/controller/application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  responders :flash
  respond_to :html

  before_action :record_user_activity

  private

  def record_user_activity
    current_user.active_now! if current_user
  end
end

The main idea - user should do any actions on pages in 3 minutes to save this time to total_online. But I have a video watching pages, and I want to calculate the time, which user was on the video page.(when user is watching videos he doesn't do any actions, so total_online not updated). I guess the time on this pages should calculate separetely and then added to total_online. How can I do this?

Damir Nurgaliev
  • 341
  • 5
  • 19
  • Sounds like you're going to need to come up with a new approach to calculating “active time”. – coreyward Jul 17 '17 at 16:35
  • It seems this could solve your problem: https://stackoverflow.com/questions/41892204/how-can-track-the-time-which-one-user-is-online-with-ruby-on-rails – Ronan Lopes Jul 17 '17 at 17:29
  • Actually sounds like your solution is decent enough, when a user does something, send off an event to the server and decide if they've been online or just came back from 'away' and all you need to finish is something like https://developers.google.com/youtube/iframe_api_reference (I'm assuming video page = embedded youtube videos, though the other companies likely offer similar APIs). This has events that allow you to be notified in javascript when a video starts/ends/gets paused/etc. – Simple Lime Jul 17 '17 at 18:12

0 Answers0