Skip to content

Commit

Permalink
코드 스타일 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
joostory committed May 1, 2022
1 parent dc89046 commit c38654c
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 108 deletions.
5 changes: 5 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"experimentalDecorators": true,
}
}
114 changes: 61 additions & 53 deletions src/main/apis/tistory-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ const appInfo = require('../appInfo')
const ExternalOAuth2 = require('../oauth/ExternalOAuth2');
const OAuthRequestManager = require('../oauth/OAuthRequestManager');

const errorHandler = (res) => {
const BASE_URL = 'https://www.tistory.com/apis'
const PROVIDER_ID = 'tistory'

function _errorHandler(res) {
if (!res.ok) {
console.error("fetch failed", res)
res.text().then(text => console.error("fetch body", text))
Expand All @@ -19,10 +22,44 @@ const errorHandler = (res) => {
return res.json()
}

const BASE_URL = 'https://www.tistory.com/apis'
const PROVIDER_ID = 'tistory'
function _uploadFile(accessToken, blogName, fileBlob, fileOption) {
let formdata = new FormData();
formdata.append("access_token", accessToken)
formdata.append("output", "json")
formdata.append("blogName", blogName)
formdata.append("uploadedfile", fileBlob, fileOption)

return fetch(BASE_URL + "/post/attach", {
method: 'post',
body: formdata
})
.then(_errorHandler)
.then(res => res.tistory.url)
}

const requestAuth = (successHandler, failureHandler) => {

function _tistoryPostToEditorPost(post) {
return {
id: post.id,
url: post.postUrl,
title: post.title,
date: post.date,
categoryId: post.categoryId,
state: post.visibility > 0? 'published' : 'draft',
tags: post.tags && post.tags.tag? [].concat(post.tags.tag) : [],
content: post.content? post.content : ''
}
}

function _tistoryPostsToEditorPosts(tistoryPosts) {
let posts = tistoryPosts? [].concat(tistoryPosts) : []
return posts.map(_tistoryPostToEditorPost)
}




function requestAuth(successHandler, failureHandler) {
const oauthInfoReader = new OauthInfoReader()
const oauth2 = new ExternalOAuth2(oauthInfoReader.getTistory())
OAuthRequestManager.saveRequestInfo("oauth", (searchParams) => {
Expand All @@ -46,7 +83,7 @@ const requestAuth = (successHandler, failureHandler) => {
oauth2.requestAuth({})
}

const fetchBlogInfo = (auth) => {
function fetchBlogInfo(auth) {
return fetch(BASE_URL + "/blog/info?" + querystring.stringify({
access_token: auth.access_token,
output: "json"
Expand All @@ -55,10 +92,10 @@ const fetchBlogInfo = (auth) => {
'User-Agent': appInfo.userAgent
}
})
.then(errorHandler)
.then(_errorHandler)
}

const fetchUser = (auth) => {
function fetchUser(auth) {
return fetch(BASE_URL + "/user?" + querystring.stringify({
access_token: auth.access_token,
output: "json"
Expand All @@ -67,26 +104,9 @@ const fetchUser = (auth) => {
'User-Agent': appInfo.userAgent
}
})
.then(errorHandler)
.then(_errorHandler)
}

function _tistoryPostToEditorPost(post) {
return {
id: post.id,
url: post.postUrl,
title: post.title,
date: post.date,
categoryId: post.categoryId,
state: post.visibility > 0? 'published' : 'draft',
tags: post.tags && post.tags.tag? [].concat(post.tags.tag) : [],
content: post.content? post.content : ''
}
}

function _tistoryPostsToEditorPosts(tistoryPosts) {
let posts = tistoryPosts? [].concat(tistoryPosts) : []
return posts.map(_tistoryPostToEditorPost)
}

const fetchPosts = (auth, blogName, options) => {
return fetch(BASE_URL + "/post/list?" + querystring.stringify({
Expand All @@ -100,15 +120,15 @@ const fetchPosts = (auth, blogName, options) => {
'User-Agent': appInfo.userAgent
}
})
.then(errorHandler)
.then(_errorHandler)
.then(res => ({
page: res.tistory.item.page,
posts: _tistoryPostsToEditorPosts(res.tistory.item.posts),
hasNext: res.tistory.item.totalCount > res.tistory.item.page * res.tistory.item.count
}))
}

const fetchPost = (auth, blogName, postId) => {
function fetchPost(auth, blogName, postId) {
return fetch(BASE_URL + "/post/read?" + querystring.stringify({
access_token: auth.access_token,
output: "json",
Expand All @@ -119,13 +139,13 @@ const fetchPost = (auth, blogName, postId) => {
'User-Agent': appInfo.userAgent
}
})
.then(errorHandler)
.then(_errorHandler)
.then(res => ({
post: _tistoryPostToEditorPost(res.tistory.item)
}))
}

const fetchCategories = (auth, blogName) => {
function fetchCategories(auth, blogName) {
return fetch(BASE_URL + "/category/list?" + querystring.stringify({
access_token: auth.access_token,
output: "json",
Expand All @@ -135,11 +155,11 @@ const fetchCategories = (auth, blogName) => {
'User-Agent': appInfo.userAgent
}
})
.then(errorHandler)
.then(_errorHandler)
.then(res => res.tistory.item.categories)
}

const savePost = (auth, blogName, post) => {
function savePost(auth, blogName, post) {
let formdata = makePostFormData(auth, blogName, post)
formdata.append("postId", post.id)

Expand All @@ -151,11 +171,11 @@ const savePost = (auth, blogName, post) => {
'User-Agent': appInfo.userAgent
}
})
.then(errorHandler)
.then(_errorHandler)
.then(res => fetchPost(auth, blogName, res.tistory.postId))
}

const addPost = (auth, blogName, post) => {
function addPost(auth, blogName, post) {
let formdata = makePostFormData(auth, blogName, post)

return fetch(BASE_URL + "/post/write", {
Expand All @@ -166,11 +186,11 @@ const addPost = (auth, blogName, post) => {
'User-Agent': appInfo.userAgent
}
})
.then(errorHandler)
.then(_errorHandler)
.then(res => fetchPost(auth, blogName, res.tistory.postId))
}

const makePostFormData = (auth, blogName, post) => {
function makePostFormData(auth, blogName, post) {
let formdata = new FormData()
formdata.append("access_token", auth.access_token)
formdata.append("output", "json")
Expand All @@ -189,19 +209,19 @@ const makePostFormData = (auth, blogName, post) => {
return formdata
}

const uploadFile = (auth, blogName, filepath) => {
function uploadFile(auth, blogName, filepath) {
console.log("uploadFile", blogName, filepath)
return _uploadFile(auth.access_token, blogName, fs.createReadStream(filepath))
}

const uploadFileWithBuffer = (auth, blogName, buffer, options) => {
function uploadFileWithBuffer(auth, blogName, buffer, options) {
console.log("uploadFileWithClipboard", blogName)
var imageStream = new stream.PassThrough()
imageStream.end(buffer)
return _uploadFile(auth.access_token, blogName, imageStream, options)
}

const uploadFileWithBlob = (auth, blogName, blob, options) => {
function uploadFileWithBlob(auth, blogName, blob, options) {
console.log("uploadFileWithBlob", blogName)
var imageStream = new stream.PassThrough()
imageStream.end(blob.buffer)
Expand All @@ -212,24 +232,12 @@ const uploadFileWithBlob = (auth, blogName, blob, options) => {
})
}

const _uploadFile = (accessToken, blogName, fileBlob, fileOption) => {
let formdata = new FormData();
formdata.append("access_token", accessToken)
formdata.append("output", "json")
formdata.append("blogName", blogName)
formdata.append("uploadedfile", fileBlob, fileOption)

return fetch(BASE_URL + "/post/attach", {
method: 'post',
body: formdata
})
.then(errorHandler)
.then(res => res.tistory.url)
function validateAuthInfo(auth) {
return auth && auth.access_token
}

const validateAuthInfo = (auth) => auth && auth.access_token

const fetchAccount = async (auth) => {
async function fetchAccount(auth) {
let user = {
name: "tistory",
image: null
Expand Down
84 changes: 44 additions & 40 deletions src/main/apis/tumblr-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,7 @@ const OAuthRequestManager = require('../oauth/OAuthRequestManager');

const PROVIDER_ID = 'tumblr'

const requestAuth = (successHandler, failureHandler) => {
const oauthInfoReader = new OauthInfoReader()
const oauth1 = new ExternalOAuth1(oauthInfoReader.getTumblr())
oauth1.requestAuth((requestTokens) => {
OAuthRequestManager.saveRequestInfo('oauth', (searchParams) => {
const verifier = searchParams.get("oauth_verifier")
oauth1.requestToken(verifier, requestTokens, (error, token, tokenSecret) => {
if (error) {
failureHandler()
return
}

successHandler({
uuid: uuid(),
provider: PROVIDER_ID,
authInfo: {
token, tokenSecret
}
})
})
})
})

}

function createTumblrClient(auth) {
function _createTumblrClient(auth) {
const oauthReader = new OauthInfoReader()
const tumblrInfo = oauthReader.getTumblr()

Expand All @@ -46,10 +21,6 @@ function createTumblrClient(auth) {
})
}

const fetchUser = (auth) => {
const client = createTumblrClient(auth)
return client.userInfo()
}

function _tumblrPostToEditorPost(post) {
return ({
Expand Down Expand Up @@ -85,39 +56,72 @@ function _editorPostToTumblrPost(editorPost) {
return tumblrPost
}

const fetchPosts = (auth, blogName, options) => {
const client = createTumblrClient(auth)
function requestAuth(successHandler, failureHandler) {
const oauthInfoReader = new OauthInfoReader()
const oauth1 = new ExternalOAuth1(oauthInfoReader.getTumblr())
oauth1.requestAuth((requestTokens) => {
OAuthRequestManager.saveRequestInfo('oauth', (searchParams) => {
const verifier = searchParams.get("oauth_verifier")
oauth1.requestToken(verifier, requestTokens, (error, token, tokenSecret) => {
if (error) {
failureHandler()
return
}

successHandler({
uuid: uuid(),
provider: PROVIDER_ID,
authInfo: {
token, tokenSecret
}
})
})
})
})

}

function fetchUser(auth) {
const client = _createTumblrClient(auth)
return client.userInfo()
}


function fetchPosts(auth, blogName, options) {
const client = _createTumblrClient(auth)
return client.blogPosts(blogName, options)
.then(res => ({
posts: _tumblrPostsToEditorPosts(res.posts),
hasNext: res.blog.posts > options.offset + res.posts.length
}))
}

const fetchPost = (auth, blogName, postId) => {
const client = createTumblrClient(auth)
function fetchPost(auth, blogName, postId) {
const client = _createTumblrClient(auth)
return client.blogPosts(blogName, {id: postId})
.then(res => ({
post: _tumblrPostToEditorPost(res.posts[0])
}))
}

const addPost = async (auth, blogName, post) => {
const client = createTumblrClient(auth)
async function addPost(auth, blogName, post) {
const client = _createTumblrClient(auth)
const res = await client.createTextPost(blogName, _editorPostToTumblrPost(post))
const fetchRes = await fetchPosts(auth, blogName, {offset:0, limit:1})
return { post: fetchRes.posts[0] }
}

const savePost = async (auth, blogName, post) => {
const client = createTumblrClient(auth)
async function savePost(auth, blogName, post) {
const client = _createTumblrClient(auth)
const res = await client.editPost(blogName, _editorPostToTumblrPost(post))
return await fetchPost(auth, blogName, post.id)
}

const validateAuthInfo = (auth) => auth && auth.token
function validateAuthInfo(auth) {
return auth && auth.token
}

const fetchAccount = async (auth) => {
async function fetchAccount(auth) {
let blogs = []
let username = ""
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import React from 'react'
import { unmountComponentAtNode } from 'react-dom'
import autobind from 'autobind-decorator'

const plugin = function (editor) {
export default function plugin(editor) {
editor.options.register("open_file_handler", {
processor: 'function',
default: () => {}
Expand All @@ -23,5 +19,3 @@ const plugin = function (editor) {
}
})
}

export default plugin
1 change: 0 additions & 1 deletion src/renderer/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react'
import { render } from 'react-dom'
import { createRoot } from 'react-dom/client'
import { Provider } from 'react-redux'

Expand Down
Loading

0 comments on commit c38654c

Please sign in to comment.