Skip to content

Commit

Permalink
Feat: create_post event impl
Browse files Browse the repository at this point in the history
  • Loading branch information
showierdata9978 committed Aug 13, 2024
1 parent 0204117 commit 5075332
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 2 deletions.
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/
2 changes: 2 additions & 0 deletions rest_api/v0/chats.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import security
from database import db, get_total_pages
from meowid import gen_id
from uploads import claim_file, delete_file
from utils import log

Expand Down Expand Up @@ -96,6 +97,7 @@ async def create_chat(data: ChatBody):
data.allow_pinning = False
chat = {
"_id": str(uuid.uuid4()),
"meowid": gen_id(),
"type": 0,
"nickname": data.nickname,
"icon": data.icon,
Expand Down
2 changes: 2 additions & 0 deletions security.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import time, requests, os, uuid, secrets, bcrypt, msgpack

from database import db, rdb
from meowid import gen_id
from utils import log
from uploads import clear_files

Expand Down Expand Up @@ -127,6 +128,7 @@ def create_account(username: str, password: str, ip: str):
# Create user
db.usersv0.insert_one({
"_id": username,
"meowid": gen_id(),
"lower_username": username.lower(),
"uuid": str(uuid.uuid4()),
"created": int(time.time()),
Expand Down
119 changes: 118 additions & 1 deletion supporter.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import hashlib
from threading import Thread
from typing import Optional, Iterable, Any
import uuid, time, msgpack, pymongo, re, copy

from cloudlink import CloudlinkServer
from database import db, rdb
from meowid import gen_id
from uploads import FileDetails

"""
Expand All @@ -14,6 +16,38 @@
FILE_ID_REGEX = "[a-zA-Z0-9]{24}"
CUSTOM_EMOJI_REGEX = f"<:({FILE_ID_REGEX})>"

OpCreateUser = 0
OpUpdateUser = 1
OpDeleteUser = 2
OpUpdateUserSettings = 3

OpRevokeSession = 4

OpUpdateRelationship = 5

OpCreateChat = 6
OpUpdateChat = 7
OpDeleteChat = 8

OpCreateChatMember = 9
OpUpdateChatMember = 10
OpDeleteChatMember = 11

OpCreateChatEmote = 12
OpUpdateChatEmote = 13
OpDeleteChatEmote = 14

OpTyping = 15

OpCreatePost = 16
OpUpdatePost = 17
OpDeletePost = 18
OpBulkDeletePosts = 19

OpPostReactionAdd = 20
OpPostReactionRemove = 21


class Supporter:
def __init__(self, cl: CloudlinkServer):
# CL server
Expand Down Expand Up @@ -84,6 +118,7 @@ def create_post(
# Construct post object
post = {
"_id": post_id,
"meowid": gen_id(),
"post_origin": origin,
"u": author,
"t": {"e": int(time.time())},
Expand All @@ -105,12 +140,16 @@ def create_post(
if nonce:
post["nonce"] = nonce



# Send live packet
if origin == "inbox":
self.cl.send_event("inbox_message", copy.copy(post), usernames=(None if author == "Server" else [author]))
else:
self.cl.send_event("post", copy.copy(post), usernames=(None if origin in ["home", "livechat"] else chat_members))

self.send_post_event(post)

# Update other database items
if origin == "inbox":
if author == "Server":
Expand Down Expand Up @@ -192,7 +231,8 @@ def parse_posts_v0(
"flags": 1,
"pfp_data": 1,
"avatar": 1,
"avatar_color": 1
"avatar_color": 1,
"meowid": 1
})})

# Replies
Expand Down Expand Up @@ -238,3 +278,80 @@ def parse_posts_v0(
})

return posts

@staticmethod
def send_event(event: int, data: dict[str, any]):
payload = bytearray(msgpack.packb(data))
payload.insert(0, event)

rdb.publish("events", payload)


def parse_post_meowid(self, post: dict[str, Any], include_replies: bool = True):
post = list(self.parse_posts_v0([post], include_replies=include_replies, include_revisions=False))[0]

match post["post_origin"]:
case "home":
chat_id = 0
case "livechat":
chat_id = 1
case "inbox":
chat_id = 2
case _:
chat_id = db.get_collection("chats").find_one({"_id": post["post_origin"]}, projection={"meowid": 1})["meowid"]

replys = []
if include_replies:
replys = [reply["meowid"] for reply in post["reply_to"]]

return {
"id": post["meowid"],
"chat_id": chat_id,
"author_id": post["author"]["meowid"],
"reply_to_ids": replys,
"emoji_ids": [emoji["id"] for emoji in post["emojis"]],
"sticker_ids": post["stickers"],
"attachments": post["attachments"],
"content": post["p"],
"reactions": [{
"emoji": reaction["emoji"],
"count": reaction["count"]
} for reaction in post["reactions"]],
"last_edited": post.get("edited_at", 0),
"pinned": post["pinned"]
}

def send_post_event(self, original_post: dict[str, Any]):
post = self.parse_post_meowid(original_post, include_replies=True)

replies = {}
for reply in post["reply_to_ids"]:
replies[reply] = self.parse_post_meowid(db.get_collection("posts").find_one({"meowid": reply}), include_replies=False)

# TODO: What is the users field?

emotes = {}
for emoji in post["emoji_ids"]:
emotes[emoji] = {
"id": int(hashlib.sha1(emoji["_id"].encode()).hexdigest(), 16),
"chat_id": db.get_collection("chats").find_one({"_id": emoji["chat_id"]}, projection={"meowid": 1})["meowid"],
"name": emoji["name"],
"animated": emoji["animated"],
}

data = {
"post": post,
"reply_to": replies,
"emotes": emotes,
"attachments": original_post["attachments"],
}

is_dm = db.get_collection("chats").find_one({"_id": original_post["post_origin"], "owner": None}, projection={"meowid": 1})
if is_dm:
data["dm_to"] = db.get_collection("users").find_one({"_id": original_post["author"]["_id"]}, projection={"meowid": 1})["meowid"]
data["dm_chat"] = None # unspecifed

if "nonce" in original_post:
data["nonce"] = original_post["nonce"]

self.send_event(OpCreatePost, data)

0 comments on commit 5075332

Please sign in to comment.