From 5567708fe9645de6e00fcc8b4df8eddc420072ae Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Mon, 13 Dec 2021 15:50:33 +0530 Subject: [PATCH 1/2] feat(llc): Add enrichUrl endpoint. Signed-off-by: xsahil03x --- .../stream_chat/lib/src/client/client.dart | 4 ++ .../lib/src/core/api/general_api.dart | 12 ++++++ .../lib/src/core/api/responses.dart | 40 +++++++++++++++++++ .../lib/src/core/api/responses.g.dart | 15 +++++++ .../test/src/client/client_test.dart | 40 +++++++++++++------ .../test/src/core/api/general_api_test.dart | 39 ++++++++++++++++-- 6 files changed, 133 insertions(+), 17 deletions(-) diff --git a/packages/stream_chat/lib/src/client/client.dart b/packages/stream_chat/lib/src/client/client.dart index 29e500ce..b2ae3657 100644 --- a/packages/stream_chat/lib/src/client/client.dart +++ b/packages/stream_chat/lib/src/client/client.dart @@ -1316,6 +1316,10 @@ class StreamChatClient { }, ); + /// Get OpenGraph data of the given [url]. + Future enrichUrl(String url) => + _chatApi.general.enrichUrl(url); + /// Closes the [_ws] connection and resets the [state] /// If [flushChatPersistence] is true the client deletes all offline /// user's data. diff --git a/packages/stream_chat/lib/src/core/api/general_api.dart b/packages/stream_chat/lib/src/core/api/general_api.dart index fa08a475..9a2e773c 100644 --- a/packages/stream_chat/lib/src/core/api/general_api.dart +++ b/packages/stream_chat/lib/src/core/api/general_api.dart @@ -96,4 +96,16 @@ class GeneralApi { return QueryMembersResponse.fromJson(response.data); } + + /// Get OpenGraph data of the given [url]. + Future enrichUrl(String url) async { + final response = await _client.get( + '/og', + queryParameters: { + 'url': url, + }, + ); + + return OGAttachmentResponse.fromJson(response.data); + } } diff --git a/packages/stream_chat/lib/src/core/api/responses.dart b/packages/stream_chat/lib/src/core/api/responses.dart index 5b52b65d..937e0150 100644 --- a/packages/stream_chat/lib/src/core/api/responses.dart +++ b/packages/stream_chat/lib/src/core/api/responses.dart @@ -442,3 +442,43 @@ class ChannelStateResponse extends _BaseResponse { static ChannelStateResponse fromJson(Map json) => _$ChannelStateResponseFromJson(json); } + +/// Model response for [Client.enrichUrl] api call. +@JsonSerializable(createToJson: false) +class OGAttachmentResponse extends _BaseResponse { + /// The URL of the page that was scraped. + late String ogScrapeUrl; + + /// The URL of the asset. + String? assetUrl; + + /// The URL of the author. + String? authorLink; + + /// The name of the author. + String? authorName; + + /// The URL of the image. + String? imageUrl; + + /// The text of the attachment. + String? text; + + /// The URL of the thumbnail. + String? thumbUrl; + + /// The title of the attachment. + String? title; + + /// The URL of the title. + String? titleLink; + + /// The type of the attachment. + /// + /// 'video' | 'audio' | 'image' + String? type; + + /// Create a new instance from a [json]. + static OGAttachmentResponse fromJson(Map json) => + _$OGAttachmentResponseFromJson(json); +} diff --git a/packages/stream_chat/lib/src/core/api/responses.g.dart b/packages/stream_chat/lib/src/core/api/responses.g.dart index 13d7d77b..fbcdd252 100644 --- a/packages/stream_chat/lib/src/core/api/responses.g.dart +++ b/packages/stream_chat/lib/src/core/api/responses.g.dart @@ -273,3 +273,18 @@ ChannelStateResponse _$ChannelStateResponseFromJson( ?.map((e) => Read.fromJson(e as Map)) .toList() ?? []; + +OGAttachmentResponse _$OGAttachmentResponseFromJson( + Map json) => + OGAttachmentResponse() + ..duration = json['duration'] as String? + ..ogScrapeUrl = json['og_scrape_url'] as String + ..assetUrl = json['asset_url'] as String? + ..authorLink = json['author_link'] as String? + ..authorName = json['author_name'] as String? + ..imageUrl = json['image_url'] as String? + ..text = json['text'] as String? + ..thumbUrl = json['thumb_url'] as String? + ..title = json['title'] as String? + ..titleLink = json['title_link'] as String? + ..type = json['type'] as String?; diff --git a/packages/stream_chat/test/src/client/client_test.dart b/packages/stream_chat/test/src/client/client_test.dart index 36c5919e..22e0edf8 100644 --- a/packages/stream_chat/test/src/client/client_test.dart +++ b/packages/stream_chat/test/src/client/client_test.dart @@ -1,20 +1,7 @@ import 'package:mocktail/mocktail.dart'; -import 'package:stream_chat/src/client/client.dart'; import 'package:stream_chat/src/core/api/device_api.dart'; -import 'package:stream_chat/src/core/api/requests.dart'; -import 'package:stream_chat/src/core/api/responses.dart'; -import 'package:stream_chat/src/core/error/error.dart'; import 'package:stream_chat/src/core/http/token.dart'; -import 'package:stream_chat/src/core/models/channel_model.dart'; -import 'package:stream_chat/src/core/models/event.dart'; -import 'package:stream_chat/src/core/models/filter.dart'; -import 'package:stream_chat/src/core/models/message.dart'; -import 'package:stream_chat/src/core/models/own_user.dart'; -import 'package:stream_chat/src/core/models/user.dart'; -import 'package:stream_chat/src/event_type.dart'; -import 'package:stream_chat/src/ws/connection_status.dart'; import 'package:stream_chat/stream_chat.dart'; -import 'package:test/scaffolding.dart'; import 'package:test/test.dart'; import '../fakes.dart'; @@ -2314,6 +2301,33 @@ void main() { verifyNoMoreInteractions(api.message); }); + test('`.enrichUrl`', () async { + const url = + 'https://www.techyourchance.com/finite-state-machine-with-unit-tests-real-world-example'; + + when(() => api.general.enrichUrl(url)).thenAnswer( + (_) async => OGAttachmentResponse() + ..type = 'image' + ..ogScrapeUrl = url + ..authorName = 'TechYourChance' + ..title = 'Finite State Machine with Unit Tests: Real World Example', + ); + + final res = await client.enrichUrl(url); + + expect(res, isNotNull); + expect(res.type, 'image'); + expect(res.ogScrapeUrl, url); + expect(res.authorName, 'TechYourChance'); + expect( + res.title, + 'Finite State Machine with Unit Tests: Real World Example', + ); + + verify(() => api.general.enrichUrl(url)).called(1); + verifyNoMoreInteractions(api.general); + }); + test( '''setting the `currentUser` should also compute and update the unreadCounts''', () { diff --git a/packages/stream_chat/test/src/core/api/general_api_test.dart b/packages/stream_chat/test/src/core/api/general_api_test.dart index 570d7c77..0a27cce4 100644 --- a/packages/stream_chat/test/src/core/api/general_api_test.dart +++ b/packages/stream_chat/test/src/core/api/general_api_test.dart @@ -3,10 +3,6 @@ import 'dart:convert'; import 'package:dio/dio.dart'; import 'package:mocktail/mocktail.dart'; import 'package:stream_chat/src/core/api/general_api.dart'; -import 'package:stream_chat/src/core/api/requests.dart'; -import 'package:stream_chat/src/core/models/channel_model.dart'; -import 'package:stream_chat/src/core/models/event.dart'; -import 'package:stream_chat/src/core/models/filter.dart'; import 'package:stream_chat/stream_chat.dart'; import 'package:test/test.dart'; @@ -281,4 +277,39 @@ void main() { verifyNoMoreInteractions(client); }); }); + + test('enrichUrl', () async { + const path = '/og'; + const url = + 'https://www.techyourchance.com/finite-state-machine-with-unit-tests-real-world-example'; + + when(() => client.get( + path, + queryParameters: {'url': url}, + )).thenAnswer((_) async => successResponse(path, data: { + 'type': 'image', + 'og_scrape_url': url, + 'author_name': 'TechYourChance', + 'title': 'Finite State Machine with Unit Tests: Real World Example', + })); + + final res = await generalApi.enrichUrl(url); + + expect(res, isNotNull); + expect(res.type, 'image'); + expect(res.ogScrapeUrl, url); + expect(res.authorName, 'TechYourChance'); + expect( + res.title, + 'Finite State Machine with Unit Tests: Real World Example', + ); + + verify( + () => client.get( + path, + queryParameters: {'url': url}, + ), + ).called(1); + verifyNoMoreInteractions(client); + }); } From a5242627f9828e9a901bc3245ebcfeee21dc908e Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Mon, 13 Dec 2021 15:53:36 +0530 Subject: [PATCH 2/2] chore(llc): update CHANGELOG.md Signed-off-by: xsahil03x --- packages/stream_chat/CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/stream_chat/CHANGELOG.md b/packages/stream_chat/CHANGELOG.md index 940d25c5..f7e02ba4 100644 --- a/packages/stream_chat/CHANGELOG.md +++ b/packages/stream_chat/CHANGELOG.md @@ -1,3 +1,9 @@ +## Upcoming + +✅ Added + +- Added `client.enrichUrl` endpoint for enriching URLs with metadata. + ## 3.3.1 🐞 Fixed