Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -836,5 +836,29 @@
}
},
"pushNotificationsDialog_selectDistributor": "Select push distributor",
"crossPost": "Cross post"
"crossPost": "Cross post",
"modlog": "Modlog",
"modlog_deletedPost": "Deleted post",
"modlog_restoredPost": "Restored post",
"modlog_deletedComment": "Deleted comment",
"modlog_restoredComment": "Restored comment",
"modlog_pinnedPost": "Pinned post",
"modlog_unpinnedPost": "Unpinned post",
"modlog_bannedUser": "Banned user",
"modlog_unbannedUser": "Unbanned user",
"modlog_addModerator": "Added moderator",
"modlog_removedModerator": "Removed moderator",
"modlog_reason": "Reason: {reason}",
"@modlog_reason": {
"placeholders": {
"reason": {
"type": "String"
}
}
},
"modlog_all": "All",
"modlog_communityAdded": "Community added",
"modlog_communityRemoved": "Community removed",
"modlog_postLocked": "Post locked",
"modlog_postUnlocked": "Post unlocked"
}
135 changes: 135 additions & 0 deletions lib/src/api/moderation.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:interstellar/src/api/client.dart';
import 'package:interstellar/src/controller/server.dart';
import 'package:interstellar/src/models/comment.dart';
import 'package:interstellar/src/models/modlog.dart';
import 'package:interstellar/src/models/post.dart';
import 'package:interstellar/src/utils/utils.dart';

Expand All @@ -10,6 +11,90 @@ const _postTypeMbinComment = {
PostType.microblog: 'post-comment',
};

enum ModLogType {
all,
postDeleted,
postRestored,
commentDeleted,
commentRestored,
postPinned,
postUnpinned,
microblogPostDeleted,
microblogPostRestored,
microblogCommentDeleted,
microblogCommentRestored,
ban,
unban,
moderatorAdded,
moderatorRemoved,
communityAdded,
communityRemoved,
postLocked,
postUnlocked;

static ModLogType fromMbin(String type) => switch (type) {
'log_entry_deleted' => ModLogType.postDeleted,
'log_entry_restored' => ModLogType.postRestored,
'log_entry_comment_deleted' => ModLogType.commentDeleted,
'log_entry_comment_restored' => ModLogType.commentRestored,
'log_entry_pinned' => ModLogType.postPinned,
'log_entry_unpinned' => ModLogType.postUnpinned,
'log_post_deleted' => ModLogType.microblogPostDeleted,
'log_post_restored' => ModLogType.microblogPostRestored,
'log_post_comment_deleted' => ModLogType.microblogCommentDeleted,
'log_post_comment_restored' => ModLogType.microblogCommentRestored,
'log_ban' => ModLogType.ban,
'log_unban' => ModLogType.unban,
'log_moderator_add' => ModLogType.moderatorAdded,
'log_moderator_remove' => ModLogType.moderatorRemoved,
String() => ModLogType.all,
};

String get toMbin => switch (this) {
ModLogType.all => 'all',
ModLogType.postDeleted => 'entry_deleted',
ModLogType.postRestored => 'entry_restored',
ModLogType.commentDeleted => 'entry_comment_deleted',
ModLogType.commentRestored => 'entry_comment_restored',
ModLogType.postPinned => 'entry_pinned',
ModLogType.postUnpinned => 'entry_unpinned',
ModLogType.microblogPostDeleted => 'post_deleted',
ModLogType.microblogPostRestored => 'post_restored',
ModLogType.microblogCommentDeleted => 'post_comment_deleted',
ModLogType.microblogCommentRestored => 'post_comment_restored',
ModLogType.ban => 'ban',
ModLogType.unban => 'unban',
ModLogType.moderatorAdded => 'moderator_add',
ModLogType.moderatorRemoved => 'moderator_remove',
ModLogType.communityAdded => 'all',
ModLogType.communityRemoved => 'all',
ModLogType.postLocked => 'all',
ModLogType.postUnlocked => 'all',
};

String get toLemmy => switch (this) {
ModLogType.all => 'All',
ModLogType.postDeleted => 'ModRemovePost',
ModLogType.postRestored => 'ModRemovePost',
ModLogType.commentDeleted => 'ModRemoveComment',
ModLogType.commentRestored => 'ModRemoveComment',
ModLogType.postPinned => 'ModFeaturePost',
ModLogType.postUnpinned => 'ModFeaturePost',
ModLogType.microblogPostDeleted => 'ModRemovePost',
ModLogType.microblogPostRestored => 'ModRemovePost',
ModLogType.microblogCommentDeleted => 'ModRemoveComment',
ModLogType.microblogCommentRestored => 'ModRemoveComment',
ModLogType.ban => 'ModBanFromCommunity',
ModLogType.unban => 'ModBanFromCommunity',
ModLogType.moderatorAdded => 'ModAddCommunity',
ModLogType.moderatorRemoved => 'ModAddCommunity',
ModLogType.communityAdded => 'ModRemoveCommunity',
ModLogType.communityRemoved => 'ModRemoveCommunity',
ModLogType.postLocked => 'ModLockPost',
ModLogType.postUnlocked => 'ModLockPost',
};
}

