diff --git a/src/common/api/private-api.ts b/src/common/api/private-api.ts index 0b8e02f355d..c547be1cf20 100644 --- a/src/common/api/private-api.ts +++ b/src/common/api/private-api.ts @@ -1,61 +1,77 @@ import axios from "axios"; -import {PointTransaction} from "../store/points/types"; -import {ApiNotification, NotificationFilter} from "../store/notifications/types"; -import {Entry} from "../store/entries/types"; +import { PointTransaction } from "../store/points/types"; +import { + ApiNotification, + NotificationFilter, +} from "../store/notifications/types"; +import { Entry } from "../store/entries/types"; -import {getAccessToken} from "../helper/user-token"; +import { getAccessToken } from "../helper/user-token"; -import {apiBase} from "./helper"; +import { apiBase } from "./helper"; -import {AppWindow} from "../../client/window"; +import { AppWindow } from "../../client/window"; declare var window: AppWindow; export interface ReceivedVestingShare { - delegatee: string; - delegator: string; - timestamp: string; - vesting_shares: string; + delegatee: string; + delegator: string; + timestamp: string; + vesting_shares: string; } -export const getReceivedVestingShares = (username: string): Promise => - axios.get(apiBase(`/private-api/received-vesting/${username}`)).then((resp) => resp.data.list); - +export const getReceivedVestingShares = ( + username: string +): Promise => + axios + .get(apiBase(`/private-api/received-vesting/${username}`)) + .then((resp) => resp.data.list); export interface RewardedCommunity { - start_date: string; - total_rewards: string; - name: string; + start_date: string; + total_rewards: string; + name: string; } export const getRewardedCommunities = (): Promise => - axios.get(apiBase(`/private-api/rewarded-communities`)).then((resp) => resp.data); + axios + .get(apiBase(`/private-api/rewarded-communities`)) + .then((resp) => resp.data); export interface LeaderBoardItem { - _id: string; - count: number; - points: string + _id: string; + count: number; + points: string; } export type LeaderBoardDuration = "day" | "week" | "month"; -export const getLeaderboard = (duration: LeaderBoardDuration): Promise => { - return axios.get(apiBase(`/private-api/leaderboard/${duration}`)).then(resp => resp.data); +export const getLeaderboard = ( + duration: LeaderBoardDuration +): Promise => { + return axios + .get(apiBase(`/private-api/leaderboard/${duration}`)) + .then((resp) => resp.data); }; export interface CurationItem { - efficiency: number; - account: string; - vests: number; - votes: number; - uniques: number; + efficiency: number; + account: string; + vests: number; + votes: number; + uniques: number; } export type CurationDuration = "day" | "week" | "month"; -export const getCuration = (duration: CurationDuration): Promise => { - return axios.get(apiBase(`/private-api/curation/${duration}`)).then(resp => resp.data); +export const getCuration = ( + duration: CurationDuration +): Promise => { + return axios + .get(apiBase(`/private-api/curation/${duration}`)) + .then((resp) => resp.data); }; export const getAvailibleAccounts = (url: string) => @@ -75,322 +91,501 @@ export const signUp = (username: string, url: string): Promise => }); export const subscribeEmail = (email: string): Promise => - axios - .post(apiBase(`/private-api/subscribe`), { - email: email - }) - .then(resp => { - return resp; - }); - -export const usrActivity = (username: string, ty: number, bl: string | number = '', tx: string | number = '') => { - if (!window.usePrivate) { - return new Promise((resolve) => resolve(null)); - } - - const params: { - code: string | undefined; - ty: number; - bl?: string | number; - tx?: string | number; - } = {code: getAccessToken(username), ty}; - - if (bl) params.bl = bl; - if (tx) params.tx = tx; + axios + .post(apiBase(`/private-api/subscribe`), { + email: email, + }) + .then((resp) => { + return resp; + }); - return axios.post(apiBase(`/private-api/usr-activity`), params); +export const usrActivity = ( + username: string, + ty: number, + bl: string | number = "", + tx: string | number = "" +) => { + if (!window.usePrivate) { + return new Promise((resolve) => resolve(null)); + } + + const params: { + code: string | undefined; + ty: number; + bl?: string | number; + tx?: string | number; + } = { code: getAccessToken(username), ty }; + + if (bl) params.bl = bl; + if (tx) params.tx = tx; + + return axios.post(apiBase(`/private-api/usr-activity`), params); }; -export const getNotifications = (username: string, filter: NotificationFilter | null, since: string | null = null): Promise => { - - const data: { code: string | undefined; filter?: string, since?: string } = {code: getAccessToken(username)}; - - if (filter) { - data.filter = filter; - } - - if (since) { - data.since = since; - } - - return axios.post(apiBase(`/private-api/notifications`), data).then(resp => resp.data); +export const getNotifications = ( + username: string, + filter: NotificationFilter | null, + since: string | null = null +): Promise => { + const data: { code: string | undefined; filter?: string; since?: string } = { + code: getAccessToken(username), + }; + + if (filter) { + data.filter = filter; + } + + if (since) { + data.since = since; + } + + return axios + .post(apiBase(`/private-api/notifications`), data) + .then((resp) => resp.data); }; -export const getCurrencyTokenRate = (currency:string, token:string): Promise => - axios.get(apiBase(`/private-api/market-data/${currency==="hbd" ? "usd" : currency}/${token}`)).then((resp:any) => resp.data) - -export const getUnreadNotificationCount = (username: string): Promise => { - const data = {code: getAccessToken(username)}; - - return data.code ? axios +export const getCurrencyTokenRate = ( + currency: string, + token: string +): Promise => + axios + .get( + apiBase( + `/private-api/market-data/${ + currency === "hbd" ? "usd" : currency + }/${token}` + ) + ) + .then((resp: any) => resp.data); + +export const getUnreadNotificationCount = ( + username: string +): Promise => { + const data = { code: getAccessToken(username) }; + + return data.code + ? axios .post(apiBase(`/private-api/notifications/unread`), data) - .then(resp => resp.data.count) : Promise.resolve(0); -} - -export const markNotifications = (username: string, id: string | null = null) => { - const data: { code: string | undefined; id?: string } = {code: getAccessToken(username)} - if (id) { - data.id = id; - } + .then((resp) => resp.data.count) + : Promise.resolve(0); +}; - return axios.post(apiBase(`/private-api/notifications/mark`), data); +export const markNotifications = ( + username: string, + id: string | null = null +) => { + const data: { code: string | undefined; id?: string } = { + code: getAccessToken(username), + }; + if (id) { + data.id = id; + } + + return axios.post(apiBase(`/private-api/notifications/mark`), data); }; export interface UserImage { - created: string - timestamp: number - url: string - _id: string + created: string; + timestamp: number; + url: string; + _id: string; } export const getImages = (username: string): Promise => { - const data = {code: getAccessToken(username)}; - return axios.post(apiBase(`/private-api/images`), data).then(resp => resp.data); -} + const data = { code: getAccessToken(username) }; + return axios + .post(apiBase(`/private-api/images`), data) + .then((resp) => resp.data); +}; -export const deleteImage = (username: string, imageID: string): Promise => { - const data = {code: getAccessToken(username), id: imageID}; - return axios.post(apiBase(`/private-api/images-delete`), data).then(resp => resp.data); -} +export const deleteImage = ( + username: string, + imageID: string +): Promise => { + const data = { code: getAccessToken(username), id: imageID }; + return axios + .post(apiBase(`/private-api/images-delete`), data) + .then((resp) => resp.data); +}; export const addImage = (username: string, url: string): Promise => { - const data = {code: getAccessToken(username), url: url}; - return axios.post(apiBase(`/private-api/images-add`), data).then(resp => resp.data); -} + const data = { code: getAccessToken(username), url: url }; + return axios + .post(apiBase(`/private-api/images-add`), data) + .then((resp) => resp.data); +}; export interface Draft { - body: string - created: string - post_type: string - tags: string - timestamp: number - title: string - _id: string + body: string; + created: string; + post_type: string; + tags: string; + timestamp: number; + title: string; + _id: string; } export const getDrafts = (username: string): Promise => { - const data = {code: getAccessToken(username)}; - return axios.post(apiBase(`/private-api/drafts`), data).then(resp => resp.data); -} + const data = { code: getAccessToken(username) }; + return axios + .post(apiBase(`/private-api/drafts`), data) + .then((resp) => resp.data); +}; -export const addDraft = (username: string, title: string, body: string, tags: string): Promise<{ drafts: Draft[] }> => { - const data = {code: getAccessToken(username), title, body, tags}; - return axios.post(apiBase(`/private-api/drafts-add`), data).then(resp => resp.data); -} +export const addDraft = ( + username: string, + title: string, + body: string, + tags: string +): Promise<{ drafts: Draft[] }> => { + const data = { code: getAccessToken(username), title, body, tags }; + return axios + .post(apiBase(`/private-api/drafts-add`), data) + .then((resp) => resp.data); +}; -export const updateDraft = (username: string, draftId: string, title: string, body: string, tags: string): Promise => { - const data = {code: getAccessToken(username), id: draftId, title, body, tags}; - return axios.post(apiBase(`/private-api/drafts-update`), data).then(resp => resp.data); -} +export const updateDraft = ( + username: string, + draftId: string, + title: string, + body: string, + tags: string +): Promise => { + const data = { + code: getAccessToken(username), + id: draftId, + title, + body, + tags, + }; + return axios + .post(apiBase(`/private-api/drafts-update`), data) + .then((resp) => resp.data); +}; -export const deleteDraft = (username: string, draftId: string): Promise => { - const data = {code: getAccessToken(username), id: draftId}; - return axios.post(apiBase(`/private-api/drafts-delete`), data).then(resp => resp.data); -} +export const deleteDraft = ( + username: string, + draftId: string +): Promise => { + const data = { code: getAccessToken(username), id: draftId }; + return axios + .post(apiBase(`/private-api/drafts-delete`), data) + .then((resp) => resp.data); +}; export interface Schedule { - _id: string; - username: string; - permlink: string; - title: string; - body: string; - tags: string[]; - tags_arr: string; - schedule: string; - original_schedule: string; - reblog: boolean; - status: 1 | 2 | 3 | 4; - message: string | null + _id: string; + username: string; + permlink: string; + title: string; + body: string; + tags: string[]; + tags_arr: string; + schedule: string; + original_schedule: string; + reblog: boolean; + status: 1 | 2 | 3 | 4; + message: string | null; } export const getSchedules = (username: string): Promise => { - const data = {code: getAccessToken(username)}; - return axios.post(apiBase(`/private-api/schedules`), data).then(resp => resp.data); -} + const data = { code: getAccessToken(username) }; + return axios + .post(apiBase(`/private-api/schedules`), data) + .then((resp) => resp.data); +}; -export const addSchedule = (username: string, permlink: string, title: string, body: string, meta: {}, options: {}, schedule: string, reblog: boolean): Promise => { - const data = {code: getAccessToken(username), permlink, title, body, meta, options, schedule, reblog} - return axios.post(apiBase(`/private-api/schedules-add`), data).then(resp => resp.data); -} +export const addSchedule = ( + username: string, + permlink: string, + title: string, + body: string, + meta: {}, + options: {}, + schedule: string, + reblog: boolean +): Promise => { + const data = { + code: getAccessToken(username), + permlink, + title, + body, + meta, + options, + schedule, + reblog, + }; + return axios + .post(apiBase(`/private-api/schedules-add`), data) + .then((resp) => resp.data); +}; export const deleteSchedule = (username: string, id: string): Promise => { - const data = {code: getAccessToken(username), id}; - return axios.post(apiBase(`/private-api/schedules-delete`), data).then(resp => resp.data); -} + const data = { code: getAccessToken(username), id }; + return axios + .post(apiBase(`/private-api/schedules-delete`), data) + .then((resp) => resp.data); +}; export const moveSchedule = (username: string, id: string): Promise => { - const data = {code: getAccessToken(username), id}; - return axios.post(apiBase(`/private-api/schedules-move`), data).then(resp => resp.data); -} + const data = { code: getAccessToken(username), id }; + return axios + .post(apiBase(`/private-api/schedules-move`), data) + .then((resp) => resp.data); +}; export interface Bookmark { - _id: string, - author: string, - permlink: string, - timestamp: number, - created: string + _id: string; + author: string; + permlink: string; + timestamp: number; + created: string; } export const getBookmarks = (username: string): Promise => { - const data = {code: getAccessToken(username)}; - return axios.post(apiBase(`/private-api/bookmarks`), data).then(resp => resp.data); -} + const data = { code: getAccessToken(username) }; + return axios + .post(apiBase(`/private-api/bookmarks`), data) + .then((resp) => resp.data); +}; -export const addBookmark = (username: string, author: string, permlink: string): Promise<{ bookmarks: Bookmark[] }> => { - const data = {code: getAccessToken(username), author, permlink}; - return axios.post(apiBase(`/private-api/bookmarks-add`), data).then(resp => resp.data); -} +export const addBookmark = ( + username: string, + author: string, + permlink: string +): Promise<{ bookmarks: Bookmark[] }> => { + const data = { code: getAccessToken(username), author, permlink }; + return axios + .post(apiBase(`/private-api/bookmarks-add`), data) + .then((resp) => resp.data); +}; -export const deleteBookmark = (username: string, bookmarkId: string): Promise => { - const data = {code: getAccessToken(username), id: bookmarkId}; - return axios.post(apiBase(`/private-api/bookmarks-delete`), data).then(resp => resp.data); -} +export const deleteBookmark = ( + username: string, + bookmarkId: string +): Promise => { + const data = { code: getAccessToken(username), id: bookmarkId }; + return axios + .post(apiBase(`/private-api/bookmarks-delete`), data) + .then((resp) => resp.data); +}; export interface Favorite { - _id: string, - account: string, - timestamp: number, + _id: string; + account: string; + timestamp: number; } export const getFavorites = (username: string): Promise => { - const data = {code: getAccessToken(username)}; - return axios.post(apiBase(`/private-api/favorites`), data).then(resp => resp.data); -} + const data = { code: getAccessToken(username) }; + return axios + .post(apiBase(`/private-api/favorites`), data) + .then((resp) => resp.data); +}; -export const checkFavorite = (username: string, account: string): Promise => { - const data = {code: getAccessToken(username), account}; - return axios.post(apiBase(`/private-api/favorites-check`), data).then(resp => resp.data); -} +export const checkFavorite = ( + username: string, + account: string +): Promise => { + const data = { code: getAccessToken(username), account }; + return axios + .post(apiBase(`/private-api/favorites-check`), data) + .then((resp) => resp.data); +}; -export const addFavorite = (username: string, account: string): Promise<{ favorites: Favorite[] }> => { - const data = {code: getAccessToken(username), account}; - return axios.post(apiBase(`/private-api/favorites-add`), data).then(resp => resp.data); -} +export const addFavorite = ( + username: string, + account: string +): Promise<{ favorites: Favorite[] }> => { + const data = { code: getAccessToken(username), account }; + return axios + .post(apiBase(`/private-api/favorites-add`), data) + .then((resp) => resp.data); +}; -export const deleteFavorite = (username: string, account: string): Promise => { - const data = {code: getAccessToken(username), account}; - return axios.post(apiBase(`/private-api/favorites-delete`), data).then(resp => resp.data); -} +export const deleteFavorite = ( + username: string, + account: string +): Promise => { + const data = { code: getAccessToken(username), account }; + return axios + .post(apiBase(`/private-api/favorites-delete`), data) + .then((resp) => resp.data); +}; export interface Fragment { - id: string; - title: string; - body: string; - created: string; - modified: string; + id: string; + title: string; + body: string; + created: string; + modified: string; } export const getFragments = (username: string): Promise => { - const data = {code: getAccessToken(username)}; - return axios.post(apiBase(`/private-api/fragments`), data).then(resp => resp.data); -} + const data = { code: getAccessToken(username) }; + return axios + .post(apiBase(`/private-api/fragments`), data) + .then((resp) => resp.data); +}; -export const addFragment = (username: string, title: string, body: string): Promise<{ fragments: Fragment[] }> => { - const data = {code: getAccessToken(username), title, body}; - return axios.post(apiBase(`/private-api/fragments-add`), data).then(resp => resp.data); -} +export const addFragment = ( + username: string, + title: string, + body: string +): Promise<{ fragments: Fragment[] }> => { + const data = { code: getAccessToken(username), title, body }; + return axios + .post(apiBase(`/private-api/fragments-add`), data) + .then((resp) => resp.data); +}; -export const updateFragment = (username: string, fragmentId: string, title: string, body: string): Promise => { - const data = {code: getAccessToken(username), id: fragmentId, title, body}; - return axios.post(apiBase(`/private-api/fragments-update`), data).then(resp => resp.data); -} +export const updateFragment = ( + username: string, + fragmentId: string, + title: string, + body: string +): Promise => { + const data = { code: getAccessToken(username), id: fragmentId, title, body }; + return axios + .post(apiBase(`/private-api/fragments-update`), data) + .then((resp) => resp.data); +}; -export const deleteFragment = (username: string, fragmentId: string): Promise => { - const data = {code: getAccessToken(username), id: fragmentId}; - return axios.post(apiBase(`/private-api/fragments-delete`), data).then(resp => resp.data); -} +export const deleteFragment = ( + username: string, + fragmentId: string +): Promise => { + const data = { code: getAccessToken(username), id: fragmentId }; + return axios + .post(apiBase(`/private-api/fragments-delete`), data) + .then((resp) => resp.data); +}; -export const getPoints = (username: string): Promise<{ - points: string; - unclaimed_points: string; +export const getPoints = ( + username: string +): Promise<{ + points: string; + unclaimed_points: string; }> => { - if (window.usePrivate) { - const data = {username}; - return axios.post(apiBase(`/private-api/points`), data).then(resp => resp.data); - } - - return new Promise((resolve) => { - resolve({ - points: "0.000", - unclaimed_points: "0.000" - }) + if (window.usePrivate) { + const data = { username }; + return axios.post(`/private-api/points`, data).then((resp) => resp.data); + } + + return new Promise((resolve) => { + resolve({ + points: "0.000", + unclaimed_points: "0.000", }); -} - -export const getPointTransactions = (username: string, type?: number): Promise => { - if (window.usePrivate) { - const data = {username, type}; - return axios.post(apiBase(`/private-api/point-list`), data).then(resp => resp.data); - } + }); +}; - return new Promise((resolve) => { - resolve([]); - }); -} +export const getPointTransactions = ( + username: string, + type?: number +): Promise => { + if (window.usePrivate) { + const data = { username, type }; + return axios + .post(apiBase(`/private-api/point-list`), data) + .then((resp) => resp.data); + } + + return new Promise((resolve) => { + resolve([]); + }); +}; export const claimPoints = (username: string): Promise => { - const data = {code: getAccessToken(username)}; - return axios.post(apiBase(`/private-api/points-claim`), data).then(resp => resp.data); -} + const data = { code: getAccessToken(username) }; + return axios + .post(apiBase(`/private-api/points-claim`), data) + .then((resp) => resp.data); +}; -export const calcPoints = (username: string, amount: string): Promise<{ usd: number, estm: number }> => { - const data = {code: getAccessToken(username), amount}; - return axios.post(apiBase(`/private-api/points-calc`), data).then(resp => resp.data); -} +export const calcPoints = ( + username: string, + amount: string +): Promise<{ usd: number; estm: number }> => { + const data = { code: getAccessToken(username), amount }; + return axios + .post(apiBase(`/private-api/points-calc`), data) + .then((resp) => resp.data); +}; export interface PromotePrice { - duration: number, - price: number + duration: number; + price: number; } export const getPromotePrice = (username: string): Promise => { - const data = {code: getAccessToken(username)}; - return axios.post(apiBase(`/private-api/promote-price`), data).then(resp => resp.data); -} + const data = { code: getAccessToken(username) }; + return axios + .post(apiBase(`/private-api/promote-price`), data) + .then((resp) => resp.data); +}; -export const getPromotedPost = (username: string, author: string, permlink: string): Promise<{ author: string, permlink: string } | ''> => { - const data = {code: getAccessToken(username), author, permlink}; - return axios.post(apiBase(`/private-api/promoted-post`), data).then(resp => resp.data); -} +export const getPromotedPost = ( + username: string, + author: string, + permlink: string +): Promise<{ author: string; permlink: string } | ""> => { + const data = { code: getAccessToken(username), author, permlink }; + return axios + .post(apiBase(`/private-api/promoted-post`), data) + .then((resp) => resp.data); +}; export const getBoostOptions = (username: string): Promise => { - const data = {code: getAccessToken(username)}; - return axios.post(apiBase(`/private-api/boost-options`), data).then(resp => resp.data); -} + const data = { code: getAccessToken(username) }; + return axios + .post(apiBase(`/private-api/boost-options`), data) + .then((resp) => resp.data); +}; -export const getBoostedPost = (username: string, author: string, permlink: string): Promise<{ author: string, permlink: string } | ''> => { - const data = {code: getAccessToken(username), author, permlink}; - return axios.post(apiBase(`/private-api/boosted-post`), data).then(resp => resp.data); -} +export const getBoostedPost = ( + username: string, + author: string, + permlink: string +): Promise<{ author: string; permlink: string } | ""> => { + const data = { code: getAccessToken(username), author, permlink }; + return axios + .post(apiBase(`/private-api/boosted-post`), data) + .then((resp) => resp.data); +}; export interface CommentHistoryListItem { - title: string; - body: string; - tags: string[]; - timestamp: string; - v: number; + title: string; + body: string; + tags: string[]; + timestamp: string; + v: number; } interface CommentHistory { - meta: { - count: number; - }, - list: CommentHistoryListItem[]; -} - -export const commentHistory = (author: string, permlink: string, onlyMeta: boolean = false): Promise => { - const data = {author, permlink, onlyMeta: onlyMeta ? '1' : ''}; - return axios.post(apiBase(`/private-api/comment-history`), data).then(resp => resp.data); -} + meta: { + count: number; + }; + list: CommentHistoryListItem[]; +} + +export const commentHistory = ( + author: string, + permlink: string, + onlyMeta: boolean = false +): Promise => { + const data = { author, permlink, onlyMeta: onlyMeta ? "1" : "" }; + return axios + .post(apiBase(`/private-api/comment-history`), data) + .then((resp) => resp.data); +}; export const getPromotedEntries = (): Promise => { - if (window.usePrivate) { - return axios.get(apiBase(`/private-api/promoted-entries`)).then((resp) => resp.data); - } - - return new Promise(resolve => resolve([])); -} - - - + if (window.usePrivate) { + return axios + .get(apiBase(`/private-api/promoted-entries`)) + .then((resp) => resp.data); + } + return new Promise((resolve) => resolve([])); +}; diff --git a/src/common/components/wallet-menu/index.tsx b/src/common/components/wallet-menu/index.tsx index 6908b13cc7b..14a96108756 100644 --- a/src/common/components/wallet-menu/index.tsx +++ b/src/common/components/wallet-menu/index.tsx @@ -1,46 +1,62 @@ -import React, {Component} from "react"; +import React, { Component } from "react"; -import {Link} from "react-router-dom"; +import { Link } from "react-router-dom"; -import {Global} from "../../store/global/types"; +import { Global } from "../../store/global/types"; import _c from "../../util/fix-class-names"; -import {hiveSvg} from "../../img/svg"; -import {hiveEngineSvg} from "../../img/svg"; - +import { hiveSvg } from "../../img/svg"; +import { hiveEngineSvg } from "../../img/svg"; interface Props { - global: Global; - username: string; - active: string; + global: Global; + username: string; + active: string; } export default class WalletMenu extends Component { - render() { - const {global, username, active} = this.props; - const logo = global.isElectron ? "./img/logo-small-transparent.png" : require('../../img/logo-small-transparent.png'); - - return ( -
- {global.usePrivate && ( - - Ecency - Points - ecency - - )} - - Hive - Wallet - {hiveSvg} - - - Engine - Tokens - {hiveEngineSvg} - -
- ); - } + render() { + const { global, username, active } = this.props; + const logo = global.isElectron + ? "./img/logo-small-transparent.png" + : require("../../img/logo-small-transparent.png"); + + return ( +
+ { + + Ecency + Points + + ecency + + + } + + Hive + Wallet + {hiveSvg} + + + Engine + Tokens + {hiveEngineSvg} + +
+ ); + } } diff --git a/src/server/handlers/points.ts b/src/server/handlers/points.ts new file mode 100644 index 00000000000..aff27862581 --- /dev/null +++ b/src/server/handlers/points.ts @@ -0,0 +1,16 @@ +import express from "express"; +import axios from "axios"; + +export const pointsHandler = async ( + req: express.Request, + res: express.Response +) => { + try { + // Include the protocol (http://) in the URL + const data = await axios.post("http://localhost:4000/user-info", req.body); // Pass req.body as the second argument + res.send(data.data); // Send the response data from the backend + } catch (e) { + console.error(e); + res.sendStatus(500); // Send a 500 status code for server errors + } +}; diff --git a/src/server/index.ts b/src/server/index.ts index ed1e83563db..6f138dd1747 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -2,16 +2,22 @@ import express from "express"; import cookieParser from "cookie-parser"; -import {EntryFilter, ProfileFilter} from "../common/store/global/types"; +import { EntryFilter, ProfileFilter } from "../common/store/global/types"; import entryIndexHandler from "./handlers/entry-index"; import communityHandler from "./handlers/community"; import profileHandler from "./handlers/profile"; import entryHandler from "./handlers/entry"; -import fallbackHandler, {healthCheck, iosURI, androidURI, nodeList} from "./handlers/fallback"; -import {entryRssHandler, authorRssHandler} from "./handlers/rss"; +import fallbackHandler, { + healthCheck, + iosURI, + androidURI, + nodeList, +} from "./handlers/fallback"; +import { entryRssHandler, authorRssHandler } from "./handlers/rss"; import * as authApi from "./handlers/auth-api"; import config from "../config"; +import { pointsHandler } from "./handlers/points"; const server = express(); @@ -19,102 +25,110 @@ const entryFilters = Object.values(EntryFilter); const profileFilters = Object.values(ProfileFilter); const cleanURL = (req: any, res: any, next: any) => { - if (req.url.includes('//')) { - res.redirect(req.url.replace(new RegExp('/{2,}', 'g'),'/')); - } - if(req.url.includes('-hs?code')){ - next(); - } - else if (req.url !== req.url.toLowerCase() && !req.url.includes('auth?code')) { - res.redirect(301, req.url.toLowerCase()); - } - else { - next(); - } -} + if (req.url.includes("//")) { + res.redirect(req.url.replace(new RegExp("/{2,}", "g"), "/")); + } + if (req.url.includes("-hs?code")) { + next(); + } else if ( + req.url !== req.url.toLowerCase() && + !req.url.includes("auth?code") + ) { + res.redirect(301, req.url.toLowerCase()); + } else { + next(); + } +}; const stripLastSlash = (req: any, res: any, next: any) => { - if (req.path.substr(-1) === '/' && req.path.length > 1) { - let query = req.url.slice(req.path.length); - res.redirect(301, req.path.slice(0, -1) + query); - } else { - next(); - } -} + if (req.path.substr(-1) === "/" && req.path.length > 1) { + let query = req.url.slice(req.path.length); + res.redirect(301, req.path.slice(0, -1) + query); + } else { + next(); + } +}; const authCheck = (req: any, res: any, next: any) => { - if (config.hsClientSecret && config.usePrivate !== "1") { - next(); - } else { - res.json({error: "Define HIVESIGNER_SECRET ENV variable or set USE_PRIVATE=1"}); - } -} + if (config.hsClientSecret && config.usePrivate !== "1") { + next(); + } else { + res.json({ + error: "Define HIVESIGNER_SECRET ENV variable or set USE_PRIVATE=1", + }); + } +}; server - .disable("x-powered-by") - .use(express.static(process.env.RAZZLE_PUBLIC_DIR!)) - .use("/assets", express.static(`${process.env.RAZZLE_PUBLIC_DIR!}/assets`)) - .use(express.json()) - .use(cookieParser()) - .use(cleanURL) - .use(stripLastSlash) + .disable("x-powered-by") + .use(express.static(process.env.RAZZLE_PUBLIC_DIR!)) + .use("/assets", express.static(`${process.env.RAZZLE_PUBLIC_DIR!}/assets`)) + .use(express.json()) + .use(cookieParser()) + .use(cleanURL) + .use(stripLastSlash) - // Common backend - .get( - [ - `^/:filter(${entryFilters.join("|")})/:tag/rss.xml$`, // /trending/esteem/rss.xml - ], - entryRssHandler - ) - .get( - [ - "^/@:author/:section(feed|blog|posts)/rss.xml$", // /posts/@esteemapp/rss.xml - "^/@:author/rss.xml$", // @esteemapp/rss.xml - ], - authorRssHandler - ) - .get( - [ - `^/:filter(${entryFilters.join("|")}|subscribers|activities|roles)/:name(hive-[\\d]+)$`, // /hot/hive-231312 - ], - communityHandler - ) - .get( - [ - "^/$", // index - `^/:filter(${entryFilters.join("|")})$`, // /trending - `^/:filter(${entryFilters.join("|")})/:tag$`, // /trending/esteem - `^/@:tag/:filter(feed)$`, // /@user/feed - ], - entryIndexHandler - ) - .get( - [ - "^/@:username$", // /@esteemapp - `^/@:username/:section(${profileFilters.join("|")}|communities|wallet|points|engine|settings)$`, // /@esteemapp/comments - ], - profileHandler - ) - .get( - [ - "^/:category/@:author/:permlink$", // /esteem/@esteemapp/rss-feeds-added-into-esteem-website - "^/@:author/:permlink$", // /@esteemapp/rss-feeds-added-into-esteem-website - ], - entryHandler - ) - // apple-app-site-association - .get("^/apple-app-site-association$", iosURI) - // android assetlinks - .get("^/.well-known/assetlinks.json$", androidURI) - // get public nodes list - .get("^/public-nodes.json$", nodeList) - // Auth Api - .post("^/auth-api/hs-token-refresh$", authCheck, authApi.hsTokenRefresh) + // Common backend + .get( + [ + `^/:filter(${entryFilters.join("|")})/:tag/rss.xml$`, // /trending/esteem/rss.xml + ], + entryRssHandler + ) + .get( + [ + "^/@:author/:section(feed|blog|posts)/rss.xml$", // /posts/@esteemapp/rss.xml + "^/@:author/rss.xml$", // @esteemapp/rss.xml + ], + authorRssHandler + ) + .get( + [ + `^/:filter(${entryFilters.join( + "|" + )}|subscribers|activities|roles)/:name(hive-[\\d]+)$`, // /hot/hive-231312 + ], + communityHandler + ) + .get( + [ + "^/$", // index + `^/:filter(${entryFilters.join("|")})$`, // /trending + `^/:filter(${entryFilters.join("|")})/:tag$`, // /trending/esteem + `^/@:tag/:filter(feed)$`, // /@user/feed + ], + entryIndexHandler + ) + .get( + [ + "^/@:username$", // /@esteemapp + `^/@:username/:section(${profileFilters.join( + "|" + )}|communities|wallet|points|engine|settings)$`, // /@esteemapp/comments + ], + profileHandler + ) + .get( + [ + "^/:category/@:author/:permlink$", // /esteem/@esteemapp/rss-feeds-added-into-esteem-website + "^/@:author/:permlink$", // /@esteemapp/rss-feeds-added-into-esteem-website + ], + entryHandler + ) + // apple-app-site-association + .get("^/apple-app-site-association$", iosURI) + // android assetlinks + .get("^/.well-known/assetlinks.json$", androidURI) + // get public nodes list + .get("^/public-nodes.json$", nodeList) + // Auth Api + .post("^/auth-api/hs-token-refresh$", authCheck, authApi.hsTokenRefresh) - // Health check script for docker swarm - .get("^/healthcheck.json$", healthCheck) + // Health check script for docker swarm + .get("^/healthcheck.json$", healthCheck) + .post("^/private-api/points", pointsHandler) - // For all others paths - .get("*", fallbackHandler); + // For all others paths + .get("*", fallbackHandler); export default server;