go_api_topic.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. from .tool.func import *
  2. def api_topic_thread_make(user_id, date, data, code, color = '', blind = '', add_style = '', admin_check = 1, topic_num = ''):
  3. if blind == 'O':
  4. if data == '':
  5. color_b = 'opennamu_comment_blind'
  6. else:
  7. color_b = 'opennamu_comment_blind_admin'
  8. class_b = 'opennamu_comment_blind_js opennamu_list_hidden'
  9. else:
  10. color_b = 'opennamu_comment_blind_not'
  11. class_b = ''
  12. admin_check_box = ''
  13. if admin_check == 1 and topic_num != '':
  14. admin_check_box = '<label><input type="checkbox" class="opennamu_blind_button __ON_CHECKBOX__" id="opennamu_blind_' + topic_num + '_' + code + '"></label>'
  15. return '''
  16. <span class="''' + class_b + '''">
  17. <table class="opennamu_comment" style="''' + add_style + '''">
  18. <tr>
  19. <td class="opennamu_comment_color_''' + color + '''">
  20. ''' + admin_check_box + '''
  21. <a href="#thread_shortcut" id="''' + code + '''">#''' + code + '''</a>
  22. ''' + user_id + '''
  23. <span style="float: right;">''' + date + '''</span>
  24. </td>
  25. </tr>
  26. <tr>
  27. <td class="''' + color_b + ''' opennamu_comment_data_main" id="thread_''' + code + '''">
  28. ''' + data + '''
  29. <div id="opennamu_topic_req_''' + code + '''"></div>
  30. </td>
  31. </tr>
  32. </table>
  33. <hr class="main_hr">
  34. </span>
  35. '''
  36. async def api_topic_thread_pre_render(conn, data, num, ip, topic_num = '', name = '', sub = '', do_type = 'thread'):
  37. curs = conn.cursor()
  38. # 이거 좀 엉성해서 언젠간 손 보고 싶음
  39. call_thread_regex = r"( |\n|^)(?:#([0-9]+)(?:-([0-9]+))?)( |\n|$)"
  40. call_thread_count = len(re.findall(call_thread_regex, data)) * 3
  41. while 1:
  42. rd_data = re.search(call_thread_regex, data)
  43. if call_thread_count < 0:
  44. break
  45. elif not rd_data:
  46. break
  47. else:
  48. rd_data = rd_data.groups()
  49. view_data = rd_data[1]
  50. send_topic_num = topic_num
  51. if rd_data[2]:
  52. view_data += '-' + rd_data[2]
  53. if do_type == 'thread':
  54. send_topic_num = rd_data[2]
  55. else:
  56. set_id = topic_num.split('-')
  57. send_topic_num = set_id[0] + '-' + rd_data[2]
  58. view_data += '-' + set_id[0]
  59. if do_type == 'thread':
  60. curs.execute(db_change("select ip from topic where code = ? and id = ?"), [send_topic_num, rd_data[1]])
  61. else:
  62. if rd_data[1] == '0':
  63. set_id = send_topic_num.split('-')
  64. set_id = ['', ''] if len(set_id) < 2 else set_id
  65. curs.execute(db_change('select set_data from bbs_data where set_name = "user_id" and set_id = ? and set_code = ?'), [set_id[0], set_id[1]])
  66. else:
  67. curs.execute(db_change('select set_data from bbs_data where set_name = "comment_user_id" and set_id = ? and set_code = ?'), [send_topic_num, rd_data[1]])
  68. ip_data = curs.fetchall()
  69. if ip_data and ip_or_user(ip_data[0][0]) == 0:
  70. if do_type == 'thread':
  71. await add_alarm(ip_data[0][0], ip, '<a href="/thread/' + topic_num + '#' + num + '">' + html.escape(name) + ' - ' + html.escape(sub) + '#' + num + '</a>')
  72. else:
  73. set_id = topic_num.split('-')
  74. set_id = ['', ''] if len(set_id) < 2 else set_id
  75. await add_alarm(ip_data[0][0], ip, 'BBS <a href="/bbs/w/' + set_id[0] + '/' + set_id[1] + '#' + num + '">' + html.escape(name) + ' - ' + html.escape(sub) + '#' + num + '</a>')
  76. data = re.sub(call_thread_regex, rd_data[0] + '<topic_a_' + do_type + '>#' + view_data + '</topic_a_' + do_type + '>' + rd_data[3], data, 1)
  77. call_thread_count -= 1
  78. call_user_regex = r"( |\n|^)(?:@([^ \n]+))( |\n|$)"
  79. call_user_count = len(re.findall(call_user_regex, data)) * 3
  80. while 1:
  81. rd_data = re.search(call_user_regex, data)
  82. if call_user_count < 0:
  83. break
  84. elif not rd_data:
  85. break
  86. else:
  87. rd_data = rd_data.groups()
  88. curs.execute(db_change("select ip from history where ip = ? limit 1"), [rd_data[1]])
  89. ip_data = curs.fetchall()
  90. if not ip_data:
  91. curs.execute(db_change("select ip from topic where ip = ? limit 1"), [rd_data[1]])
  92. ip_data = curs.fetchall()
  93. if ip_data and ip_or_user(ip_data[0][0]) == 0:
  94. if do_type == 'thread':
  95. await add_alarm(ip_data[0][0], ip, '<a href="/thread/' + topic_num + '#' + num + '">' + html.escape(name) + ' - ' + html.escape(sub) + '#' + num + '</a>')
  96. else:
  97. set_id = topic_num.split('-')
  98. await add_alarm(ip_data[0][0], ip, 'BBS <a href="/bbs/w/' + set_id[0] + '/' + set_id[1] + '#' + num + '">' + html.escape(name) + ' - ' + html.escape(sub) + '#' + num + '</a>')
  99. data = re.sub(call_user_regex, rd_data[0] + '<topic_call>@' + rd_data[1] + '</topic_call>' + rd_data[2], data, 1)
  100. call_user_count -= 1
  101. return data
  102. async def api_topic(topic_num = 1, tool = 'normal', s_num = '', e_num = ''):
  103. with get_db_connect() as conn:
  104. topic_num = str(topic_num)
  105. if await acl_check('', 'topic_view', topic_num) != 1:
  106. other_set = {}
  107. other_set["topic_num"] = topic_num
  108. other_set["tool"] = tool
  109. other_set["s_num"] = str(s_num)
  110. other_set["e_num"] = str(e_num)
  111. return flask.jsonify(await python_to_golang(sys._getframe().f_code.co_name, other_set))
  112. else:
  113. return flask.jsonify({})