lemmur-app-android/lib/util/extensions/api.dart

54 lines
1.4 KiB
Dart
Raw Normal View History

2021-01-24 20:01:55 +01:00
import 'package:lemmy_api_client/v2.dart';
2021-01-02 01:13:13 +01:00
import '../cleanup_url.dart';
// Extensions to lemmy api objects which give a [.originInstanceHost] getter
// allowing for a convenient way of knowing what is the origin of the object
// For example if a post on lemmy.ml is federated from lemmygrad.ml then
// `post.instanceHost == 'lemmy.ml'
// && post.originInstanceHost == 'lemmygrad.ml``
2021-01-27 20:53:09 +01:00
extension GetOriginInstanceCommunitySafe on CommunitySafe {
String get originInstanceHost => _extract(actorId);
2021-01-02 01:13:13 +01:00
}
2021-01-27 20:53:09 +01:00
extension GetOriginInstanceUserSafe on UserSafe {
String get originInstanceHost => _extract(actorId);
2021-01-02 01:13:13 +01:00
}
2021-01-27 20:53:09 +01:00
extension GetOriginInstancePostView on Post {
String get originInstanceHost => _extract(apId);
2021-01-02 01:13:13 +01:00
}
2021-01-27 20:53:09 +01:00
extension GetOriginInstanceCommentView on Comment {
String get originInstanceHost => _extract(apId);
2021-01-02 01:13:13 +01:00
}
2021-01-27 20:53:09 +01:00
String _extract(String url) => urlHost(url);
2021-02-24 20:52:18 +01:00
extension CommunityDisplayNames on CommunitySafe {
String get displayName => '!$name';
String get originDisplayName =>
local ? displayName : '!$name@$originInstanceHost';
}
extension UserDisplayNames on UserSafe {
String get displayName {
2021-01-31 14:38:47 +01:00
if (preferredUsername != null && preferredUsername.isNotEmpty) {
2021-01-27 21:11:36 +01:00
return preferredUsername;
}
2021-01-27 21:11:36 +01:00
return '@$name';
}
String get originDisplayName {
if (!local) return '$displayName@$originInstanceHost';
2021-01-27 21:11:36 +01:00
return displayName;
}
}
2021-02-03 13:12:28 +01:00
extension CommentLink on Comment {
String get link => 'https://$instanceHost/post/$postId/comment/$id';
}