GitHub - flow-state-15/NotDiscord: Pixel perfect clone of discord complete with friends, messaging, servers, channels and group messaging features.
Pixel perfect clone of discord complete with friends, messaging, servers, channels and group messaging features. - flow-state-15/NotDiscord

a discord clone built as a group project with three others. we used flask and sqlalchemy on the back end, react/redux on the front end, flask-socketio for the live functionality, and docker for deployment.

a server with channels, the member list and a message history.
a server with channels, the member list and a message history.
server discovery.
server discovery.

messaging is over websockets. a client joins a room per channel and sending a message emits to that room so everyone in it sees it without refreshing. the handler broadcasts the message as-is, including the id the database already assigned to it, so the clients can slot it into their existing message list without a refetch.

@socketio.on("message")
def handle_chat(data):
    send({
        'id': data['id'],
        'content': data['content'],
        'user': data['user'],
        'sent_date': data['sent_date'],
    }, room=data['room'])

@socketio.on('join')
def join(data):
    join_room(data['room'])

there's also a history endpoint for loading older messages when you scroll up. when a message contains a link it gets unfurled into an embed. images, videos and regular pages each get their own format.

links get unfurled into embeds when a message is sent.
links get unfurled into embeds when a message is sent.

a friendship is a single row with a sender, a receiver and an accepted flag, which means it's directional. listing someones friends therefore has to run two queries. one for rows where they sent the request and one for rows where they received it, and merge the two.

@friend_routes.route('/<int:user_id>')
@login_required
def get_friends(user_id):
    sent = Friend.query.filter(Friend.sender_user_id == user_id).all()
    received = Friend.query.filter(Friend.rec_user_id == user_id).all()

    friends = []
    for row in sent:
        user = User.query.get(row.rec_user_id).to_dict()
        user['friend_data'] = row.to_dict()
        friends.append(user)
    for row in received:
        user = User.query.get(row.sender_user_id).to_dict()
        user['friend_data'] = row.to_dict()
        friends.append(user)

    return {"friends": friends}

accepting a request is a put that flips the flag, and removing a friend deletes the row, which works for either direction since the row doesn't care who's asking. once you're friends you can open a private dm, which is a two-person room on the same socket setup as channels, and group dms are the same thing with more people in the room.

sign up. every username gets a #1234 style tag so names don't have to be unique.
sign up. every username gets a #1234 style tag so names don't have to be unique.

during signup each account is assigned a randomly generated four-digit discriminator, allowing multiple users to share the same username without conflicts (e.g., neb#1234 and neb#5678). a random default avatar is also assigned as part of the registration process.

user = User(
    email=form.data['email'],
    hashed_password=generate_password_hash(form.data['password']),
    avatar=choice(avatars),
    tagged_name=form.data['username'] + '#' + str(randint(1000, 10000)),
    created_at=dt.datetime.now()
)