forked from Nexus/nexus
use gomuks for URL previews
This commit is contained in:
parent
16fdf0c420
commit
eca9d3b411
6 changed files with 18 additions and 69 deletions
|
|
@ -1,50 +1,22 @@
|
|||
import "dart:convert";
|
||||
import "package:flutter_riverpod/flutter_riverpod.dart";
|
||||
import "package:http/http.dart";
|
||||
import "package:nexus/controllers/client_state.dart";
|
||||
import "package:nexus/controllers/header.dart";
|
||||
import "package:nexus/controllers/client.dart";
|
||||
import "package:nexus/models/open_graph_data.dart";
|
||||
|
||||
class UrlPreviewController extends AsyncNotifier<OpenGraphData?> {
|
||||
final String link;
|
||||
UrlPreviewController(this.link);
|
||||
final Uri url;
|
||||
UrlPreviewController(this.url);
|
||||
|
||||
@override
|
||||
Future<OpenGraphData?> build() async {
|
||||
try {
|
||||
final homeserver = ref.watch(
|
||||
ClientStateController.provider.select((value) => value?.homeserverUrl),
|
||||
);
|
||||
if (url.host == "matrix.to") return null;
|
||||
|
||||
if (homeserver != null && !link.contains("matrix.to")) {
|
||||
{
|
||||
final response = await get(
|
||||
.parse(homeserver)
|
||||
.resolve("/_matrix/client/v1/media/preview_url")
|
||||
.replace(queryParameters: {"url": link}),
|
||||
headers: await ref.watch(HeaderController.provider.future),
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final decodedValue = json.decode(response.body);
|
||||
if (decodedValue is! Map<String, dynamic>) return null;
|
||||
|
||||
return .fromJson(
|
||||
decodedValue,
|
||||
).copyWith(imageUrl: decodedValue["og:image"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
return await ref
|
||||
.watch(ClientController.provider.notifier)
|
||||
.getUrlPreview(url);
|
||||
}
|
||||
|
||||
static final provider =
|
||||
AsyncNotifierProvider.family<
|
||||
UrlPreviewController,
|
||||
OpenGraphData?,
|
||||
String
|
||||
>(UrlPreviewController.new);
|
||||
AsyncNotifierProvider.family<UrlPreviewController, OpenGraphData?, Uri>(
|
||||
UrlPreviewController.new,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue