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

55 lines
1.5 KiB
Dart

import 'package:lemmy_api_client/v3.dart';
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``
extension GetOriginInstanceCommunitySafe on CommunitySafe {
String get originInstanceHost => _extract(actorId);
}
extension GetOriginInstancePersonSafe on PersonSafe {
String get originInstanceHost => _extract(actorId);
}
extension GetOriginInstancePostView on Post {
String get originInstanceHost => _extract(apId);
}
extension GetOriginInstanceCommentView on Comment {
String get originInstanceHost => _extract(apId);
}
String _extract(String url) => urlHost(url);
extension CommunityPreferredNames on CommunitySafe {
String get preferredName => '!$name';
String get originPreferredName =>
local ? preferredName : '!$name@$originInstanceHost';
}
extension UserPreferredNames on PersonSafe {
String get preferredName {
final dispName = displayName;
if (dispName != null && dispName.isNotEmpty) {
return dispName;
}
return '@$name';
}
String get originPreferredName {
if (!local) return '$preferredName@$originInstanceHost';
return preferredName;
}
}
extension CommentLink on Comment {
String get link => 'https://$instanceHost/post/$postId/comment/$id';
}