From 56fb82181062923c2afa22efa63a8c8a63bb8fec Mon Sep 17 00:00:00 2001
From: roadscape <roadscape@users.noreply.github.com>
Date: Thu, 30 Jan 2020 11:41:48 -0600
Subject: [PATCH] posting_json_md upgrade path

---
 hive/indexer/accounts.py          |  1 +
 hive/utils/account.py             | 13 ++++++++++---
 tests/utils/test_utils_account.py |  7 ++++++-
 3 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/hive/indexer/accounts.py b/hive/indexer/accounts.py
index f1bb974fb..485320cf3 100644
--- a/hive/indexer/accounts.py
+++ b/hive/indexer/accounts.py
@@ -185,6 +185,7 @@ class Accounts:
 
         # pull out valid profile md and delete the key
         profile = safe_profile_metadata(account)
+        del account['json_metadata']
         del account['posting_json_metadata']
 
         active_at = max(account['created'],
diff --git a/hive/utils/account.py b/hive/utils/account.py
index 0b2ad7729..caf38307c 100644
--- a/hive/utils/account.py
+++ b/hive/utils/account.py
@@ -6,12 +6,19 @@ from hive.utils.normalize import trunc
 def safe_profile_metadata(account):
     """Given an account, return sanitized profile data."""
     prof = {}
+
     try:
+        # read from posting_json_metadata, if version==2
         prof = json.loads(account['posting_json_metadata'])['profile']
-        if not isinstance(prof, dict):
-            prof = {}
+        assert isinstance(prof, dict)
+        assert 'version' in prof and prof['version'] == 2
     except Exception:
-        pass
+        try:
+            # fallback to json_metadata
+            prof = json.loads(account['json_metadata'])['profile']
+            assert isinstance(prof, dict)
+        except Exception:
+            prof = {}
 
     name = str(prof['name']) if 'name' in prof else None
     about = str(prof['about']) if 'about' in prof else None
diff --git a/tests/utils/test_utils_account.py b/tests/utils/test_utils_account.py
index 9c4e726b8..deb0db110 100644
--- a/tests/utils/test_utils_account.py
+++ b/tests/utils/test_utils_account.py
@@ -11,6 +11,7 @@ def test_valid_account():
         website='http://www.davincilife.com/',
         cover_image='https://steemitimages.com/0x0/https://pbs.twimg.com/profile_banners/816255358066946050/1483447009/1500x500',
         profile_image='https://www.parhlo.com/wp-content/uploads/2016/01/tmp617041537745813506.jpg',
+        version=2,
     )
     account = {'name': 'foo', 'posting_json_metadata': json.dumps(dict(profile=raw_profile))}
 
@@ -26,7 +27,11 @@ def test_invalid_account():
         cover_image='example.com/avatar.jpg',
         profile_image='https://example.com/valid-url-but-longer-than-1024-chars' + 'x' * 1024,
     )
-    account = {'name': 'foo', 'json_metadata': json.dumps(dict(profile=raw_profile))}
+    ignore_prof = dict(
+        name='Ignore me -- missing version:2!',
+    )
+    account = {'name': 'foo', 'json_metadata': json.dumps(dict(profile=raw_profile)),
+               'posting_json_metadata': json.dumps(dict(profile=ignore_prof))}
 
     safe_profile = safe_profile_metadata(account)
     assert safe_profile['name'] == 'NameIsTooBigByOne...'
-- 
GitLab