## Graph Api:
### Sdk:
* https://learn.microsoft.com/en-us/graph/sdks/sdk-installation
* https://github.com/microsoftgraph/msgraph-sdk-java
### Oauth flow:
* Use current oauth flow with some modification for `sdk`
### Email folders:
* Permissions: `Mail.ReadBasic`, `Mail.Read`, `Mail.ReadWrite`
* Api: `GET` `/me/mailFolders/?includeHiddenFolders=true`
* Response:
> HTTP/1.1 200 OK
> Content-type: application/json
> {
> "@odata.context": "https://graph.microsoft.com/beta/$metadata#users('68ca8ec0-11f8-456b-a785-70d9936650d5')/mailFolders",
> "value": [
> {
> "id": "AAMkADg3NTY5MDg4LWMzYmQtNDQzNi05OTgwLWAAA=",
> "displayName": "Clutters",
> "parentFolderId": "AAMkADg3NTY5MDg4LWMzYmQtEIAAA=",
> "childFolderCount": 0,
> "unreadItemCount": 0,
> "totalItemCount": 0,
> "isHidden": true
> }
> ]
> }
* For child folder:
* Api: `GET` `/me/mailFolders/{id}/childFolders`
* `id` parent folder id
* https://learn.microsoft.com/en-us/graph/api/user-list-mailfolders?view=graph-rest-1.0&tabs=http
### Create new folder:
* Permission: `Mail.ReadWrite`
* Api: `POST` `/me/mailFolders`
* Sub-folders:
* Api: `POST` `/me/mailFolders/{id}/childFolders`
* `id` parent folder id
* Example:
> POST https://graph.microsoft.com/v1.0/me/mailFolders
> Content-type: application/json
>
> {
> "displayName": "Clutter",
> "isHidden": true
> }
### Delete folder:
* Permission: `Mail.ReadWrite`
* Api: `DELETE` `/me/mailFolders/{id}`
* `id` is folder ID or well-known folder name
### Rename folder:
* Permission: `Mail.ReadWrite`
* Api: `PATCH` `https://graph.microsoft.com/v1.0/me/mailFolders/{id}`
* `id` is folder ID or well-known folder name
### Move folder:
* Permission: `Mail.ReadWrite`
* Api: `POST` `/me/mailFolders/{id}/copy`
* Api: `DELETE` `/me/mailFolders/{id}`
* `id` is folder ID or well-known folder name
* Copy folder and delete old one
### New email idle:
* Need to use webhook
> * POST https://graph.microsoft.com/v1.0/subscriptions
Content-Type: application/json
{
"changeType": "created,updated",
"notificationUrl": "https://webhook.azurewebsites.net/notificationClient",
"resource": "/me/mailfolders('inbox')/messages",
"expirationDateTime": "2016-03-20T11:00:00.0000000Z",
"clientState": "SecretClientState"
}
* https://learn.microsoft.com/en-us/graph/webhooks?tabs=http#creating-a-subscription
* Can be configured to send change status and email identifier
* Latency: unknown
* Has a life time limit
### Email delete idle(get deleted email ids/something):
* Need to use webhook
> * POST https://graph.microsoft.com/v1.0/subscriptions
Content-Type: application/json
{
"changeType": "created,updated",
"notificationUrl": "https://webhook.azurewebsites.net/notificationClient",
"resource": "/me/mailfolders('inbox')/messages",
"expirationDateTime": "2016-03-20T11:00:00.0000000Z",
"clientState": "SecretClientState"
}
* https://learn.microsoft.com/en-us/graph/webhooks?tabs=http#creating-a-subscription
* Can be configured to send change status and email identifier
* Latency: unknown
* Has a life time limit
### Email read status change idle:
* Need to use webhook
> * POST https://graph.microsoft.com/v1.0/subscriptions
Content-Type: application/json
{
"changeType": "created,updated",
"notificationUrl": "https://webhook.azurewebsites.net/notificationClient",
"resource": "/me/mailfolders('inbox')/messages",
"expirationDateTime": "2016-03-20T11:00:00.0000000Z",
"clientState": "SecretClientState"
}
* https://learn.microsoft.com/en-us/graph/webhooks?tabs=http#creating-a-subscription
* Can be configured to send change status and email identifier
* Latency: unknown
* Has a life time limit
### Email list fetch:
* Permissions: `Mail.ReadBasic`, `Mail.Read`, `Mail.ReadWrite`
* Api: `GET` `/me/mailFolders/{id}/messages`
* `id` folder id or well known folder name
* Use `$top` to change response list size within 1-1000
* Use `$select` change response properties
* Use `$select=internetMessageHeaders` for headers
* Use `$expand=attachments` for attachments
* Response with 100 emails with full body can result in `gateway timeout (HTTP 504)` error
* Use `@odata.nextLink` in response to get next batch
* https://learn.microsoft.com/en-us/graph/api/user-list-messages?view=graph-rest-1.0&tabs=http
* https://learn.microsoft.com/en-us/graph/delta-query-messages#example-1-synchronize-messages-in-a-folder
#### Meta fetch:
* Api: `GET` `https://graph.microsoft.com/v1.0/me/mailFolders/{id}/messages?$top=100&$select=internetMessageHeaders,sentDateTime,hasAttachments,subject,isRead,from,toRecipients,ccRecipients,replyTo`
* `id` is folder full name or id
#### Body fetch:
* Api: `GET` `/me/messages/{id}?select=body&expand=attachments`
* Api: `GET` `/me/messages/{id}/$value`
* `id` is `id` field provided in response
* Returns `Mime Message` with body
#### Old email update:
* Api: `GET` `https://graph.microsoft.com/v1.0/me/mailFolders/Inbox/messages?$top=1000&$select=internetMessageId,id,isRead,flag`
### Email meta data update for all emails in db:
* Can be done using email meta fetch process by increasing `$top` value
### Email send:
* Permissions: `Mail.Send`
* Api: `POST` `/me/sendMail`
* Send `Mime Message` with attachments encoded in `Base64`
* Content type: `text/plain`
* https://learn.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0&tabs=http
### Draft email save:
* Permissions: Mail.ReadWrite
* Api: `POST` `/me/messages`
* Save `Mime Message` with attachments encoded in `Base64`
* Content type: `text/plain`
* https://learn.microsoft.com/en-us/graph/api/inferenceclassification-post-overrides?view=graph-rest-1.0&tabs=http
### Email read/unread operation:
* Permission: `Mail.ReadWrite`
* Api: `PATCH` `/me/mailFolders/{id1}/messages/{id2}`
* `id1` folder name or id
* `id2` email `id` field
* Body: `isRead` with `Boolean` value
* https://learn.microsoft.com/en-us/graph/api/message-update?view=graph-rest-1.0&tabs=http
### Email move:
* Permission: `Mail.ReadWrite`
* Api: `POST` `/me/mailFolders/{id1}/messages/{id2}/move`
* `id1` folder id or name
* `id2` email `id` field
* Request body: `destinationId` destination folder id
* Example:
> Content-type: application/json
>
> {
> "destinationId": "deleteditems"
> }
* https://learn.microsoft.com/en-us/graph/api/message-move?view=graph-rest-1.0&tabs=http
### Email copy:
* Permission: `Mail.ReadWrite`
* Api: `POST` `/me/mailFolders/{id1}/messages/{id2}/copy`
* `id1` folder id or name
* `id2` email `id` field
* Request body: `destinationId` destination folder id
* https://learn.microsoft.com/en-us/graph/api/message-copy?view=graph-rest-1.0&tabs=http
### Email delete:
* Permission: `Mail.ReadWrite`
* Api: `DELETE` `/me/mailFolders/{id1}/messages/{id2}`
* `id1` folder id or name
* `id2` email `id` field
* https://learn.microsoft.com/en-us/graph/api/message-delete?view=graph-rest-1.0&tabs=http
### Conversation list (how to build):
* We can get a conversation id while fatching emails. But may not suitable with our process.
### Query parameters:
* These parameters can be used for most of the requests
* https://learn.microsoft.com/en-us/graph/query-parameters?tabs=http
### Database structure:
* #### Database changes:
* Need some changes
* #### Database migration:
* Delete old database and create new one.
* Remove old data. No migration.
### Class refactor:
## This process will only be used for users using **Microsoft OAuth**. Users using username/password will have old flow.