Skip to content

Commit

Permalink
AV-2579 Add threads support
Browse files Browse the repository at this point in the history
  • Loading branch information
mrashed-dev committed Oct 16, 2023
1 parent 13e9c76 commit aebf90e
Show file tree
Hide file tree
Showing 2 changed files with 283 additions and 0 deletions.
156 changes: 156 additions & 0 deletions src/models/threads.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { Message } from './messages.js';
import { Draft } from './drafts.js';
import { EmailName } from './events.js';
import { ListQueryParams } from './listQueryParams.js';

/**
* Interface representing a Nylas thread object.
*/
export interface Thread {
/**
* The unique identifier for the thread.
*/
id: string;
/**
* Grant ID of the Nylas account.
*/
grantId: string;
/**
* The latest message or draft in the thread.
*/
latestDraftOrMessage: Message | Draft;
/**
* Whether or not a message in a thread has attachments.
*/
hasAttachments: boolean;
/**
* Whether or not a message in a thread has drafts.
*/
hasDrafts: boolean;
/**
* A boolean indicating whether the thread is starred or not.
*/
starred: boolean;
/**
* A boolean indicating if all messages within the thread are read or not.
*/
unread: boolean;
/**
* Unix timestamp of the earliest or first message in the thread.
*/
earliestMessageDate: number;
/**
* Unix timestamp of the most recent message received in the thread.
*/
latestMessageReceivedDate: number;
/**
* Unix timestamp of the most recent message sent in the thread.
*/
latestMessageSentDate: number;
/**
* An array of participants in the thread.
*/
participants: EmailName[];
/**
* An array of message IDs in the thread.
*/
messageIds: string[];
/**
* An array of draft IDs in the thread.
*/
draftIds: string[];
/**
* An array of folder IDs the thread appears in.
*/
folders: string[];
/**
* The type of object.
*/
object: 'thread';
/**
* A short snippet of the last received message/draft body.
* This is the first 100 characters of the message body, with any HTML tags removed.
*/
snippet?: string;
/**
* The subject line of the thread.
*/
subject?: string;
}

/**
* Interface representing a request to update a thread.
*/
export interface UpdateThreadRequest {
/**
* Sets all messages in the thread as starred or unstarred.
*/
starred?: boolean;
/**
* Sets all messages in the thread as read or unread.
*/
unread?: boolean;
/**
* The IDs of the folders to apply, overwriting all previous folders for all messages in the thread.
*/
folders?: string[];
}

/**
* Interface representing the query parameters for listing drafts.
*/
export interface ListThreadsQueryParams extends ListQueryParams {
/**
* Return items with a matching literal subject.
*/
subject?: string;
/**
* Return threads that contain messages that have been sent or received from this list of email addresses.
*/
anyEmail?: string[];
/**
* Return threads containing messages sent to these email address.
*/
to?: string[];
/**
* Return threads containing messages sent from these email address.
*/
from?: string[];
/**
* Return threads containing messages cc'd on these email address.
*/
cc?: string[];
/**
* Return threads containing messages bcc'd on these email address.
*/
bcc?: string[];
/**
* Return threads with messages that belong to these specified folder IDs.
*/
in?: string[];
/**
* Return threads with unread messages.
*/
unread?: boolean;
/**
* Return threads with starred messages.
*/
starred?: boolean;
/**
* Return threads whose most recent message was received before this Unix timestamp.
*/
latestMessageBefore?: number;
/**
* Return threads whose most recent message was received after this Unix timestamp.
*/
latestMessageAfter?: number;
/**
* Return threads with messages that contain attachments.
*/
hasAttachment?: boolean;
/**
* The provider-specific query string used to search threads.
* Available for Google only.
*/
searchQueryNative?: string;
}
127 changes: 127 additions & 0 deletions src/resources/threads.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import {
ListThreadsQueryParams,
Thread,
UpdateThreadRequest,
} from '../models/threads.js';
import { AsyncListResponse, Resource } from './resource.js';
import { Overrides } from '../config.js';
import {
NylasDeleteResponse,
NylasListResponse,
NylasResponse,
} from '../models/response.js';

/**
* The parameters for the {@link Threads.list} method
* @property identifier The identifier of the grant to act upon
* @property queryParams The query parameters to include in the request
*/
export interface ListThreadsParams {
identifier: string;
queryParams?: ListThreadsQueryParams;
}

/**
* The parameters for the {@link Threads.find} method
* @property identifier The identifier of the grant to act upon
* @property threadId The id of the thread to retrieve.
* @property queryParams The query parameters to include in the request
*/
export interface FindThreadParams {
identifier: string;
threadId: string;
}

/**
* The parameters for the {@link Threads.update} method
* @property identifier The identifier of the grant to act upon
* @property threadId The id of the thread to update
* @property requestBody The values to update the thread with
*/
export interface UpdateThreadParams {
identifier: string;
threadId: string;
requestBody: UpdateThreadRequest;
}

/**
* The parameters for the {@link Threads.destroy} method
* @property identifier The identifier of the grant to act upon
* @property threadId The id of the thread to delete
*/
export interface DestroyThreadParams {
identifier: string;
threadId: string;
}

/**
* Nylas Threads API
*
* The Nylas Threads API allows you to list, find, update, and delete threads on user accounts.
*/
export class Threads extends Resource {
/**
* Return all Threads
* @return A list of threads
*/
public list({
identifier,
queryParams,
overrides,
}: ListThreadsParams & Overrides): AsyncListResponse<
NylasListResponse<Thread>
> {
return super._list<NylasListResponse<Thread>>({
queryParams,
overrides,
path: `/v3/grants/${identifier}/threads`,
});
}

/**
* Return a Thread
* @return The thread
*/
public find({
identifier,
threadId,
overrides,
}: FindThreadParams & Overrides): Promise<NylasResponse<Thread>> {
return super._find({
path: `/v3/grants/${identifier}/threads/${threadId}`,
overrides,
});
}

/**
* Update a Thread
* @return The updated thread
*/
public update({
identifier,
threadId,
requestBody,
overrides,
}: UpdateThreadParams & Overrides): Promise<NylasResponse<Thread>> {
return super._update({
path: `/v3/grants/${identifier}/threads/${threadId}`,
requestBody,
overrides,
});
}

/**
* Delete a Thread
* @return The deleted thread
*/
public destroy({
identifier,
threadId,
overrides,
}: DestroyThreadParams & Overrides): Promise<NylasDeleteResponse> {
return super._destroy({
path: `/v3/grants/${identifier}/threads/${threadId}`,
overrides,
});
}
}

0 comments on commit aebf90e

Please sign in to comment.