Exemple #1
0
def user_notif_page():

    pend = current_user.get_notifications()

    return render_template('pages/user_notifications_page.html',
                           pending=pend,
                           other=[])
Exemple #2
0
def route_notifications():
    lang = Language(get_user_lang(request.headers, current_user))
    if current_user.is_authenticated and current_user.is_banned():
        return render_template("errors/banned.html", lang=lang)
    return render_template("notification/notifications.html",
                           lang=lang,
                           notifs=current_user.get_notifications(False),
                           render_notification=bind1(render_notification,
                                                     lang))
Exemple #3
0
def notifications(page):
    '''Allow users to check their notifications'''
    note_num = 20
    notes_all = current_user.get_notifications(limit=((page - 1) * note_num,
                                                      note_num))
    current_user.set_notifications_readall()
    invitations_all = current_user.get_invitation()
    num = current_user.get_unread_notifications_num()
    return render_template('user/notifications.jinja2',
                           notifications=notes_all[1],
                           number=num,
                           pagination=Pagination(page, note_num, notes_all[0]),
                           invitations=invitations_all)
Exemple #4
0
def read_notifications():
    # Change the last time the <current_user> looked at the notifications and commit the changes
    current_user.last_notif_read_time = datetime.utcnow()
    db.session.commit()

    # Get the user's notfications
    notifications = current_user.get_notifications()

    results = []
    if notifications:
        for info in notifications:
            timestamp = info.timestamp.replace(tzinfo=pytz.UTC).isoformat()
            results.append({
                'media_type': info.media_type,
                'media_id': info.media_id,
                'timestamp': timestamp,
                'payload': json.loads(info.payload_json)
            })

    return jsonify(results=results), 200
def group_chats_route():
    # TODO permissions to show

    action = get_arg_or_400('action')
    if action == 'all':
        group_id = get_arg_or_400('id', to_int=True)
        chats = Chat.query.filter_by(group_id=group_id, deleted=None).all()
        return render_template("group_chats.html",
                               chats=chats,
                               group=Group.get_or_404(group_id),
                               notification=current_user.get_notifications())

    chat_id = get_arg_or_400('chat_id', to_int=True)
    chat = Chat.get_or_404(chat_id)

    if action == 'delete':
        chat.deleted = timestamp()
        members = ChatMember.query.filter_by(chat_id=chat_id).all()
        for member in members:
            member.deleted = timestamp()
        db.session.commit()
        return redirect(request.referrer)

    elif action == 'show':
        group = Group.get_or_404(chat.group_id)
        chat.add_member(id=current_user.id)
        ChatNotification.remove(user_id=current_user.id, chat_id=chat_id)
        return render_template("chat.html",
                               group=group,
                               chat=chat,
                               current_user=current_user,
                               messages=chat.get_history())

    elif action == 'add_members':
        abort(400)
    else:
        abort(400)
Exemple #6
0
def home():
    # home if auth
    if current_user.is_authenticated():

        # projects

        projects_where_is_owner = []
        for project in current_user.projects:
            projects_where_is_owner.append({
                "id":
                project.id,
                "ownerName":
                project.owner_name,
                "name":
                project.name,
                "goToUrl":
                f"/{project.owner_name}/{project.name}"
            })

        projects_where_is_collab = current_user.get_projects_where_is_collaborator(
        )
        for i, project in enumerate(projects_where_is_collab):
            projects_where_is_collab[i] = {
                "id": project.id,
                "ownerName": project.owner_name,
                "name": project.name,
                "goToUrl": f"/{project.owner_name}/{project.name}"
            }

        # notifications

        notifications = current_user.get_notifications()
        for i, notification in enumerate(notifications):
            from_user = User.get_by_id(notification.from_id)

            if isinstance(notification, Msg):
                notifications[i] = {
                    "id": notification.id,
                    "type": "msg",
                    "chatType": "userToUser",
                    "picUrl": from_user.small_avatar_url,
                    "goToUrl": f"/{from_user.name}",
                    "goToChatUrl": f"/chat?with={from_user.name}",
                    "datetime": f"{notification.datetime}+00:00",
                    "content": notification.content
                }

            elif isinstance(notification, ChatGroupMsg):
                notifications[i] = {
                    "id": notification.id,
                    "type": "msg",
                    "chatType": "chatGroup",
                    "picUrl": from_user.small_avatar_url,
                    "goToUrl": f"/{from_user.name}",
                    "goToChatUrl": "/chat",
                    "datetime": f"{notification.datetime}+00:00",
                    "content": notification.content
                }

            elif isinstance(notification, ProjectChatGroupMsg):
                notifications[i] = {
                    "id": notification.id,
                    "type": "msg",
                    "chatType": "projectChatGroup",
                    "picUrl": from_user.small_avatar_url,
                    "goToUrl": f"/{from_user.name}",
                    "goToChatUrl": "/chat",
                    "datetime": f"{notification.datetime}+00:00",
                    "content": notification.content
                }

            elif isinstance(notification, FriendshipRequest):
                notifications[i] = {
                    "id": notification.id,
                    "type": "friendshipRequest",
                    "picUrl": from_user.small_avatar_url,
                    "goToUrl": f"/{from_user.name}",
                    "datetime": f"{notification.datetime}+00:00"
                }

        # friends

        friends = current_user.get_friends()
        for i, friend in enumerate(friends):
            friends[i] = {
                "id": friend.id,
                "name": friend.name,
                "picUrl": friend.small_avatar_url,
                "goToUrl": f"/{friend.name}"
            }

        return render_template("home.auth.html.j2",
                               projects={
                                   "whereOwner": projects_where_is_owner,
                                   "whereCollab": projects_where_is_collab
                               },
                               notifications=notifications,
                               friends=friends)

    # home if not auth
    return render_template("home.unauth.html.j2")