class APIModeration {
final ServerClient client;

Expand Down Expand Up @@ -152,4 +237,54 @@ class APIModeration {
);
}
}

Future<ModlogListModel> modLog({
int? communityId,
int? userId,
ModLogType type = ModLogType.all,
String? page,
}) async {
switch (client.software) {
case ServerSoftware.mbin:
final path = communityId != null ? '/magazine/$communityId/log' : '/modlog';
final query = {
'p': page,
if (type != ModLogType.all)
'types[0]': type.toMbin,

// if type set to post or comment also include the corresponding type for microblogs
if (type == ModLogType.postDeleted)
'types[1]': ModLogType.microblogPostDeleted.toMbin,
if (type == ModLogType.postRestored)
'types[1]': ModLogType.microblogPostRestored.toMbin,
if (type == ModLogType.commentDeleted)
'types[1]': ModLogType.microblogCommentDeleted.toMbin,
if (type == ModLogType.commentRestored)
'types[1]': ModLogType.microblogCommentRestored.toMbin,
};

final response = await client.get(path, queryParams: query);
return ModlogListModel.fromMbin(response.bodyJson);

case ServerSoftware.lemmy:
const path = '/modlog';
final query = {
if (communityId != null) 'community_id': communityId.toString(),
if (userId != null) 'mod_person_id': userId.toString(),
'page': page,
'type_': type.toLemmy,
};
final response = await client.get(path, queryParams: query);
final json = response.bodyJson;
return ModlogListModel.fromLemmy({
'next_page':
(int.parse(((page?.isNotEmpty ?? false) ? page : '0') ?? '0') + 1)
.toString(),
...json,
}, langCodeIdPairs: await client.languageCodeIdPairs());

case ServerSoftware.piefed:
throw UnimplementedError('Not yet implemented for PieFed');
}
}
}
10 changes: 5 additions & 5 deletions lib/src/models/comment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ abstract class CommentModel with _$CommentModel {
required List<(String, int)> langCodeIdPairs,
}) {
final lemmyComment = json['comment'] as JsonMap;
final lemmyCounts = json['counts'] as JsonMap;
final lemmyCounts = json['counts'] as JsonMap?;

final lemmyPath = lemmyComment['path'] as String;
final lemmyPathSegments = lemmyPath
Expand Down Expand Up @@ -216,21 +216,21 @@ abstract class CommentModel with _$CommentModel {
.where((pair) => pair.$2 == lemmyComment['language_id'] as int)
.firstOrNull
?.$1,
upvotes: lemmyCounts['upvotes'] as int,
downvotes: lemmyCounts['downvotes'] as int,
upvotes: lemmyCounts?['upvotes'] as int? ?? 0,
downvotes: lemmyCounts?['downvotes'] as int? ?? 0,
boosts: null,
myVote: json['my_vote'] as int?,
myBoost: null,
createdAt: DateTime.parse(lemmyComment['published'] as String),
editedAt: optionalDateTime(json['updated'] as String?),
children: children,
childCount: lemmyCounts['child_count'] as int,
childCount: lemmyCounts?['child_count'] as int? ?? 0,
visibility: 'visible',
canAuthUserModerate: null,
notificationControlStatus: null,
bookmarks: [
// Empty string indicates comment is saved. No string indicates comment is not saved.
if (json['saved'] as bool) '',
if (((json['saved'] as bool?) != null) ? json['saved'] as bool : false) '',
],
apId: lemmyComment['ap_id'] as String,
);
Expand Down
15 changes: 14 additions & 1 deletion lib/src/models/community.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ abstract class CommunityBanModel with _$CommunityBanModel {
required DateTime? expiresAt,
required CommunityModel community,
required UserModel bannedUser,
required UserModel bannedBy,
required UserModel? bannedBy,
required bool expired,
}) = _CommunityBanModel;

Expand All @@ -275,6 +275,19 @@ abstract class CommunityBanModel with _$CommunityBanModel {
expired: json['expired'] as bool,
);

factory CommunityBanModel.fromLemmy(JsonMap json) {
final expiration = json['expires'] != null ? DateTime.parse(json['expires'] as String) : null;

return CommunityBanModel(
reason: json['reason'] as String?,
expiresAt: expiration,
community: CommunityModel.fromLemmy(json['community'] as JsonMap),
bannedUser: UserModel.fromLemmy(json['banned_person'] as JsonMap),
bannedBy: json['moderator'] != null ? UserModel.fromLemmy(json['moderator'] as JsonMap) : null,
expired: expiration?.isBefore(DateTime.now()) ?? false,
);
}

factory CommunityBanModel.fromPiefed(JsonMap json) => CommunityBanModel(
reason: json['reason'] as String?,
expiresAt: optionalDateTime(json['expires_at'] as String?),
Expand Down
Loading