Skip to content

Commit a0eb950

Browse files
authored
Merge pull request linagora#5 from ee-email/email-query-get-support
Initial support for email query and get actions
2 parents 7c9e583 + 19453cb commit a0eb950

File tree

3 files changed

+121
-31
lines changed

3 files changed

+121
-31
lines changed

.gitignore

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
node_modules
2-
/lib
2+
/lib
3+
4+
# IDEs and editors
5+
/.idea
6+
.project
7+
.classpath
8+
.c9/
9+
*.launch
10+
.settings/
11+
*.sublime-workspace

src/index.ts

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import { HttpRequest } from './http-request';
2-
import { IGetArguments, IMailbox, IMailboxProperties, ISession } from './types';
2+
import {
3+
IEmailFilterCondition,
4+
IEmailGetResponse,
5+
IEmailProperties,
6+
IEmailQueryResponse,
7+
IGetArguments,
8+
IMailboxGetResponse,
9+
IMailboxProperties,
10+
IQueryArguments,
11+
ISession,
12+
} from './types';
313

414
export class Client {
515
private readonly DEFAULT_USING = [
@@ -69,38 +79,20 @@ export class Client {
6979
const accountIds = this.getAccountIds();
7080

7181
if (accountIds.length === 0) {
72-
throw new Error(
73-
'No account available for this session'
74-
);
82+
throw new Error('No account available for this session');
7583
}
7684

7785
return accountIds[0];
7886
}
7987

8088
public mailbox_get(
8189
args: IGetArguments<IMailboxProperties>
82-
): Promise<{
83-
accountId: string | null;
84-
state: string;
85-
list: IMailbox[];
86-
notFound: string;
87-
}> {
90+
): Promise<IMailboxGetResponse> {
8891
const apiUrl = this.overriddenApiUrl || this.getSession().apiUrl;
8992
return this.httpRequest
9093
.post<{
9194
sessionState: string;
92-
methodResponses: [
93-
[
94-
'Mailbox/get',
95-
{
96-
accountId: string;
97-
state: string;
98-
list: IMailbox[];
99-
notFound: string;
100-
},
101-
string
102-
]
103-
];
95+
methodResponses: [['Mailbox/get', IMailboxGetResponse, string]];
10496
}>(
10597
apiUrl,
10698
{
@@ -112,6 +104,44 @@ export class Client {
112104
.then((response) => response.methodResponses[0][1]);
113105
}
114106

107+
public email_query(
108+
args: IQueryArguments<IEmailFilterCondition>
109+
): Promise<IEmailQueryResponse> {
110+
const apiUrl = this.overriddenApiUrl || this.getSession().apiUrl;
111+
return this.httpRequest
112+
.post<{
113+
sessionState: string;
114+
methodResponses: [['Email/query', IEmailQueryResponse, string]];
115+
}>(
116+
apiUrl,
117+
{
118+
using: this.getCapabilities(),
119+
methodCalls: [['Email/query', this.replaceAccountId(args), '0']],
120+
},
121+
this.httpHeaders
122+
)
123+
.then((response) => response.methodResponses[0][1]);
124+
}
125+
126+
public email_get(
127+
args: IGetArguments<IEmailProperties>
128+
): Promise<IEmailGetResponse> {
129+
const apiUrl = this.overriddenApiUrl || this.getSession().apiUrl;
130+
return this.httpRequest
131+
.post<{
132+
sessionState: string;
133+
methodResponses: [['Email/get', IEmailGetResponse, string]];
134+
}>(
135+
apiUrl,
136+
{
137+
using: this.getCapabilities(),
138+
methodCalls: [['Email/get', this.replaceAccountId(args), '0']],
139+
},
140+
this.httpHeaders
141+
)
142+
.then((response) => response.methodResponses[0][1]);
143+
}
144+
115145
private replaceAccountId<U extends { accountId: string }>(input: U): U {
116146
return {
117147
...input,

src/types.ts

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
export interface ITypeMap {
22
['Mailbox/get']: IMailbox;
3+
['Email/query']: string[];
4+
['Email/get']: IEmailProperties;
35
}
46

57
/**
@@ -17,6 +19,11 @@ export interface IGetArguments<Properties> {
1719
properties?: Properties[];
1820
}
1921

22+
export interface IQueryArguments<FilterCondition> {
23+
accountId: string;
24+
filter?: FilterCondition;
25+
}
26+
2027
export interface IRequest {
2128
using: string[];
2229
methodCalls: IMethodCall[];
@@ -52,28 +59,44 @@ export interface ISession {
5259
state: string;
5360
}
5461

55-
export type Emailer = string;
56-
5762
export type EmailHeader = string;
5863

5964
export type Attachment = File;
6065

61-
export interface IEmail {
66+
export interface IEmailProperties {
6267
id: string;
6368
blobId: string;
6469
threadId: string;
6570
mailboxIds: { [key: string]: boolean };
66-
keywords: { [key: string]: boolean };
67-
from: Emailer[] | null;
68-
to: Emailer[] | null;
71+
keywords: IEmailKeywords;
72+
from: IEmailAddress[] | null;
73+
to: IEmailAddress[] | null;
6974
subject: string;
70-
date: Date;
7175
size: number;
7276
preview: string;
7377
attachments: Attachment[] | null;
7478
createdModSeq: number;
7579
updatedModSeq: number;
76-
deleted: Date | null;
80+
receivedAt: IUtcDate;
81+
}
82+
83+
export type IUtcDate = string;
84+
export type ITrue = true;
85+
86+
export interface IEmailKeywords {
87+
$draft?: ITrue;
88+
$seen?: ITrue;
89+
$flagged?: ITrue;
90+
$answered?: ITrue;
91+
$forwarded?: ITrue;
92+
$phishing?: ITrue;
93+
$junk?: ITrue;
94+
$notjunk?: ITrue;
95+
}
96+
97+
export interface IEmailAddress {
98+
name: string;
99+
email: string;
77100
}
78101

79102
export interface IThreadEmail {
@@ -125,6 +148,13 @@ export interface IMailboxProperties {
125148
deleted?: Date;
126149
}
127150

151+
export interface IMailboxGetResponse {
152+
accountId: string | null;
153+
state: string;
154+
list: IMailbox[];
155+
notFound: string[];
156+
}
157+
128158
export interface IMaiboxEmailList {
129159
id: string; // mailboxId . (Max_Int64 - EmailDate) . uid
130160
messageId: string;
@@ -194,3 +224,24 @@ export interface IEmailBodyPart {
194224
hasAttachment: boolean;
195225
preview: string;
196226
}
227+
228+
export interface IEmailFilterCondition {
229+
inMailbox: string;
230+
}
231+
232+
export interface IEmailQueryResponse {
233+
accountId: string;
234+
queryState: string;
235+
canCalculateChanges: boolean;
236+
position: number;
237+
ids: string[];
238+
total?: number;
239+
limit?: number;
240+
}
241+
242+
export interface IEmailGetResponse {
243+
accountId: string | null;
244+
state: string;
245+
list: IEmailProperties[];
246+
notFound: string[];
247+
}

0 commit comments

Comments
 (0)