Skip to content

Commit

Permalink
AAA
Browse files Browse the repository at this point in the history
  • Loading branch information
HoodieRocks committed Dec 8, 2023
2 parents cc32eb5 + edcc19b commit b094f16
Show file tree
Hide file tree
Showing 18 changed files with 682 additions and 306 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,6 @@ fabric.properties
.idea/caches/build_file_checksums.ser

.dccache
config-unsafe.py
# for now
# config.py
config.py
11 changes: 10 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,14 @@
"rowid",
"Silabear",
"sqlalchemy"
]
],
"[python]": {
"diffEditor.ignoreTrimWhitespace": false,
"gitlens.codeLens.symbolScopes": [
"!Module"
],
"editor.formatOnType": true,
"editor.wordBasedSuggestions": false,
"editor.defaultFormatter": "charliermarsh.ruff"
}
}
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ Get started with the Datapack Hub API development and contribute to its enhancem

1. **Clone the Repository:** Begin by cloning the repository using the following command:

```bash
git clone https://github.com/Datapack-Hub/api.git
```

2. **Configure `prod.py`:** Create a `prod.py` file to set the environment:

```python
Expand Down
4 changes: 2 additions & 2 deletions data/roles.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"name": "donator",
"color": "#00ff00",
"permissions": [],
"verified": "true"
"verified": true
},
{
"name": "beta access",
Expand All @@ -61,4 +61,4 @@
"verified": false
}
]
}
}
9 changes: 8 additions & 1 deletion gen_example_data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from utilities import util
from utilities import test_utils


def reset(table: str):
Expand Down Expand Up @@ -133,5 +134,11 @@ def reset(table: str):
reset("no-drop")

util.commit_query(
"""INSERT INTO users (username, token, role, bio, github_id, profile_icon) VALUES ("HoodieRocks", "LOREMIPSUM", "admin", "rock", 123897432978, "https://example.com/")""",
"""INSERT INTO users (username, token, role, bio, github_id, profile_icon, join_date) VALUES ("HoodieRocks", "LOREMIPSUM", "admin", "rock", 123897432978, "https://example.com/", 0)""",
)

util.commit_query(
"""INSERT INTO users (username, token, role, bio, github_id, profile_icon, join_date) VALUES ("sacrifice", "POTATOES", "default", "paper", 238746238746, "https://example.com/1", 0)""",
)

test_utils.commit_fake_project(10)
2 changes: 1 addition & 1 deletion index.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from routes.versions import versions

app = flask.Flask(__name__)
CORS(app)
CORS(app, supports_credentials=True)
Compress(app)


Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.ruff]
# Enable pycodestyle (`E`) and Pyflakes (`F`) codes by default.
select = ["E", "F", "PERF", "RUF", "B", "N", "PTH"]
select = ["E", "F", "PERF", "RUF", "B", "N", "PTH", "TCH", "UP"]
ignore = ["E501", "E722"]

# Allow autofix for all enabled rules (when `--fix`) is provided.
Expand Down Expand Up @@ -39,7 +39,7 @@ line-length = 88
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"

# Assume Python 3.9
target-version = "py39"
target-version = "py310"

[tool.ruff.mccabe]
# Unlike Flake8, default to a complexity level of 10.
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
bleach==6.1.0
disnake==2.9.0
Faker==20.1.0
Flask==3.0.0
Flask_Compress==1.14
Flask_Cors==4.0.0
Expand Down
12 changes: 10 additions & 2 deletions routes/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def login_dc():
def callback_gh():
# Get an access token
code = request.args.get("code")

if code is None:
return "Github screwed up :(", 500

access_token = requests.post(
f"https://github.com/login/oauth/access_token?client_id={config.GitHub.client_id}&client_secret={config.GitHub.client_secret}&code={quote(code)}",
headers={"Accept": "application/json"},
Expand Down Expand Up @@ -172,7 +176,9 @@ def link_discord():
usr = utilities.auth_utils.authenticate(request.headers.get("Authorization"))
if usr == 32:
return "Please make sure authorization type = Basic", 400
if usr == 33:
elif usr == 31:
return "Provide Authorization header!", 400
elif usr == 33:
return "Token Expired", 401

# Get discord user info
Expand Down Expand Up @@ -238,7 +244,9 @@ def link_github():
usr = utilities.auth_utils.authenticate(request.headers.get("Authorization"))
if usr == 32:
return "Please make sure authorization type = Basic", 400
if usr == 33:
elif usr == 31:
return "Provide Authorization header!", 400
elif usr == 33:
return "Token Expired", 401

# Get github ID
Expand Down
45 changes: 34 additions & 11 deletions routes/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import sqlite3
import time
from typing import Any

import regex
from flask import Blueprint, request
Expand All @@ -16,26 +17,33 @@


@comments.route("/thread/<int:thread>")
def messages_from_thread(thread: int):
def messages_from_thread(thread: int) -> tuple[dict[str, Any] | str, int]:
conn = util.make_connection()
cmts = util.exec_query(
conn,
"select rowid, message, author, sent from comments where thread_id = :thread and parent_id is null order by sent desc",
thread=thread,
).all()

out = []
out: list[dict[str, Any]] = []
for cmt in cmts:
author = utilities.get_user.from_id(cmt[2])

if author is None:
return "User is not defined!", 400
replies = util.exec_query(
conn,
"select rowid, message, author, sent from comments where thread_id = :thread and parent_id = :comment order by sent desc",
thread=thread,
comment=cmt[0],
).all()
reps = []
reps: list[dict[str, Any]] = []
for reply in replies:
repl_auth = utilities.get_user.from_id(reply[2])

if repl_auth is None:
return "User is not defined!", 400

reps.append(
{
"id": reply[0],
Expand Down Expand Up @@ -67,7 +75,7 @@ def messages_from_thread(thread: int):
"replies": reps,
}
)
return {"count": out.__len__(), "result": out}
return {"count": out.__len__(), "result": out}, 200


@comments.route("/thread/<int:thread>/post", methods=["POST"])
Expand All @@ -77,6 +85,8 @@ def post_msg(thread: int):
usr = utilities.auth_utils.authenticate(request.headers.get("Authorization"))
if usr == 32:
return "Please make sure authorization type = Basic", 400
if usr == 31:
return "Provide Authorization header", 400
if usr == 33:
return "Token Expired", 401

Expand All @@ -88,7 +98,7 @@ def post_msg(thread: int):

conn = util.make_connection()
try:
mentions = regex.findall("@(\w+)", cmt_data["message"])
mentions = regex.findall(r"@(\w+)", cmt_data["message"])
for user in mentions:
if utilities.get_user.from_username(user):
auth = util.exec_query(
Expand Down Expand Up @@ -174,7 +184,7 @@ def post_msg(thread: int):


@comments.route("/id/<int:id>", methods=["GET", "DELETE"])
def get_comment(id: int):
def get_comment(id: int) -> tuple[dict[str, Any] | str, int]:
if request.method == "GET":
conn = util.make_connection()
comment = util.exec_query(
Expand All @@ -190,6 +200,9 @@ def get_comment(id: int):

author = utilities.get_user.from_id(comment[2])

if author is None:
return "Something went wrong!", 500

replies = util.exec_query(
conn,
"select rowid, message, author, sent from comments where parent_id = :id order by sent desc",
Expand All @@ -198,6 +211,10 @@ def get_comment(id: int):
reps = []
for reply in replies:
repl_auth = utilities.get_user.from_id(reply[2])

if repl_auth is None:
return "We don't know how this happened, but it did", 500

reps.append(
{
"id": reply[0],
Expand Down Expand Up @@ -227,12 +244,12 @@ def get_comment(id: int):
},
"sent": comment[3],
"replies": reps,
}
}, 200
elif request.method == "DELETE":
conn = util.make_connection()
comment = util.exec_query(
conn,
"select rowid, message, author, sent from comments where rowid = :id and parent_id is null order by sent desc",
"select rowid, message, author, sent, parent_id from comments where rowid = :id order by sent desc",
id=id,
).all()

Expand All @@ -246,15 +263,21 @@ def get_comment(id: int):
usr = utilities.auth_utils.authenticate(request.headers.get("Authorization"))
if usr == 32:
return "Please make sure authorization type = Basic", 400
if usr == 31:
return "Provide Authorization header", 400
if usr == 33:
return "Token Expired", 401

if not (usr.id == comment[2] or usr.role in ["admin", "moderator"]):
return "This isn't your comment.", 403

util.exec_query(conn, "delete from comments where rowid = :id", id=id)
util.exec_query(conn, "delete from comments where parent_id = :id", id=id)
if comment[4] is None:
util.exec_query(conn, "delete from comments where rowid = :id", id=id)
util.exec_query(conn, "delete from comments where parent_id = :id", id=id)
else:
util.exec_query(conn, "delete from comments where rowid = :id", id=id)

conn.commit()

return "Deleted comment."
return "Deleted comment.", 200
return "HTTP Method disallowed", 400
Loading

0 comments on commit b094f16

Please sign in to comment.