vkdart

Форк
0
/
message.dart 
194 строки · 6.4 Кб
1
import 'package:vkdart/util.dart';
2
import 'package:vkdart/vkontakte.dart';
3

4
/// Model Message.
5
///
6
/// See https://dev.vk.com/ru/reference/objects/message
7
class MessageModel {
8
  /// Update data.
9
  late final Map<String, dynamic> payload;
10

11
  // ignore: public_member_api_docs
12
  MessageModel(Map<String, dynamic> payload) {
13
    applyPayload(payload);
14
  }
15

16
  /// Apply payload.
17
  void applyPayload(Map<String, dynamic> payload) {
18
    final checkClientInfo = payload['client_info'] != null;
19

20
    this.payload = checkClientInfo
21
        ? payload
22
        : {
23
            'message': payload,
24
            'client_info': {
25
              'button_actions': [],
26
              'keyboard': false,
27
              'inline_keyboard': false,
28
              'carousel': false,
29
              'lang_id': 0
30
            }
31
          };
32
  }
33

34
  /// Message object.
35
  Map<String, dynamic> get message => payload['message'];
36

37
  /// Client info.
38
  Map<String, dynamic> get clientInfo => payload['client_info'];
39

40
  /// The ID of the message.
41
  int? get id => message['id'];
42

43
  /// The time of sending in Unix time.
44
  int? get createdAt => message['date'];
45

46
  /// The destination ID.
47
  int get peerId => message['peer_id'];
48

49
  /// The type of destination.
50
  MessageSource get peerType => getPeerType(peerId);
51

52
  /// The sender's ID.
53
  int get senderId => message['from_id'];
54

55
  /// The sender's type.
56
  MessageSource get senderType => getPeerType(senderId);
57

58
  /// The text of the message.
59
  String get text => message['text'];
60

61
  /// The ID used when sending the message.
62
  /// It is returned only for outgoing messages.
63
  int? get randomId => message['random_id'];
64

65
  /// An arbitrary parameter for working with transition
66
  /// [sources](https://dev.vk.com/ru/api/community-messages/getting-started).
67
  String? get ref => message['ref'];
68

69
  /// An arbitrary parameter for working with transition
70
  /// [sources](https://dev.vk.com/ru/api/community-messages/getting-started).
71
  String? get refSource => message['ref_source'];
72

73
  /// Media attachments of the message (photos, links, etc.).
74
  List<AttachmentModel> get attachments =>
75
      transformAttachments((message['attachments'] as List?)
76
              ?.map((e) => (e as Map).cast<String, dynamic>())
77
              .toList() ??
78
          const []);
79

80
  /// true if the message is marked as important.
81
  bool? get isImportant => message['important'];
82

83
  /// Location Information.
84
  Map<String, dynamic>? get geo => message['geo'];
85

86
  /// A service field for messages to bots (payload).
87
  String? get messagePayload => message['payload'];
88

89
  /// A keyboard object for bots.
90
  Map<String, dynamic>? get keyboard => message['keyboard'];
91

92
  /// the archive of forwarded messages (if any).
93
  /// The maximum number of elements is 100.
94
  /// The maximum nesting depth for forwarded messages is 45, the total maximum number in the chain, taking into account nesting, is 500.
95
  List<MessageModel>? get forwards => (message['fwd_messages'] as List?)
96
      ?.map((e) => MessageModel((e as Map).cast<String, dynamic>()))
97
      .toList();
98

99
  /// The message that the current one was sent in response to.
100
  MessageModel? get replyMessage => message['reply_message'] != null
101
      ? MessageModel(message['reply_message'])
102
      : null;
103

104
  /// Information about the service action with the chat.
105
  ///
106
  /// If the property returns `null`, then most likely the field type in the payload is [String].
107
  /// Try referring to the [actionType] property,
108
  /// if this property is also `null`,
109
  /// then the **API** has not returned a field.
110
  Map<String, dynamic>? get action {
111
    final actionPayload = message['action'];
112
    if (actionPayload is String) return null;
113

114
    return actionPayload;
115
  }
116

117
  /// Chat action type.
118
  String? get actionType => action?['type'] ?? message['action'];
119

120
  /// The ID of the user who was invited or excluded from the chat, or who pinned or unpinned the message.
121
  int? get actionMemberId => action?['member_id'] ?? message['action_mid'];
122

123
  /// New title of the conversation.
124
  String? get actionText => action?['text'] ?? message['action_text'];
125

126
  /// An old conversation title.
127
  String? get actionOldText => action?['old_text'];
128

129
  /// Email invited or excluded.
130
  String? get actionEmail => action?['email'] ?? message['action_email'];
131

132
  /// The URL of the 50 px wide copy of the conversation photo.
133
  String? get actionPhoto50 =>
134
      action?['photo']?['photo_50'] ?? message['photo_50'];
135

136
  /// The URL of the 100 px wide copy of the conversation photo.
137
  String? get actionPhoto100 =>
138
      action?['photo']?['photo_100'] ?? message['photo_100'];
139

140
  /// The URL of the 200 px wide copy of the conversation photo.
141
  String? get actionPhoto200 =>
142
      action?['photo']?['photo_200'] ?? message['photo_200'];
143

144
  /// For community messages only. Contains the ID of the user (community administrator) who sent this message.
145
  int? get adminAuthorId => message['admin_author_id'];
146

147
  /// A unique automatically incrementing number for all messages with this peer.
148
  int? get conversationMessageId => message['conversation_message_id'];
149

150
  /// This message is cropped for the bot.
151
  bool? get isCropped => message['is_cropped'];
152

153
  /// The number of participants.
154
  int? get membersCount => message['members_count'];
155

156
  /// The date when the message was updated in Unixtime.
157
  int? get updateTime => message['update_time'];
158

159
  /// Whether the attached audio message has already been listened to by you.
160
  bool? get isWasListened => message['was_listened'];
161

162
  /// The date when the message was pinned to Unixtime.
163
  int? get pinnedAt => message['pinned_at'];
164

165
  /// A string for matching the Notify user and VKontakte.
166
  ///
167
  /// See https://dev.vk.com/ru/reference/objects/message#message_tag
168
  String? get messageTag => message['message_tag'];
169

170
  /// The flag indicates whether the user is mentioned in this message.
171
  /// Added since version 5.217
172
  bool? get isMentionedUser => message['is_mentioned_user'];
173

174
  /// The ID of the conversation.
175
  int? get chatId {
176
    if (peerType != MessageSource.chat) {
177
      return null;
178
    }
179

180
    return peerId - peerChatIdOffset;
181
  }
182

183
  /// IDs of the authors of the last messages of the conversation.
184
  List<int>? get chatActive => (message['chat_active'] as List?)?.cast<int>();
185

186
  /// Notification settings for the conversation, if any.
187
  Map<String, dynamic>? get pushSettings => message['push_settings'];
188

189
  /// The number of participants in the conversation.
190
  int? get usersCount => message['users_count'];
191

192
  /// ID of the conversation creator.
193
  int? get adminId => message['admin_id'];
194
}
195

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.