How to view the user's status? I copied the twig file from the user view in my theme.

I want to display:


"Online" if the user is logged in.
"Absent" if the user has not had any activity for more than 15 minutes.
"Offline" if the user is disconnected.


How to do this with twig ?

{#
/**
 * @file
 * Default theme implementation to present all user data.
 *
 * This template is used when viewing a registered user's page,
 * e.g., example.com/user/123. 123 being the user's ID.
 *
 * Available variables:
 * - content: A list of content items. Use 'content' to print all content, or
 *   print a subset such as 'content.field_example'. Fields attached to a user
 *   such as 'user_picture' are available as 'content.user_picture'.
 * - attributes: HTML attributes for the container element.
 * - user: A Drupal User entity.
 *
 * @see template_preprocess_user()
 *
 * @ingroup themeable
 */
#}
&lt;article{{ attributes.addClass('user--comment') }}&gt;

  &lt;div class="row"&gt;

    {{ content.user_picture }}
    {{ user.label }}

    {% if logged_in %}
      &lt;i class="user-online fa fa-circle fa-lg"&gt;&lt;/i&gt; En ligne
    {% else %}
      &lt;i class="user-offline fa fa-circle fa-lg"&gt;&lt;/i&gt; Hors ligne
    {% endif %}

    &lt;div class="bs-field-link"&gt;
      &lt;a href="/profile/{{ content.personnel_profiles.0 }}" title="{{ user.label }}"&gt;&lt;/a&gt;
    &lt;/div&gt;

  &lt;/div&gt;

&lt;/article&gt;


The preceding code displays the status of the current user.

For example for comments I want to display the status of the author of the comment (not the node). I have this line of code in the comments twig file.

Can I reuse the code to obtain the status of the author of the comment ?

{{ drupal_entity('user', comment.uid.target_id, 'comment') }}
You can't know if the person is behind its pc, reading the website but you can know the last access of the user (last time he done an http request to the site I suppose, load a page, submit a comment, etc, but I'm not totally sure about that). You need to look the property "access" of the UserSession. 

For example, for the current user, the code is:

$session_current_user = \Drupal::currentUser()-&gt;getAccount();
$last_access = $session_current_user-&gt;getLastAccessedTime();


The property is a timestamp. You need convert if you want display it on you're site. So, you can now calculate when you considere your user Absent and determine after how many time you considere your user Offline. You can do all these calculation in your template.php file and used your results in your twig file.