Browse Source

Check if actor has changed

Can sometimes happen if Person Update has not been received
merge-requests/18/head
Bob Mottram 2 months ago
parent
commit
783711b46f
3 changed files with 56 additions and 12 deletions
  1. 21
    2
      cache.py
  2. 6
    10
      daemon.py
  3. 29
    0
      session.py

+ 21
- 2
cache.py View File

@@ -8,13 +8,14 @@ __status__ = "Production"
8 8
 
9 9
 import os
10 10
 import datetime
11
+from session import urlExists
11 12
 from utils import loadJson
12 13
 from utils import saveJson
13 14
 from utils import getFileCaseInsensitive
14 15
 
15 16
 
16
-def removePersonFromCache(baseDir: str, personUrl: str,
17
-                          personCache: {}) -> bool:
17
+def _removePersonFromCache(baseDir: str, personUrl: str,
18
+                           personCache: {}) -> bool:
18 19
     """Removes an actor from the cache
19 20
     """
20 21
     cacheFilename = baseDir + '/cache/actors/' + \
@@ -28,6 +29,24 @@ def removePersonFromCache(baseDir: str, personUrl: str,
28 29
         del personCache[personUrl]
29 30
 
30 31
 
32
+def checkForChangedActor(session, baseDir: str,
33
+                         httpPrefix: str, domainFull: str,
34
+                         personUrl: str, avatarUrl: str, personCache: {},
35
+                         timeoutSec: int):
36
+    """Checks if the avatar url exists and if not then
37
+    the actor has probably changed without receiving an actor/Person Update.
38
+    So clear the actor from the cache and it will be refreshed when the next
39
+    post from them is sent
40
+    """
41
+    if not session or not avatarUrl:
42
+        return
43
+    if domainFull in avatarUrl:
44
+        return
45
+    if urlExists(session, avatarUrl, timeoutSec, httpPrefix, domainFull):
46
+        return
47
+    _removePersonFromCache(baseDir, personUrl, personCache)
48
+
49
+
31 50
 def storePersonInCache(baseDir: str, personUrl: str,
32 51
                        personJson: {}, personCache: {},
33 52
                        allowWriteToFile: bool) -> None:

+ 6
- 10
daemon.py View File

@@ -20,7 +20,6 @@ import pyqrcode
20 20
 # for saving images
21 21
 from hashlib import sha256
22 22
 from hashlib import sha1
23
-from siteactive import siteIsActive
24 23
 from session import createSession
25 24
 from webfinger import webfingerMeta
26 25
 from webfinger import webfingerNodeInfo
@@ -229,7 +228,7 @@ from content import extractMediaInFormPOST
229 228
 from content import saveMediaInFormPOST
230 229
 from content import extractTextFieldsInPOST
231 230
 from media import removeMetaData
232
-from cache import removePersonFromCache
231
+from cache import checkForChangedActor
233 232
 from cache import storePersonInCache
234 233
 from cache import getPersonFromCache
235 234
 from httpsig import verifyPostHeaders
@@ -5491,14 +5490,11 @@ class PubServer(BaseHTTPRequestHandler):
5491 5490
                 if actorJson.get('alsoKnownAs'):
5492 5491
                     alsoKnownAs = actorJson['alsoKnownAs']
5493 5492
 
5494
-            # check if the avatar image exists and if not then update
5495
-            # the actor cache
5496
-            if optionsProfileUrl:
5497
-                if self.server.domainFull not in optionsProfileUrl:
5498
-                    if not siteIsActive(optionsProfileUrl, 3):
5499
-                        removePersonFromCache(self.server.baseDir,
5500
-                                              optionsActor,
5501
-                                              self.server.personCache)
5493
+            checkForChangedActor(self.server.session,
5494
+                                 self.server.baseDir, self.server.httpPrefix,
5495
+                                 self.server.domainFull,
5496
+                                 optionsActor, optionsProfileUrl,
5497
+                                 self.server.personCache, 3)
5502 5498
 
5503 5499
             msg = htmlPersonOptions(self.server.defaultTimeline,
5504 5500
                                     self.server.cssCache,

+ 29
- 0
session.py View File

@@ -53,6 +53,34 @@ def createSession(proxyType: str):
53 53
     return session
54 54
 
55 55
 
56
+def urlExists(session, url: str, timeoutSec=3,
57
+              httpPrefix='https', domain='testdomain') -> bool:
58
+    if not isinstance(url, str):
59
+        print('url: ' + str(url))
60
+        print('ERROR: urlExists failed, url should be a string')
61
+        return False
62
+    sessionParams = {}
63
+    sessionHeaders = {}
64
+    sessionHeaders['User-Agent'] = 'Epicyon/' + __version__
65
+    if domain:
66
+        sessionHeaders['User-Agent'] += \
67
+            '; +' + httpPrefix + '://' + domain + '/'
68
+    if not session:
69
+        print('WARN: urlExists failed, no session specified')
70
+        return True
71
+    try:
72
+        result = session.get(url, headers=sessionHeaders,
73
+                             params=sessionParams,
74
+                             timeout=timeoutSec)
75
+        if result:
76
+            if result.status_code == 200 or \
77
+               result.status_code == 304:
78
+                return True
79
+    except BaseException:
80
+        pass
81
+    return False
82
+
83
+
56 84
 def getJson(session, url: str, headers: {}, params: {},
57 85
             version='1.2.0', httpPrefix='https',
58 86
             domain='testdomain') -> {}:
@@ -72,6 +100,7 @@ def getJson(session, url: str, headers: {}, params: {},
72 100
             '; +' + httpPrefix + '://' + domain + '/'
73 101
     if not session:
74 102
         print('WARN: getJson failed, no session specified for getJson')
103
+        return None
75 104
     try:
76 105
         result = session.get(url, headers=sessionHeaders, params=sessionParams)
77 106
         return result.json()

Loading…
Cancel
Save