comments.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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(win, 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. def send_reaction(w, reaction):
  64. remove = not w.get_active()
  65. print("remove", remove)
  66. sigs = sign(win.channel["name"], win.channel["name"])
  67. params = {
  68. "channel_name": win.channel["name"],
  69. "channel_id": win.channel["claim_id"],
  70. "comment_ids": i.get("comment_id"),
  71. "type": reaction,
  72. **sigs
  73. }
  74. if remove:
  75. params["remove"] = True
  76. out = comment_request("reaction.React", params)
  77. print(out)
  78. # Like / Dislike ratio
  79. reactionbox = Gtk.HBox()
  80. like_dislike_ratio_box = Gtk.VBox()
  81. like_dislike_box = Gtk.HBox()
  82. print(i)
  83. likes = i.get("reactions", {}).get("like", 0)+i.get("my_reactions", {}).get("like", 0)
  84. like = Gtk.ToggleButton(" 👍 "+str(likes)+" ")
  85. like.set_active(i.get("my_reactions", {}).get("like", 0))
  86. like.connect("clicked", send_reaction, "like")
  87. like.set_relief(Gtk.ReliefStyle.NONE)
  88. like_dislike_box.pack_start(like, False, False, 0)
  89. dislikes = i.get("reactions", {}).get("dislike", 0)+i.get("my_reactions", {}).get("dislike", 0)
  90. dislike = Gtk.ToggleButton(" 👎 "+str(dislikes)+" ")
  91. dislike.set_active(i.get("my_reactions", {}).get("dislike", 0))
  92. dislike.set_relief(Gtk.ReliefStyle.NONE)
  93. dislike.connect("clicked", send_reaction, "dislike")
  94. like_dislike_box.pack_start(dislike, False, False, 0)
  95. like_dislike_ratio_box.pack_start(like_dislike_box, False, False, False)
  96. try:
  97. frac = likes / (likes + dislikes)
  98. except:
  99. frac = 0
  100. ratio_bar = Gtk.ProgressBar()
  101. ratio_bar.set_fraction(frac)
  102. like_dislike_ratio_box.pack_start(ratio_bar, True, True,0)
  103. reactionbox.pack_start(like_dislike_ratio_box, False, False, False)
  104. #creator_like
  105. creator_likes = i.get("reactions", {}).get("creator_like", 0)+i.get("my_reactions", {}).get("creator_like", 0)
  106. creator_like = Gtk.ToggleButton(" ❤ "+str(creator_likes)+" ")
  107. creator_like.set_relief(Gtk.ReliefStyle.NONE)
  108. creator_like.set_active(i.get("my_reactions", {}).get("creator_like", 0))
  109. creator_like.connect("clicked", send_reaction, "creator_like")
  110. reactionbox.pack_start(creator_like, False, False, 0)
  111. bones = i.get("reactions", {}).get("bones", 0)+i.get("my_reactions", {}).get("bones", 0)
  112. bone = Gtk.ToggleButton(" 🍖 "+str(bones)+" ")
  113. bone.set_relief(Gtk.ReliefStyle.NONE)
  114. bone.set_active(i.get("my_reactions", {}).get("bones", 0))
  115. bone.connect("clicked", send_reaction, "bones")
  116. reactionbox.pack_start(bone, False, False, 0)
  117. frozen_toms = i.get("reactions", {}).get("frozen_tom", 0)+i.get("my_reactions", {}).get("frozen_tom", 0)
  118. frozen_tom = Gtk.ToggleButton(" 🍦 "+str(frozen_toms)+" ")
  119. frozen_tom.set_relief(Gtk.ReliefStyle.NONE)
  120. frozen_tom.set_active(i.get("my_reactions", {}).get("frozen_tom", 0))
  121. frozen_tom.connect("clicked", send_reaction, "frozen_tom")
  122. reactionbox.pack_start(frozen_tom, False, False, 0)
  123. mind_blowns = i.get("reactions", {}).get("mind_blown", 0)+i.get("my_reactions", {}).get("mind_blown", 0)
  124. mind_blown = Gtk.ToggleButton(" 🤯 "+str(mind_blowns)+" ")
  125. mind_blown.set_relief(Gtk.ReliefStyle.NONE)
  126. mind_blown.connect("clicked", send_reaction, "mind_blown")
  127. mind_blown.set_active(i.get("my_reactions", {}).get("mind_blown", 0))
  128. reactionbox.pack_start(mind_blown, False, False, 0)
  129. ########### REPLY ############
  130. def reply_set(w):
  131. for ch in win.commenting[claim_id]["reply"]["box"].get_children():
  132. ch.destroy()
  133. win.commenting[claim_id]["reply"]["to"] = i["comment_id"]
  134. reply_frame = Gtk.Frame(label=" Replying to: ")
  135. reply_frame.set_shadow_type(Gtk.ShadowType.ETCHED_OUT)
  136. reply_box = Gtk.HBox()
  137. reply_frame.add(reply_box)
  138. chbox = Gtk.HBox()
  139. channel_button = ui.load(win, resolve_channel, render_channel, win, i["channel_url"])
  140. chbox.pack_start(channel_button, False, False, 0)
  141. reply_box.pack_start(chbox, False, False, 4)
  142. comment_label = Gtk.Label(i["comment"].split("\n")[0][:100]+" ...")
  143. comment_label.set_line_wrap_mode( Gtk.WrapMode.WORD )
  144. comment_label.set_line_wrap(True)
  145. reply_box.pack_start(comment_label, False, False, 10)
  146. win.commenting[claim_id]["reply"]["box"].pack_start(reply_frame, True, True, 5)
  147. ##### DELETE REPLY ####
  148. def delete_set(w):
  149. for ch in win.commenting[claim_id]["reply"]["box"].get_children():
  150. ch.destroy()
  151. win.commenting[claim_id]["reply"]["to"] = ""
  152. delete = Gtk.Button()
  153. delete.connect("clicked", delete_set)
  154. delete.set_relief(Gtk.ReliefStyle.NONE)
  155. delete.add(ui.icon(win, "edit-delete"))
  156. win.commenting[claim_id]["reply"]["box"].pack_end(delete, False, False, 0)
  157. win.commenting[claim_id]["reply"]["box"].show_all()
  158. reply = Gtk.Button(" Reply ")
  159. reply.set_relief(Gtk.ReliefStyle.NONE)
  160. reply.connect("clicked", reply_set)
  161. reactionbox.pack_end(reply, False, False, False)
  162. box.pack_end(reactionbox, False, False, 0)
  163. comment_text = i["comment"]
  164. supporter = False
  165. if BUTTON_GTK_PROMOTE in comment_text:
  166. supporter = True
  167. comment_text = comment_text.replace(BUTTON_GTK_PROMOTE, "")
  168. def force_size(w, e):
  169. w.queue_resize()
  170. commentbox = Gtk.Frame()
  171. commentbox.set_border_width(5)
  172. comment_field = Gtk.TextView()
  173. #comment_field.connect("draw", force_size)
  174. comment_field.set_wrap_mode(Gtk.WrapMode.WORD_CHAR )
  175. comment_field.set_editable(False)
  176. comment_field.set_hexpand(False)
  177. comment_buffer = comment_field.get_buffer()
  178. comment_buffer.set_text(comment_text)
  179. markdown.convert(win, comment_field, imwidth=200)
  180. commentbox.add(comment_field)
  181. box.pack_end(commentbox, True, True, 0)
  182. # comment_label = Gtk.Label(comment_text)
  183. # comment_label.set_selectable(True)
  184. # comment_label.set_line_wrap_mode( Gtk.WrapMode.WORD )
  185. # comment_label.set_line_wrap(True)
  186. # comment_label.set_max_width_chars(20)
  187. # box.pack_end(comment_label, True, True, 10)
  188. def resolve_channel(win, url):
  189. out = fetch.lbrynet("resolve", {"urls":url})
  190. out = out[url]
  191. return [win, out]
  192. def render_channel(out):
  193. win, out = out
  194. return ui.go_to_channel(win, out)
  195. # Sometimes it's a reply
  196. if "parent_id" in i:
  197. for b in items:
  198. if b["comment_id"] == i["parent_id"]:
  199. expand = Gtk.Expander(label=" In Reply to: ")
  200. reply_frame = Gtk.Frame()
  201. reply_frame.set_border_width(10)
  202. expand.add(reply_frame)
  203. reply_frame.set_shadow_type(Gtk.ShadowType.ETCHED_OUT)
  204. reply_box = Gtk.VBox()
  205. reply_frame.add(reply_box)
  206. render_single_comment(win, reply_box, b)
  207. box.pack_end(expand, True, True, 10)
  208. break
  209. # each channel will need to be resolved on fly
  210. if "channel_url" in i:
  211. chbox = Gtk.HBox()
  212. channel_button = ui.load(win, resolve_channel, render_channel, win, i["channel_url"])
  213. chbox.pack_start(channel_button, False, False, 0)
  214. if "support_amount" in i and i["support_amount"]:
  215. chbox.pack_start(ui.icon(win, "emblem-favorite"), False, False, 0)
  216. chbox.pack_start(Gtk.Label(" "+str(i["support_amount"])+" LBC"), False, False, 0)
  217. if supporter:
  218. chbox.pack_start(ui.icon(win, "emblem-favorite"), False, False, 0)
  219. chbox.pack_start(Gtk.Label(" Promoter of FastLBRY "), False, False, 0)
  220. box.pack_end(chbox, False, False, 4)
  221. box.pack_end(Gtk.HSeparator(), False, False, 0)
  222. def render_comments(out, megabox=False):
  223. win, out, data, megabox = out
  224. if not megabox:
  225. megabox = Gtk.VBox()
  226. megabox.show_all()
  227. # renders a list of comments
  228. box = Gtk.VBox()
  229. box.set_hexpand(False)
  230. # I want to setup a little daemon to update the comments
  231. # automatically
  232. claim_id = data["claim_id"]
  233. try:
  234. the_title = data["value"]["title"]
  235. except:
  236. the_title = data["name"]
  237. if out["result"]["page"] == 1: # If it's the first page
  238. win.commenting[claim_id]["box"] = box
  239. win.commenting[claim_id]["data"] = out
  240. win.check_comment_daemon_keep_alive = True
  241. load_thread = threading.Thread(target=comment_daemon, args=[win, data, the_title])
  242. load_thread.setDaemon(True)
  243. load_thread.start()
  244. def kill_daemon(w):
  245. win.check_comment_daemon_keep_alive = False
  246. box.connect("destroy", kill_daemon)
  247. try:
  248. items = out["result"]["items"]
  249. for i in items:
  250. if i not in win.commenting[claim_id]["data"]["result"]["items"]:
  251. win.commenting[claim_id]["data"]["result"]["items"].append(i)
  252. except:
  253. items = []
  254. for i in reversed(items):
  255. #print("\n\n:::::::::::::::::\n\n", i)
  256. render_single_comment(win, box, i)
  257. box.show_all()
  258. # The top level box containing about 50 comments
  259. megabox.pack_start(box, False, False, 0)
  260. lastthing(win, out, data, megabox)
  261. megabox.show_all()
  262. return megabox
  263. def comment_daemon(win, data, the_title=""):
  264. # This function will be a daemon for the current comment system to
  265. # load all new comments that happen while the user it on the page.
  266. claim_id = data["claim_id"]
  267. while win.commenting[claim_id]["keep_alive"]:
  268. time.sleep(2) # Update every two seconds ( I don't want it to crash )
  269. n = win.commenting[claim_id]["data"]
  270. try:
  271. claim_id = n["result"]["items"][0]["claim_id"]
  272. except:
  273. n["result"]["items"] = []
  274. continue
  275. win, out, data, megabox = list_comments(win, data)
  276. ##### LOGIC OF ADDING MISSING COMMENTS INTO THE BOX ####
  277. for i in reversed(out["result"]["items"]):
  278. if i not in n["result"]["items"]:
  279. ids = []
  280. for b in n["result"]["items"]:
  281. ids.append(b["comment_id"])
  282. ######## FOUND A NEW COMMENT #######
  283. if i["comment_id"] not in ids:
  284. try:
  285. channel = i["channel_name"]
  286. except:
  287. channel = "[anonymous]"
  288. text = i["comment"]
  289. print("COMMENT FROM ", channel, "IS", text)
  290. force = win.resolved["claim_id"] != claim_id
  291. ui.notify(win, channel+" Commented on "+the_title, text, force=force)
  292. win.commenting[claim_id]["data"]["result"]["items"].append(i)
  293. print(i["comment_id"])
  294. box = win.commenting[claim_id]["box"]
  295. def render(win, box, i):
  296. render_single_comment(win, box, i)
  297. box.show_all()
  298. GLib.idle_add(render, win, box, i)
  299. def br(w):
  300. if not win.commenting[claim_id]["listen"]:
  301. win.commenting[claim_id]["keep_alive"] = False
  302. win.commenting[claim_id]["box"].connect("destroy", br)
  303. def lastthing(win, out, data, megabox):
  304. # This will hack itself to load more comments when you reached the end.
  305. spinner_more = ui.icon(win, "loading", "gif")
  306. megabox.pack_start(spinner_more, False, False, 0)
  307. def draw_event(w, e):
  308. print("EVENT")
  309. w.destroy()
  310. try:
  311. claim_id = out["result"]["items"][0]["claim_id"]
  312. page = out["result"]["page"] + 1
  313. ui.load(win, list_comments, render_comments, win, data, page, megabox, wait=False )
  314. except Exception as e:
  315. print("ERROR IS:", e)
  316. spinner_more.connect("draw", draw_event)
  317. def get_reactions(win, out):
  318. # Adds reactions list to the out
  319. try:
  320. claim_id = out["result"]["items"][0]["claim_id"]
  321. ids = ""
  322. for i in out["result"]["items"]:
  323. ids = ids + i["comment_id"] + ","
  324. ids = ids[:-1]
  325. except:
  326. return out
  327. sigs = sign(win.channel["name"], win.channel["name"])
  328. rec = {
  329. "channel_name":win.channel["name"],
  330. "channel_id":win.channel["claim_id"],
  331. "claim_id":claim_id,
  332. "comment_ids":ids,
  333. **sigs
  334. }
  335. rec = comment_request("reaction.List", rec, addition="?m=reaction.List")
  336. for i in out["result"]["items"]:
  337. try:
  338. i["reactions"] = rec["result"]["others_reactions"][i["comment_id"]]
  339. except:
  340. pass
  341. try:
  342. i["my_reactions"] = rec["result"]["my_reactions"][i["comment_id"]]
  343. except:
  344. pass
  345. return out
  346. def comment_request(method: str, params: dict, addition=""):
  347. """
  348. Sends a request to the comment API
  349. """
  350. data = {
  351. "method": method,
  352. "id": 1,
  353. "jsonrpc":"2.0",
  354. "params": params
  355. }
  356. data = json.dumps(data).encode()
  357. headers = {
  358. "Content-Type": "application/json"
  359. }
  360. try:
  361. req = urllib.request.Request(comments_api, data, headers)
  362. res = urllib.request.urlopen(req)
  363. out = res.read().decode()
  364. return json.loads(out)
  365. except Exception as e:
  366. print("COMMENT ERROR:", e)
  367. return
  368. def comment_input(win, claim_id):
  369. # claim_id = win.resolved["claim_id"]
  370. # This is the input for new comments.
  371. vbox = Gtk.VBox()
  372. pannel = Gtk.HBox()
  373. vbox.pack_start(pannel, False, False, False)
  374. ################## LISTEN BUTTON ###############
  375. try:
  376. give_listen = win.commenting[claim_id]["listen"]
  377. except:
  378. give_listen = False
  379. win.commenting[claim_id] = {"box": False,
  380. "data":{},
  381. "listen":give_listen,
  382. "keep_alive":True}
  383. if win.settings["notifications"]:
  384. lbox = Gtk.HBox()
  385. lbox.pack_start(Gtk.Label(" Listen "), False, False, False)
  386. listen = Gtk.Switch()
  387. def lclick(w, u):
  388. win.commenting[claim_id]["listen"] = listen.get_active()
  389. try:
  390. listen.set_active(win.commenting[claim_id]["listen"])
  391. except Exception as e:
  392. print("Switch is:", e)
  393. listen.connect("notify::active", lclick)
  394. lbox.pack_start(listen, False, False, False)
  395. pannel.pack_start(lbox, False, False, False)
  396. if win.channel:
  397. ############# BOX FOR REPLIES ##############
  398. # There will be a hidden box for replies. And when
  399. # the user presses the 'reply' button on any
  400. # of the current comments, it will fill up the box.
  401. win.commenting[claim_id]["reply"] = {"box":Gtk.HBox(),
  402. "to":""}
  403. vbox.pack_start( win.commenting[claim_id]["reply"]["box"], False, False, False)
  404. ############### SEND BUTTON ###################
  405. def send_do(w):
  406. tb = view.get_buffer()
  407. message = tb.get_text(tb.get_start_iter(), tb.get_end_iter(), True)
  408. #tb.set_text("")
  409. send_comment(win, message, claim_id, tb, support=support_bid_entry)
  410. send = Gtk.Button()
  411. send.connect("clicked", send_do)
  412. send.set_relief(Gtk.ReliefStyle.NONE)
  413. sendbox = Gtk.HBox()
  414. sendbox.pack_start(ui.icon(win, "document-send"), False, False, 0)
  415. sendbox.pack_start(Gtk.Label(" Send "), False, False, 0)
  416. pannel.pack_end(send, False, False, False)
  417. send.add(sendbox)
  418. ############## TEXT INPUT ##############
  419. inputbox = Gtk.HPaned()
  420. inputbox.set_position(500)
  421. vbox.pack_start(inputbox, True, True, True)
  422. frame = Gtk.Frame()
  423. scrl = Gtk.ScrolledWindow()
  424. view = Gtk.TextView()
  425. view.override_font(Pango.FontDescription("Monospace"))
  426. view.set_wrap_mode(Gtk.WrapMode.WORD)
  427. scrl.set_size_request(100,100)
  428. scrl.add(view)
  429. #view.set_size_request(100,100)
  430. frame.add(scrl)
  431. inputbox.add1(frame)
  432. ############## MARKDOWN PREVIEW ##############
  433. commentbox = Gtk.Frame()
  434. scrl2 = Gtk.ScrolledWindow()
  435. comment_field = Gtk.TextView()
  436. comment_field.set_wrap_mode(Gtk.WrapMode.WORD )
  437. comment_field.set_editable(False)
  438. comment_field.set_hexpand(False)
  439. comment_buffer = comment_field.get_buffer()
  440. scrl2.add(comment_field)
  441. commentbox.add(scrl2)
  442. inputbox.add2(commentbox)
  443. def on_changed(w):
  444. tb = view.get_buffer()
  445. message = tb.get_text(tb.get_start_iter(), tb.get_end_iter(), True)
  446. comment_buffer.set_text(message)
  447. markdown.convert(win, comment_field, 200)
  448. comment_field.show_all()
  449. # Scrolling to the end
  450. # TODO: Make it scroll to the character
  451. adj = scrl2.get_vadjustment()
  452. adj.set_value( adj.get_upper() )
  453. view.get_buffer().connect("changed", on_changed)
  454. ################# MARKDOWN CONTROLLS #################
  455. pannel.pack_start(Gtk.VSeparator(), False, False, 10)
  456. def do_mark(w, r, l):
  457. tb = view.get_buffer()
  458. s, e = tb.get_selection_bounds()
  459. tb.insert(s, r)
  460. s, e = tb.get_selection_bounds()
  461. tb.insert(e, l)
  462. print(r, l)
  463. bold = Gtk.Button()
  464. bold.connect("clicked", do_mark, "**", "**")
  465. bold.add(ui.icon(win, "format-text-bold"))
  466. bold.set_relief(Gtk.ReliefStyle.NONE)
  467. pannel.pack_start(bold, False, False, False)
  468. italic = Gtk.Button()
  469. italic.connect("clicked", do_mark, "*", "*")
  470. italic.add(ui.icon(win, "format-text-italic"))
  471. italic.set_relief(Gtk.ReliefStyle.NONE)
  472. pannel.pack_start(italic, False, False, False)
  473. code = Gtk.Button("</>")
  474. code.connect("clicked", do_mark, "`", "`")
  475. code.set_relief(Gtk.ReliefStyle.NONE)
  476. pannel.pack_start(code, False, False, False)
  477. pannel.pack_start(Gtk.VSeparator(), False, False, 10)
  478. #################### PROMOTE BUTTON ######################
  479. pannel.pack_start(ui.icon(win, "emblem-favorite"), False, False, 0)
  480. pannel.pack_start(Gtk.Label(" Promote FastLBRY: "), False, False, 0)
  481. switch = Gtk.Switch()
  482. switch.set_tooltip_text("Adds a little link to FastLBRY visible in all other LBRY apps.")
  483. pannel.pack_start(switch, False, False, 0)
  484. switch.set_active(win.settings["promote_fast_lbry_in_comments"])
  485. def on_promote(w, e):
  486. win.settings["promote_fast_lbry_in_comments"] = switch.get_active()
  487. settings.save(win.settings)
  488. switch.connect("notify::active", on_promote)
  489. pannel.pack_start(Gtk.VSeparator(), False, False, 10)
  490. ################# SUPPORT AMOUNT ##########################
  491. pannel.pack_start(ui.icon(win, "emblem-favorite"), False, False, 0)
  492. pannel.pack_start(Gtk.Label(" Support Amount: "), False, False, 0)
  493. support_bid_adjust = Gtk.Adjustment(0,
  494. lower=0,
  495. upper=1000000000,
  496. step_increment=0.1)
  497. support_bid_entry = Gtk.SpinButton(adjustment=support_bid_adjust,
  498. digits=4)
  499. pannel.pack_start(support_bid_entry, False, False, 0)
  500. pannel.pack_start(Gtk.VSeparator(), False, False, 10)
  501. else:
  502. vbox.pack_start(Gtk.Label("To comment, Make a channel"), False, False, 30)
  503. return vbox
  504. def send_comment(win, message, claim_id, tb, support=0):
  505. support = support.get_value()
  506. if support:
  507. support_out = fetch.lbrynet("support_create",
  508. {"amount":str(float(round(support, 6))),
  509. "claim_id":claim_id,
  510. "channel_id":win.channel["claim_id"],
  511. "tip":True})
  512. # if the user wants to promote FastLBRY
  513. if win.settings["promote_fast_lbry_in_comments"]:
  514. message = message + BUTTON_GTK_PROMOTE # See the beginning of the file
  515. # claim_id = win.resolved["claim_id"]
  516. channel_name = win.channel["name"]
  517. channel_id = win.channel["claim_id"]
  518. sigs = sign(message, channel_name)
  519. if not sigs:
  520. return
  521. params = {
  522. "channel_id": channel_id,
  523. "channel_name": channel_name,
  524. "claim_id": claim_id,
  525. "comment": message,
  526. **sigs
  527. }
  528. if support:
  529. params["support_tx_id"] = support_out["txid"]
  530. if win.commenting[claim_id]["reply"]["to"]:
  531. params["parent_id"] = win.commenting[claim_id]["reply"]["to"]
  532. # Make sure to clean that up
  533. for ch in win.commenting[claim_id]["reply"]["box"].get_children():
  534. ch.destroy()
  535. win.commenting[claim_id]["reply"]["to"] = ""
  536. out = comment_request("comment.Create", params)
  537. i = out["result"] # This will error out if comment is not sent
  538. tb.set_text("") # Only if the comment is sent, we clean the text
  539. # RENDERING THE NEWLY DONW COMMENT INTO THE PAGE
  540. # TODO: It duplicates it when the comment is actually loaded
  541. win.commenting[claim_id]["data"]["result"]["items"].append(i)
  542. print(i["comment_id"])
  543. box = win.commenting[claim_id]["box"]
  544. def render(win, box, i):
  545. render_single_comment(win, box, i)
  546. box.show_all()
  547. GLib.idle_add(render, win, box, i)
  548. def sign(data: str = "", channel: str = "", message: str = "Channel to sign data with:", hexdata: str = ""):
  549. """
  550. Sign a string or hexdata and return the signatures
  551. Keyword arguments:
  552. data -- a string to sign
  553. channel -- channel name to sign with (e.g. "@example"). Will prompt for one if not given.
  554. message -- message to give when selecting a channel. Please pass this if not passing channel.
  555. hexdata -- direct hexadecimal data to sign
  556. """
  557. if (not data and not hexdata) or (data and hexdata):
  558. raise ValueError("Must give either data or hexdata")
  559. elif data:
  560. hexdata = data.encode().hex()
  561. if not channel:
  562. channel = select(message)
  563. if not channel.startswith("@"):
  564. channel = "@" + channel
  565. try:
  566. sigs = fetch.lbrynet("channel_sign", {"channel_name":channel, "hexdata":hexdata})
  567. # sigs = check_output([flbry_globals["lbrynet"],
  568. # "channel", "sign",
  569. # "--channel_name=" + channel,
  570. # "--hexdata=" + hexdata])
  571. # sigs = json.loads(sigs)
  572. return sigs
  573. except:
  574. print("Stupid Error")
  575. return