aboutsummaryrefslogtreecommitdiff
path: root/lib/models/message.dart
blob: 4026215500053ad065add02639d1d5f1cd182bea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:simple_link_preview/simple_link_preview.dart';

class Message {
  final String messageText;
  final String? linkTitle;
  final String? linkDescription;
  final String? linkPhotoURL;
  final DateTime sentAt;
  final String sentBy;

  const Message({
    required this.messageText,
    this.linkTitle,
    this.linkDescription,
    this.linkPhotoURL,
    required this.sentAt,
    required this.sentBy,
  });

  Map<String, dynamic> toMap({LinkPreview? preview}) {
    return {
      "messageText": messageText,
      "linkTitle": preview?.title,
      "linkDescription": preview?.description,
      "linkPhotoURL": preview?.image,
      "sentAt": sentAt,
      "sentBy": sentBy,
    };
  }

  factory Message.fromMap(Map<String, dynamic> map) {
    return Message(
      messageText: map['messageText'],
      linkTitle: map['linkTitle'],
      linkDescription: map['linkDescription'],
      linkPhotoURL: map['linkPhotoURL'],
      sentAt: (map['sentAt'] as Timestamp).toDate(),
      sentBy: map['sentBy'],
    );
  }
}