Skip to content

Commit

Permalink
move importHistory to its own function, and correct the display of nu…
Browse files Browse the repository at this point in the history
…ll entries
  • Loading branch information
enkiv2 committed Jul 14, 2022
1 parent 8d5881c commit fd152c9
Showing 1 changed file with 91 additions and 95 deletions.
186 changes: 91 additions & 95 deletions fern
Original file line number Diff line number Diff line change
Expand Up @@ -285,44 +285,44 @@ def drawPanelContents(toots, selectedIdx, skipSeen=False):
i=0
for toot in toots:
if(i>(ROWS-3)/4): continue
if toot==None:
drawMsgPanel(i, "", "", "Content unavailable", "", True)
if toot==None:
if i==selectedIdx:
selectedIdx+=1
next
if "reblog" in toot and toot["reblog"] and type(toot["reblog"])==type(toot): toot=toot["reblog"]
if "muted" in toot or not "id" in toot: next
if not toot["id"] in tootbase: tootbase[toot["id"]]=copy.deepcopy(toot)
seen=(toot["id"] in seen_toots)
if skipSeen and seen: next
acct=toot["account"]
username=fillInDomain(acct["acct"])
content=processContents(toot["content"])
cw=applyCWRules(username, content, toot["spoiler_text"])
drawMsgPanel(i, username, cw, content, str(toot["created_at"]), seen)
if i==selectedIdx:
if not seen:
seen_toots.append(toot["id"])
if "media_attachments" in toot:
for attachment in toot["media_attachments"]:
content+="\n"+attachment["url"]
flags=""
if "in_reply_to_id" in toot and toot["in_reply_to_id"]: flags+="["+str(toot["in_reply_to_id"])+"<-]"
if toot["reblogged"]: flags+="[b]"
if toot["favourited"]: flags+="[f]"
if toot["sensitive"]: flags+="[sensitive]"
if "visibility" in toot: flags+="["+toot["visibility"]+"]"
if "mentions" in toot and toot["mentions"]:
if len(flags)>0: flags+="\n"
flags+="TO: "
for mention in toot["mentions"]: flags+=fillInDomain(mention["acct"])+" "
flags+="\n"
content=flags+content
if "tags" in toot and toot["tags"]:
tags=[]
for tag in toot["tags"]: tags.append(tag["name"])
content+="\n\nTAGS: "+(" ".join(tags))
drawSelectedMsg(username, acct["display_name"], cw, content, isCWOpen)
drawMsgPanel(i, "", "", "MESSAGE UNAVAILABLE", "", True)
else:
if "reblog" in toot and toot["reblog"] and type(toot["reblog"])==type(toot): toot=toot["reblog"]
if "muted" in toot or not "id" in toot: next
if not toot["id"] in tootbase: tootbase[toot["id"]]=copy.deepcopy(toot)
seen=(toot["id"] in seen_toots)
if skipSeen and seen: next
acct=toot["account"]
username=fillInDomain(acct["acct"])
content=processContents(toot["content"])
cw=applyCWRules(username, content, toot["spoiler_text"])
drawMsgPanel(i, username, cw, content, str(toot["created_at"]), seen)
if i==selectedIdx:
if not seen:
seen_toots.append(toot["id"])
if "media_attachments" in toot:
for attachment in toot["media_attachments"]:
content+="\n"+attachment["url"]
flags=""
if "in_reply_to_id" in toot and toot["in_reply_to_id"]: flags+="["+str(toot["in_reply_to_id"])+"<-]"
if toot["reblogged"]: flags+="[b]"
if toot["favourited"]: flags+="[f]"
if toot["sensitive"]: flags+="[sensitive]"
if "visibility" in toot: flags+="["+toot["visibility"]+"]"
if "mentions" in toot and toot["mentions"]:
if len(flags)>0: flags+="\n"
flags+="TO: "
for mention in toot["mentions"]: flags+=fillInDomain(mention["acct"])+" "
flags+="\n"
content=flags+content
if "tags" in toot and toot["tags"]:
tags=[]
for tag in toot["tags"]: tags.append(tag["name"])
content+="\n\nTAGS: "+(" ".join(tags))
drawSelectedMsg(username, acct["display_name"], cw, content, isCWOpen)
i+=1
scr.refresh()

