client/server: lint
This commit is contained in:
parent
6afc4c915e
commit
85ae8173bb
20 changed files with 323 additions and 328 deletions
|
@ -77,7 +77,7 @@ const Admin = () => {
|
|||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{posts?.map((post, i) => (
|
||||
{posts?.map((post) => (
|
||||
<tr key={post.id}>
|
||||
<td><PostModal id={post.id} /></td>
|
||||
<td>{post.visibility}</td>
|
||||
|
|
|
@ -53,10 +53,9 @@ export const allowedFileNames = [
|
|||
".fish_prompt",
|
||||
".zshrc",
|
||||
".zsh",
|
||||
".zprofile",
|
||||
".zprofile"
|
||||
]
|
||||
|
||||
|
||||
export const codeFileExtensions = [
|
||||
"awk",
|
||||
"bat",
|
||||
|
@ -120,5 +119,5 @@ export const allowedFileExtensions = [
|
|||
"txt",
|
||||
"webmanifest",
|
||||
"log",
|
||||
...codeFileExtensions,
|
||||
...codeFileExtensions
|
||||
]
|
|
@ -1,11 +1,13 @@
|
|||
import { User } from "@lib/types";
|
||||
import Cookies from "js-cookie";
|
||||
import { useRouter } from "next/router";
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { User } from "@lib/types"
|
||||
import Cookies from "js-cookie"
|
||||
import { useRouter } from "next/router"
|
||||
import { useEffect, useMemo, useState } from "react"
|
||||
|
||||
const useUserData = () => {
|
||||
const [authToken, setAuthToken] = useState<string>(Cookies.get("drift-token") || '');
|
||||
const [user, setUser] = useState<User>();
|
||||
const [authToken, setAuthToken] = useState<string>(
|
||||
Cookies.get("drift-token") || ""
|
||||
)
|
||||
const [user, setUser] = useState<User>()
|
||||
const router = useRouter()
|
||||
useEffect(() => {
|
||||
const token = Cookies.get("drift-token")
|
||||
|
@ -19,7 +21,7 @@ const useUserData = () => {
|
|||
const fetchUser = async () => {
|
||||
const response = await fetch(`/server-api/users/self`, {
|
||||
headers: {
|
||||
"Authorization": `Bearer ${authToken}`
|
||||
Authorization: `Bearer ${authToken}`
|
||||
}
|
||||
})
|
||||
if (response.ok) {
|
||||
|
@ -35,7 +37,7 @@ const useUserData = () => {
|
|||
}
|
||||
}, [authToken, router])
|
||||
|
||||
return user;
|
||||
return user
|
||||
}
|
||||
|
||||
export default useUserData
|
|
@ -1,14 +1,11 @@
|
|||
import databasePath from "@lib/get-database-path";
|
||||
import databasePath from "@lib/get-database-path"
|
||||
import { Sequelize } from "sequelize-typescript"
|
||||
import { SequelizeStorage, Umzug } from "umzug"
|
||||
|
||||
export const sequelize = new Sequelize({
|
||||
dialect: "sqlite",
|
||||
database: "drift",
|
||||
storage:
|
||||
process.env.MEMORY_DB === "true"
|
||||
? ":memory:"
|
||||
: databasePath,
|
||||
storage: process.env.MEMORY_DB === "true" ? ":memory:" : databasePath,
|
||||
models: [__dirname + "/lib/models"],
|
||||
logging: true
|
||||
})
|
||||
|
@ -20,14 +17,18 @@ if (process.env.MEMORY_DB !== "true") {
|
|||
}
|
||||
|
||||
const umzug = new Umzug({
|
||||
migrations: { glob: process.env.NODE_ENV === "production" ? __dirname + "/migrations/*.js" : __dirname + "/migrations/*.ts" },
|
||||
migrations: {
|
||||
glob:
|
||||
process.env.NODE_ENV === "production"
|
||||
? __dirname + "/migrations/*.js"
|
||||
: __dirname + "/migrations/*.ts"
|
||||
},
|
||||
context: sequelize.getQueryInterface(),
|
||||
storage: new SequelizeStorage({ sequelize }),
|
||||
logger: console
|
||||
})
|
||||
|
||||
export type Migration = typeof umzug._types.migration
|
||||
|
||||
;(async () => {
|
||||
// Checks migrations and run them if they are not already applied. To keep
|
||||
// track of the executed migrations, a table (and sequelize model) called SequelizeMeta
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
export default {
|
||||
port: process.env.PORT || 3000,
|
||||
jwt_secret: process.env.JWT_SECRET || "myjwtsecret",
|
||||
drift_home: process.env.DRIFT_HOME || "~/.drift",
|
||||
drift_home: process.env.DRIFT_HOME || "~/.drift"
|
||||
}
|
||||
|
|
|
@ -7,10 +7,11 @@ import config from "./config"
|
|||
// This does *not* support `~other_user/tmp` => `/home/other_user/tmp`.
|
||||
function getDatabasePath() {
|
||||
const fileName = "drift.sqlite"
|
||||
const databasePath = `${config.drift_home}/${fileName}` || `~/.drift/${fileName}`
|
||||
const databasePath =
|
||||
`${config.drift_home}/${fileName}` || `~/.drift/${fileName}`
|
||||
|
||||
const home = os.homedir().replace("$", "$$$$");
|
||||
return path.resolve(databasePath.replace(/^~($|\/|\\)/, home + "$1"));
|
||||
const home = os.homedir().replace("$", "$$$$")
|
||||
return path.resolve(databasePath.replace(/^~($|\/|\\)/, home + "$1"))
|
||||
}
|
||||
|
||||
export default getDatabasePath()
|
||||
|
|
|
@ -25,11 +25,11 @@ export default function authenticateToken(
|
|||
if (err) return res.sendStatus(403)
|
||||
const userObj = await UserModel.findByPk(user.id, {
|
||||
attributes: {
|
||||
exclude: ["password"],
|
||||
exclude: ["password"]
|
||||
}
|
||||
})
|
||||
|
||||
if (!userObj || userObj.role !== 'admin') {
|
||||
if (!userObj || userObj.role !== "admin") {
|
||||
return res.sendStatus(403)
|
||||
}
|
||||
|
||||
|
|
|
@ -28,11 +28,9 @@ import { User } from "./User"
|
|||
]
|
||||
}
|
||||
}))
|
||||
|
||||
@Table({
|
||||
tableName: "files",
|
||||
tableName: "files"
|
||||
})
|
||||
|
||||
export class File extends Model {
|
||||
@IsUUID(4)
|
||||
@PrimaryKey
|
||||
|
|
|
@ -38,11 +38,9 @@ import { File } from "./File"
|
|||
]
|
||||
}
|
||||
}))
|
||||
@Table(
|
||||
{
|
||||
tableName: "posts",
|
||||
}
|
||||
)
|
||||
@Table({
|
||||
tableName: "posts"
|
||||
})
|
||||
export class Post extends Model {
|
||||
@IsUUID(4)
|
||||
@PrimaryKey
|
||||
|
|
|
@ -12,9 +12,8 @@ import { Post } from "./Post"
|
|||
import { User } from "./User"
|
||||
|
||||
@Table({
|
||||
tableName: "post_authors",
|
||||
tableName: "post_authors"
|
||||
})
|
||||
|
||||
export class PostAuthor extends Model {
|
||||
@IsUUID(4)
|
||||
@PrimaryKey
|
||||
|
|
|
@ -30,9 +30,8 @@ import { PostAuthor } from "./PostAuthor"
|
|||
}
|
||||
}))
|
||||
@Table({
|
||||
tableName: "users",
|
||||
tableName: "users"
|
||||
})
|
||||
|
||||
export class User extends Model {
|
||||
@IsUUID(4)
|
||||
@PrimaryKey
|
||||
|
|
|
@ -11,19 +11,19 @@ export const up: Migration = async ({ context: queryInterface }) =>
|
|||
unique: true
|
||||
},
|
||||
username: {
|
||||
type: DataTypes.STRING,
|
||||
type: DataTypes.STRING
|
||||
},
|
||||
password: {
|
||||
type: DataTypes.STRING,
|
||||
type: DataTypes.STRING
|
||||
},
|
||||
createdAt: {
|
||||
type: DataTypes.DATE,
|
||||
type: DataTypes.DATE
|
||||
},
|
||||
updatedAt: {
|
||||
type: DataTypes.DATE,
|
||||
type: DataTypes.DATE
|
||||
},
|
||||
deletedAt: {
|
||||
type: DataTypes.DATE,
|
||||
type: DataTypes.DATE
|
||||
}
|
||||
})
|
||||
|
||||
|
|
|
@ -11,22 +11,22 @@ export const up: Migration = async ({ context: queryInterface }) =>
|
|||
unique: true
|
||||
},
|
||||
title: {
|
||||
type: DataTypes.STRING,
|
||||
type: DataTypes.STRING
|
||||
},
|
||||
visibility: {
|
||||
type: DataTypes.STRING,
|
||||
type: DataTypes.STRING
|
||||
},
|
||||
password: {
|
||||
type: DataTypes.STRING,
|
||||
type: DataTypes.STRING
|
||||
},
|
||||
createdAt: {
|
||||
type: DataTypes.DATE,
|
||||
type: DataTypes.DATE
|
||||
},
|
||||
updatedAt: {
|
||||
type: DataTypes.DATE,
|
||||
type: DataTypes.DATE
|
||||
},
|
||||
deletedAt: {
|
||||
type: DataTypes.DATE,
|
||||
type: DataTypes.DATE
|
||||
}
|
||||
})
|
||||
|
||||
|
|
|
@ -12,26 +12,25 @@ export const up: Migration = async ({ context: queryInterface }) =>
|
|||
unique: true
|
||||
},
|
||||
title: {
|
||||
type: DataTypes.STRING,
|
||||
type: DataTypes.STRING
|
||||
},
|
||||
content: {
|
||||
type: DataTypes.STRING,
|
||||
type: DataTypes.STRING
|
||||
},
|
||||
sha: {
|
||||
type: DataTypes.STRING,
|
||||
|
||||
type: DataTypes.STRING
|
||||
},
|
||||
html: {
|
||||
type: DataTypes.STRING,
|
||||
type: DataTypes.STRING
|
||||
},
|
||||
createdAt: {
|
||||
type: DataTypes.DATE,
|
||||
type: DataTypes.DATE
|
||||
},
|
||||
updatedAt: {
|
||||
type: DataTypes.DATE,
|
||||
type: DataTypes.DATE
|
||||
},
|
||||
deletedAt: {
|
||||
type: DataTypes.DATE,
|
||||
type: DataTypes.DATE
|
||||
},
|
||||
userId: {
|
||||
type: DataTypes.UUID,
|
||||
|
|
|
@ -7,13 +7,13 @@ export const up: Migration = async ({ context: queryInterface }) =>
|
|||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
primaryKey: true
|
||||
},
|
||||
createdAt: {
|
||||
type: DataTypes.DATE,
|
||||
type: DataTypes.DATE
|
||||
},
|
||||
updatedAt: {
|
||||
type: DataTypes.DATE,
|
||||
type: DataTypes.DATE
|
||||
},
|
||||
postId: {
|
||||
type: DataTypes.UUID,
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import isAdmin from "@lib/middleware/is-admin";
|
||||
import { Post } from "@lib/models/Post";
|
||||
import { User } from "@lib/models/User";
|
||||
import { File } from "@lib/models/File";
|
||||
import { Router } from "express";
|
||||
import isAdmin from "@lib/middleware/is-admin"
|
||||
import { Post } from "@lib/models/Post"
|
||||
import { User } from "@lib/models/User"
|
||||
import { File } from "@lib/models/File"
|
||||
import { Router } from "express"
|
||||
|
||||
export const admin = Router()
|
||||
|
||||
|
@ -27,7 +27,7 @@ admin.get("/users", async (req, res, next) => {
|
|||
as: "posts",
|
||||
attributes: ["id"]
|
||||
}
|
||||
],
|
||||
]
|
||||
})
|
||||
res.json(users)
|
||||
} catch (e) {
|
||||
|
@ -112,4 +112,3 @@ admin.delete("/post/:id", async (req, res, next) => {
|
|||
next(e)
|
||||
}
|
||||
})
|
||||
|
||||
|
|
|
@ -68,7 +68,10 @@ auth.post(
|
|||
const user = {
|
||||
username: username as string,
|
||||
password: await hash(req.body.password, salt),
|
||||
role: (!!process.env.MEMORY_DB && process.env.ENABLE_ADMIN && count === 0) ? "admin" : "user"
|
||||
role:
|
||||
!!process.env.MEMORY_DB && process.env.ENABLE_ADMIN && count === 0
|
||||
? "admin"
|
||||
: "user"
|
||||
}
|
||||
|
||||
const created_user = await User.create(user)
|
||||
|
|
|
@ -80,7 +80,7 @@ posts.post(
|
|||
.update(file.content)
|
||||
.digest("hex")
|
||||
.toString(),
|
||||
html: html || '',
|
||||
html: html || "",
|
||||
userId: req.body.userId,
|
||||
postId: newPost.id
|
||||
})
|
||||
|
@ -183,9 +183,7 @@ posts.get(
|
|||
{ "$files.title$": { [Op.like]: `%${q}%` } },
|
||||
{ "$files.content$": { [Op.like]: `%${q}%` } }
|
||||
],
|
||||
[Op.and]: [
|
||||
{ "$users.id$": req.user?.id || "" },
|
||||
]
|
||||
[Op.and]: [{ "$users.id$": req.user?.id || "" }]
|
||||
},
|
||||
include: [
|
||||
{
|
||||
|
@ -195,7 +193,7 @@ posts.get(
|
|||
},
|
||||
{
|
||||
model: User,
|
||||
as: "users",
|
||||
as: "users"
|
||||
}
|
||||
],
|
||||
attributes: ["id", "title", "visibility", "createdAt", "deletedAt"],
|
||||
|
@ -296,7 +294,6 @@ posts.delete("/:id", jwt, async (req: UserJwtRequest, res, next) => {
|
|||
return res.status(404).json({ error: "Post not found" })
|
||||
}
|
||||
|
||||
|
||||
if (req.user?.id !== post.users![0].id) {
|
||||
return res.status(403).json({ error: "Forbidden" })
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue