Search in sources :

Example 1 with OpenGraph

use of org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil.OpenGraph in project Signal-Android by WhisperSystems.

the class LinkPreviewRepository method fetchMetadata.

@NonNull
private RequestController fetchMetadata(@NonNull String url, Consumer<Metadata> callback) {
    Call call = client.newCall(new Request.Builder().url(url).cacheControl(NO_CACHE).build());
    call.enqueue(new okhttp3.Callback() {

        @Override
        public void onFailure(@NonNull Call call, @NonNull IOException e) {
            Log.w(TAG, "Request failed.", e);
            callback.accept(Metadata.empty());
        }

        @Override
        public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
            if (!response.isSuccessful()) {
                Log.w(TAG, "Non-successful response. Code: " + response.code());
                callback.accept(Metadata.empty());
                return;
            } else if (response.body() == null) {
                Log.w(TAG, "No response body.");
                callback.accept(Metadata.empty());
                return;
            }
            String body = OkHttpUtil.readAsString(response.body(), FAILSAFE_MAX_TEXT_SIZE);
            OpenGraph openGraph = LinkPreviewUtil.parseOpenGraphFields(body);
            Optional<String> title = openGraph.getTitle();
            Optional<String> description = openGraph.getDescription();
            Optional<String> imageUrl = openGraph.getImageUrl();
            long date = openGraph.getDate();
            if (imageUrl.isPresent() && !LinkPreviewUtil.isValidPreviewUrl(imageUrl.get())) {
                Log.i(TAG, "Image URL was invalid or for a non-whitelisted domain. Skipping.");
                imageUrl = Optional.absent();
            }
            callback.accept(new Metadata(title, description, date, imageUrl));
        }
    });
    return new CallRequestController(call);
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Optional(org.whispersystems.libsignal.util.guava.Optional) IOException(java.io.IOException) OpenGraph(org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil.OpenGraph) CallRequestController(org.thoughtcrime.securesms.net.CallRequestController) NonNull(androidx.annotation.NonNull)

Example 2 with OpenGraph

use of org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil.OpenGraph in project Signal-Android by signalapp.

the class LinkPreviewRepository method fetchMetadata.

@NonNull
private RequestController fetchMetadata(@NonNull String url, Consumer<Metadata> callback) {
    Call call = client.newCall(new Request.Builder().url(url).cacheControl(NO_CACHE).build());
    call.enqueue(new okhttp3.Callback() {

        @Override
        public void onFailure(@NonNull Call call, @NonNull IOException e) {
            Log.w(TAG, "Request failed.", e);
            callback.accept(Metadata.empty());
        }

        @Override
        public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
            if (!response.isSuccessful()) {
                Log.w(TAG, "Non-successful response. Code: " + response.code());
                callback.accept(Metadata.empty());
                return;
            } else if (response.body() == null) {
                Log.w(TAG, "No response body.");
                callback.accept(Metadata.empty());
                return;
            }
            String body = OkHttpUtil.readAsString(response.body(), FAILSAFE_MAX_TEXT_SIZE);
            OpenGraph openGraph = LinkPreviewUtil.parseOpenGraphFields(body);
            Optional<String> title = openGraph.getTitle();
            Optional<String> description = openGraph.getDescription();
            Optional<String> imageUrl = openGraph.getImageUrl();
            long date = openGraph.getDate();
            if (imageUrl.isPresent() && !LinkPreviewUtil.isValidPreviewUrl(imageUrl.get())) {
                Log.i(TAG, "Image URL was invalid or for a non-whitelisted domain. Skipping.");
                imageUrl = Optional.absent();
            }
            callback.accept(new Metadata(title, description, date, imageUrl));
        }
    });
    return new CallRequestController(call);
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Optional(org.whispersystems.libsignal.util.guava.Optional) IOException(java.io.IOException) OpenGraph(org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil.OpenGraph) CallRequestController(org.thoughtcrime.securesms.net.CallRequestController) NonNull(androidx.annotation.NonNull)

Aggregations

NonNull (androidx.annotation.NonNull)2 IOException (java.io.IOException)2 Call (okhttp3.Call)2 Response (okhttp3.Response)2 OpenGraph (org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil.OpenGraph)2 CallRequestController (org.thoughtcrime.securesms.net.CallRequestController)2 Optional (org.whispersystems.libsignal.util.guava.Optional)2