def converse(messaging_user=""): # Implement socket functionality # check log in and grab user name if is_logged_in(): #check log in and grab user name main_user = get_username() main_user_id = get_id_from_username(main_user) #Grab friends friends = get_friends_db(main_user_id) #Grab every conversation all_conversations = get_conversations(main_user_id) #map friends to conversations and messages conversations = {} for friend in friends: friend_id = get_id_from_username(friend) for conversation in all_conversations: if conversation.first_user_id == friend_id or conversation.second_user_id == friend_id: conversations[friend] = conversation.messages else: #send to home screen if not logged in user return redirect('/auth/login') return render_template('conversation.html', username=main_user, friends=friends, conversations=conversations)
def add_friend(): """ Adds a friend submitted by form from the account page """ if request.method == 'POST': username = get_username() user_id = get_id_from_username(username) friend_to_add = get_id_from_username(request.form['add_user']) if not friend_to_add or friend_to_add==user_id: return redirect(url_for('message.converse')) add_friend_db(user_id, friend_to_add) return redirect(url_for('message.converse'))
def block_user(): """ Blocks a user submitted by form from account page """ if request.method == 'POST': username = get_username() user_id = get_id_from_username(username) to_block = get_id_from_username(request.form['block_user']) if not to_block or to_block==user_id: #TODO: some sort of error if blockee doesn't exist return redirect(url_for('users.account_page', username=username)) block_user_db(user_id, to_block) return redirect(url_for('users.account_page', username=username))
def add_message(message): sender_id = get_id_from_username(message['sender']) receiver_id = get_id_from_username(message['receiver']) #Find conversation to add message to conversation = get_conversation(sender_id, receiver_id) if not conversation: add_conversation(sender_id, receiver_id) conversation = get_conversation(sender_id, receiver_id) new_message = Message(link=message['link'], image=message['image'], sender=message['sender'], receiver=message['receiver'], timestamp=message['timestamp']) conversation.messages.append(new_message) db.session.add(new_message) db.session.commit() return
def change_username(): """ Changes a username based on form on account page """ if request.method == 'POST': username = get_username() new_username = request.form['change_username'] user_id = get_id_from_username(username) #TODO: Error handling on database writes lol change_username_from_id(user_id, new_username ) return redirect(url_for('users.account_page', username=new_username))
def account_page(username): """ Loads account page """ # See if this user is the user looking # Check to see if a user exists with that name current_user = get_username() viewing_self = (username==current_user) user_data = {"username": username, "view_self": viewing_self} user_id = get_id_from_username(current_user) friends = get_friends_db(user_id) users_friends = {} if (friends): for friend in friends: users_friends[friend] = 1 user = User.query.filter_by(username=username).first() if not user: return render_template('user_nonexistent.html') if viewing_self: blocked_group = Blocked.query.filter_by(user=user.id).all() blocked_names = [] if len(blocked_group) > 1: for blocked_user in blocked_group: username = get_username_from_id(blocked_user.blocked) blocked_names.append(username) elif len(blocked_group) == 1: blocked_names.append(get_username_from_id(blocked_group[0].blocked)) privacy = user.privacy user_data.update({"blocked_users": blocked_names, "privacy": privacy}) return render_template( 'account_page.html', user_data=user_data, logged_in=is_logged_in(), friends=users_friends)
def update_privacy(): if request.method == 'POST': username = get_username() user_id = get_id_from_username(username) toggle_privacy(user_id) return redirect(url_for('users.account_page', username=username))