Expand Down Expand Up @@ -481,61 +481,7 @@ def execCommand(cmd):
elif cmd=="import_history":
(tlIdx, hlIdx) = [0]*2
drawPanelContents(tl, hlIdx)
statusMsg("Getting statuses...", False)
statuses=mastodon.account_statuses(mastodon.account_verify_credentials()["id"])
page=1
while statuses != None:
try:
ensureCached(statuses, True)
for status in statuses:
try:
tid=status["id"]
tl=ensureCached(expandThread(tid), True)+tl
except Exception as e: statusMsg(str(e), False)
drawPanelContents(tl[tlIdx:], hlIdx)
page+=1
statusMsg("Getting statuses... page "+str(page), False)
try: statuses=mastodon.fetch_next(statuses)
except: statuses=None
except Exception as e: pass
drawPanelContents(tl[tlIdx:], hlIdx)
statusMsg("Getting favorites...", False)
favourites=mastodon.favourites()
page=1
while favourites != None:
for status in favourites:
try:
tid=status["id"]
if not tid in tootbase:
statusMsg("Fetching "+tid, False)
tootbase[tid]=copy.deepcopy(status)
tb_dirty[int(tid[-2:])]=True
tl=expandThread(tid)+tl
except Exception as e: statusMsg(str(e), False)
drawPanelContents(tl[tlIdx:], hlIdx)
page+=1
statusMsg("Getting favourites... page "+str(page), False)
try: favourites=mastodon.fetch_next(favourites)
except: favourites=None
drawPanelContents(tl[tlIdx:], hlIdx)
statusMsg("Getting mentions...", False)
statuses=mastodon.notifications()
while statuses!=None:
for status in statuses:
try:
if(status["type"]!="mention"): next
tid=status["status"]["id"]
if not tid in tootbase:
statusMsg("Fetching "+tid, False)
tootbase[tid]=copy.deepcopy(status["status"])
tb_dirty[int(tid[-2:])]=True
tl=expandThread(tid)+tl
except Exception as e: statusMsg(str(e), False)
drawPanelContents(tl[tlIdx:], hlIdx)
page+=1
statusMsg("Getting mentions... page "+str(page), False)
try: statuses=mastodon.fetch_next(statuses)
except: statuses=None
tl=importHistory(verbose=True)
if hlIdx+tlIdx<0:(hlIdx, tlIdx)=[0]*2
position=hlIdx+tlIdx
if(position>=len(tl) and position<len(tootbase.keys())):
Expand Down Expand Up @@ -586,16 +532,66 @@ def expandThread(tid, verbose=False):
if verbose:
statusMsg("Expanding thread...", False)
ensureCached(tid)
while tootbase[tid]["in_reply_to_id"]!=None:
if not tid in tootbase:
if verbose:
statusMsg("Could not fetch message ID "+str(tid), False)
return []
tid2=tid
while tid2 in tootbase and tootbase[tid]["in_reply_to_id"]!=None:
if verbose:
statusMsg("Expanding thread... (ID "+str(tid)+")", False)
tid=tootbase[tid]["in_reply_to_id"]
ensureCached(tid)
toots=[tootbase[tid]]
tid2=tootbase[tid]["in_reply_to_id"]
ensureCached(tid2)
if tid2 in tootbase:
tid=tid2
toots=[ensureCached(tid)]
toots.extend(ensureCached(mastodon.status_context(tid)["descendants"], True))
saveBase()
return toots

def importHistory(src="all", verbose=False):
if type(src)!=list:
src=src.split()
if len(src)>1:
ret=[]
for s in src:
ret=importHistory([s], verbose)+ret
if verbose:
drawPanelContents(ret, 0)
return ret
src=str(src[0])
if src=="all":
return importHistory(["statuses", "favorites", "mentions"], verbose)
page=None
statusMsg("Getting "+src+"...", False)
if src=="statuses":
page=mastodon.account_statuses(mastodon.account_verify_credentials()["id"])
elif src in ["favorites", "favourites"]:
page=mastodon.favourites()
elif src=="notifications":
page=mastodon.notifications()
else:
statusMsg("Unknown history source: "+src)
return []
toots=allPages(page, True, "Getting "+src+"...")
return expandThreads(list(map(lambda x: x["id"], toots)), True)

def allPages(page, verbose=False, msg=None):
myMsg="Getting statuses..."
if msg:
myMsg=msg
ret=[]
pageNum=1
while None!=page:
ret+=ensureCached(page)
pageNum+=1
if verbose:
statusMsg(msg+" page "+str(pageNum), False)
try: page=mastodon.fetch_next(page)
except: page=None
return ret


def ensureCachedAcct(acct, verbose=False, ignoreErrors=True, msg=None, force=False):
myMsg="fetching account ID"
if msg:
Expand Down

0 comments on commit fd152c9

Please sign in to comment.