Skip to content

Commit

Permalink
Adds support for editing and deleting lemmy posts. (#48)
Browse files Browse the repository at this point in the history
* Adds support for editing and deleting lemmy posts.

* Fix issues with flutter_localizations

---------

Co-authored-by: John Wesley <dev@jwr.one>
  • Loading branch information
olorin99 and jwr1 authored May 30, 2024
1 parent a5e7d96 commit 3720b4c
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 38 deletions.
88 changes: 64 additions & 24 deletions lib/src/api/threads.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,36 +207,76 @@ class APIThreads {
Future<PostModel> edit(
int entryID,
String title,
bool isOc,
bool? isOc,
String body,
String lang,
bool isAdult,
String? lang,
bool? isAdult,
) async {
final path = '/api/entry/$entryID';

final response = await httpClient.put(
Uri.https(server, path),
body: jsonEncode({
'title': title,
'tags': [],
'isOc': isOc,
'body': body,
'lang': lang,
'isAdult': isAdult
}),
);

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

return PostModel.fromKbinEntry(
jsonDecode(response.body) as Map<String, Object?>);
switch (software) {
case ServerSoftware.kbin:
case ServerSoftware.mbin:
final path = '/api/entry/$entryID';

final response = await httpClient.put(
Uri.https(server, path),
body: jsonEncode({
'title': title,
'tags': [],
'isOc': isOc,
'body': body,
'lang': lang,
'isAdult': isAdult
}),
);

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

return PostModel.fromKbinEntry(
jsonDecode(response.body) as Map<String, Object?>);

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

final response = await httpClient.put(
Uri.https(server, path),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({
'post_id': entryID,
'name': title,
'body': body,
'nsfw': isAdult
})
);

httpErrorHandler(response, message: 'Failed to edit entry');

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

Future<void> delete(int postID) async {
final response =
await httpClient.delete(Uri.https(server, '/api/entry/$postID'));
switch (software) {
case ServerSoftware.kbin:
case ServerSoftware.mbin:
final response =
await httpClient.delete(Uri.https(server, '/api/entry/$postID'));

httpErrorHandler(response, message: "Failed to delete entry");
break;

httpErrorHandler(response, message: "Failed to delete entry");
case ServerSoftware.lemmy:
final response =
await httpClient.post(Uri.https(server, '/api/v3/post/delete'),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({
'post_id': postID,
'deleted': true
}));

httpErrorHandler(response, message: "Failed to delete entry");
break;
}
}

Future<PostModel> createArticle(
Expand Down
10 changes: 10 additions & 0 deletions lib/src/app.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:interstellar/src/screens/explore/explore_screen.dart';
import 'package:interstellar/src/screens/feed/feed_screen.dart';
import 'package:interstellar/src/screens/profile/notification/notification_badge.dart';
Expand Down Expand Up @@ -43,6 +44,15 @@ class _AppState extends State<App> {
..updateSettingsController(settingsController),
child: MaterialApp(
restorationScopeId: 'app',
localizationsDelegates: const [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [
Locale('en', ''),
],
onGenerateTitle: (BuildContext context) =>
AppLocalizations.of(context)!.appTitle,
theme: ThemeData(
Expand Down
4 changes: 2 additions & 2 deletions lib/src/screens/feed/post_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ class _PostPageState extends State<PostPage> {
.edit(
post.id,
post.title!,
post.isOC!,
post.isOC,
body,
post.lang!,
post.lang,
post.isNSFW,
),
PostType.microblog => context
Expand Down
29 changes: 17 additions & 12 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,11 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.0.2"
flutter_localizations:
dependency: "direct main"
description: flutter
source: sdk
version: "0.0.0"
flutter_markdown:
dependency: "direct main"
description:
Expand Down Expand Up @@ -549,26 +554,26 @@ packages:
dependency: transitive
description:
name: leak_tracker
sha256: "78eb209deea09858f5269f5a5b02be4049535f568c07b275096836f01ea323fa"
sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a"
url: "https://pub.dev"
source: hosted
version: "10.0.0"
version: "10.0.4"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: b46c5e37c19120a8a01918cfaf293547f47269f7cb4b0058f21531c2465d6ef0
sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8"
url: "https://pub.dev"
source: hosted
version: "2.0.1"
version: "3.0.3"
leak_tracker_testing:
dependency: transitive
description:
name: leak_tracker_testing
sha256: a597f72a664dbd293f3bfc51f9ba69816f84dcd403cdac7066cb3f6003f3ab47
sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3"
url: "https://pub.dev"
source: hosted
version: "2.0.1"
version: "3.0.1"
lints:
dependency: transitive
description:
Expand Down Expand Up @@ -685,10 +690,10 @@ packages:
dependency: transitive
description:
name: meta
sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04
sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136"
url: "https://pub.dev"
source: hosted
version: "1.11.0"
version: "1.12.0"
mime:
dependency: "direct main"
description:
Expand Down Expand Up @@ -1042,10 +1047,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b"
sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f"
url: "https://pub.dev"
source: hosted
version: "0.6.1"
version: "0.7.0"
timing:
dependency: transitive
description:
Expand Down Expand Up @@ -1162,10 +1167,10 @@ packages:
dependency: transitive
description:
name: vm_service
sha256: b3d56ff4341b8f182b96aceb2fa20e3dcb336b9f867bc0eafc0de10f1048e957
sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec"
url: "https://pub.dev"
source: hosted
version: "13.0.0"
version: "14.2.1"
volume_controller:
dependency: transitive
description:
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ environment:
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
flutter_markdown: ^0.6.23
http: ^1.2.1
http_parser: ^4.0.2
Expand Down

0 comments on commit 3720b4c

Please sign in to comment.