Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Pronouns #274

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ __pycache__/

*.json

*.env
*.env

.idea/*
7 changes: 6 additions & 1 deletion database.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from utils import log

CURRENT_DB_VERSION = 9
CURRENT_DB_VERSION = 10

# Create Redis connection
log("Connecting to Redis...")
Expand Down Expand Up @@ -306,7 +306,12 @@ def get_total_pages(collection: str, query: dict, page_size: int = 25) -> int:
"mfa_recovery_code": user["mfa_recovery_code"][:10]
}})

log("[Migrator] Adding pronouns to users")
db.usersv0.update_many({"pronouns": {"$exists": False}}, {"$set": {"pronouns": []}})

db.config.update_one({"_id": "migration"}, {"$set": {"database": CURRENT_DB_VERSION}})
log(f"[Migrator] Finished Migrating DB to version {CURRENT_DB_VERSION}")



print("") # finished startup logs
28 changes: 14 additions & 14 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
bcrypt
requests
bcrypt~=4.1.2
requests~=2.31.0
quart-cors
quart-schema[pydantic]
uuid
quart
pymongo
redis
python-dotenv
uvicorn
uuid~=1.30
quart~=0.19.4
pymongo~=4.6.2
redis~=5.0.2
python-dotenv~=1.0.1
uvicorn~=0.27.1
py-radix
pydantic
msgpack
pydantic~=2.6.3
msgpack~=1.0.8
grpcio
protobuf
pyotp
emoji
websockets
qrcode
pyotp~=2.9.0
emoji~=2.12.1
websockets~=12.0
qrcode~=7.4.2
10 changes: 9 additions & 1 deletion rest_api/v0/me.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class UpdateConfigBody(BaseModel):
debug: Optional[bool] = Field(default=None)
hide_blocked_users: Optional[bool] = Field(default=None)
favorited_chats: Optional[List[str]] = Field(default=None)
pronouns: Optional[List[str]] = Field(default=None, max_length=10)

class Config:
validate_assignment = True
Expand Down Expand Up @@ -79,7 +80,9 @@ async def get_me():
db.usersv0.update_one({"_id": request.user}, {"$set": {"last_seen": int(time.time())}})

# Get and return account
return security.get_account(request.user, include_config=True), 200
account = security.get_account(request.user, include_config=True)
account["error"] = False
return account, 200


@me_bp.delete("/")
Expand Down Expand Up @@ -160,6 +163,8 @@ async def update_config(data: UpdateConfigBody):
except Exception as e:
log(f"Unable to delete avatar: {e}")



# Update config
security.update_settings(request.user, new_config)

Expand All @@ -176,6 +181,9 @@ async def update_config(data: UpdateConfigBody):
updated_profile_data["avatar_color"] = new_config["avatar_color"]
if "quote" in new_config:
updated_profile_data["quote"] = new_config["quote"]
if "pronouns" in new_config:
updated_profile_data["pronouns"] = new_config["pronouns"]

if len(updated_profile_data) > 1:
app.cl.send_event("update_profile", updated_profile_data)

Expand Down
5 changes: 4 additions & 1 deletion security.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ def create_account(username: str, password: str, ip: str):
"reason": ""
},
"last_seen": int(time.time()),
"delete_after": None
"delete_after": None,
"pronouns": []
})
db.user_settings.insert_one({"_id": username})

Expand Down Expand Up @@ -279,6 +280,8 @@ def update_settings(username, newdata):
updated_user_vals["avatar"] = newdata["avatar"]
if "avatar_color" in newdata and isinstance(newdata["avatar_color"], str) and len(newdata["avatar_color"]) == 6:
updated_user_vals["avatar_color"] = newdata["avatar_color"]
if "pronouns" in newdata and isinstance(newdata["pronouns"], list):
updated_user_vals["pronouns"] = newdata["pronouns"]

# Update quote
if "quote" in newdata and isinstance(newdata["quote"], str) and len(newdata["quote"]) <= 360:
Expand Down
3 changes: 2 additions & 1 deletion supporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ def parse_posts_v0(
"flags": 1,
"pfp_data": 1,
"avatar": 1,
"avatar_color": 1
"avatar_color": 1,
"pronouns": 1,
})})

# Replies
Expand Down
74 changes: 74 additions & 0 deletions test/test_pronouns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import requests
import json

import websockets.sync.client
from websockets.sync.client import connect
import pymongo
import dotenv

config = dotenv.dotenv_values(".env") # Main environment variables that the server uses

if config["CAPTCHA_SITEKEY"] != "":
print("Please disable captchas in your .env file before running this script.")
exit(1)


db = pymongo.MongoClient(config["MONGO_URI"])[config["MONGO_DB"]]


USERNAME = "test"
PRONOUNS = [
["he", "him"],
["they", "them"]
]

API = config["INTERNAL_API_ENDPOINT"]

db.get_collection("usersv0").delete_one({"_id": USERNAME})
db.get_collection("user_settings").delete_one({"_id": USERNAME})

resp = requests.post(API + "/auth/register", json={
"username": USERNAME,
"password": "password",
}).json()

websocket = connect(f"ws://localhost:{config["CL3_PORT"]}/?v=1&token={resp["token"]}")

USER = json.loads(websocket.recv())["val"]

TOKEN = USER["token"]

if USER["account"]["pronouns"]:
print("Pronouns already set.")
exit(1)


requests.patch(API + "/me/config", json={
"pronouns": PRONOUNS
}, headers={
"token": TOKEN

})

websocket.recv()
websocket.recv()
websocket.recv()



requests.post(API + "/home/", json={
"content": "content"
}, headers={
"token": TOKEN
})
p = websocket.recv()
post = json.loads(p)["val"]

if post["author"]["pronouns"] != PRONOUNS:
print("Pronouns not set.")
websocket.close()
exit(1)

websocket.close()

print("Pronouns test passed.")