Пример #1
0
def alter_thread_subscription(thread_id: int) -> flask.Response:
    """
    This is the endpoint for forum thread subscription. The ``forums_subscriptions_modify``
    permission is required to access this endpoint. A POST request creates a subscription,
    whereas a DELETE request removes a subscription.

    .. :quickref: ForumThreadSubscription; Subscribe to a forum thread.

    **Example response**:

    .. parsed-literal::

       {
         "status": "success",
         "response": "Successfully subscribed to thread 2."
       }

    :>json str response: Success or failure message

    :statuscode 200: Subscription alteration successful
    :statuscode 400: Subscription alteration unsuccessful
    :statuscode 404: Forum thread does not exist
    """
    thread = ForumThread.from_pk(thread_id, _404=True)
    subscription = ForumThreadSubscription.from_attrs(
        user_id=flask.g.user.id, thread_id=thread.id
    )
    if flask.request.method == 'POST':
        if subscription:
            raise APIException(
                f'You are already subscribed to thread {thread_id}.'
            )
        ForumThreadSubscription.new(
            user_id=flask.g.user.id, thread_id=thread_id
        )
        return flask.jsonify(f'Successfully subscribed to thread {thread_id}.')
    else:  # method = DELETE
        if not subscription:
            raise APIException(
                f'You are not subscribed to thread {thread_id}.'
            )
        db.session.delete(subscription)
        db.session.commit()
        cache.delete(
            ForumThreadSubscription.__cache_key_users__.format(
                thread_id=thread_id
            )
        )
        cache.delete(
            ForumThreadSubscription.__cache_key_of_user__.format(
                user_id=flask.g.user.id
            )
        )
        return flask.jsonify(
            f'Successfully unsubscribed from thread {thread_id}.'
        )
Пример #2
0
def test_subscribe_thread_deletes_cache_keys(app, authed_client):
    add_permissions(app, ForumPermissions.MODIFY_SUBSCRIPTIONS)
    ForumThread.from_subscribed_user(1)
    ForumThreadSubscription.user_ids_from_thread(5)
    response = authed_client.post('/subscriptions/threads/5')
    assert response.status_code == 200
    assert not cache.get(
        ForumThreadSubscription.__cache_key_users__.format(thread_id=5)
    )
    assert not cache.get(
        ForumThreadSubscription.__cache_key_of_user__.format(user_id=1)
    )
    assert ForumThreadSubscription.user_ids_from_thread(5) == [1]
Пример #3
0
def subscribe_users_to_new_thread(thread: 'ForumThread') -> None:
    """
    Subscribes all users subscribed to the parent forum to the new forum thread.

    :param thread: The newly-created forum thread
    """
    from forums.models import ForumSubscription, ForumThreadSubscription

    user_ids = ForumSubscription.user_ids_from_forum(thread.forum_id)
    db.session.bulk_save_objects(
        [
            ForumThreadSubscription.new(user_id=uid, thread_id=thread.id)
            for uid in user_ids
        ]
    )
    ForumThreadSubscription.clear_cache_keys(user_ids=user_ids)
Пример #4
0
def send_subscription_notices(post: 'ForumPost') -> None:
    from forums.models import ForumThreadSubscription

    user_ids = ForumThreadSubscription.user_ids_from_thread(post.thread_id)
    if post.user_id in user_ids:
        user_ids.remove(post.user_id)
    _dispatch_notifications(
        post, type='forums_subscription', user_ids=user_ids
    )
Пример #5
0
def test_forum_thread_subscriptions_cache_keys_user_ids(app, authed_client):
    user_ids = ForumThreadSubscription.user_ids_from_thread(4)  # noqa
    cache.set(
        ForumThreadSubscription.__cache_key_of_user__.format(user_id=1),
        [14, 23],
    )
    cache.set(
        ForumThreadSubscription.__cache_key_users__.format(thread_id=4),
        [3, 4, 5],
    )
    assert 3 == len(
        cache.get(
            ForumThreadSubscription.__cache_key_users__.format(thread_id=4)))
    ForumThreadSubscription.clear_cache_keys(user_ids=[1, 2])
    assert 3 == len(
        cache.get(
            ForumThreadSubscription.__cache_key_users__.format(thread_id=4)))
    assert not cache.get(
        ForumThreadSubscription.__cache_key_of_user__.format(user_id=1))
Пример #6
0
def test_add_thread_forum_subscriptions(app, authed_client):
    add_permissions(app, 'forums_view', 'forums_threads_create')
    ForumThread.from_subscribed_user(user_id=1)  # cache
    response = authed_client.post(
        '/forums/threads',
        data=json.dumps(
            {'topic': 'New Thread', 'forum_id': 4, 'contents': 'aa'}
        ),
    )
    check_json_response(response, {'id': 6})
    assert response.get_json()['response']['forum']['id'] == 4
    assert {1, 2} == set(ForumThreadSubscription.user_ids_from_thread(6))
    assert 6 in {t.id for t in ForumThread.from_subscribed_user(user_id=1)}
def test_subscribe_users_to_new_thread(app, authed_client):
    thread = ForumThread.new(topic='aa',
                             forum_id=5,
                             creator_id=1,
                             post_contents='hello')
    assert ForumThreadSubscription.user_ids_from_thread(thread.id) == [3, 4]
Пример #8
0
def test_unsubscribe_from_thread(app, authed_client):
    add_permissions(app, 'forums_subscriptions_modify')
    response = authed_client.delete('/subscriptions/threads/3')
    check_json_response(response, 'Successfully unsubscribed from thread 3.')
    assert not ForumThreadSubscription.from_attrs(user_id=1, thread_id=3)
Пример #9
0
def test_subscribe_to_thread(app, authed_client):
    add_permissions(app, 'forums_subscriptions_modify')
    response = authed_client.post('/subscriptions/threads/5')
    check_json_response(response, 'Successfully subscribed to thread 5.')
    assert ForumThreadSubscription.from_attrs(user_id=1, thread_id=5)
Пример #10
0
def test_user_ids_from_thread_subscription(app, authed_client):
    assert [1] == ForumThreadSubscription.user_ids_from_thread(2)
    assert {1, 2} == set(ForumThreadSubscription.user_ids_from_thread(4))