LCOV - code coverage report
Current view: top level - lib/src - presence.dart (source / functions) Hit Total Coverage
Test: merged.info Lines: 27 41 65.9 %
Date: 2024-09-30 15:57:20 Functions: 0 0 -

          Line data    Source code
       1             : /*
       2             :  *   Famedly Matrix SDK
       3             :  *   Copyright (C) 2019, 2020, 2021 Famedly GmbH
       4             :  *
       5             :  *   This program is free software: you can redistribute it and/or modify
       6             :  *   it under the terms of the GNU Affero General Public License as
       7             :  *   published by the Free Software Foundation, either version 3 of the
       8             :  *   License, or (at your option) any later version.
       9             :  *
      10             :  *   This program is distributed in the hope that it will be useful,
      11             :  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      13             :  *   GNU Affero General Public License for more details.
      14             :  *
      15             :  *   You should have received a copy of the GNU Affero General Public License
      16             :  *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
      17             :  */
      18             : 
      19             : import 'package:matrix/matrix.dart';
      20             : 
      21             : class CachedPresence {
      22             :   PresenceType presence;
      23             :   DateTime? lastActiveTimestamp;
      24             :   String? statusMsg;
      25             :   bool? currentlyActive;
      26             :   String userid;
      27             : 
      28           1 :   factory CachedPresence.fromJson(Map<String, Object?> json) =>
      29           1 :       CachedPresence._(
      30             :         presence: PresenceType.values
      31           5 :             .singleWhere((type) => type.name == json['presence']),
      32           1 :         lastActiveTimestamp: json['last_active_timestamp'] != null
      33           1 :             ? DateTime.fromMillisecondsSinceEpoch(
      34           1 :                 json['last_active_timestamp'] as int)
      35             :             : null,
      36           1 :         statusMsg: json['status_msg'] as String?,
      37           1 :         currentlyActive: json['currently_active'] as bool?,
      38           1 :         userid: json['user_id'] as String,
      39             :       );
      40             : 
      41          62 :   Map<String, Object?> toJson() => {
      42          62 :         'user_id': userid,
      43          93 :         'presence': presence.name,
      44          31 :         if (lastActiveTimestamp != null)
      45           6 :           'last_active_timestamp': lastActiveTimestamp?.millisecondsSinceEpoch,
      46          33 :         if (statusMsg != null) 'status_msg': statusMsg,
      47          35 :         if (currentlyActive != null) 'currently_active': currentlyActive,
      48             :       };
      49             : 
      50           1 :   CachedPresence._({
      51             :     required this.userid,
      52             :     required this.presence,
      53             :     this.lastActiveTimestamp,
      54             :     this.statusMsg,
      55             :     this.currentlyActive,
      56             :   });
      57             : 
      58          33 :   CachedPresence(this.presence, int? lastActiveAgo, this.statusMsg,
      59             :       this.currentlyActive, this.userid) {
      60             :     if (lastActiveAgo != null) {
      61           2 :       lastActiveTimestamp =
      62           6 :           DateTime.now().subtract(Duration(milliseconds: lastActiveAgo));
      63             :     }
      64             :   }
      65             : 
      66          32 :   CachedPresence.fromMatrixEvent(Presence event)
      67          32 :       : this(
      68          64 :             event.presence.presence,
      69          64 :             event.presence.lastActiveAgo,
      70          64 :             event.presence.statusMsg,
      71          64 :             event.presence.currentlyActive,
      72          32 :             event.senderId);
      73             : 
      74           0 :   CachedPresence.fromPresenceResponse(GetPresenceResponse event, String userid)
      75           0 :       : this(event.presence, event.lastActiveAgo, event.statusMsg,
      76           0 :             event.currentlyActive, userid);
      77             : 
      78           0 :   CachedPresence.neverSeen(this.userid) : presence = PresenceType.offline;
      79             : 
      80           0 :   Presence toPresence() {
      81           0 :     final content = <String, dynamic>{
      82           0 :       'presence': presence.name.toString(),
      83             :     };
      84           0 :     if (currentlyActive != null) content['currently_active'] = currentlyActive!;
      85           0 :     if (lastActiveTimestamp != null) {
      86           0 :       content['last_active_ago'] =
      87           0 :           DateTime.now().difference(lastActiveTimestamp!).inMilliseconds;
      88             :     }
      89           0 :     if (statusMsg != null) content['status_msg'] = statusMsg!;
      90             : 
      91           0 :     final json = {
      92             :       'content': content,
      93             :       'sender': '@example:localhost',
      94             :       'type': 'm.presence'
      95             :     };
      96             : 
      97           0 :     return Presence.fromJson(json);
      98             :   }
      99             : }

Generated by: LCOV version 1.14