web feed intermediate
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:lensai/extensions/nullable.dart';
|
||||
import 'package:lensai/features/web_feed/data/models/feed_author.dart';
|
||||
import 'package:lensai/features/web_feed/data/models/feed_category.dart';
|
||||
import 'package:lensai/features/web_feed/data/models/feed_link.dart';
|
||||
import 'package:rss_dart/dart_rss.dart';
|
||||
|
||||
extension ParseAtomLink on List<AtomLink> {
|
||||
List<FeedLink> toFeedLinks() {
|
||||
return map((link) {
|
||||
final uri = link.href.mapNotNull(Uri.tryParse);
|
||||
if (uri == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final relation = link.rel.mapNotNull(
|
||||
(rel) => FeedLinkRelation.values.firstWhereOrNull((e) => e.name == rel),
|
||||
);
|
||||
|
||||
return FeedLink(
|
||||
uri: uri,
|
||||
relation: relation,
|
||||
title: link.title.whenNotEmpty,
|
||||
);
|
||||
}).nonNulls.toList();
|
||||
}
|
||||
}
|
||||
|
||||
extension ParseAtomCategory on List<AtomCategory> {
|
||||
List<FeedCategory> toFeedCategories() {
|
||||
return where((category) => category.term.isNotEmpty)
|
||||
.map(
|
||||
(category) => FeedCategory(
|
||||
id: category.term!,
|
||||
title: category.label.whenNotEmpty,
|
||||
),
|
||||
)
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
||||
extension ParseAtomPerson on List<AtomPerson> {
|
||||
List<FeedAuthor> toFeedAuthors() {
|
||||
return where(
|
||||
(author) =>
|
||||
author.name.whenNotEmpty != null ||
|
||||
author.email.whenNotEmpty != null,
|
||||
)
|
||||
.map(
|
||||
(author) => FeedAuthor(
|
||||
name: author.name.whenNotEmpty,
|
||||
email: author.email.whenNotEmpty,
|
||||
),
|
||||
)
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:lensai/extensions/nullable.dart';
|
||||
import 'package:lensai/features/web_feed/data/models/feed_article.dart';
|
||||
import 'package:lensai/features/web_feed/data/models/feed_link.dart';
|
||||
|
||||
extension FeedArticleX on FeedArticle {
|
||||
String get displayTitle =>
|
||||
title ??
|
||||
links
|
||||
?.firstWhereOrNull(
|
||||
(link) => link.relation == FeedLinkRelation.alternate,
|
||||
)
|
||||
.mapNotNull(
|
||||
(link) => link.title.whenNotEmpty ?? link.uri.toString(),
|
||||
) ??
|
||||
'Unnamed Article';
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import 'package:lensai/extensions/nullable.dart';
|
||||
import 'package:lensai/features/web_feed/data/models/feed_category.dart';
|
||||
import 'package:rss_dart/dart_rss.dart';
|
||||
import 'package:rss_dart/domain/dublin_core/dublin_core.dart';
|
||||
|
||||
extension ParseDublinCategory on DublinCore {
|
||||
List<FeedCategory> toFeedCategories() {
|
||||
return subjects
|
||||
.where((subject) => subject.isNotEmpty)
|
||||
.map((subject) => FeedCategory(id: subject))
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
||||
extension ParseRssCategory on List<RssCategory> {
|
||||
List<FeedCategory> toFeedCategories() {
|
||||
return where((category) => category.value.isNotEmpty)
|
||||
.map(
|
||||
(category) =>
|
||||
FeedCategory(id: '${category.domain} ${category.value}'.trim()),
|
||||
)
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import 'package:rss_dart/dart_rss.dart';
|
||||
|
||||
extension WebFeedJson on WebFeed {
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'title': title,
|
||||
'description': description,
|
||||
'links': links.where((link) => link != null).toList(),
|
||||
'items': items.map((item) => item.toJson()).toList(),
|
||||
};
|
||||
}
|
||||
|
||||
static WebFeed fromJson(Map<String, dynamic> json) {
|
||||
return WebFeed(
|
||||
title: json['title'] as String,
|
||||
description: json['description'] as String,
|
||||
links: (json['links'] as List).cast<String?>(),
|
||||
items:
|
||||
(json['items'] as List)
|
||||
.map(
|
||||
(item) =>
|
||||
WebFeedItemJson.fromJson(item as Map<String, dynamic>),
|
||||
)
|
||||
.toList(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
extension WebFeedItemJson on WebFeedItem {
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'title': title,
|
||||
'body': body,
|
||||
'links': links.where((link) => link != null).toList(),
|
||||
'updated': updated?.toIso8601String(),
|
||||
};
|
||||
}
|
||||
|
||||
static WebFeedItem fromJson(Map<String, dynamic> json) {
|
||||
return WebFeedItem(
|
||||
title: json['title'] as String,
|
||||
body: json['body'] as String,
|
||||
links: (json['links'] as List).cast<String?>(),
|
||||
updated:
|
||||
json['updated'] != null
|
||||
? DateTime.parse(json['updated'] as String)
|
||||
: null,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user