Exemple #7
0
def notifications():
    current_user.notification_last_read_time = datetime.utcnow()
    current_user.commit()
    notifications = current_user.get_notifications()
    return render_template('users/notifications.html',
                           notifications=notifications)
def chats_route():
    action = get_arg_or_400('action')

    if action == 'show':
        chat_id = get_arg_or_none('chat_id', to_int=True)
        user_id = get_arg_or_none('user_id', to_int=True)    # С кем чат
        if user_id is None:
            if chat_id is None:
                return redirect(url_for('chat', action='all'))
            chat = Chat.get_or_404(chat_id)
            if chat.deleted is not None:
                redirect(url_for('chats_route', action='all'))
            members = chat.get_members()
            if len(members) > 2:
                return redirect(url_for('group_chats_route', action='show', chat_id=chat_id, group_id=chat.group_id))
            elif len(members) < 2:
                user_id = 'Deleted'
            else:
                user_id = members[0].id if members[0].id != current_user.id else members[1].id
            return redirect(url_for("chats_route", action='show', chat_id=chat_id, user_id=user_id))
        if chat_id is None:
            chat = ChatMember.get_private_chat(current_user.id, user_id)
            if chat is None:
                chat = Chat(admin_id=current_user.id, time=timestamp())
                db.session.add(chat)
                db.session.commit()
                chat_id = chat.id
                chat.add_member(current_user.id, is_group=False)
                chat.add_member(user_id, is_group=False)
                db.session.commit()
                history = []
            elif chat.deleted is not None:
                return redirect(url_for('chats_route', action='all'))
            else:
                return redirect(url_for("chats_route", action='show', chat_id=chat.id, user_id=user_id))
        else:
            history = Chat.get_or_404(int(chat_id)).get_history()
            ChatNotification.remove(user_id=current_user.id, chat_id=chat_id)

        members = Chat.get_or_404(chat_id).get_members()
        if user_id not in list(map(lambda x: x.id, members)):
            abort(404)
        user_id = members[0].id if members[0].id != current_user.id else members[1].id
        return render_template(
            "chat.html",
            user_id=user_id,
            current_user=current_user,
            chat_name=User.get_or_404(user_id).username,
            chat=Chat.get_or_404(chat_id),
            messages=history
        )

    elif action == 'all':
        chats = ChatMember.get_user_chats(current_user.id)
        for i in chats:
            if i.name is None:
                members = i.get_members()
                for u in members:
                    if u is not None and u.id != current_user.id:
                        i.name = u.username
        return render_template(
            "my_chats.html",
            notifications=current_user.get_notifications(),
            current_user=current_user,
            chats=chats
        )

    elif action == 'delete':
        chat_id = get_arg_or_400('chat_id', to_int=True)
        if chat_id is None:
            return redirect(url_for('chats_route', action='all'))
        chat_id = int(chat_id)
        chat_member = ChatMember.query.filter_by(chat_id=chat_id, user_id=current_user.id).first()
        if chat_member is None:
            return redirect(request.referrer)
        chat_member.deleted = timestamp()
        db.session.commit()
        if not current_user.is_notified():
            socketIO.emit("clear_message", {}, room=current_user.id)
        if chat_member.is_group:
            return redirect(request.referrer)
        return redirect(url_for('chats_route', action='all'))
    else:
        abort(400)