-
-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Pavel Mineev
committed
Aug 20, 2022
1 parent
75427df
commit 5f376b1
Showing
33 changed files
with
2,797 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
name: "@remark42/api" | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
paths: | ||
- ".github/workflows/ci-frontend-api.yml" | ||
- "frontend/packages/**" | ||
- "!**.md" | ||
pull_request: | ||
paths: | ||
- ".github/workflows/ci-frontend-api.yml" | ||
- "frontend/packages/**" | ||
- "!**.md" | ||
|
||
jobs: | ||
type-check: | ||
name: Type check | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
node: [16.15.1] | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Install node | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: ${{ matrix.node }} | ||
|
||
- name: Install pnpm | ||
uses: pnpm/[email protected] | ||
id: pnpm-install | ||
with: | ||
version: 7 | ||
run_install: false | ||
|
||
- name: Get pnpm store directory | ||
id: pnpm-cache | ||
run: | | ||
echo "::set-output name=pnpm_cache_dir::$(pnpm store path)" | ||
- name: Setup pnpm cache | ||
uses: actions/cache@v3 | ||
with: | ||
path: ${{ steps.pnpm-cache.outputs.pnpm_cache_dir }} | ||
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} | ||
restore-keys: | | ||
${{ runner.os }}-pnpm-store- | ||
- name: Install dependencies | ||
run: pnpm i | ||
working-directory: ./frontend | ||
|
||
- name: Run type check | ||
run: pnpm type-check:api | ||
working-directory: ./frontend | ||
|
||
lint: | ||
name: Lint | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
node: [16.15.1] | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Install node | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: ${{ matrix.node }} | ||
|
||
- name: Install pnpm | ||
uses: pnpm/[email protected] | ||
id: pnpm-install | ||
with: | ||
version: 7 | ||
run_install: false | ||
|
||
- name: Get pnpm store directory | ||
id: pnpm-cache | ||
run: | | ||
echo "::set-output name=pnpm_cache_dir::$(pnpm store path)" | ||
- name: Setup pnpm cache | ||
uses: actions/cache@v3 | ||
with: | ||
path: ${{ steps.pnpm-cache.outputs.pnpm_cache_dir }} | ||
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} | ||
restore-keys: | | ||
${{ runner.os }}-pnpm-store- | ||
- name: Install dependencies | ||
run: pnpm i | ||
working-directory: ./frontend | ||
|
||
- name: Run linters | ||
run: pnpm lint:api | ||
working-directory: ./frontend/ | ||
|
||
test: | ||
name: Tests & Coverage | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
node: [16.15.1] | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Install node | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: ${{ matrix.node }} | ||
|
||
- name: Install pnpm | ||
uses: pnpm/[email protected] | ||
id: pnpm-install | ||
with: | ||
version: 7 | ||
run_install: false | ||
|
||
- name: Get pnpm store directory | ||
id: pnpm-cache | ||
run: | | ||
echo "::set-output name=pnpm_cache_dir::$(pnpm store path)" | ||
- name: Setup pnpm cache | ||
uses: actions/cache@v3 | ||
with: | ||
path: ${{ steps.pnpm-cache.outputs.pnpm_cache_dir }} | ||
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} | ||
restore-keys: | | ||
${{ runner.os }}-pnpm-store- | ||
- name: Install dependencies | ||
run: pnpm i | ||
working-directory: ./frontend | ||
|
||
- name: Test & Coverage | ||
run: pnpm coverage:api | ||
working-directory: ./frontend | ||
|
||
- name: Submit coverage | ||
run: ${{ github.workspace }}/frontend/apps/remark42/node_modules/.bin/codecov | ||
env: | ||
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
dist | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module.exports = { | ||
env: { | ||
browser: true, | ||
es2021: true, | ||
node: true, | ||
}, | ||
extends: [ | ||
'eslint:recommended', | ||
'plugin:@typescript-eslint/recommended', | ||
'plugin:@typescript-eslint/recommended-requiring-type-checking', | ||
'plugin:@typescript-eslint/strict', | ||
'plugin:prettier/recommended', | ||
], | ||
overrides: [], | ||
parser: '@typescript-eslint/parser', | ||
parserOptions: { | ||
ecmaVersion: 'latest', | ||
sourceType: 'module', | ||
project: './tsconfig.eslint.json', | ||
}, | ||
plugins: ['@typescript-eslint'], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
node_modules | ||
coverage | ||
dist | ||
**/*.js | ||
**/*.d.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
'./**/*.ts': ['pnpm lint-staged:lint'], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
coverage/** | ||
dist/** | ||
node_modules/** | ||
src/** | ||
test/** | ||
.editorconfig | ||
.eslintrc.cjs | ||
.prettierrc | ||
files.txt | ||
jest.config.ts | ||
pnpm-lock.yaml | ||
postpublish.sh | ||
prepublish.sh | ||
tsconfig.common.json | ||
tsconfig.dev.json | ||
tsconfig.json | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
16.17.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"semi": false, | ||
"printWidth": 100, | ||
"quoteProps": "consistent", | ||
"singleQuote": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# @remark42/api | ||
|
||
Implementation of API methods for Remark42 | ||
|
||
## Development | ||
|
||
- If you don't have `pnpm` installed run `npm i -g pnpm` | ||
- Install dependencies `pnpm i` | ||
- Run development mode with `pnpm run dev` | ||
- Build lib with `pnpm run build` | ||
- Run tests with `pnpm run test` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import type { ClientParams, User } from './index' | ||
import { API_BASE } from '../consts' | ||
import { createFetcher } from '../lib/fetcher' | ||
|
||
export type BlockTTL = 'permanently' | '43200m' | '10080m' | '1440m' | ||
export interface BlockUserResponse { | ||
block: boolean | ||
site_id: string | ||
user_id: string | ||
} | ||
|
||
export function createAdminClient({ siteId, baseUrl }: ClientParams) { | ||
const fetcher = createFetcher(siteId, `${baseUrl}${API_BASE}`) | ||
|
||
async function toggleUserVerification(id: string, verified: 0 | 1): Promise<void> { | ||
return fetcher.put(`/verify/${id}`, { verified }) | ||
} | ||
|
||
async function toggleCommentPin(id: string, pinned: 0 | 1): Promise<void> { | ||
return fetcher.put(`/pin/${id}`, { pinned }) | ||
} | ||
|
||
async function toggleCommenting(url: string, ro: 0 | 1): Promise<void> { | ||
return fetcher.put('/readonly', { url, ro }) | ||
} | ||
|
||
async function toggleUserBlock(id: string, ttl?: BlockTTL): Promise<BlockUserResponse> { | ||
const params = ttl | ||
? { | ||
block: 1, | ||
ttl: ttl === 'permanently' ? 0 : ttl, | ||
} | ||
: { block: 0 } | ||
|
||
return fetcher.put<BlockUserResponse>(`/user/${id}`, params) | ||
} | ||
|
||
/** | ||
* Request list of blocked users | ||
*/ | ||
async function getBlockedUsers(): Promise<User[]> { | ||
return fetcher.get<User[]>('/blocked') | ||
} | ||
|
||
/** | ||
* Block user from commenting | ||
* @param id user ID | ||
* @param ttl block duration | ||
*/ | ||
async function blockUser(id: string, ttl: BlockTTL): Promise<BlockUserResponse> { | ||
return toggleUserBlock(id, ttl) | ||
} | ||
|
||
/** | ||
* Unblock user from commenting | ||
* @param id user ID | ||
*/ | ||
async function unblockUser(id: string): Promise<BlockUserResponse> { | ||
return toggleUserBlock(id) | ||
} | ||
|
||
/** | ||
* Mark user as verified | ||
* @param id user ID | ||
*/ | ||
async function verifyUser(id: string): Promise<void> { | ||
return toggleUserVerification(id, 1) | ||
} | ||
/** | ||
* Mark user as unverified | ||
* @param id user ID | ||
*/ | ||
async function unverifyUser(id: string): Promise<void> { | ||
return toggleUserVerification(id, 0) | ||
} | ||
/** | ||
* Approve request to remove user data | ||
* @param token token from email | ||
*/ | ||
async function approveRemovingRequest(token: string): Promise<void> { | ||
return fetcher.get('/deleteme', { token }) | ||
} | ||
|
||
/** | ||
* Mark comment as pinned | ||
* @param id comment ID | ||
*/ | ||
async function pinComment(id: string): Promise<void> { | ||
return toggleCommentPin(id, 1) | ||
} | ||
/** | ||
* Mark comment as unpinned | ||
* @param id comment ID | ||
*/ | ||
async function unpinComment(id: string): Promise<void> { | ||
return toggleCommentPin(id, 0) | ||
} | ||
/** | ||
* Remove comment | ||
* @param url page URL | ||
* @param id comment ID | ||
*/ | ||
async function removeComment(url: string, id: string): Promise<void> { | ||
return fetcher.delete(`/comment/${id}`, { url }) | ||
} | ||
/** | ||
* Enable commenting on a page | ||
* @param url page URL | ||
*/ | ||
async function enableCommenting(url: string) { | ||
return toggleCommenting(url, 1) | ||
} | ||
/** | ||
* Disable commenting on a page | ||
* @param url page URL | ||
*/ | ||
async function disableCommenting(url: string) { | ||
return toggleCommenting(url, 0) | ||
} | ||
|
||
return { | ||
getBlockedUsers, | ||
blockUser, | ||
unblockUser, | ||
verifyUser, | ||
unverifyUser, | ||
approveRemovingRequest, | ||
pinComment, | ||
unpinComment, | ||
removeComment, | ||
enableCommenting, | ||
disableCommenting, | ||
} | ||
} |
Oops, something went wrong.