63 lines
2.6 KiB
Python
63 lines
2.6 KiB
Python
"""What the town's objects should look like to a reader, for decePub's e2e tests: for every object PrivaPub holds and some
|
|||
|
|
town persona may see, its ref, URI, PrivaPub id, origin, kind, visibility, the text marker, content warning, media alt
|
||
|
|
texts, poll options, quoted and parent URIs, and the personas that may see it.
|
||
|
|
|
||
|
|
town.sh expect [run] [--persona <username>]"""
|
||
|
|
import json
|
||
|
|
import sys
|
||
|
|
|
||
|
|
import check
|
||
|
|
import seed
|
||
|
|
from dialects import driver
|
||
|
|
|
||
|
|
|
||
|
|
def edited_cw(world, ref, args):
|
||
|
|
"""The warning an object shows now: an edit made before plans kept warnings replaced the post without one."""
|
||
|
|
edit = next((s for s in world.planner.steps if s["verb"] == "edit" and s["args"]["target"] == ref), None)
|
||
|
|
if edit is None or ref not in world.edited:
|
||
|
|
return args.get("cw")
|
||
|
|
return edit["args"].get("cw")
|
||
|
|
|
||
|
|
|
||
|
|
def objects(world):
|
||
|
|
steps = {s["ref"]: s for s in world.planner.steps if s.get("ref") and s["verb"] == "post"}
|
||
|
|
held = driver("privapub").stored([o["uri"] for o in world.objects.values()])
|
||
|
|
personas = [k for k in world.planner.accounts if k.startswith("privapub/")]
|
||
|
|
out = []
|
||
|
|
for ref, o in sorted(world.objects.items(), key=lambda kv: int(kv[0][1:])):
|
||
|
|
row = held.get(o["uri"])
|
||
|
|
if not row or not row.exists or row.deleted or ref in world.deleted:
|
||
|
|
continue
|
||
|
|
recipients = world.recipients(o)
|
||
|
|
readers = [p.split("/")[1] for p in personas
|
||
|
|
if p == o["author"] or p in recipients or o["visibility"] in ("public", "unlisted", "community")]
|
||
|
|
if not readers:
|
||
|
|
continue
|
||
|
|
args = steps[ref]["args"]
|
||
|
|
out.append({
|
||
|
|
"ref": ref, "uri": o["uri"], "id": row.local_id, "origin": o["origin"], "author": o["author"],
|
||
|
|
"kind": o["kind"], "visibility": o["visibility"], "marker": f"({ref})", "cw": edited_cw(world, ref, args),
|
||
|
|
"alts": [m["alt"] for m in args.get("media", [])], "poll": (args.get("poll") or {}).get("options"),
|
||
|
|
"quote": world.objects.get(o.get("quote") or "", {}).get("uri"),
|
||
|
|
"parent": world.objects.get(o.get("parent") or "", {}).get("uri"),
|
||
|
|
"edited": ref in world.edited, "title": args.get("title"), "readers": readers,
|
||
|
|
"followers_of_author": sorted(p.split("/")[1] for p in world.follows.get(o["author"], set()) if p.startswith("privapub/")),
|
||
|
|
})
|
||
|
|
return out
|
||
|
|
|
||
|
|
|
||
|
|
def main(args):
|
||
|
|
name = next((a for a in args if not a.startswith("--") and not a.startswith("privapub/")), "village")
|
||
|
|
persona = None
|
||
|
|
if "--persona" in args:
|
||
|
|
persona = args[args.index("--persona") + 1].split("/")[-1]
|
||
|
|
world = check.World(seed.run_dir(name))
|
||
|
|
items = objects(world)
|
||
|
|
if persona:
|
||
|
|
items = [i for i in items if persona in i["readers"]]
|
||
|
|
print(json.dumps(items, ensure_ascii=False))
|
||
|
|
return 0
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
sys.exit(main(sys.argv[1:]))
|