vkdart

Форк
0
123 строки · 4.2 Кб
1
import 'package:vkdart/util.dart';
2
import 'package:vkdart/vkdart.dart' show VkDartException;
3
import 'package:vkdart/vkontakte.dart';
4

5
/// Base class Attachment
6
///
7
/// See https://dev.vk.com/ru/docs/attachments
8
abstract class AttachmentModel {
9
  /// Payload.
10
  final Map<String, dynamic> payload;
11

12
  /// Attachment type.
13
  final String attachType;
14

15
  // ignore: public_member_api_docs
16
  AttachmentModel(this.payload, {required this.attachType});
17

18
  /// Transforms the attachment object into a specific model, depending on its type.
19
  /// There may be exceptions [VkDartException] in the case of a non-correct object,
20
  /// or if the type is not supported
21
  factory AttachmentModel.fromSpecificModel(
22
          Map<String, dynamic> payload, String attachType) =>
23
      switch (attachType) {
24
        'photo' => PhotoAttachmentModel(payload),
25
        'video' => VideoAttachmentModel(payload),
26
        'audio' => AudioAttachmentModel(payload),
27
        'doc' => DocumentAttachmentModel(payload),
28
        'link' => LinkAttachmentModel(payload),
29
        'market' => MarketAttachmentModel(payload),
30
        'market_album' => MarketAlbumAttachmentModel(payload),
31
        'wall' => WallAttachmentModel(payload),
32
        'wall_reply' => WallReplyAttachmentModel(payload),
33
        'sticker' => StickerAttachmentModel(payload),
34
        'gift' => GiftAttachmentModel(payload),
35
        'graffiti' => GraffitiAttachmentModel(payload),
36
        'poll' => PollAttachmentModel(payload),
37
        'note' => NoteAttachmentModel(payload),
38
        'page' => WikiPageAttachmentModel(payload),
39
        _ => throw VkDartException('$attachType of attachment has no support!')
40
      };
41

42
  // ignore: public_member_api_docs
43
  bool? checkBoolInProperty(String key) => checkBoolUtil(payload[key]);
44
}
45

46
/// Model Custom Attachment.
47
class CustomAttachmentModel extends AttachmentModel {
48
  // ignore: public_member_api_docs
49
  CustomAttachmentModel(super.payload, {required super.attachType});
50

51
  /// Parses an attachment from a string.
52
  /// Example:
53
  /// ```dart
54
  /// CustomAttachmentModel.fromString('photo-1_2_ACCESS_KEY');
55
  /// ```
56
  factory CustomAttachmentModel.fromString(String attachment) {
57
    final parseAttachmentRe =
58
        RegExp(r'([a-z_]+)(-?\d+)_(\d+)_?(\w+)?', multiLine: true);
59

60
    if (!parseAttachmentRe.hasMatch(attachment)) {
61
      throw VkDartException('Incorrect attachment!');
62
    }
63

64
    final match = parseAttachmentRe.firstMatch(attachment)!;
65
    final attachType = match[1]!;
66

67
    final attachmentPayload = <String, dynamic>{
68
      'owner_id': int.parse(match[2]!),
69
      'id': int.parse(match[3]!),
70
      if (match[4] != null) 'access_key': match[4],
71
    };
72

73
    return AttachmentModel.fromSpecificModel(attachmentPayload, attachType)
74
        as CustomAttachmentModel;
75
  }
76

77
  /// Attachment owner identifier.
78
  int get ownerId => payload['owner_id'];
79

80
  /// Attachment ID.
81
  int get id => payload['id'];
82

83
  /// Attachment access key.
84
  String? get accessKey => payload['access_key'];
85

86
  @override
87
  String toString() =>
88
      '$attachType${ownerId}_$id${accessKey != null ? '_$accessKey' : ''}';
89
}
90

91
/// Mixin for attachment likes.
92
mixin AttachmentLikesMixin on AttachmentModel {
93
  Map<String, dynamic>? get _likes => payload['likes'];
94

95
  /// the number of users who liked the post;
96
  int? get likesCount => _likes?['count'];
97

98
  /// the presence of a "like" mark from the current user ;
99
  bool? get isUserLikes => checkBoolUtil(_likes?['user_likes']);
100

101
  /// information about whether the current user can mark "Like";
102
  bool? get isLikesCanLike => checkBoolUtil(_likes?['can_like']);
103

104
  /// information about whether the current user can repost the post;
105
  bool? get isLikesCanPublish => checkBoolUtil(_likes?['can_publish']);
106
}
107

108
/// Mixin for attachment reposts.
109
mixin AttachmentRepostsMixin on AttachmentModel {
110
  Map<String, dynamic>? get _reposts => payload['reposts'];
111

112
  /// the number of reposts;
113
  int? get repostsCount => _reposts?['count'];
114

115
  /// Indicates whether the current user has reposted the attachment.
116
  bool? get isUserReposted => checkBoolUtil(_reposts?['user_reposted']);
117

118
  /// repost counter on the wall.
119
  int? get repostsWallCount => _reposts?['wall_count'];
120

121
  /// counter of reposts in personal messages.
122
  int? get repostsMailCount => _reposts?['mail_count'];
123
}
124

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

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

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

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