Skip to content

Commit 113eea3

Browse files
Piefed - Community Moderation (#191)
* Piefed - Community Moderation * adding canAuthUserModerate for piefed posts and comments, pagination tweaks * enable createBan for piefed * adding mark-nsfw ability * Update lib/src/api/magazine_moderation.dart * convert get bans to queryParams
1 parent 98469c6 commit 113eea3

6 files changed

Lines changed: 71 additions & 8 deletions

File tree

lib/src/api/magazine_moderation.dart

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,18 @@ class APIMagazineModeration {
2222
throw Exception('List banned users not allowed on lemmy');
2323

2424
case ServerSoftware.piefed:
25-
throw UnimplementedError();
25+
final path = '/community/moderate/bans';
26+
27+
final page_ = page ?? 1;
28+
29+
final query = {
30+
'community_id': magazineId.toString(),
31+
'page': page_.toString(),
32+
};
33+
34+
final response = await client.get(path, queryParams: query);
35+
36+
return MagazineBanListModel.fromPiefed(response.bodyJson);
2637
}
2738
}
2839

@@ -47,7 +58,17 @@ class APIMagazineModeration {
4758
throw Exception('Ban update not implemented on Lemmy yet');
4859

4960
case ServerSoftware.piefed:
50-
throw UnimplementedError();
61+
final path = '/community/moderate/ban';
62+
final body = {
63+
'community_id': magazineId,
64+
'user_id': userId,
65+
'reason': reason,
66+
'expiredAt': expiredAt?.toIso8601String(),
67+
};
68+
69+
final response = await client.post(path, body: body);
70+
71+
return MagazineBanModel.fromPiefed(response.bodyJson);
5172
}
5273
}
5374

@@ -64,7 +85,13 @@ class APIMagazineModeration {
6485
throw Exception('Ban update not implemented on Lemmy yet');
6586

6687
case ServerSoftware.piefed:
67-
throw UnimplementedError();
88+
final path = '/community/moderate/unban';
89+
90+
final body = {'community_id': magazineId, 'user_id': userId};
91+
92+
final response = await client.put(path, body: body);
93+
94+
return MagazineBanModel.fromPiefed(response.bodyJson);
6895
}
6996
}
7097

lib/src/api/moderation.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,12 @@ class APIModeration {
6767
throw Exception('Moderation not implemented on Lemmy yet');
6868

6969
case ServerSoftware.piefed:
70-
throw UnimplementedError();
70+
final path = '/community/moderate/post/nsfw';
71+
final body = {'post_id': postId, 'nsfw_status': status};
72+
73+
final response = await client.post(path, body: body);
74+
75+
return PostModel.fromPiefed(response.bodyJson);
7176
}
7277
}
7378

lib/src/models/comment.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ class CommentModel with _$CommentModel {
244244
children: children,
245245
childCount: piefedCounts['child_count'] as int,
246246
visibility: 'visible',
247-
canAuthUserModerate: null,
247+
canAuthUserModerate: json['canAuthUserModerate'] as bool?,
248248
notificationControlStatus: json['activity_alert'] == null
249249
? null
250250
: json['activity_alert'] as bool

lib/src/models/magazine.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,18 @@ class MagazineBanListModel with _$MagazineBanListModel {
215215
.toList(),
216216
nextPage: mbinCalcNextPaginationPage(json['pagination'] as JsonMap),
217217
);
218+
219+
factory MagazineBanListModel.fromPiefed(JsonMap json) => MagazineBanListModel(
220+
items: (json['items'] as List<dynamic>)
221+
.map((item) => MagazineBanModel.fromPiefed(item as JsonMap))
222+
.toList(),
223+
// if next_page is None we have reached the end of the bans
224+
// so set nextPage to null. Otherwise set it to the next page number
225+
// to request
226+
nextPage: (json['next_page'] as String?) != 'None'
227+
? json['next_page'] as String?
228+
: null,
229+
);
218230
}
219231

220232
@freezed
@@ -236,4 +248,13 @@ class MagazineBanModel with _$MagazineBanModel {
236248
bannedBy: UserModel.fromMbin(json['bannedBy'] as JsonMap),
237249
expired: json['expired'] as bool,
238250
);
251+
252+
factory MagazineBanModel.fromPiefed(JsonMap json) => MagazineBanModel(
253+
reason: json['reason'] as String?,
254+
expiresAt: optionalDateTime(json['expiresAt'] as String?),
255+
magazine: MagazineModel.fromPiefed(json['community'] as JsonMap),
256+
bannedUser: UserModel.fromPiefed(json['bannedUser'] as JsonMap),
257+
bannedBy: UserModel.fromPiefed(json['bannedBy'] as JsonMap),
258+
expired: json['expired'] as bool,
259+
);
239260
}

lib/src/models/post.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ class PostListModel with _$PostListModel {
4343
items: (json['posts'] as List<dynamic>)
4444
.map((post) => PostModel.fromPiefed(post as JsonMap))
4545
.toList(),
46-
nextPage: json['next_page'] as String?,
46+
// if next_page is None we have reached the end of the notifications
47+
// so set nextPage to null. Otherwise set it to the next page number
48+
// to request
49+
nextPage: (json['next_page'] as String?) != 'None'
50+
? json['next_page'] as String?
51+
: null,
4752
);
4853
}
4954

@@ -240,7 +245,7 @@ class PostModel with _$PostModel {
240245
editedAt: optionalDateTime(piefedPost['updated'] as String?),
241246
lastActive: DateTime.parse(piefedCounts['newest_comment_time'] as String),
242247
visibility: 'visible',
243-
canAuthUserModerate: null,
248+
canAuthUserModerate: json['canAuthUserModerate'] as bool?,
244249
notificationControlStatus: json['activity_alert'] == null
245250
? null
246251
: json['activity_alert'] as bool

lib/src/models/user.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ class DetailedUserListModel with _$DetailedUserListModel {
3434
items: (json['users'] as List<dynamic>)
3535
.map((item) => DetailedUserModel.fromPiefed(item as JsonMap))
3636
.toList(),
37-
nextPage: json['next_page'] as String?,
37+
// if next_page is None we have reached the end of the notifications
38+
// so set nextPage to null. Otherwise set it to the next page number
39+
// to request
40+
nextPage: (json['next_page'] as String?) != 'None'
41+
? json['next_page'] as String?
42+
: null,
3843
);
3944
}
4045

0 commit comments

Comments
 (0)