Skip to content

Commit

Permalink
Add comment create, edit, and delete functionality for lemmy
Browse files Browse the repository at this point in the history
  • Loading branch information
jwr1 committed Feb 26, 2024
1 parent 8a5e49e commit 21cf65a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 29 deletions.
109 changes: 83 additions & 26 deletions lib/src/api/comments.dart
Original file line number Diff line number Diff line change
Expand Up @@ -254,49 +254,106 @@ class APIComments {
String body, {
int? parentCommentId,
}) async {
final path =
'/api/${_postTypeKbin[postType]}/$postId/comments${parentCommentId != null ? '/$parentCommentId/reply' : ''}';
switch (software) {
case ServerSoftware.kbin:
case ServerSoftware.mbin:
final path =
'/api/${_postTypeKbin[postType]}/$postId/comments${parentCommentId != null ? '/$parentCommentId/reply' : ''}';

final response = await httpClient.post(
Uri.https(server, path),
body: jsonEncode({'body': body}),
);

httpErrorHandler(response, message: 'Failed to create comment');

final response = await httpClient.post(
Uri.https(server, path),
body: jsonEncode({'body': body}),
);
return CommentModel.fromKbin(
jsonDecode(response.body) as Map<String, Object?>);

httpErrorHandler(response, message: 'Failed to post comment');
case ServerSoftware.lemmy:
const path = '/api/v3/comment';

return CommentModel.fromKbin(
jsonDecode(response.body) as Map<String, Object?>);
final response = await httpClient.post(
Uri.https(server, path),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({
'content': body,
'post_id': postId,
'parent_id': parentCommentId
}),
);

httpErrorHandler(response, message: 'Failed to create comment');

return CommentModel.fromLemmy(
jsonDecode(response.body)['comment_view'] as Map<String, Object?>);
}
}

Future<CommentModel> edit(
PostType postType,
int commentId,
String body,
String lang,
bool? isAdult,
) async {
final path = '/api/${_postTypeKbinComment[postType]}/$commentId';
switch (software) {
case ServerSoftware.kbin:
case ServerSoftware.mbin:
final path = '/api/${_postTypeKbinComment[postType]}/$commentId';

final response = await httpClient.put(
Uri.https(server, path),
body: jsonEncode({
'body': body,
}),
);

final response = await httpClient.put(
Uri.https(server, path),
body: jsonEncode({
'body': body,
'lang': lang,
'isAdult': isAdult ?? false,
}),
);
httpErrorHandler(response, message: "Failed to edit comment");

httpErrorHandler(response, message: "Failed to edit comment");
return CommentModel.fromKbin(
jsonDecode(response.body) as Map<String, Object?>);

case ServerSoftware.lemmy:
const path = '/api/v3/comment';

final response = await httpClient.put(
Uri.https(server, path),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({
'comment_id': commentId,
'content': body,
}),
);

httpErrorHandler(response, message: "Failed to edit comment");

return CommentModel.fromKbin(
jsonDecode(response.body) as Map<String, Object?>);
return CommentModel.fromLemmy(
jsonDecode(response.body)['comment_view'] as Map<String, Object?>);
}
}

Future<void> delete(PostType postType, int commentId) async {
final path = '/api/${_postTypeKbinComment[postType]}/$commentId';
switch (software) {
case ServerSoftware.kbin:
case ServerSoftware.mbin:
final path = '/api/${_postTypeKbinComment[postType]}/$commentId';

final response = await httpClient.delete(Uri.https(server, path));

final response = await httpClient.delete(Uri.https(server, path));
httpErrorHandler(response, message: "Failed to delete comment");

httpErrorHandler(response, message: "Failed to delete comment");
case ServerSoftware.lemmy:
const path = '/api/v3/comment/delete';

final response = await httpClient.post(
Uri.https(server, path),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({
'comment_id': commentId,
'deleted': true,
}),
);

httpErrorHandler(response, message: "Failed to delete comment");
}
}
}
5 changes: 4 additions & 1 deletion lib/src/models/comment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ class CommentModel with _$CommentModel {
? lemmyPathSegments[lemmyPathSegments.length - 2]
: null,
image: null,
body: lemmyComment['content'] as String,
body:
(lemmyComment['deleted'] as bool) || (lemmyComment['removed'] as bool)
? null
: lemmyComment['content'] as String,
lang: null,
upvotes: lemmyCounts['upvotes'] as int,
downvotes: lemmyCounts['downvotes'] as int,
Expand Down
2 changes: 0 additions & 2 deletions lib/src/screens/feed/post_comment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ class _EntryCommentState extends State<PostComment> {
widget.comment.postType,
widget.comment.id,
body,
widget.comment.lang!,
widget.comment.isAdult,
);

widget.onUpdate(newValue.copyWith(
Expand Down

0 comments on commit 21cf65a

Please sign in to comment.