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

Remove tweet when processing posts #957

Merged
merged 1 commit into from
Feb 24, 2024
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
5 changes: 2 additions & 3 deletions packages/notion-post-publisher/__mocks__/notion-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { NotionBlock } from "../src/types/notion";
import type { PostProperties } from "../src/types/post";
import { faker } from "@faker-js/faker";
import { mockParagraphBlock } from ".";
import type { NotionBlock } from "../src/types/notion";
import type { PostProperties } from "../src/types/post";

export function mockPageBlocksApiResponse(): NotionBlock[] {
return [mockParagraphBlock(), mockParagraphBlock()];
Expand All @@ -14,6 +14,5 @@ export function mockPagePropertiesResponse(): PostProperties {
title: faker.lorem.words(5),
description: faker.lorem.words(5),
tags: ["JavaScript"],
tweet: faker.lorem.words(5),
};
}
4 changes: 1 addition & 3 deletions packages/notion-post-publisher/__tests__/lib/Post.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ describe("Post", () => {
// These are set manually by the mock, so we can do this with confidence.
const frontmatter = `---\ntitle: ${properties.title}\ndescription: ${
properties.description
}\ntags:${properties.tags!.map((t) => `\n - ${t}`)}\ntweet: ${
properties.tweet
}\n---\n`;
}\ntags:${properties.tags!.map((t) => `\n - ${t}`)}\n---\n`;
let blocks: CreatableBlock[] = [];
for (const res of blockResponse) {
blocks.push(await Block.create(res));
Expand Down
74 changes: 6 additions & 68 deletions packages/notion-post-publisher/dist/lib/blocks/EmbedBlock.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EmbedBlock = void 0;
const axios_1 = __importDefault(require("axios"));
const date_fns_1 = require("date-fns");
const prettier_1 = __importDefault(require("prettier"));
class EmbedBlock {
constructor(params) {
Expand All @@ -23,13 +12,11 @@ class EmbedBlock {
this.embedBlock = new embedBlockMap[this.domain](params);
}
}
prerender() {
return __awaiter(this, void 0, void 0, function* () {
if (this.embedBlock && "prerender" in this.embedBlock) {
yield this.embedBlock.prerender();
}
});
}
// async prerender() {
// if (this.embedBlock && "prerender" in this.embedBlock) {
// await this.embedBlock.prerender();
// }
// }
render() {
if (!this.embedBlock) {
throw new Error(`Embed block not supported: ${this.domain}`);
Expand All @@ -50,59 +37,10 @@ class TwitterEmbedBlock {
}
this.id = id;
}
prerender() {
return __awaiter(this, void 0, void 0, function* () {
// const client = new Client(process.env.TWITTER_BEARER_TOKEN!);
// console.log(process.env.TWITTER_BEARER_TOKEN);
const tweetUrl = `https://twitter.com/_/status/${this.id}`;
const response = yield axios_1.default.get(tweetUrl);
console.log(response.data, response.status);
// let tweet: any;
// try {
// tweet = await client.tweets.findTweetById(this.id, {
// "tweet.fields": ["created_at", "text", "author_id"],
// });
// } catch (error) {
// console.log(error);
// throw new Error(`Could not find tweet: ${this.id}`);
// }
// if (
// !tweet.data?.author_id ||
// !tweet.data?.created_at ||
// !tweet.data?.text
// ) {
// throw new Error(
// `Could not find appropriate attributes for tweet: ${this.id}`
// );
// }
// const author = await client.users.findUserById(tweet.data.author_id, {
// "user.fields": ["name", "username"],
// });
// if (!author.data?.name || !author.data?.username) {
// throw new Error(
// `Could not find appropriate attributes for author: ${tweet.data.author_id}`
// );
// }
// this.tweet = {
// created_at: new Date(tweet.data.created_at),
// text: tweet.data.text,
// author: {
// name: author.data.name,
// username: author.data.username,
// },
// };
});
}
render() {
if (!this.tweet) {
throw new Error(`Tweet not properly prerendered: ${this.id}`);
}
const output = `
<blockquote class="twitter-tweet">
<p lang="en" dir="ltr">
${this.tweet.text}
</p>
&mdash; ${this.tweet.author.name} (@${this.tweet.author.username}) <a href="${this.url}">${(0, date_fns_1.format)(this.tweet.created_at, "MMMM d, yyyy")}</a>
<a href="https://twitter.com/username/status/${this.id}"></a>
</blockquote>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
`;
Expand Down
3 changes: 1 addition & 2 deletions packages/notion-post-publisher/dist/utils/notion-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ exports.getAllPageBlocks = getAllPageBlocks;
* @returns {Promise<PostProperties>}
*/
function getPageProperties(page_id) {
var _a, _b, _c, _d, _e, _f, _g, _h;
var _a, _b, _c, _d, _e;
return __awaiter(this, void 0, void 0, function* () {
const page = (yield notion.pages.retrieve({ page_id }));
const properties = page === null || page === void 0 ? void 0 : page.properties;
Expand All @@ -90,7 +90,6 @@ function getPageProperties(page_id) {
title: (_b = (_a = properties["Name"].title) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.plain_text,
description: (_d = (_c = properties["Description"].rich_text) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.plain_text,
tags: (_e = properties["Tags"].multi_select) === null || _e === void 0 ? void 0 : _e.map((tag) => tag.name),
tweet: (_h = (_g = (_f = properties["Tweet"]) === null || _f === void 0 ? void 0 : _f.rich_text) === null || _g === void 0 ? void 0 : _g[0]) === null || _h === void 0 ? void 0 : _h.plain_text,
};
});
}
Expand Down
8 changes: 0 additions & 8 deletions packages/notion-post-publisher/src/lib/blocks/EmbedBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@ export class EmbedBlock {
class TwitterEmbedBlock {
id: string;
url: string;
tweet?: {
created_at: Date;
text: string;
author: {
name: string;
username: string;
};
};

constructor(params: NotionEmbedBlock) {
this.url = params.embed.url;
Expand Down
1 change: 0 additions & 1 deletion packages/notion-post-publisher/src/types/post.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export type PostProperties = {
title: string;
description: string;
tweet?: string;
tags?: string[];
};
1 change: 0 additions & 1 deletion packages/notion-post-publisher/src/utils/notion-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ export async function getPageProperties(
tags: properties["Tags"].multi_select?.map(
(tag: { name: string }) => tag.name
),
tweet: properties["Tweet"]?.rich_text?.[0]?.plain_text,
};
}

Expand Down
Loading