comments.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. #####################################################################
  2. # #
  3. # THIS IS A SOURCE CODE FILE FROM A PROGRAM TO INTERACT WITH THE #
  4. # LBRY PROTOCOL ( lbry.com ). IT WILL USE THE LBRY SDK ( lbrynet ) #
  5. # FROM THEIR REPOSITORY ( https://github.com/lbryio/lbry-sdk ) #
  6. # WHICH I GONNA PRESENT TO YOU AS A BINARY. SINCE I DID NOT DEVELOP #
  7. # IT AND I'M LAZY TO INTEGRATE IN A MORE SMART WAY. THE SOURCE CODE #
  8. # OF THE SDK IS AVAILABLE IN THE REPOSITORY MENTIONED ABOVE. #
  9. # #
  10. # ALL THE CODE IN THIS REPOSITORY INCLUDING THIS FILE IS #
  11. # (C) J.Y.Amihud and Other Contributors 2021. EXCEPT THE LBRY SDK. #
  12. # YOU CAN USE THIS FILE AND ANY OTHER FILE IN THIS REPOSITORY UNDER #
  13. # THE TERMS OF GNU GENERAL PUBLIC LICENSE VERSION 3 OR ANY LATER #
  14. # VERSION. TO FIND THE FULL TEXT OF THE LICENSE GO TO THE GNU.ORG #
  15. # WEBSITE AT ( https://www.gnu.org/licenses/gpl-3.0.html ). #
  16. # #
  17. # THE LBRY SDK IS UNFORTUNATELY UNDER THE MIT LICENSE. IF YOU ARE #
  18. # NOT INTENDING TO USE MY CODE AND JUST THE SDK. YOU CAN FIND IT ON #
  19. # THEIR OFFICIAL REPOSITORY ABOVE. THEIR LICENSE CHOICE DOES NOT #
  20. # SPREAD ONTO THIS PROJECT. DON'T GET A FALSE ASSUMPTION THAT SINCE #
  21. # THEY USE A PUSH-OVER LICENSE, I GONNA DO THE SAME. I'M NOT. #
  22. # #
  23. # THE LICENSE CHOSEN FOR THIS PROJECT WILL PROTECT THE 4 ESSENTIAL #
  24. # FREEDOMS OF THE USER FURTHER, BY NOT ALLOWING ANY WHO TO CHANGE #
  25. # THE LICENSE AT WILL. SO NO PROPRIETARY SOFTWARE DEVELOPER COULD #
  26. # TAKE THIS CODE AND MAKE THEIR USER-SUBJUGATING SOFTWARE FROM IT. #
  27. # #
  28. #####################################################################
  29. import time
  30. import json
  31. import threading
  32. import urllib.request
  33. from gi.repository import Gtk
  34. from gi.repository import Gdk
  35. from gi.repository import GLib
  36. from gi.repository import Pango
  37. from gi.repository import GdkPixbuf
  38. from flbry import markdown
  39. from flbry import ui
  40. from flbry import fetch
  41. from flbry import settings
  42. # TODO: Get rid of this string and use win.settings["comment_api"] instead
  43. comments_api = "https://comments.odysee.com/api/v2"
  44. # This is going into the end of comments to promote FastLBRY GTK
  45. BUTTON_GTK_PROMOTE = "\n\n[![](https://player.odycdn.com/api/v4/streams/free/button_GTK/d025c8ec2cdb5122a85b374b7bc453ba11d9409d/6526ef)](https://notabug.org/jyamihud/FastLBRY-GTK)"
  46. def list_comments( win, data, page=1, megabox=False):
  47. claim_id = data["claim_id"]
  48. # gets list of comment
  49. params = {
  50. "claim_id": claim_id,
  51. "page": page,
  52. # "page_size": page_size,
  53. "sort_by": 0,
  54. "top_level": False,
  55. }
  56. time.sleep(1) # too fast
  57. out = comment_request("comment.List", params)
  58. out = get_reactions(out)
  59. return [win, out, data, megabox]
  60. def render_single_comment(win, box, i):
  61. claim_id = i["claim_id"]
  62. items = win.commenting[claim_id]["data"]["result"]["items"]
  63. # Like / Dislike ratio
  64. reactionbox = Gtk.HBox()
  65. like_dislike_ratio_box = Gtk.VBox()
  66. like_dislike_box = Gtk.HBox()
  67. likes = i.get("reactions", {}).get("like", 0)
  68. like = Gtk.ToggleButton(" 👍 "+str(likes)+" ")
  69. like.set_relief(Gtk.ReliefStyle.NONE)
  70. like_dislike_box.pack_start(like, False, False, 0)
  71. dislikes = i.get("reactions", {}).get("dislike", 0)
  72. dislike = Gtk.ToggleButton(" 👎 "+str(dislikes)+" ")
  73. dislike.set_relief(Gtk.ReliefStyle.NONE)
  74. like_dislike_box.pack_start(dislike, False, False, 0)
  75. like_dislike_ratio_box.pack_start(like_dislike_box, False, False, False)
  76. try:
  77. frac = likes / (likes + dislikes)
  78. except:
  79. frac = 0
  80. ratio_bar = Gtk.ProgressBar()
  81. ratio_bar.set_fraction(frac)
  82. like_dislike_ratio_box.pack_start(ratio_bar, True, True,0)
  83. reactionbox.pack_start(like_dislike_ratio_box, False, False, False)
  84. ########### REPLY ############
  85. def reply_set(w):
  86. for ch in win.commenting[claim_id]["reply"]["box"].get_children():
  87. ch.destroy()
  88. win.commenting[claim_id]["reply"]["to"] = i["comment_id"]
  89. reply_frame = Gtk.Frame(label=" Replying to: ")
  90. reply_frame.set_shadow_type(Gtk.ShadowType.ETCHED_OUT)
  91. reply_box = Gtk.HBox()
  92. reply_frame.add(reply_box)
  93. chbox = Gtk.HBox()
  94. channel_button = ui.load(win, resolve_channel, render_channel, win, i["channel_url"])
  95. chbox.pack_start(channel_button, False, False, 0)
  96. reply_box.pack_start(chbox, False, False, 4)
  97. comment_label = Gtk.Label(i["comment"].split("\n")[0][:100]+" ...")
  98. comment_label.set_line_wrap_mode( Gtk.WrapMode.WORD )
  99. comment_label.set_line_wrap(True)
  100. reply_box.pack_start(comment_label, False, False, 10)
  101. win.commenting[claim_id]["reply"]["box"].pack_start(reply_frame, True, True, 5)
  102. ##### DELETE REPLY ####
  103. def delete_set(w):
  104. for ch in win.commenting[claim_id]["reply"]["box"].get_children():
  105. ch.destroy()
  106. win.commenting[claim_id]["reply"]["to"] = ""
  107. delete = Gtk.Button()
  108. delete.connect("clicked", delete_set)
  109. delete.set_relief(Gtk.ReliefStyle.NONE)
  110. delete.add(ui.icon(win, "edit-delete"))
  111. win.commenting[claim_id]["reply"]["box"].pack_end(delete, False, False, 0)
  112. win.commenting[claim_id]["reply"]["box"].show_all()
  113. reply = Gtk.Button(" Reply ")
  114. reply.set_relief(Gtk.ReliefStyle.NONE)
  115. reply.connect("clicked", reply_set)
  116. reactionbox.pack_end(reply, False, False, False)
  117. box.pack_end(reactionbox, False, False, 0)
  118. comment_text = i["comment"]
  119. supporter = False
  120. if BUTTON_GTK_PROMOTE in comment_text:
  121. supporter = True
  122. comment_text = comment_text.replace(BUTTON_GTK_PROMOTE, "")
  123. def force_size(w, e):
  124. w.queue_resize()
  125. commentbox = Gtk.Frame()
  126. commentbox.set_border_width(5)
  127. comment_field = Gtk.TextView()
  128. comment_field.connect("draw", force_size)
  129. comment_field.set_wrap_mode(Gtk.WrapMode.WORD_CHAR )
  130. comment_field.set_editable(False)
  131. comment_field.set_hexpand(False)
  132. comment_buffer = comment_field.get_buffer()
  133. comment_buffer.set_text(comment_text)
  134. markdown.convert(win, comment_field, imwidth=200)
  135. commentbox.add(comment_field)
  136. box.pack_end(commentbox, True, True, 0)
  137. # comment_label = Gtk.Label(comment_text)
  138. # comment_label.set_selectable(True)
  139. # comment_label.set_line_wrap_mode( Gtk.WrapMode.WORD )
  140. # comment_label.set_line_wrap(True)
  141. # comment_label.set_max_width_chars(20)
  142. # box.pack_end(comment_label, True, True, 10)
  143. def resolve_channel(win, url):
  144. out = fetch.lbrynet("resolve", {"urls":url})
  145. out = out[url]
  146. return [win, out]
  147. def render_channel(out):
  148. win, out = out
  149. return ui.go_to_channel(win, out)
  150. # Sometimes it's a reply
  151. if "parent_id" in i:
  152. for b in items:
  153. if b["comment_id"] == i["parent_id"]:
  154. expand = Gtk.Expander(label=" In Reply to: ")
  155. reply_frame = Gtk.Frame()
  156. reply_frame.set_border_width(10)
  157. expand.add(reply_frame)
  158. reply_frame.set_shadow_type(Gtk.ShadowType.ETCHED_OUT)
  159. reply_box = Gtk.VBox()
  160. reply_frame.add(reply_box)
  161. render_single_comment(win, reply_box, b)
  162. # if "channel_url" in b:
  163. # chbox = Gtk.HBox()
  164. # channel_button = ui.load(win, resolve_channel, render_channel, win, b["channel_url"])
  165. # chbox.pack_start(channel_button, False, False, 0)
  166. # reply_box.pack_start(chbox, True, False, 4)
  167. # comment_field = Gtk.TextView()
  168. # comment_field.set_wrap_mode(Gtk.WrapMode.WORD )
  169. # comment_field.set_editable(False)
  170. # comment_field.set_hexpand(False)
  171. # comment_buffer = comment_field.get_buffer()
  172. # comment_buffer.set_text(b["comment"].replace(BUTTON_GTK_PROMOTE, ""))
  173. # markdown.convert(win, comment_field, imwidth=200)
  174. # comment_label = Gtk.Label(b["comment"].replace(BUTTON_GTK_PROMOTE, ""))
  175. # comment_label.set_line_wrap_mode( Gtk.WrapMode.WORD )
  176. # comment_label.set_line_wrap(True)
  177. # comment_label.set_max_width_chars(20)
  178. #reply_box.pack_start(comment_field, True, True, 10)
  179. box.pack_end(expand, True, True, 10)
  180. break
  181. # each channel will need to be resolved on fly
  182. if "channel_url" in i:
  183. chbox = Gtk.HBox()
  184. channel_button = ui.load(win, resolve_channel, render_channel, win, i["channel_url"])
  185. chbox.pack_start(channel_button, False, False, 0)
  186. if "support_amount" in i and i["support_amount"]:
  187. chbox.pack_start(ui.icon(win, "emblem-favorite"), False, False, 0)
  188. chbox.pack_start(Gtk.Label(" "+str(i["support_amount"])+" LBC"), False, False, 0)
  189. if supporter:
  190. chbox.pack_start(ui.icon(win, "emblem-favorite"), False, False, 0)
  191. chbox.pack_start(Gtk.Label(" Promoter of FastLBRY "), False, False, 0)
  192. box.pack_end(chbox, False, False, 4)
  193. box.pack_end(Gtk.HSeparator(), False, False, 0)
  194. def render_comments(out, megabox=False):
  195. win, out, data, megabox = out
  196. if not megabox:
  197. megabox = Gtk.VBox()
  198. megabox.show_all()
  199. # renders a list of comments
  200. box = Gtk.VBox()
  201. box.set_hexpand(False)
  202. # I want to setup a little daemon to update the comments
  203. # automatically
  204. claim_id = data["claim_id"]
  205. try:
  206. the_title = data["value"]["title"]
  207. except:
  208. the_title = data["name"]
  209. if out["result"]["page"] == 1: # If it's the first page
  210. win.commenting[claim_id]["box"] = box
  211. win.commenting[claim_id]["data"] = out
  212. win.check_comment_daemon_keep_alive = True
  213. load_thread = threading.Thread(target=comment_daemon, args=[win, data, the_title])
  214. load_thread.setDaemon(True)
  215. load_thread.start()
  216. def kill_daemon(w):
  217. win.check_comment_daemon_keep_alive = False
  218. box.connect("destroy", kill_daemon)
  219. try:
  220. items = out["result"]["items"]
  221. for i in items:
  222. if i not in win.commenting[claim_id]["data"]["result"]["items"]:
  223. win.commenting[claim_id]["data"]["result"]["items"].append(i)
  224. except:
  225. items = []
  226. for i in reversed(items):
  227. #print("\n\n:::::::::::::::::\n\n", i)
  228. render_single_comment(win, box, i)
  229. box.show_all()
  230. # The top level box containing about 50 comments
  231. megabox.pack_start(box, False, False, 0)
  232. lastthing(win, out, data, megabox)
  233. megabox.show_all()
  234. return megabox
  235. def comment_daemon(win, data, the_title=""):
  236. # This function will be a daemon for the current comment system to
  237. # load all new comments that happen while the user it on the page.
  238. claim_id = data["claim_id"]
  239. while win.commenting[claim_id]["keep_alive"]:
  240. time.sleep(2) # Update every two seconds ( I don't want it to crash )
  241. n = win.commenting[claim_id]["data"]
  242. try:
  243. claim_id = n["result"]["items"][0]["claim_id"]
  244. except:
  245. n["result"]["items"] = []
  246. continue
  247. win, out, data, megabox = list_comments(win, data)
  248. ##### LOGIC OF ADDING MISSING COMMENTS INTO THE BOX ####
  249. for i in reversed(out["result"]["items"]):
  250. if i not in n["result"]["items"]:
  251. ids = []
  252. for b in n["result"]["items"]:
  253. ids.append(b["comment_id"])
  254. ######## FOUND A NEW COMMENT #######
  255. if i["comment_id"] not in ids:
  256. try:
  257. channel = i["channel_name"]
  258. except:
  259. channel = "[anonymous]"
  260. text = i["comment"]
  261. print("COMMENT FROM ", channel, "IS", text)
  262. force = win.resolved["claim_id"] != claim_id
  263. ui.notify(win, channel+" Commented on "+the_title, text, force=force)
  264. win.commenting[claim_id]["data"]["result"]["items"].append(i)
  265. print(i["comment_id"])
  266. box = win.commenting[claim_id]["box"]
  267. def render(win, box, i):
  268. render_single_comment(win, box, i)
  269. box.show_all()
  270. GLib.idle_add(render, win, box, i)
  271. def br(w):
  272. if not win.commenting[claim_id]["listen"]:
  273. win.commenting[claim_id]["keep_alive"] = False
  274. win.commenting[claim_id]["box"].connect("destroy", br)
  275. def lastthing(win, out, data, megabox):
  276. # This will hack itself to load more comments when you reached the end.
  277. spinner_more = ui.icon(win, "loading", "gif")
  278. megabox.pack_start(spinner_more, False, False, 0)
  279. def draw_event(w, e):
  280. print("EVENT")
  281. w.destroy()
  282. try:
  283. claim_id = out["result"]["items"][0]["claim_id"]
  284. page = out["result"]["page"] + 1
  285. ui.load(win, list_comments, render_comments, win, data, page, megabox, wait=False )
  286. except Exception as e:
  287. print("ERROR IS:", e)
  288. spinner_more.connect("draw", draw_event)
  289. def get_reactions(out):
  290. # Adds reactions list to the out
  291. try:
  292. claim_id = out["result"]["items"][0]["claim_id"]
  293. ids = ""
  294. for i in out["result"]["items"]:
  295. ids = ids + i["comment_id"] + ","
  296. ids = ids[:-1]
  297. except:
  298. return out
  299. rec = {
  300. "claim_id":claim_id,
  301. "comment_ids":ids}
  302. rec = comment_request("reaction.List", rec, addition="?m=reaction.List")
  303. for i in out["result"]["items"]:
  304. try:
  305. i["reactions"] = rec["result"]["others_reactions"][i["comment_id"]]
  306. except:
  307. pass
  308. return out
  309. def comment_request(method: str, params: dict, addition=""):
  310. """
  311. Sends a request to the comment API
  312. """
  313. data = {
  314. "method": method,
  315. "id": 1,
  316. "jsonrpc":"2.0",
  317. "params": params
  318. }
  319. data = json.dumps(data).encode()
  320. headers = {
  321. "Content-Type": "application/json"
  322. }
  323. try:
  324. req = urllib.request.Request(comments_api, data, headers)
  325. res = urllib.request.urlopen(req)
  326. out = res.read().decode()
  327. return json.loads(out)
  328. except Exception as e:
  329. print("COMMENT ERROR:", e)
  330. return
  331. def comment_input(win, claim_id):
  332. # claim_id = win.resolved["claim_id"]
  333. # This is the input for new comments.
  334. vbox = Gtk.VBox()
  335. pannel = Gtk.HBox()
  336. vbox.pack_start(pannel, False, False, False)
  337. ################## LISTEN BUTTON ###############
  338. try:
  339. give_listen = win.commenting[claim_id]["listen"]
  340. except:
  341. give_listen = False
  342. win.commenting[claim_id] = {"box": False,
  343. "data":{},
  344. "listen":give_listen,
  345. "keep_alive":True}
  346. if win.settings["notifications"]:
  347. lbox = Gtk.HBox()
  348. lbox.pack_start(Gtk.Label(" Listen "), False, False, False)
  349. listen = Gtk.Switch()
  350. def lclick(w, u):
  351. win.commenting[claim_id]["listen"] = listen.get_active()
  352. try:
  353. listen.set_active(win.commenting[claim_id]["listen"])
  354. except Exception as e:
  355. print("Switch is:", e)
  356. listen.connect("notify::active", lclick)
  357. lbox.pack_start(listen, False, False, False)
  358. pannel.pack_start(lbox, False, False, False)
  359. if win.channel:
  360. ############# BOX FOR REPLIES ##############
  361. # There will be a hidden box for replies. And when
  362. # the user presses the 'reply' button on any
  363. # of the current comments, it will fill up the box.
  364. win.commenting[claim_id]["reply"] = {"box":Gtk.HBox(),
  365. "to":""}
  366. vbox.pack_start( win.commenting[claim_id]["reply"]["box"], False, False, False)
  367. ############### SEND BUTTON ###################
  368. def send_do(w):
  369. tb = view.get_buffer()
  370. message = tb.get_text(tb.get_start_iter(), tb.get_end_iter(), True)
  371. tb.set_text("")
  372. send_comment(win, message, claim_id)
  373. send = Gtk.Button()
  374. send.connect("clicked", send_do)
  375. send.set_relief(Gtk.ReliefStyle.NONE)
  376. sendbox = Gtk.HBox()
  377. sendbox.pack_start(ui.icon(win, "document-send"), False, False, 0)
  378. sendbox.pack_start(Gtk.Label(" Send "), False, False, 0)
  379. pannel.pack_end(send, False, False, False)
  380. send.add(sendbox)
  381. ############## TEXT INPUT ##############
  382. inputbox = Gtk.HPaned()
  383. inputbox.set_position(500)
  384. vbox.pack_start(inputbox, False, False, False)
  385. frame = Gtk.Frame()
  386. scrl = Gtk.ScrolledWindow()
  387. view = Gtk.TextView()
  388. view.override_font(Pango.FontDescription("Monospace"))
  389. view.set_wrap_mode(Gtk.WrapMode.WORD)
  390. scrl.set_size_request(100,100)
  391. scrl.add(view)
  392. #view.set_size_request(100,100)
  393. frame.add(scrl)
  394. inputbox.add1(frame)
  395. ############## MARKDOWN PREVIEW ##############
  396. commentbox = Gtk.Frame()
  397. scrl2 = Gtk.ScrolledWindow()
  398. comment_field = Gtk.TextView()
  399. comment_field.set_wrap_mode(Gtk.WrapMode.WORD )
  400. comment_field.set_editable(False)
  401. comment_field.set_hexpand(False)
  402. comment_buffer = comment_field.get_buffer()
  403. scrl2.add(comment_field)
  404. commentbox.add(scrl2)
  405. inputbox.add2(commentbox)
  406. def on_changed(w):
  407. tb = view.get_buffer()
  408. message = tb.get_text(tb.get_start_iter(), tb.get_end_iter(), True)
  409. comment_buffer.set_text(message)
  410. markdown.convert(win, comment_field, 200)
  411. comment_field.show_all()
  412. # Scrolling to the end
  413. # TODO: Make it scroll to the character
  414. adj = scrl2.get_vadjustment()
  415. adj.set_value( adj.get_upper() )
  416. view.get_buffer().connect("changed", on_changed)
  417. ################# MARKDOWN CONTROLLS #################
  418. pannel.pack_start(Gtk.VSeparator(), False, False, 10)
  419. def do_mark(w, r, l):
  420. tb = view.get_buffer()
  421. s, e = tb.get_selection_bounds()
  422. tb.insert(s, r)
  423. s, e = tb.get_selection_bounds()
  424. tb.insert(e, l)
  425. print(r, l)
  426. bold = Gtk.Button()
  427. bold.connect("clicked", do_mark, "**", "**")
  428. bold.add(ui.icon(win, "format-text-bold"))
  429. bold.set_relief(Gtk.ReliefStyle.NONE)
  430. pannel.pack_start(bold, False, False, False)
  431. italic = Gtk.Button()
  432. italic.connect("clicked", do_mark, "*", "*")
  433. italic.add(ui.icon(win, "format-text-italic"))
  434. italic.set_relief(Gtk.ReliefStyle.NONE)
  435. pannel.pack_start(italic, False, False, False)
  436. code = Gtk.Button("</>")
  437. code.connect("clicked", do_mark, "`", "`")
  438. code.set_relief(Gtk.ReliefStyle.NONE)
  439. pannel.pack_start(code, False, False, False)
  440. pannel.pack_start(Gtk.VSeparator(), False, False, 10)
  441. #################### PROMOTE BUTTON ######################
  442. pannel.pack_start(ui.icon(win, "emblem-favorite"), False, False, 0)
  443. pannel.pack_start(Gtk.Label(" Promote FastLBRY: "), False, False, 0)
  444. switch = Gtk.Switch()
  445. switch.set_tooltip_text("Adds a little link to FastLBRY visible in all other LBRY apps.")
  446. pannel.pack_start(switch, False, False, 0)
  447. switch.set_active(win.settings["promote_fast_lbry_in_comments"])
  448. def on_promote(w, e):
  449. win.settings["promote_fast_lbry_in_comments"] = switch.get_active()
  450. settings.save(win.settings)
  451. switch.connect("notify::active", on_promote)
  452. pannel.pack_start(Gtk.VSeparator(), False, False, 10)
  453. else:
  454. vbox.pack_start(Gtk.Label("To comment, Make a channel"), False, False, 30)
  455. return vbox
  456. def send_comment(win, message, claim_id):
  457. # if the user wants to promote FastLBRY
  458. if win.settings["promote_fast_lbry_in_comments"]:
  459. message = message + BUTTON_GTK_PROMOTE # See the beginning of the file
  460. # claim_id = win.resolved["claim_id"]
  461. channel_name = win.channel["name"]
  462. channel_id = win.channel["claim_id"]
  463. sigs = sign(message, channel_name)
  464. if not sigs:
  465. return
  466. params = {
  467. "channel_id": channel_id,
  468. "channel_name": channel_name,
  469. "claim_id": claim_id,
  470. "comment": message,
  471. **sigs
  472. }
  473. if win.commenting[claim_id]["reply"]["to"]:
  474. params["parent_id"] = win.commenting[claim_id]["reply"]["to"]
  475. # Make sure to clean that up
  476. for ch in win.commenting[claim_id]["reply"]["box"].get_children():
  477. ch.destroy()
  478. win.commenting[claim_id]["reply"]["to"] = ""
  479. out = comment_request("comment.Create", params)
  480. i = out["result"]
  481. # RENDERING THE NEWLY DONW COMMENT INTO THE PAGE
  482. # TODO: It duplicates it when the comment is actually loaded
  483. win.commenting[claim_id]["data"]["result"]["items"].append(i)
  484. print(i["comment_id"])
  485. box = win.commenting[claim_id]["box"]
  486. def render(win, box, i):
  487. render_single_comment(win, box, i)
  488. box.show_all()
  489. GLib.idle_add(render, win, box, i)
  490. def sign(data: str = "", channel: str = "", message: str = "Channel to sign data with:", hexdata: str = ""):
  491. """
  492. Sign a string or hexdata and return the signatures
  493. Keyword arguments:
  494. data -- a string to sign
  495. channel -- channel name to sign with (e.g. "@example"). Will prompt for one if not given.
  496. message -- message to give when selecting a channel. Please pass this if not passing channel.
  497. hexdata -- direct hexadecimal data to sign
  498. """
  499. if (not data and not hexdata) or (data and hexdata):
  500. raise ValueError("Must give either data or hexdata")
  501. elif data:
  502. hexdata = data.encode().hex()
  503. if not channel:
  504. channel = select(message)
  505. if not channel.startswith("@"):
  506. channel = "@" + channel
  507. try:
  508. sigs = fetch.lbrynet("channel_sign", {"channel_name":channel, "hexdata":hexdata})
  509. # sigs = check_output([flbry_globals["lbrynet"],
  510. # "channel", "sign",
  511. # "--channel_name=" + channel,
  512. # "--hexdata=" + hexdata])
  513. # sigs = json.loads(sigs)
  514. print(sigs)
  515. return sigs
  516. except:
  517. print("Stupid Error")
  518. return