Skip to content

Commit

Permalink
Adds like, dislike, and notes endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkSuckerberg committed Aug 4, 2023
1 parent 95f7361 commit edd8b3e
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/functions/AccessTumblrApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export async function accessTumblrAPI(
throw new Error(
`${response.meta.status}: ${response.meta.msg} - ${JSON.stringify(
response.errors
)} (Query URL: ${url})`
)} (Query URL: ${method} ${url} ${method === "GET" ? "" : JSON.stringify(params)})`
);
}

Expand Down
29 changes: 29 additions & 0 deletions src/functions/Like.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { accessTumblrAPI } from "./AccessTumblrApi";

export async function LikePost(token: string, id: number | string, reblogKey: string) {
return (
(
await accessTumblrAPI(
token,
"user/like",
{ id: id.toString(), reblog_key: reblogKey },
"POST",
"https://www.tumblr.com/api/v2/"
)
).meta.status === 200
);
}

export async function UnlikePost(token: string, id: number | string, reblogKey: string) {
return (
(
await accessTumblrAPI(
token,
"user/unlike",
{ id: id.toString(), reblog_key: reblogKey },
"POST",
"https://www.tumblr.com/api/v2/"
)
).meta.status === 200
);
}
18 changes: 17 additions & 1 deletion src/functions/Post.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NewPostDetails, TumblrPost } from "../interfaces";
import { NewPostDetails, TumblrNoteResponse, TumblrPost } from "../interfaces";
import { accessTumblrAPI } from "./AccessTumblrApi";

/**
Expand Down Expand Up @@ -196,3 +196,19 @@ export async function FetchPosts<PostType extends TumblrPost = TumblrPost>(
)
).response.posts as PostType[];
}

export async function GetNotes(
token: string,
blogIdentifier: string,
id: number | string,
beforeTimestamp = Number.MAX_VALUE,
mode: "all" | "likes" | "conversation" | "rollup" | "reblogs_with_tags" = "all"
) {
return (
await accessTumblrAPI(token, `blog/${blogIdentifier}/notes`, {
id: id.toString(),
mode,
before_timestamp: beforeTimestamp.toString(),
})
).response as TumblrNoteResponse;
}
1 change: 1 addition & 0 deletions src/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export * from "./AccessTumblrApi";
export * from "./Block";
export * from "./BlogInfo";
export * from "./Follow";
export * from "./Like";
export * from "./Post";
export * from "./UserInfo";
12 changes: 12 additions & 0 deletions src/interfaces/TumblrPost.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TumblrLink } from "./TumblrAPIResponse";
import { TumblrFollowerBlog } from "./TumblrBlog";
import { TumblrBlocksPost } from "./TumblrNeuePost";

Expand Down Expand Up @@ -151,6 +152,15 @@ interface TumblrDialogue {
phrase: string;
}

export interface TumblrNoteResponse {
notes: TumblrNote[];
rollup_notes?: TumblrNote[];
total_notes: number;
total_likes?: number;
total_reblogs?: number;
_links: TumblrLink;
}

interface TumblrNote {
type: "like" | "reblog";
timestamp: number;
Expand All @@ -163,4 +173,6 @@ interface TumblrNote {
"64": string;
"128": string;
};
tags?: string[];
reblog_parent_blog_name?: string;
}
37 changes: 30 additions & 7 deletions tests/Posts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@ import {
FetchPost,
FetchPostNeue,
FetchPosts,
GetNotes,
LikePost,
NewPostDetails,
TumblrBlocksPost,
UnlikePost,
} from "../src";

const token = process.env.TUMBLR_TOKEN;
const consumerID = process.env.CONSUMER_ID;
const testBlog = "typeble-bot";

if (!token) throw new Error("No token provided");
if (!consumerID) throw new Error("No consumer ID provided");

let postID: string | undefined;
let post: TumblrBlocksPost | undefined;

it("should post a text post", async () => {
const postDetails: NewPostDetails = {
Expand All @@ -26,7 +31,7 @@ it("should post a text post", async () => {
tags: "test,typeble",
};

postID = await CreatePost(token, "typeble-bot", postDetails);
postID = await CreatePost(token, testBlog, postDetails);
expect(postID).toBeDefined();
});

Expand All @@ -40,13 +45,13 @@ it("should edit the post", async () => {
};

if (!postID) throw new Error("No post ID provided");
const returnedID = await EditPost(token, "typeble-bot", postID, postDetails);
const returnedID = await EditPost(token, testBlog, postID, postDetails);
expect(returnedID).toBe(postID);
});

it("should fetch the post", async () => {
if (!postID) throw new Error("No post ID provided");
const post = (await FetchPostNeue(token, "typeble-bot", postID)) as TumblrBlocksPost;
post = (await FetchPostNeue(token, testBlog, postID)) as TumblrBlocksPost;
expect(post.content).toEqual([
{ type: "text", text: "Hello, world!", subtype: "heading1" },
{ type: "text", text: "This is a test post. It has been edited!" },
Expand All @@ -59,7 +64,7 @@ it("should fetch the post with a consumer ID only", async () => {
if (!postID) throw new Error("No post ID provided");
const post = await FetchPost<TumblrBlocksPost>(
consumerID,
"typeble-bot",
testBlog,
postID,
undefined,
undefined,
Expand All @@ -76,13 +81,31 @@ it("should fetch the post with a consumer ID only", async () => {
});

it("should fetch the blog's posts", async () => {
const posts = await FetchPosts(token, "typeble-bot");
const posts = await FetchPosts(token, testBlog);
const post = posts.find(post => post.id_string === postID);
expect(post).toBeDefined();
});

it("should like the post", async () => {
if (!post) throw new Error("No post object retrieved");
expect(await LikePost(token, post.id_string, post.reblog_key)).toBe(true);
});

it("should retrieve the notes count", async () => {
if (!post) throw new Error("No post object retrieved");
const response = await GetNotes(token, testBlog, post.id_string);
expect(response).toBeDefined();
expect(response.total_notes).toBeGreaterThan(0);
expect(response.notes.find(note => note.blog_name == testBlog)).toBeDefined();
});

it("should unlike the post", async () => {
if (!post) throw new Error("No post object retrieved");
expect(await UnlikePost(token, post.id_string, post.reblog_key)).toBe(true);
});

it("should delete the post", async () => {
if (!postID) throw new Error("No post ID provided");
expect(await DeletePost(token, "typeble-bot", postID)).toBe(true);
await expect(FetchPost(token, "typeble-bot", postID)).rejects.toThrow();
expect(await DeletePost(token, testBlog, postID)).toBe(true);
await expect(FetchPost(token, testBlog, postID)).rejects.toThrow();
});

0 comments on commit edd8b3e

Please sign in to comment.