From c2c62c98f387bbf52b32195f4f007589267723b2 Mon Sep 17 00:00:00 2001 From: Androz2091 Date: Thu, 27 Aug 2020 13:40:25 +0200 Subject: [PATCH 1/7] feat: add options object to specify custom params for feed.items() --- src/feeds/direct-inbox.feed.ts | 18 ++++++++++++------ src/feeds/direct-pending.feed.ts | 18 ++++++++++++------ src/feeds/direct-thread.feed.ts | 15 ++++++++++----- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/feeds/direct-inbox.feed.ts b/src/feeds/direct-inbox.feed.ts index ba97f8724..1683d1f32 100644 --- a/src/feeds/direct-inbox.feed.ts +++ b/src/feeds/direct-inbox.feed.ts @@ -3,6 +3,12 @@ import { Feed } from '../core/feed'; import { DirectInboxFeedResponse, DirectInboxFeedResponseThreadsItem } from '../responses'; import { DirectThreadEntity } from '../entities'; +interface DirectInboxFeedItemsOptions { + cursor?: string; + thread_message_limit?: number; + limit?: number; +} + export class DirectInboxFeed extends Feed { @Expose() private cursor: string; @@ -15,25 +21,25 @@ export class DirectInboxFeed extends Feed({ url: `/api/v1/direct_v2/inbox/`, qs: { visual_message_return_type: 'unseen', - cursor: this.cursor, + cursor: opts.cursor ?? this.cursor, direction: this.cursor ? 'older' : void 0, seq_id: this.seqId, - thread_message_limit: 10, + thread_message_limit: opts.thread_message_limit ?? 10, persistentBadging: true, - limit: 20, + limit: opts.limit ?? 20, }, }); this.state = body; return body; } - async items() { - const response = await this.request(); + async items(opts?: DirectInboxFeedItemsOptions) { + const response = await this.request(opts); return response.inbox.threads; } diff --git a/src/feeds/direct-pending.feed.ts b/src/feeds/direct-pending.feed.ts index c2e5d4a6e..3d54a2d43 100644 --- a/src/feeds/direct-pending.feed.ts +++ b/src/feeds/direct-pending.feed.ts @@ -3,6 +3,12 @@ import { Feed } from '../core/feed'; import { DirectInboxFeedResponse, DirectInboxFeedResponseThreadsItem } from '../responses'; import { DirectThreadEntity } from '../entities'; +interface DirectPendingInboxFeedItemsOptions { + cursor?: string; + thread_message_limit?: number; + limit?: number; +} + export class DirectPendingInboxFeed extends Feed { @Expose() private cursor: string; @@ -15,25 +21,25 @@ export class DirectPendingInboxFeed extends Feed({ url: `/api/v1/direct_v2/pending_inbox/`, qs: { visual_message_return_type: 'unseen', - cursor: this.cursor, + cursor: opts.cursor ?? this.cursor, direction: this.cursor ? 'older' : void 0, seq_id: this.seqId, - thread_message_limit: 10, + thread_message_limit: opts.thread_message_limit ?? 10, persistentBadging: true, - limit: 20, + limit: opts.limit ?? 20, }, }); this.state = body; return body; } - async items() { - const response = await this.request(); + async items(opts?: DirectPendingInboxFeedItemsOptions) { + const response = await this.request(opts); return response.inbox.threads; } diff --git a/src/feeds/direct-thread.feed.ts b/src/feeds/direct-thread.feed.ts index 51041bb17..7d9bb1687 100644 --- a/src/feeds/direct-thread.feed.ts +++ b/src/feeds/direct-thread.feed.ts @@ -2,6 +2,11 @@ import { Expose } from 'class-transformer'; import { Feed } from '../core/feed'; import { DirectThreadFeedResponse, DirectThreadFeedResponseItemsItem } from '../responses'; +interface DirectThreadFeedItemsOptions { + cursor?: string; + limit?: number; +} + export class DirectThreadFeed extends Feed { public id: string; public seqId: number; @@ -11,23 +16,23 @@ export class DirectThreadFeed extends Feed({ url: `/api/v1/direct_v2/threads/${this.id}/`, qs: { visual_message_return_type: 'unseen', - cursor: this.cursor, + cursor: opts.cursor ?? this.cursor, direction: 'older', seq_id: this.seqId, - limit: 10, + limit: opts.limit ?? 10, }, }); this.state = body; return body; } - async items() { - const response = await this.request(); + async items(opts?: DirectThreadFeedItemsOptions) { + const response = await this.request(opts); return response.thread.items; } } From f6bbebca39e86ac7bcc28759dde15fc8d1fc6a91 Mon Sep 17 00:00:00 2001 From: Androz2091 Date: Thu, 27 Aug 2020 16:42:46 +0200 Subject: [PATCH 2/7] use class instead of options object --- src/core/feed.factory.ts | 17 ++++++++++++----- src/feeds/direct-inbox.feed.ts | 8 ++++++-- src/feeds/direct-pending.feed.ts | 10 +++++++--- src/feeds/direct-thread.feed.ts | 6 ++++-- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/core/feed.factory.ts b/src/core/feed.factory.ts index 7d9f30d74..5ac9c7146 100644 --- a/src/core/feed.factory.ts +++ b/src/core/feed.factory.ts @@ -84,22 +84,29 @@ export class FeedFactory { return new BlockedUsersFeed(this.client); } - public directInbox(): DirectInboxFeed { - return new DirectInboxFeed(this.client); + public directInbox(options: { limit: number; thread_message_limit: number }): DirectInboxFeed { + const feed = new DirectInboxFeed(this.client); + feed.limit = options.limit; + feed.thread_message_limit = options?.thread_message_limit; + return feed; } - public directPending(): DirectPendingInboxFeed { - return new DirectPendingInboxFeed(this.client); + public directPending(options: { limit: number; thread_message_limit: number }): DirectPendingInboxFeed { + const feed = new DirectPendingInboxFeed(this.client); + feed.limit = options.limit; + feed.thread_message_limit = options?.thread_message_limit; + return feed; } public directThread( - options: Pick, + options: Pick & { limit: number }, seqId?: number, ): DirectThreadFeed { const feed = new DirectThreadFeed(this.client); feed.id = options.thread_id; feed.cursor = options.oldest_cursor; feed.seqId = seqId; + feed.limit = options?.limit; return feed; } diff --git a/src/feeds/direct-inbox.feed.ts b/src/feeds/direct-inbox.feed.ts index 1683d1f32..2e5a274cd 100644 --- a/src/feeds/direct-inbox.feed.ts +++ b/src/feeds/direct-inbox.feed.ts @@ -15,6 +15,10 @@ export class DirectInboxFeed extends Feed Date: Thu, 27 Aug 2020 16:48:02 +0200 Subject: [PATCH 3/7] remove unused options param --- src/feeds/direct-inbox.feed.ts | 16 +++++----------- src/feeds/direct-pending.feed.ts | 12 +++--------- src/feeds/direct-thread.feed.ts | 11 +++-------- 3 files changed, 11 insertions(+), 28 deletions(-) diff --git a/src/feeds/direct-inbox.feed.ts b/src/feeds/direct-inbox.feed.ts index 2e5a274cd..1af5348a2 100644 --- a/src/feeds/direct-inbox.feed.ts +++ b/src/feeds/direct-inbox.feed.ts @@ -3,12 +3,6 @@ import { Feed } from '../core/feed'; import { DirectInboxFeedResponse, DirectInboxFeedResponseThreadsItem } from '../responses'; import { DirectThreadEntity } from '../entities'; -interface DirectInboxFeedItemsOptions { - cursor?: string; - thread_message_limit?: number; - limit?: number; -} - export class DirectInboxFeed extends Feed { @Expose() private cursor: string; @@ -25,25 +19,25 @@ export class DirectInboxFeed extends Feed({ url: `/api/v1/direct_v2/inbox/`, qs: { visual_message_return_type: 'unseen', - cursor: opts.cursor ?? this.cursor, + cursor: this.cursor ?? this.cursor, direction: this.cursor ? 'older' : void 0, seq_id: this.seqId, thread_message_limit: this.thread_message_limit ?? this.defaultThreadMessageLimit, persistentBadging: true, - limit: opts.limit ?? this.limit, + limit: this.limit ?? this.limit, }, }); this.state = body; return body; } - async items(opts?: DirectInboxFeedItemsOptions) { - const response = await this.request(opts); + async items() { + const response = await this.request(); return response.inbox.threads; } diff --git a/src/feeds/direct-pending.feed.ts b/src/feeds/direct-pending.feed.ts index be15d458d..f7e8cb721 100644 --- a/src/feeds/direct-pending.feed.ts +++ b/src/feeds/direct-pending.feed.ts @@ -3,12 +3,6 @@ import { Feed } from '../core/feed'; import { DirectInboxFeedResponse, DirectInboxFeedResponseThreadsItem } from '../responses'; import { DirectThreadEntity } from '../entities'; -interface DirectPendingInboxFeedItemsOptions { - cursor?: string; - thread_message_limit?: number; - limit?: number; -} - export class DirectPendingInboxFeed extends Feed { @Expose() private cursor: string; @@ -25,7 +19,7 @@ export class DirectPendingInboxFeed extends Feed({ url: `/api/v1/direct_v2/pending_inbox/`, qs: { @@ -42,8 +36,8 @@ export class DirectPendingInboxFeed extends Feed { public id: string; public seqId: number; @@ -18,7 +13,7 @@ export class DirectThreadFeed extends Feed({ url: `/api/v1/direct_v2/threads/${this.id}/`, qs: { @@ -33,8 +28,8 @@ export class DirectThreadFeed extends Feed Date: Thu, 27 Aug 2020 16:49:27 +0200 Subject: [PATCH 4/7] mark options as... optional --- src/core/feed.factory.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/core/feed.factory.ts b/src/core/feed.factory.ts index 5ac9c7146..2bb81ace0 100644 --- a/src/core/feed.factory.ts +++ b/src/core/feed.factory.ts @@ -84,16 +84,16 @@ export class FeedFactory { return new BlockedUsersFeed(this.client); } - public directInbox(options: { limit: number; thread_message_limit: number }): DirectInboxFeed { + public directInbox(options?: { limit: number; thread_message_limit: number }): DirectInboxFeed { const feed = new DirectInboxFeed(this.client); - feed.limit = options.limit; + feed.limit = options?.limit; feed.thread_message_limit = options?.thread_message_limit; return feed; } - public directPending(options: { limit: number; thread_message_limit: number }): DirectPendingInboxFeed { + public directPending(options?: { limit: number; thread_message_limit: number }): DirectPendingInboxFeed { const feed = new DirectPendingInboxFeed(this.client); - feed.limit = options.limit; + feed.limit = options?.limit; feed.thread_message_limit = options?.thread_message_limit; return feed; } From 4ea2295d819b49d2df78e8e96169e87178300205 Mon Sep 17 00:00:00 2001 From: Androz2091 Date: Thu, 27 Aug 2020 16:50:11 +0200 Subject: [PATCH 5/7] use default limit --- src/feeds/direct-inbox.feed.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/feeds/direct-inbox.feed.ts b/src/feeds/direct-inbox.feed.ts index 1af5348a2..8922e19ca 100644 --- a/src/feeds/direct-inbox.feed.ts +++ b/src/feeds/direct-inbox.feed.ts @@ -29,7 +29,7 @@ export class DirectInboxFeed extends Feed Date: Thu, 27 Aug 2020 17:34:47 +0200 Subject: [PATCH 6/7] changes based on the review --- src/core/feed.factory.ts | 16 +++++----------- src/feeds/direct-inbox.feed.ts | 10 ++++------ src/feeds/direct-pending.feed.ts | 10 ++++------ src/feeds/direct-thread.feed.ts | 3 +-- 4 files changed, 14 insertions(+), 25 deletions(-) diff --git a/src/core/feed.factory.ts b/src/core/feed.factory.ts index 2bb81ace0..0bd0bc94e 100644 --- a/src/core/feed.factory.ts +++ b/src/core/feed.factory.ts @@ -84,18 +84,12 @@ export class FeedFactory { return new BlockedUsersFeed(this.client); } - public directInbox(options?: { limit: number; thread_message_limit: number }): DirectInboxFeed { - const feed = new DirectInboxFeed(this.client); - feed.limit = options?.limit; - feed.thread_message_limit = options?.thread_message_limit; - return feed; + public directInbox(options: { limit?: number; thread_message_limit?: number } = {}): DirectInboxFeed { + return plainToClassFromExist(new DirectInboxFeed(this.client), options); } - public directPending(options?: { limit: number; thread_message_limit: number }): DirectPendingInboxFeed { - const feed = new DirectPendingInboxFeed(this.client); - feed.limit = options?.limit; - feed.thread_message_limit = options?.thread_message_limit; - return feed; + public directPending(options: { limit?: number; thread_message_limit?: number } = {}): DirectPendingInboxFeed { + return plainToClassFromExist(new DirectPendingInboxFeed(this.client), options); } public directThread( @@ -106,7 +100,7 @@ export class FeedFactory { feed.id = options.thread_id; feed.cursor = options.oldest_cursor; feed.seqId = seqId; - feed.limit = options?.limit; + feed.limit = options.limit; return feed; } diff --git a/src/feeds/direct-inbox.feed.ts b/src/feeds/direct-inbox.feed.ts index 8922e19ca..3049e8fb1 100644 --- a/src/feeds/direct-inbox.feed.ts +++ b/src/feeds/direct-inbox.feed.ts @@ -9,10 +9,8 @@ export class DirectInboxFeed extends Feed Date: Thu, 27 Aug 2020 17:36:00 +0200 Subject: [PATCH 7/7] remove useless condition --- src/feeds/direct-inbox.feed.ts | 2 +- src/feeds/direct-pending.feed.ts | 2 +- src/feeds/direct-thread.feed.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/feeds/direct-inbox.feed.ts b/src/feeds/direct-inbox.feed.ts index 3049e8fb1..c1607c2d8 100644 --- a/src/feeds/direct-inbox.feed.ts +++ b/src/feeds/direct-inbox.feed.ts @@ -22,7 +22,7 @@ export class DirectInboxFeed extends Feed