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] add content to page from notion using contextualBar #65

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion enum/CommandParam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export enum CommandParam {
WORKSPACE = "workspace",
WS = "ws",
SHARE = "share",
VIEW = "view"
VIEW = "view",
APPEND = "append"
}

export enum SubCommandParam {
Expand Down
3 changes: 2 additions & 1 deletion enum/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export enum Messages {
• use \`/notion create\` to create page or record
• use \`/notion create db\` to create database
• use \`/notion workspace\` to change workspace
• use \`/notion share\` to share pages
• use \`/notion share\` to share pages
• use \`/notion append\` to add content to your page

`,
HELPER_TEXT = `Need some help with \`/notion\`?`,
Expand Down
23 changes: 23 additions & 0 deletions enum/modals/NotionAppendContent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export enum NotionAppendContent {
VIEW_ID = "notion-append-content-view-id",
TITLE = "Add Content to Page",
HEADING_SELECT_PLACEHOLDER = "Heading Type",
HEADING_SELECT_BLOCK_ID = "select-heading-component-block-id",
HEADING_SELECT_ACTION_ID = "select-heading-component-action-id",
HEADING_SELECT_LABEL = "Select Heading Type",
HEADING_PLACEHOLDER = "Enter Your Heading",
HEADING_LABEL = "Heading",
HEADING_BLOCK = "heading-input-block-id",
HEADING_ACTION = "heading-input-action-id",
CONTENT_PLACEHOLDER = "Enter your content...",
CONTENT_LABEL = "Content *",
CONTENT_BLOCK = "content-input-block-id",
CONTENT_ACTION = "content-input-action-id",
SAVE = "Save",
SAVE_ACTION = "append-content-action-id",
SAVE_BLOCK = "append-content-block-id",
CLOSE = "Close",
CLOSE_ACTION = "close-append-content-action-id",
CLOSE_BLOCK = "close-append-content-block-id",
}

4 changes: 4 additions & 0 deletions src/commands/CommandUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ export class CommandUtility implements ICommandUtility {
await handler.viewNotionTable();
break;
}
case CommandParam.APPEND: {
await handler.appendContent();
break;
}
case CommandParam.HELP:
default: {
await sendHelperNotification(
Expand Down
65 changes: 65 additions & 0 deletions src/handlers/ExecuteBlockActionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ import { getPropertiesIdsObject } from "../helper/getPropertiesIdsObject";
import { SharePage } from "../../enum/modals/SharePage";
import { NotionTable } from "../../enum/modals/NotionTable";
import { SendMessagePage } from "../../enum/modals/SendMessagePage";
import { NotionAppendContent } from "../../enum/modals/NotionAppendContent";
import { appendContentModal } from "../modals/appendContentModal";

export class ExecuteBlockActionHandler {
private context: UIKitBlockInteractionContext;
Expand Down Expand Up @@ -176,6 +178,14 @@ export class ExecuteBlockActionHandler {
return this.handleSelectDatabaseAction();
break;
}

case NotionAppendContent.HEADING_SELECT_ACTION_ID: {
return this.handleHeadingSelectAction(
modalInteraction,
oAuth2Storage,
roomInteractionStorage
);
}
default: {
// Property Type Select Action
const propertyTypeSelected =
Expand Down Expand Up @@ -1021,4 +1031,59 @@ export class ExecuteBlockActionHandler {
errors: {},
});
}

private async handleHeadingSelectAction(
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 isHeadingInput = false;

if(value !== 'no_heading'){
isHeadingInput = true;
}

const modal = await appendContentModal(
this.app,
user,
this.read,
this.persistence,
this.modify,
room,
modalInteraction,
tokenInfo,
isHeadingInput
);

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

return this.context
.getInteractionResponder()
.updateModalViewResponse(modal);
}
}
106 changes: 104 additions & 2 deletions src/handlers/ExecuteViewSubmitHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import { SendMessagePage } from "../../enum/modals/SendMessagePage";
import { NotionTable } from "../../enum/modals/NotionTable";
import { SearchDatabaseComponent } from "../../enum/modals/common/SearchDatabaseComponent";
import { table } from "table";
import { NotionAppendContent } from "../../enum/modals/NotionAppendContent";

export class ExecuteViewSubmitHandler {
private context: UIKitViewSubmitInteractionContext;
Expand Down Expand Up @@ -139,6 +140,14 @@ export class ExecuteViewSubmitHandler {
);
break;
}

case NotionAppendContent.VIEW_ID: {
return this.handleAppendContent(
room,
oAuth2Storage,
modalInteraction
);
}
default: {
}
}
Expand Down Expand Up @@ -341,7 +350,6 @@ export class ExecuteViewSubmitHandler {
const parentType: string = parent.type;

if (parentType.includes(NotionObjectTypes.PAGE_ID)) {

return this.handleCreationOfPage(
tokenInfo,
room,
Expand All @@ -350,7 +358,7 @@ export class ExecuteViewSubmitHandler {
Objects as IPage
);
}

return this.handleCreationOfRecord(
tokenInfo,
room,
Expand Down Expand Up @@ -998,4 +1006,98 @@ export class ExecuteViewSubmitHandler {

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

private async handleAppendContent(
room: IRoom,
oAuth2Storage: OAuth2Storage,
modalInteraction: ModalInteractionStorage
): Promise<IUIKitResponse> {
const { NotionSdk } = this.app.getUtils();
const { view, user } = this.context.getInteractionData();
const { state } = view;

const tokenInfo = await oAuth2Storage.getCurrentWorkspace(user.id);

if (!tokenInfo) {
await sendNotificationWithConnectBlock(
this.app,
user,
this.read,
this.modify,
room
);
return this.context.getInteractionResponder().errorResponse();
}
const { access_token, workspace_name } = tokenInfo;

const pageId: string | undefined =
state?.[SearchPage.BLOCK_ID]?.[SearchPage.ACTION_ID];

const headingType: string | undefined =
state?.[NotionAppendContent.HEADING_SELECT_BLOCK_ID]?.[NotionAppendContent.HEADING_SELECT_ACTION_ID];

const headingInput: string | undefined =
state?.[NotionAppendContent.HEADING_BLOCK]?.[
NotionAppendContent.HEADING_ACTION
];

const contentInput: string | undefined =
state?.[NotionAppendContent.CONTENT_BLOCK]?.[
NotionAppendContent.CONTENT_ACTION
];

if (!pageId) {
return this.context.getInteractionResponder().viewErrorResponse({
viewId: view.id,
errors: {
[SharePage.ACTION_ID]: "Please Select a Page",
},
});
} else {
if (!contentInput) {
return this.context
.getInteractionResponder()
.viewErrorResponse({
viewId: view.id,
errors: {
[NotionAppendContent.CONTENT_ACTION]:
"Content is required. Please provide some information.",
},
});
}
}

const appendMsgStatus = await NotionSdk.appendContentToPage(
access_token,
pageId,
contentInput,
headingType && headingType !== "no_heading" && headingInput
? { headingType, headingInput }
: undefined
);

let message: string;

if (appendMsgStatus instanceof Error) {
this.app.getLogger().error(appendMsgStatus.message);
message = `Something went wrong while appending content in **${workspace_name}**.`;
} else {
const pageInfo = await NotionSdk.retrievePage(access_token, pageId);

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

const { name, url } = pageInfo;

message = `Your content has been sucessfully saved in [**${name}**](${url})`;
}

await sendNotification(this.read, this.modify, user, room, {
message,
});

return this.context.getInteractionResponder().successResponse();
}
}
68 changes: 67 additions & 1 deletion src/handlers/Handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ import { SendMessagePage } from "../../enum/modals/SendMessagePage";
import { sendMessagePageModal } from "../modals/sendMessagePageModal";
import { NotionTable } from "../../enum/modals/NotionTable";
import { viewNotionTableModal } from "../modals/viewNotionTableModal";
import { appendContentModal } from "../modals/appendContentModal";
import { NotionAppendContent } from "../../enum/modals/NotionAppendContent";
import { SearchPage } from "../../enum/modals/common/SearchPageComponent";

export class Handler implements IHandler {
public app: NotionApp;
Expand Down Expand Up @@ -66,6 +69,69 @@ export class Handler implements IHandler {
);
}

public async appendContent(
): Promise<void> {
const userId = this.sender.id;
const roomId = this.room.id;
const tokenInfo = await this.oAuth2Storage.getCurrentWorkspace(userId);

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

const persistenceRead = this.read.getPersistenceReader();
const modalInteraction = new ModalInteractionStorage(
this.persis,
persistenceRead,
userId,
NotionAppendContent.VIEW_ID
);

const { workspace_id, access_token } = tokenInfo;

await Promise.all([
this.roomInteractionStorage.storeInteractionRoomId(roomId),
modalInteraction.clearPagesOrDatabase(workspace_id),
modalInteraction.clearInputElementState(
SearchPage.ACTION_ID
),
modalInteraction.clearAllInteractionActionId(),
]);

const modal = await appendContentModal(
this.app,
this.sender,
this.read,
this.persis,
this.modify,
this.room,
modalInteraction,
tokenInfo
);

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

const triggerId = this.triggerId;

if (triggerId) {
await this.modify
.getUiController()
.openSurfaceView(modal, { triggerId }, this.sender);
}

return;
}

public async createNotionDatabase(): Promise<void> {
const userId = this.sender.id;
const roomId = this.room.id;
Expand Down Expand Up @@ -515,4 +581,4 @@ export class Handler implements IHandler {
.openSurfaceView(modal, { triggerId }, this.sender);
}
}
}
}
Loading