Skip to content

Commit

Permalink
Finishes the revert
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkSuckerberg committed Mar 12, 2024
1 parent e7c8f49 commit f2ce55c
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 168 deletions.
21 changes: 7 additions & 14 deletions src/functions/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,13 @@ export async function getBlogBlocks(
offset = 0
): Promise<TumblrFollowerBlog[]> {
// Tumblr API only allows a maximum of 20 blogs per request
if (limit > 20) {
limit = 20;
}

const response = await accessTumblrAPI(token, `blog/${blogIdentifier}/blocks`, {
limit: limit.toString(),
offset: offset.toString(),
});

if (response.meta.status !== 200) {
throw new Error(`Failed to get blocked blogs: ${response.meta.msg}`);
}

return response.response.blocked_tumblelogs;
if (limit > 20) limit = 20;
return await (
await accessTumblrAPI(token, `blog/${blogIdentifier}/blocks`, {
limit: limit.toString(),
offset: offset.toString(),
})
).response.blocked_tumblelogs;
}

/**
Expand Down
70 changes: 25 additions & 45 deletions src/functions/BlogInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,13 @@ export async function getBlogInfo(
blogIdentifier: string,
responseFields?: BlogInfoResponseFields //TODO: Find a way to make this dynamically return the correct type
): Promise<{ [field: string]: string; thing: string }> {
const response = await accessTumblrAPI(
token,
`blog/${blogIdentifier}/info`,
responseFields ? { fields: responseFields.join(",") } : undefined
);

if (response.meta.status !== 200) {
throw new Error(`Failed to get blog info: ${response.meta.msg}`);
}

return response.response.blog;
return (
await accessTumblrAPI(
token,
`blog/${blogIdentifier}/info`,
responseFields ? { fields: responseFields.join(",") } : undefined
)
).response.blog;
}

/**
Expand All @@ -64,20 +60,13 @@ export async function getBlogFollowers(
offset = 0
): Promise<TumblrFollowingBlog[]> {
// Tumblr API only allows a maximum of 20 blogs per request
if (limit > 20) {
limit = 20;
}

const response = await accessTumblrAPI(token, `blog/${blogIdentifier}/followers`, {
limit: limit.toString(),
offset: offset.toString(),
});

if (response.meta.status !== 200) {
throw new Error(`Failed to get blog followers: ${response.meta.msg}`);
}

return response.response.users;
if (limit > 20) limit = 20;
return (
await accessTumblrAPI(token, `blog/${blogIdentifier}/followers`, {
limit: limit.toString(),
offset: offset.toString(),
})
).response.users;
}

/**
Expand All @@ -97,17 +86,12 @@ export async function getBlogFollowing(
): Promise<TumblrFollowerBlog[]> {
// Tumblr API only allows a maximum of 20 blogs per request
if (limit > 20) limit = 20;

const response = await accessTumblrAPI(token, `blog/${blogIdentifier}/following`, {
limit: limit.toString(),
offset: offset.toString(),
});

if (response.meta.status !== 200) {
throw new Error(`Failed to get following blogs: ${response.meta.msg}`);
}

return response.response.blogs;
return (
await accessTumblrAPI(token, `blog/${blogIdentifier}/following`, {
limit: limit.toString(),
offset: offset.toString(),
})
).response.blogs;
}

/**
Expand Down Expand Up @@ -141,13 +125,9 @@ export async function getBlogFollowedBy(
blogIdentifier: string,
followedBy: string
): Promise<boolean> {
const response = await accessTumblrAPI(token, `blog/${blogIdentifier}/followed_by`, {
query: followedBy,
});

if (response.meta.status !== 200) {
throw new Error(`Failed to get blog followed by: ${response.meta.msg}`);
}

return response.response.followed_by;
return (
await accessTumblrAPI(token, `blog/${blogIdentifier}/followed_by`, {
query: followedBy,
})
).response.followed_by;
}
24 changes: 10 additions & 14 deletions src/functions/Follow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,16 @@ import { accessTumblrAPI } from "./AccessTumblrApi";
* @link https://www.tumblr.com/docs/en/api/v2#userfollow--follow-a-blog
*/
export async function followBlog(token: string, url: string): Promise<TumblrFollowingBlog> {
const response = await accessTumblrAPI(
token,
`user/follow`,
{
url: url,
},
"POST"
);

if (response.meta.status !== 200) {
throw new Error(`Failed to follow blog: ${response.meta.msg}`);
}

return response.response.blog;
return (
await accessTumblrAPI(
token,
`user/follow`,
{
url: url,
},
"POST"
)
).response.blog;
}

