Perform capabilities check on comments

This commit is contained in:
Hank Grabowski 2024-06-26 15:11:58 -04:00
parent ee2421f691
commit dfb3474bd3
2 changed files with 43 additions and 25 deletions

View file

@ -171,18 +171,21 @@ class _InteractionsBarControlState extends State<InteractionsBarControl> {
} }
Widget buildCommentButton() { Widget buildCommentButton() {
final canComment = widget.entry.getCanComment();
final tooltip =
canComment.canDo ? 'Press to add a comment' : canComment.reason;
return buildButton( return buildButton(
Icons.comment, Icons.comment,
comments, comments,
true, true,
'Press to add a comment', tooltip,
() async => await addComment(), canComment.canDo ? () async => await addComment() : null,
); );
} }
Widget buildReshareButton() { Widget buildReshareButton() {
final reshareable = widget.entry.getIsReshareable(widget.isMine); final reshareable = widget.entry.getIsReshareable(widget.isMine);
final canReshare = reshareable.isReshareable == Reshareable.yes; final canReshare = reshareable.canDo;
late final String tooltip; late final String tooltip;
if (canReshare) { if (canReshare) {
tooltip = youReshared ? 'Press to undo reshare' : 'Press to reshare'; tooltip = youReshared ? 'Press to undo reshare' : 'Press to reshare';

View file

@ -5,30 +5,45 @@ import '../models/visibility.dart';
import '../services/setting_service.dart'; import '../services/setting_service.dart';
import 'known_network_extensions.dart'; import 'known_network_extensions.dart';
enum Reshareable { class InteractionCapabilityResult {
no, final bool canDo;
yes,
}
class ReshareableResult {
final Reshareable isReshareable;
final String reason; final String reason;
const ReshareableResult({required this.isReshareable, required this.reason}); const InteractionCapabilityResult(
{required this.canDo, required this.reason});
} }
extension InteractionAvailabilityExtension on TimelineEntry { extension InteractionAvailabilityExtension on TimelineEntry {
ReshareableResult getIsReshareable(bool isMine) { InteractionCapabilityResult getCanComment() {
final settingsService = getIt<SettingsService>();
final nc = settingsService.networkCapabilities
.getCapabilities(networkInfo.network);
if (!nc.comment) {
return InteractionCapabilityResult(
canDo: false,
reason:
"User disabled commenting on ${networkInfo.network.labelName} items. Go into settings to change.",
);
}
return InteractionCapabilityResult(
canDo: true,
reason: "Can comment on item",
);
}
InteractionCapabilityResult getIsReshareable(bool isMine) {
if (isMine) { if (isMine) {
return const ReshareableResult( return const InteractionCapabilityResult(
isReshareable: Reshareable.no, canDo: false,
reason: "Can't reshare your own post", reason: "Can't reshare your own post",
); );
} }
if (networkInfo.network == KnownNetworks.bluesky) { if (networkInfo.network == KnownNetworks.bluesky) {
return const ReshareableResult( return const InteractionCapabilityResult(
isReshareable: Reshareable.no, canDo: false,
reason: reason:
"Resharing of Bluesky posts through the API isn't supported by Friendica.", "Resharing of Bluesky posts through the API isn't supported by Friendica.",
); );
@ -39,24 +54,24 @@ extension InteractionAvailabilityExtension on TimelineEntry {
.getCapabilities(networkInfo.network); .getCapabilities(networkInfo.network);
if (!nc.reshare) { if (!nc.reshare) {
return ReshareableResult( return InteractionCapabilityResult(
isReshareable: Reshareable.no, canDo: false,
reason: reason:
"User disabled resharing ${networkInfo.network.labelName}. Go into settings to change.", "User disabled resharing ${networkInfo.network.labelName} items. Go into settings to change.",
); );
} }
if (visibility.type == VisibilityType.public || if (visibility.type == VisibilityType.public ||
visibility.type == VisibilityType.unlisted) { visibility.type == VisibilityType.unlisted) {
return const ReshareableResult( return const InteractionCapabilityResult(
isReshareable: Reshareable.yes, canDo: true,
reason: "Can reshare post", reason: "Can reshare item",
); );
} }
return const ReshareableResult( return const InteractionCapabilityResult(
isReshareable: Reshareable.no, canDo: false,
reason: "Can't reshare private posts", reason: "Can't reshare private items",
); );
} }
} }