|
@@ -31,6 +31,7 @@
|
|
|
|
|
|
from subprocess import *
|
|
|
import json
|
|
|
+import urllib.request
|
|
|
|
|
|
from flbry import url
|
|
|
from flbry.variables import *
|
|
@@ -38,44 +39,40 @@ from flbry import markdown
|
|
|
from flbry import channel
|
|
|
from flbry import settings
|
|
|
|
|
|
-def list(claim_id, link, comment_id=""):
|
|
|
-
|
|
|
-
|
|
|
- # This function will list a list of comments of a certain claim_id
|
|
|
- # It will preview a very basic form of comment. You will have
|
|
|
- # to select a comment to interact with it further. Like read the
|
|
|
- # whole text, if it not short ( or in a markdown format ). Or
|
|
|
- # do other things like replies.
|
|
|
+def list(claim_id: str, link: str, comment_id: str = ""):
|
|
|
+ """
|
|
|
+ Lists comments in a short form, truncating some if needed. Comments can be selected for further actions.
|
|
|
|
|
|
+ Keyword arguments:
|
|
|
+ claim_id -- ID of the claim to get comments from
|
|
|
+ link -- LBRY URL of the claim, used for display
|
|
|
+ comment_id -- Comment ID to get sub-comments of, optional
|
|
|
+ """
|
|
|
w, h = tsize()
|
|
|
|
|
|
page_size = h - 5
|
|
|
page = 1
|
|
|
|
|
|
while True:
|
|
|
-
|
|
|
# Printing the search query and page number
|
|
|
center("COMMENTS OF: "+link+" PAGE : "+str(page))
|
|
|
|
|
|
- if not comment_id:
|
|
|
- out = check_output([lbrynet_binary["b"],
|
|
|
- "comment", "list", claim_id,
|
|
|
- '--page='+str(page),
|
|
|
- '--page_size='+str(page_size)])
|
|
|
- else:
|
|
|
- out = check_output([lbrynet_binary["b"],
|
|
|
- "comment", "list", '--claim_id='+claim_id,
|
|
|
- '--parent_id='+comment_id,
|
|
|
- '--page='+str(page),
|
|
|
- '--page_size='+str(page_size),
|
|
|
- '--include_replies'])
|
|
|
+ params = {
|
|
|
+ "claim_id": claim_id,
|
|
|
+ "page": page,
|
|
|
+ "page_size": page_size,
|
|
|
+ "sort_by": 3,
|
|
|
+ "top_level": True,
|
|
|
+ }
|
|
|
|
|
|
- # Now we want to parse the JSON
|
|
|
- try:
|
|
|
- out = json.loads(out)
|
|
|
- except:
|
|
|
- center("Connect to LBRY first.", "bdrd")
|
|
|
+ if comment_id:
|
|
|
+ params["parent_id"] = comment_id
|
|
|
+ params["top_level"] = False
|
|
|
+
|
|
|
+ out = comment_request("comment.List", params)
|
|
|
+ if not out:
|
|
|
return
|
|
|
+ out = out["result"]
|
|
|
|
|
|
if not "items" in out:
|
|
|
center("Publication has no comments", "bdrd")
|
|
@@ -224,15 +221,8 @@ def view(i):
|
|
|
post(i["claim_id"], c[c.find(" "):], i["comment_id"])
|
|
|
|
|
|
elif c == "delete":
|
|
|
- out = check_output([lbrynet_binary["b"], "comment", "abandon", i["comment_id"]])
|
|
|
- out = json.loads(out)
|
|
|
- try:
|
|
|
- if out["abandoned"] == True:
|
|
|
- center("Comment deleted!", "bdgr")
|
|
|
- break
|
|
|
- except:
|
|
|
- if out["message"].startswith("Couldn't find channel with channel_id"):
|
|
|
- center("You can't delete a comment you didn't post", "bdrd")
|
|
|
+ delete(i["comment_id"], bywho)
|
|
|
+
|
|
|
elif c.startswith("edit"):
|
|
|
c = c + ' '
|
|
|
update(i, c[c.find(" "):])
|
|
@@ -252,31 +242,32 @@ def post(claim_id, args, parent_id=""):
|
|
|
else:
|
|
|
text = input(typing_dots("Comment text", give_space=True))
|
|
|
|
|
|
-
|
|
|
- post_as = channel.select("Reply as who? Select Channel.")
|
|
|
-
|
|
|
+ post_as, post_as_id = channel.select("Reply as who? Select Channel.", True)
|
|
|
|
|
|
if not post_as.startswith("@"):
|
|
|
post_as = "@"+post_as
|
|
|
|
|
|
+ sigs = channel.sign(text, post_as)
|
|
|
+ if not sigs:
|
|
|
+ return
|
|
|
|
|
|
- if not parent_id:
|
|
|
- out = check_output([lbrynet_binary["b"],
|
|
|
- "comment", "create",
|
|
|
- text,
|
|
|
- '--channel_name='+post_as,
|
|
|
- '--claim_id='+claim_id])
|
|
|
- else:
|
|
|
- out = check_output([lbrynet_binary["b"],
|
|
|
- "comment", "create",
|
|
|
- text,
|
|
|
- '--channel_name='+post_as,
|
|
|
- '--parent_id='+parent_id,
|
|
|
- '--claim_id='+claim_id])
|
|
|
-
|
|
|
- out = json.loads(out)
|
|
|
- if "message" in out:
|
|
|
- center("Error: "+out["message"], "bdrd")
|
|
|
+ params = {
|
|
|
+ "channel_id": post_as_id,
|
|
|
+ "channel_name": post_as,
|
|
|
+ "claim_id": claim_id,
|
|
|
+ "comment": text,
|
|
|
+ **sigs
|
|
|
+ }
|
|
|
+
|
|
|
+ if parent_id:
|
|
|
+ params["parent_id"] = parent_id
|
|
|
+
|
|
|
+ out = comment_request("comment.Create", params)
|
|
|
+ if not out:
|
|
|
+ return
|
|
|
+
|
|
|
+ if "error" in out:
|
|
|
+ center("Error: "+out["error"]["message"], "bdrd")
|
|
|
else:
|
|
|
center("Comment is sent.", "bdgr")
|
|
|
|
|
@@ -365,22 +356,22 @@ def inbox(opt=10):
|
|
|
|
|
|
|
|
|
if page != 1:
|
|
|
- out = check_output([lbrynet_binary["b"],
|
|
|
+ out = check_output([flbry_globals["lbrynet"],
|
|
|
"stream", "list",
|
|
|
'--page='+str(page),
|
|
|
'--page_size='+str(page_size),
|
|
|
"--no_totals"])
|
|
|
- out2 = check_output([lbrynet_binary["b"],
|
|
|
+ out2 = check_output([flbry_globals["lbrynet"],
|
|
|
"channel", "list",
|
|
|
'--page='+str(page),
|
|
|
'--page_size='+str(page_size),
|
|
|
"--no_totals"])
|
|
|
else:
|
|
|
- out = check_output([lbrynet_binary["b"],
|
|
|
+ out = check_output([flbry_globals["lbrynet"],
|
|
|
"stream", "list",
|
|
|
'--page='+str(page),
|
|
|
'--page_size='+str(page_size)])
|
|
|
- out2 = check_output([lbrynet_binary["b"],
|
|
|
+ out2 = check_output([flbry_globals["lbrynet"],
|
|
|
"channel", "list",
|
|
|
'--page='+str(page),
|
|
|
'--page_size='+str(page_size)])
|
|
@@ -444,16 +435,18 @@ def inbox(opt=10):
|
|
|
|
|
|
comment_page = comment_page + 1
|
|
|
|
|
|
- cout = check_output([lbrynet_binary["b"],
|
|
|
- "comment", "list", '--claim_id='+claim_id,
|
|
|
- '--page='+str(comment_page),
|
|
|
- '--page_size='+str(50),
|
|
|
- '--include_replies'])
|
|
|
+ params = {
|
|
|
+ "claim_id": claim_id,
|
|
|
+ "page": comment_page,
|
|
|
+ "page_size": 50,
|
|
|
+ "sort_by": 3,
|
|
|
+ "top_level": False,
|
|
|
+ }
|
|
|
|
|
|
- try:
|
|
|
- cout = json.loads(cout)
|
|
|
- except:
|
|
|
- break
|
|
|
+ out = comment_request("comment.List", params)
|
|
|
+ if not out:
|
|
|
+ return
|
|
|
+ cout = out["result"]
|
|
|
|
|
|
# TODO: For now I'm stopping on first page when ever I'm
|
|
|
# loading channel's comments ( community disscussion ).
|
|
@@ -561,6 +554,8 @@ def inbox(opt=10):
|
|
|
|
|
|
def update(i, args):
|
|
|
comment = i["comment"]
|
|
|
+ comment_id = i["comment_id"]
|
|
|
+ commenter = i["channel_name"]
|
|
|
editor = settings.get("default_editor")
|
|
|
|
|
|
if len(args) > 1:
|
|
@@ -572,12 +567,66 @@ def update(i, args):
|
|
|
print("Comment: "+comment)
|
|
|
text = input(typing_dots("Edited comment", give_space=True))
|
|
|
|
|
|
- out = check_output([lbrynet_binary["b"],
|
|
|
- "comment", "update", "--comment_id="+i["comment_id"],
|
|
|
- "--comment="+text])
|
|
|
- out = json.loads(out)
|
|
|
- try:
|
|
|
- if out["message"].startswith("Couldn't find channel with channel_id"):
|
|
|
- center("You cant' edit a comment that isn't yours", "bdrd")
|
|
|
- except:
|
|
|
+
|
|
|
+ sigs = channel.sign(text, commenter)
|
|
|
+ if not sigs:
|
|
|
+ return
|
|
|
+
|
|
|
+ params = {
|
|
|
+ "comment": text,
|
|
|
+ "comment_id": comment_id,
|
|
|
+ **sigs
|
|
|
+ }
|
|
|
+
|
|
|
+ out = comment_request("comment.Edit", params)
|
|
|
+
|
|
|
+ if "error" in out:
|
|
|
+ center("Error updating comment: " + out["error"]["message"], "bdrd")
|
|
|
+ center("Make sure you are editing a comment you posted.", "bdrd")
|
|
|
+ elif "result" in out:
|
|
|
center("Comment edited!", "bdgr")
|
|
|
+
|
|
|
+def delete(comment_id: str, commenter: str):
|
|
|
+ """
|
|
|
+ Deletes a comment you posted by its comment ID
|
|
|
+ """
|
|
|
+ sigs = channel.sign(comment_id, commenter)
|
|
|
+ if not sigs:
|
|
|
+ return
|
|
|
+
|
|
|
+ params = {
|
|
|
+ "comment_id": comment_id,
|
|
|
+ **sigs
|
|
|
+ }
|
|
|
+
|
|
|
+ out = comment_request("comment.Abandon", params)
|
|
|
+ if "result" in out:
|
|
|
+ center("Comment deleted!", "bdgr")
|
|
|
+ elif "error" in out:
|
|
|
+ center("Error deleting comment: " + out["error"]["message"], "bdrd")
|
|
|
+ center("Make sure you posted this comment.", "bdrd")
|
|
|
+
|
|
|
+def comment_request(method: str, params: dict):
|
|
|
+ """
|
|
|
+ Sends a request to the comment API
|
|
|
+ """
|
|
|
+ data = {
|
|
|
+ "method": method,
|
|
|
+ "id": 1,
|
|
|
+ "jsonrpc":"2.0",
|
|
|
+ "params": params
|
|
|
+ }
|
|
|
+ data = json.dumps(data).encode()
|
|
|
+
|
|
|
+ headers = {
|
|
|
+ "Content-Type": "application/json"
|
|
|
+ }
|
|
|
+
|
|
|
+ try:
|
|
|
+ req = urllib.request.Request(flbry_globals["comment_api"], data, headers)
|
|
|
+ res = urllib.request.urlopen(req)
|
|
|
+ out = res.read().decode()
|
|
|
+ return json.loads(out)
|
|
|
+ except Exception as e:
|
|
|
+ center("Comment Error: "+str(e))
|
|
|
+ return
|