/**
Expand Down
128 changes: 54 additions & 74 deletions src/functions/Post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,15 @@ export async function CreatePost(
blogIdentifier: string,
postDetails: NewPostDetails
): Promise<string | undefined> {
const response = await accessTumblrAPI(
token,
`blog/${blogIdentifier}/posts`,
//TODO: This is a hacky way to do this. Find a better way to make this into a string array.
JSON.parse(JSON.stringify(postDetails)),
"POST"
);

if (response.meta.status !== 201) {
throw new Error(`Failed to create post: ${response.meta.msg}`);
}

return response.response.id;
return (
await accessTumblrAPI(
token,
`blog/${blogIdentifier}/posts`,
//TODO: This is a hacky way to do this. Find a better way to make this into a string array.
JSON.parse(JSON.stringify(postDetails)),
"POST"
)
).response.id;
}

/**
Expand All @@ -41,19 +37,15 @@ export async function EditPost(
postId: string,
postDetails: NewPostDetails
): Promise<string | undefined> {
const response = await accessTumblrAPI(
token,
`blog/${blogIdentifier}/posts/${postId}`,
//TODO: This is a hacky way to do this. Find a better way to make this into a string array.
JSON.parse(JSON.stringify(postDetails)),
"PUT"
);

if (response.meta.status !== 200) {
throw new Error(`Failed to edit post: ${response.meta.msg}`);
}

return response.response.id;
return (
await accessTumblrAPI(
token,
`blog/${blogIdentifier}/posts/${postId}`,
//TODO: This is a hacky way to do this. Find a better way to make this into a string array.
JSON.parse(JSON.stringify(postDetails)),
"PUT"
)
).response.id;
}

/**
Expand All @@ -75,21 +67,17 @@ export async function FetchPostNeue(
postId: string,
postFormat: "npf" | "legacy" = "npf"
): Promise<TumblrPost> {
const response = await accessTumblrAPI(
token,
`blog/${blogIdentifier}/posts/${postId}`,
{
post_format: postFormat,
},
"GET",
"https://www.tumblr.com/api/v2/" // Yes, getting posts needs to be done on a different API endpoint for some reason. Ask Tumblr why.
);

if (response.meta.status !== 200) {
throw new Error(`Failed to fetch post: ${response.meta.msg}`);
}

return response.response;
return (
await accessTumblrAPI(
token,
`blog/${blogIdentifier}/posts/${postId}`,
{
post_format: postFormat,
},
"GET",
"https://www.tumblr.com/api/v2/" // Yes, getting posts needs to be done on a different API endpoint for some reason. Ask Tumblr why.
)
).response;
}

/**
Expand Down Expand Up @@ -196,21 +184,17 @@ export async function FetchPosts<PostType extends TumblrPost = TumblrPost>(
type: type,
};

const response = await accessTumblrAPI(
token,
`blog/${blogIdentifier}/posts`,
//TODO: This is a hacky way to do this. Find a better way to make this into a string array.
JSON.parse(JSON.stringify(args)),
"GET",
"https://www.tumblr.com/api/v2/", // Yes, getting posts needs to be done on a different API endpoint for some reason. Ask Tumblr why.
basicAuth
);

if (response.meta.status !== 200) {
throw new Error(`Failed to fetch posts: ${response.meta.msg}`);
}

return response.response.posts as PostType[];
return (
await accessTumblrAPI(
token,
`blog/${blogIdentifier}/posts`,
//TODO: This is a hacky way to do this. Find a better way to make this into a string array.
JSON.parse(JSON.stringify(args)),
"GET",
"https://www.tumblr.com/api/v2/", // Yes, getting posts needs to be done on a different API endpoint for some reason. Ask Tumblr why.
basicAuth
)
).response.posts as PostType[];
}

export async function GetNotes(
Expand All @@ -221,22 +205,18 @@ export async function GetNotes(
mode: "all" | "likes" | "conversation" | "rollup" | "reblogs_with_tags" = "all",
basicAuth = false
) {
const response = await accessTumblrAPI(
token,
`blog/${blogIdentifier}/notes`,
{
id: id.toString(),
mode,
before_timestamp: beforeTimestamp.toString(),
},
"GET",
undefined,
basicAuth
);

if (response.meta.status !== 200) {
throw new Error(`Failed to fetch notes: ${response.meta.msg}`);
}

return response.response as TumblrNoteResponse;
return (
await accessTumblrAPI(
token,
`blog/${blogIdentifier}/notes`,
{
id: id.toString(),
mode,
before_timestamp: beforeTimestamp.toString(),
},
"GET",
undefined,
basicAuth
)
).response as TumblrNoteResponse;
}
31 changes: 10 additions & 21 deletions src/functions/UserInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@ import { TumblrFollowerBlog } from "../interfaces";
import { accessTumblrAPI } from "./AccessTumblrApi";

export async function getUserInfo(token: string): Promise<TumblrUserInfo> {
const response = await accessTumblrAPI(token, "user/info");

if (response.meta.status !== 200) {
throw new Error(`Failed to get user info: ${response.meta.msg}`);
}

return response.response.user;
return await (
await accessTumblrAPI(token, "user/info")
).response.user;
}

export async function getUserFollowing(
Expand All @@ -18,18 +14,11 @@ export async function getUserFollowing(
offset = 0
): Promise<TumblrFollowerBlog[]> {
// Tumblr API only allows a maximum of 20 blogs per request
if (limit > 20) {
limit = 20;
}

const response = await accessTumblrAPI(token, "user/following", {
limit: limit.toString(),
offset: offset.toString(),
});

if (response.meta.status !== 200) {
throw new Error(`Failed to get blogs following: ${response.meta.msg}`);
}

return response.response.blogs;
if (limit > 20) limit = 20;
return await (
await accessTumblrAPI(token, "user/following", {
limit: limit.toString(),
offset: offset.toString(),
})
).response.blogs;
}

0 comments on commit f2ce55c

Please sign in to comment.