Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] Create Notion Page From RocketChat #20

Merged
merged 12 commits into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions definition/handlers/IHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export interface IHandler extends Omit<ICommandUtilityParams, "params"> {
oAuth2Storage: OAuth2Storage;
roomInteractionStorage: RoomInteractionStorage;
createNotionDatabase(): Promise<void>;
commentOnPages(): Promise<void>;
createNotionPageOrRecord(): Promise<void>;
}

export type IHanderParams = Omit<ICommandUtilityParams, "params">;
26 changes: 26 additions & 0 deletions definition/lib/INotion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ export interface INotionSDK extends INotion {
pageId: string,
comment: string
): Promise<ICommentInfo | Error>;
searchPagesAndDatabases(
token: string
): Promise<Array<IPage | IDatabase> | Error>;
createPage(
token: string,
page: IPage,
prop: IPageProperties
): Promise<INotionPage | Error>;
}

export interface IParentPage {
Expand All @@ -50,6 +58,16 @@ export interface IPage {
parent: IParentPage;
}

export interface IParentDatabase {
type: NotionObjectTypes.DATABASE_ID;
database_id: string;
}

export interface IDatabase {
info: INotionDatabase;
parent: IParentDatabase;
}

export interface INotionDatabase {
name: string;
link: string;
Expand Down Expand Up @@ -88,3 +106,11 @@ interface INotionBot {
};
workspace_name: string;
}

export interface IPageProperties {
title: string;
}

export interface INotionPage extends INotionDatabase {
title: string;
}
6 changes: 6 additions & 0 deletions enum/Notion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export enum NotionApi {
CREATE_DATABASE = `https://api.notion.com/v1/databases`,
COMMENTS = `https://api.notion.com/v1/comments`,
USERS = `https://api.notion.com/v1/users`,
PAGES = `https://api.notion.com/v1/pages`,
}

export enum Notion {
Expand All @@ -38,4 +39,9 @@ export enum NotionObjectTypes {
COMMENT = "comment",
PARENT = "parent",
MENTION = "mention",
DATABASE_ID = "database_id",
OBJECT = "object",
TITLE = "title",
INFO = "info",
NAME = "name",
}
1 change: 1 addition & 0 deletions enum/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export enum Messages {
HELPER_COMMANDS = `• use \`/notion connect\` to connect your workspace
• use \`/notion disconnect\` to disconnect workspace
• use \`/notion comment\` to comment on notion page
• use \`/notion create\` to create page or record
• use \`/notion create database\` to create database
`,
HELPER_TEXT = `:wave: Need some help with \`/notion\`?`,
Expand Down
2 changes: 2 additions & 0 deletions enum/modals/NotionDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ export enum DatabaseModal {
REMOVE_OPTION_BLOCK = "remove-option-notion-database-block-id",
SELECT_PROPERTY_OPTION_NAME = "select-property-option-name-action",
PROPERTY_TYPE_TITLE = "title",
OVERFLOW_MENU_ACTION = "create-notion-database-overflow-menu-action-id",
OVERFLOW_MENU_TEXT = "Create Database",
}
19 changes: 19 additions & 0 deletions enum/modals/NotionPageOrRecord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export enum NotionPageOrRecord {
VIEW_ID = "notion-page-or-record-view-id",
TITLE = "Create Page or Record",
CREATE = "Create",
CREATE_ACTION = "create-page-or-record-action-id",
CREATE_BLOCK = "create-page-or-record-block-id",
CLOSE = "Close",
CLOSE_ACTION = "close-page-or-record-action-id",
CLOSE_BLOCK = "close-page-or-record-block-id",
TITLE_PLACEHOLDER = "Enter Title of Page or Record",
TITLE_LABEL = "Title *",
TITLE_BLOCK = "title-page-or-record-block-id",
TITLE_ACTION = "title-page-or-record-action-id",
CHANGE_DATABASE_TEXT = "Change Database",
CHANGE_DATABASE_ACTION = "create-page-or-record-change-database-action-id",
ADD_PROPERTY_ACTION = "add-property-create-page-or-record-action-id",
ADD_PROPERTY_BLOCK = "add-property-create-page-or-record-action-id",
ADD_PROPERTY_BUTTON_TEXT = "Add Property",
}
6 changes: 6 additions & 0 deletions enum/modals/common/SearchPageAndDatabaseComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum SearchPageAndDatabase {
PLACEHOLDER = "Select a Page or Database",
BLOCK_ID = "search-page-and-database-component-block-id",
ACTION_ID = "search-page-and-database-component-action-id",
LABEL = "Page or Database Name *",
}
4 changes: 4 additions & 0 deletions src/commands/CommandUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ export class CommandUtility implements ICommandUtility {
await handler.commentOnPages();
break;
}
case CommandParam.CREATE: {
await handler.createNotionPageOrRecord();
break;
}
case CommandParam.HELP:
default: {
await sendHelperNotification(
Expand Down
134 changes: 132 additions & 2 deletions src/handlers/ExecuteBlockActionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ import { createCommentContextualBar } from "../modals/createCommentContextualBar
import { CommentPage } from "../../enum/modals/CommentPage";
import { NotionObjectTypes } from "../../enum/Notion";
import { ITokenInfo } from "../../definition/authorization/IOAuth2Storage";
import { ICommentInfo } from "../../definition/lib/INotion";
import {
ICommentInfo,
IDatabase,
IPage,
IParentDatabase,
IParentPage,
} from "../../definition/lib/INotion";
import { SearchPageAndDatabase } from "../../enum/modals/common/SearchPageAndDatabaseComponent";
import { Handler } from "./Handler";
import { createPageOrRecordModal } from "../modals/createPageOrRecordModal";
import { NotionPageOrRecord } from "../../enum/modals/NotionPageOrRecord";

export class ExecuteBlockActionHandler {
private context: UIKitBlockInteractionContext;
Expand Down Expand Up @@ -121,13 +131,22 @@ export class ExecuteBlockActionHandler {
break;
}
case Modals.OVERFLOW_MENU_ACTION: {
return this.handleRefreshCommentAction(
return this.handleOverFlowMenuAction(
modalInteraction,
oAuth2Storage,
roomInteractionStorage
);
break;
}
case SearchPageAndDatabase.ACTION_ID: {
return this.handleSearchPageAndDatabaseAction(
modalInteraction,
oAuth2Storage,
roomInteractionStorage
);

break;
}
default: {
// Property Type Select Action
const propertyTypeSelected =
Expand Down Expand Up @@ -741,4 +760,115 @@ export class ExecuteBlockActionHandler {
.getInteractionResponder()
.updateContextualBarViewResponse(contextualBar);
}

private async handleOverFlowMenuAction(
modalInteraction: ModalInteractionStorage,
oAuth2Storage: OAuth2Storage,
roomInteractionStorage: RoomInteractionStorage
): Promise<IUIKitResponse> {
const { value, user, triggerId } = this.context.getInteractionData();

if (!value) {
return this.context.getInteractionResponder().errorResponse();
}

// Check if the value is pageId. if not then it is not a refresh comment action
const OverFlowActions = [
DatabaseModal.OVERFLOW_MENU_ACTION.toString(),
NotionPageOrRecord.CHANGE_DATABASE_ACTION.toString(),
];

if (!OverFlowActions.includes(value)) {
return this.handleRefreshCommentAction(
modalInteraction,
oAuth2Storage,
roomInteractionStorage
);
}

const roomId = await roomInteractionStorage.getInteractionRoomId();
const room = (await this.read.getRoomReader().getById(roomId)) as IRoom;

const handler = new Handler({
app: this.app,
read: this.read,
modify: this.modify,
persis: this.persistence,
http: this.http,
sender: user,
room,
triggerId,
});

switch (value) {
case DatabaseModal.OVERFLOW_MENU_ACTION: {
await handler.createNotionDatabase();
break;
}
case NotionPageOrRecord.CHANGE_DATABASE_ACTION: {
await handler.createNotionPageOrRecord(true);
break;
}
}

return this.context.getInteractionResponder().successResponse();
}

private async handleSearchPageAndDatabaseAction(
modalInteraction: ModalInteractionStorage,
oAuth2Storage: OAuth2Storage,
roomInteractionStorage: RoomInteractionStorage
): Promise<IUIKitResponse> {
const { value, user } = this.context.getInteractionData();

const tokenInfo = await oAuth2Storage.getCurrentWorkspace(user.id);
const roomId = await roomInteractionStorage.getInteractionRoomId();
const room = (await this.read.getRoomReader().getById(roomId)) as IRoom;

if (!tokenInfo) {
await sendNotificationWithConnectBlock(
this.app,
user,
this.read,
this.modify,
room
);

return this.context.getInteractionResponder().errorResponse();
}

if (!value) {
return this.context.getInteractionResponder().errorResponse();
}

let Object: IPage | IDatabase = JSON.parse(value);
let parentObject: IParentPage | IParentDatabase = Object.parent;

// update the modal if database is selected
if (parentObject.type.includes(NotionObjectTypes.PAGE_ID)) {
return this.context.getInteractionResponder().successResponse();
}

const database = Object as IDatabase;
const modal = await createPageOrRecordModal(
this.app,
user,
this.read,
this.persistence,
this.modify,
room,
modalInteraction,
tokenInfo,
database
);

if (modal instanceof Error) {
this.app.getLogger().error(modal.message);
return this.context.getInteractionResponder().errorResponse();
}

return this.context
.getInteractionResponder()
.updateModalViewResponse(modal);
}
}
15 changes: 13 additions & 2 deletions src/handlers/ExecuteViewClosedHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { ModalInteractionStorage } from "../storage/ModalInteraction";
import { OAuth2Storage } from "../authorization/OAuth2Storage";
import { ITokenInfo } from "../../definition/authorization/IOAuth2Storage";
import { CommentPage } from "../../enum/modals/CommentPage";
import { SearchPage } from "../../enum/modals/common/SearchPageComponent";
import { NotionPageOrRecord } from "../../enum/modals/NotionPageOrRecord";

export class ExecuteViewClosedHandler {
private context: UIKitViewCloseInteractionContext;
Expand Down Expand Up @@ -57,7 +57,6 @@ export class ExecuteViewClosedHandler {
break;
}
case CommentPage.VIEW_ID: {

await Promise.all([
modalInteraction.clearInputElementState(
CommentPage.COMMENT_INPUT_ACTION
Expand All @@ -68,6 +67,18 @@ export class ExecuteViewClosedHandler {
]);
break;
}
case NotionPageOrRecord.VIEW_ID: {
const { workspace_id } =
(await oAuth2Storage.getCurrentWorkspace(
user.id
)) as ITokenInfo;

await Promise.all([
modalInteraction.clearPagesOrDatabase(workspace_id),
]);

break;
}
default: {
}
}
Expand Down
Loading