Skip to content

Commit

Permalink
Add back the users cache
Browse files Browse the repository at this point in the history
Signed-off-by: Allie Crevier <[email protected]>
  • Loading branch information
Allie Crevier committed Feb 8, 2022
1 parent 2b93c90 commit 7314ad2
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions securedrop_client/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import shutil
from datetime import datetime
from pathlib import Path
from typing import Any, List, Tuple, Type, Union
from typing import Any, Dict, List, Tuple, Type, Union

from dateutil.parser import parse
from sdclientapi import API
Expand Down Expand Up @@ -413,21 +413,30 @@ def update_replies(
"""
local_replies_by_uuid = {r.uuid: r for r in local_replies}
deleted_user = session.query(User).filter_by(username="deleted").one_or_none()
user_cache: Dict[str, User] = {}
source_cache = SourceCache(session)
for reply in remote_replies:
# If the account for the sender does not exist, then replies will need to be re-associated
# to a local "deleted" user account. The only reason this might happen is if the pre-2.2.0
# server returns a `journalist_uuid` that does not correspond to a uuid for a real user
# account.
user = session.query(User).filter_by(uuid=reply.journalist_uuid).one_or_none()
user = user_cache.get(reply.journalist_uuid)
if not user:
logger.debug(f"Sender account with uuid='{reply.journalist_uuid}' does not exist")
if not deleted_user:
deleted_user = DeletedUser()
session.add(deleted_user)
session.commit()
user = deleted_user
logger.debug(f"Setting `journalist_id` for Reply '{reply.uuid}' to '{user.id}'")
user = session.query(User).filter_by(uuid=reply.journalist_uuid).one_or_none()

# If the account for the sender does not exist, then replies will need to be associated
# to a local "deleted" user account.
#
# Once support for the pre-2.2.0 server is deprecated, this code can be removed, and the
# client can rely entirely on the /users endpoint to manage user accounts. Until then,
# we must handle the case where the pre-2.2.0 server returns a `journalist_uuid` of
# "deleted" for a reply's sender when no actual account exists with that uuid.
if not user:
user = deleted_user
if not user:
user = DeletedUser()
session.add(user)
session.commit()
deleted_user = user

# Add the retrieved or newly created "deleted" user to the cache
user_cache[reply.journalist_uuid] = user

local_reply = local_replies_by_uuid.get(reply.uuid)
if local_reply:
Expand Down

0 comments on commit 7314ad2

Please sign in to comment.