LCOV - code coverage report
Current view: top level - lib/src - presence.dart (source / functions) Hit Total Coverage
Test: merged.info Lines: 27 44 61.4 %
Date: 2024-11-12 07:37:08 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             :               )
      36             :             : null,
      37           1 :         statusMsg: json['status_msg'] as String?,
      38           1 :         currentlyActive: json['currently_active'] as bool?,
      39           1 :         userid: json['user_id'] as String,
      40             :       );
      41             : 
      42          64 :   Map<String, Object?> toJson() => {
      43          64 :         'user_id': userid,
      44          96 :         'presence': presence.name,
      45          32 :         if (lastActiveTimestamp != null)
      46           6 :           'last_active_timestamp': lastActiveTimestamp?.millisecondsSinceEpoch,
      47          34 :         if (statusMsg != null) 'status_msg': statusMsg,
      48          36 :         if (currentlyActive != null) 'currently_active': currentlyActive,
      49             :       };
      50             : 
      51           1 :   CachedPresence._({
      52             :     required this.userid,
      53             :     required this.presence,
      54             :     this.lastActiveTimestamp,
      55             :     this.statusMsg,
      56             :     this.currentlyActive,
      57             :   });
      58             : 
      59          34 :   CachedPresence(
      60             :     this.presence,
      61             :     int? lastActiveAgo,
      62             :     this.statusMsg,
      63             :     this.currentlyActive,
      64             :     this.userid,
      65             :   ) {
      66             :     if (lastActiveAgo != null) {
      67           2 :       lastActiveTimestamp =
      68           6 :           DateTime.now().subtract(Duration(milliseconds: lastActiveAgo));
      69             :     }
      70             :   }
      71             : 
      72          33 :   CachedPresence.fromMatrixEvent(Presence event)
      73          33 :       : this(
      74          66 :           event.presence.presence,
      75          66 :           event.presence.lastActiveAgo,
      76          66 :           event.presence.statusMsg,
      77          66 :           event.presence.currentlyActive,
      78          33 :           event.senderId,
      79             :         );
      80             : 
      81           0 :   CachedPresence.fromPresenceResponse(GetPresenceResponse event, String userid)
      82           0 :       : this(
      83           0 :           event.presence,
      84           0 :           event.lastActiveAgo,
      85           0 :           event.statusMsg,
      86           0 :           event.currentlyActive,
      87             :           userid,
      88             :         );
      89             : 
      90           0 :   CachedPresence.neverSeen(this.userid) : presence = PresenceType.offline;
      91             : 
      92           0 :   Presence toPresence() {
      93           0 :     final content = <String, dynamic>{
      94           0 :       'presence': presence.name.toString(),
      95             :     };
      96           0 :     if (currentlyActive != null) content['currently_active'] = currentlyActive!;
      97           0 :     if (lastActiveTimestamp != null) {
      98           0 :       content['last_active_ago'] =
      99           0 :           DateTime.now().difference(lastActiveTimestamp!).inMilliseconds;
     100             :     }
     101           0 :     if (statusMsg != null) content['status_msg'] = statusMsg!;
     102             : 
     103           0 :     final json = {
     104             :       'content': content,
     105             :       'sender': '@example:localhost',
     106             :       'type': 'm.presence',
     107             :     };
     108             : 
     109           0 :     return Presence.fromJson(json);
     110             :   }
     111             : }

Generated by: LCOV version 1.14