Skip to content

Commit

Permalink
feat: add who can reply logic to article creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-ajax committed Dec 30, 2024
1 parent 3f9aca4 commit b4c8777
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:ion/app/features/feed/create_article/providers/draft_article_pro
import 'package:ion/app/features/feed/create_article/views/pages/create_article_preview_modal/components/article_preview.dart';
import 'package:ion/app/features/feed/create_article/views/pages/create_article_preview_modal/components/select_article_topics_item.dart';
import 'package:ion/app/features/feed/create_article/views/pages/create_article_preview_modal/components/select_article_who_can_reply_item.dart';
import 'package:ion/app/features/feed/providers/selected_who_can_reply_option_provider.c.dart';
import 'package:ion/app/router/components/navigation_app_bar/navigation_app_bar.dart';
import 'package:ion/app/router/components/sheet_content/sheet_content.dart';
import 'package:ion/generated/assets.gen.dart';
Expand All @@ -24,6 +25,7 @@ class CreateArticlePreviewModal extends HookConsumerWidget {
final paddingValue = 20.0.s;

final DraftArticleState(:title, :image, :imageIds, :content) = ref.watch(draftArticleProvider);
final whoCanReply = ref.watch(selectedWhoCanReplyOptionProvider);

return SheetContent(
bottomPadding: 0,
Expand Down Expand Up @@ -60,6 +62,7 @@ class CreateArticlePreviewModal extends HookConsumerWidget {
content: content,
imageId: image?.path,
mediaIds: imageIds,
whoCanReply: whoCanReply,
);

if (!ref.read(createArticleProvider).hasError && ref.context.mounted) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class ArticleData with _$ArticleData implements EventSerializable {
summary: summary,
publishedAt: publishedAt,
relatedHashtags: tags[RelatedHashtag.tagName]?.map(RelatedHashtag.fromTag).toList(),
settings: tags[EventSetting.settingTagName]?.map(EventSetting.fromTag).toList(),
);
}

Expand All @@ -117,11 +118,12 @@ class ArticleData with _$ArticleData implements EventSerializable {
String? summary,
DateTime? publishedAt,
List<RelatedHashtag>? relatedHashtags,
Set<WhoCanReplySettingsOption>? whoCanReplySettings,
Set<WhoCanReplySettingsOption> whoCanReplySettings = const {},
}) {
final setting = (whoCanReplySettings?.isNotEmpty ?? false)
? WhoCanReplyEventSetting(value: whoCanReplySettings!.join(','))
final setting = whoCanReplySettings.isNotEmpty
? WhoCanReplyEventSetting(values: whoCanReplySettings)
: null;

return ArticleData(
content: content,
media: media,
Expand Down Expand Up @@ -155,6 +157,7 @@ class ArticleData with _$ArticleData implements EventSerializable {
['published_at', (publishedAt!.millisecondsSinceEpoch ~/ 1000).toString()],
if (media.isNotEmpty) ...media.values.map((mediaAttachment) => mediaAttachment.toTag()),
['d', uniqueIdForEditing],
if (settings != null) ...settings!.map((setting) => setting.toTag()),
],
content: content,
);
Expand Down
7 changes: 4 additions & 3 deletions lib/app/features/feed/data/models/entities/post_data.c.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class PostData with _$PostData, EntityMediaDataMixin implements EventSerializabl
relatedEvents: tags[RelatedEvent.tagName]?.map(RelatedEvent.fromTag).toList(),
relatedPubkeys: tags[RelatedPubkey.tagName]?.map(RelatedPubkey.fromTag).toList(),
relatedHashtags: tags[RelatedHashtag.tagName]?.map(RelatedHashtag.fromTag).toList(),
settings: tags[EventSetting.settingTagName]?.map(EventSetting.fromTag).toList(),
);
}

Expand All @@ -105,9 +106,9 @@ class PostData with _$PostData, EntityMediaDataMixin implements EventSerializabl
.map((match) => RelatedHashtag(value: match.text))
.toList();

final whoCanReply =
whoCanReplySettings.map((setting) => setting.toValue()).whereNotNull().join(',');
final setting = whoCanReply.isNotEmpty ? WhoCanReplyEventSetting(value: whoCanReply) : null;
final setting = whoCanReplySettings.isNotEmpty
? WhoCanReplyEventSetting(values: whoCanReplySettings)
: null;

return PostData(
content: parsedContent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@ import 'package:ion/app/extensions/extensions.dart';
import 'package:ion/generated/assets.gen.dart';

enum WhoCanReplySettingsOption {
everyone,
followedAccounts,
verifiedAccounts,
mentionedAccounts;
everyone(null),
followedAccounts('following'),
// TODO: Add support for verified accounts setting when API is ready
verifiedAccounts(null),
mentionedAccounts('mentioned');

const WhoCanReplySettingsOption(this.tagValue);

final String? tagValue;

static WhoCanReplySettingsOption fromTagValue(String value) {
return values.firstWhere((option) => option.tagValue == value, orElse: () => everyone);
}

String getTitle(BuildContext context) {
return switch (this) {
Expand All @@ -32,14 +41,4 @@ enum WhoCanReplySettingsOption {

return icon.icon(color: context.theme.appColors.primaryAccent);
}

String? toValue() {
return switch (this) {
WhoCanReplySettingsOption.followedAccounts => 'following',
WhoCanReplySettingsOption.mentionedAccounts => 'mentioned',
// TODO: Add verified accounts option when API is ready
WhoCanReplySettingsOption.verifiedAccounts => null,
WhoCanReplySettingsOption.everyone => null,
};
}
}
9 changes: 6 additions & 3 deletions lib/app/features/nostr/model/event_setting.c.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:ion/app/exceptions/exceptions.dart';
import 'package:ion/app/features/feed/data/models/who_can_reply_settings_option.dart';

part 'event_setting.c.freezed.dart';

Expand Down Expand Up @@ -30,7 +31,7 @@ abstract class EventSetting {
@freezed
class WhoCanReplyEventSetting with _$WhoCanReplyEventSetting implements EventSetting {
const factory WhoCanReplyEventSetting({
required String value,
required Set<WhoCanReplySettingsOption> values,
}) = _WhoCanReplyEventSetting;

const WhoCanReplyEventSetting._();
Expand All @@ -43,15 +44,17 @@ class WhoCanReplyEventSetting with _$WhoCanReplyEventSetting implements EventSet
if (tag.length < 4) {
throw IncorrectEventTagException(tag: tag.toString());
}
return WhoCanReplyEventSetting(value: tag[2]);

final values = tag[2].split(',').map(WhoCanReplySettingsOption.fromTagValue).toSet();
return WhoCanReplyEventSetting(values: values);
}

@override
List<String> toTag() {
return [
EventSetting.settingTagName,
tagName,
value,
values.map((value) => value.tagValue).nonNulls.join(','),
(DateTime.now().millisecondsSinceEpoch ~/ 1000).toString(),
];
}
Expand Down

0 comments on commit b4c8777

Please sign in to comment.