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

44 lines
1.2 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);
extension DisplayName on UserSafe {
String get displayName {
final name = () {
if (preferredUsername != null && preferredUsername != '') {
return preferredUsername;
} else {
return '@${this.name}';
}
}();
if (!local) return '$name@$originInstanceHost';
return name;
}
}