api_topic.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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'
  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 = '<input type="checkbox" class="opennamu_blind_button" id="opennamu_blind_' + topic_num + '_' + code + '">'
  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. def api_topic_thread_pre_render(curs, data, num, ip, topic_num = '', name = '', sub = '', do_type = 'thread'):
  37. # 이거 좀 엉성해서 언젠간 손 보고 싶음
  38. call_thread_regex = r"( |\n|^)(?:#([0-9]+)(?:-([0-9]+))?)( |\n|$)"
  39. call_thread_count = len(re.findall(call_thread_regex, data)) * 3
  40. while 1:
  41. rd_data = re.search(call_thread_regex, data)
  42. if call_thread_count < 0:
  43. break
  44. elif not rd_data:
  45. break
  46. else:
  47. rd_data = rd_data.groups()
  48. view_data = rd_data[1]
  49. send_topic_num = topic_num
  50. if rd_data[2]:
  51. view_data += '-' + rd_data[2]
  52. if do_type == 'thread':
  53. send_topic_num = rd_data[2]
  54. else:
  55. set_id = topic_num.split('-')
  56. send_topic_num = set_id[0] + '-' + rd_data[2]
  57. view_data += '-' + set_id[0]
  58. if do_type == 'thread':
  59. curs.execute(db_change("select ip from topic where code = ? and id = ?"), [send_topic_num, rd_data[1]])
  60. else:
  61. if rd_data[1] == '0':
  62. set_id = send_topic_num.split('-')
  63. set_id = ['', ''] if len(set_id) < 2 else set_id
  64. 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]])
  65. else:
  66. 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]])
  67. ip_data = curs.fetchall()
  68. if ip_data and ip_or_user(ip_data[0][0]) == 0:
  69. if do_type == 'thread':
  70. add_alarm(ip_data[0][0], ip, '<a href="/thread/' + topic_num + '#' + num + '">' + html.escape(name) + ' - ' + html.escape(sub) + '#' + num + '</a>')
  71. else:
  72. set_id = topic_num.split('-')
  73. set_id = ['', ''] if len(set_id) < 2 else set_id
  74. 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>')
  75. data = re.sub(call_thread_regex, rd_data[0] + '<topic_a_' + do_type + '>#' + view_data + '</topic_a_' + do_type + '>' + rd_data[3], data, 1)
  76. call_thread_count -= 1
  77. call_user_regex = r"( |\n|^)(?:@([^ \n]+))( |\n|$)"
  78. call_user_count = len(re.findall(call_user_regex, data)) * 3
  79. while 1:
  80. rd_data = re.search(call_user_regex, data)
  81. if call_user_count < 0:
  82. break
  83. elif not rd_data:
  84. break
  85. else:
  86. rd_data = rd_data.groups()
  87. curs.execute(db_change("select ip from history where ip = ? limit 1"), [rd_data[1]])
  88. ip_data = curs.fetchall()
  89. if not ip_data:
  90. curs.execute(db_change("select ip from topic where ip = ? limit 1"), [rd_data[1]])
  91. ip_data = curs.fetchall()
  92. if ip_data and ip_or_user(ip_data[0][0]) == 0:
  93. if do_type == 'thread':
  94. add_alarm(ip_data[0][0], ip, '<a href="/thread/' + topic_num + '#' + num + '">' + html.escape(name) + ' - ' + html.escape(sub) + '#' + num + '</a>')
  95. else:
  96. set_id = topic_num.split('-')
  97. 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>')
  98. data = re.sub(call_user_regex, rd_data[0] + '<topic_call>@' + rd_data[1] + '</topic_call>' + rd_data[2], data, 1)
  99. call_user_count -= 1
  100. return data
  101. def api_topic(topic_num = 1, tool = 'normal', num = '', render = ''):
  102. with get_db_connect() as conn:
  103. curs = conn.cursor()
  104. topic_num = str(topic_num)
  105. if acl_check('', 'topic_view', topic_num) != 1:
  106. if tool == 'normal':
  107. if num != '':
  108. curs.execute(db_change("select id, data, date, ip, block, top from topic where code = ? and id + 0 = ? + 0 order by id + 0 asc"), [topic_num, num])
  109. else:
  110. curs.execute(db_change("select id, data, date, ip, block, top from topic where code = ? order by id + 0 asc"), [topic_num])
  111. elif tool == 'top':
  112. curs.execute(db_change("select id, data, date, ip, block, top from topic where code = ? and top = 'O' order by id + 0 asc"), [topic_num])
  113. else:
  114. # tool == 'length'
  115. curs.execute(db_change("select id from topic where code = ? order by id + 0 desc limit 1"), [topic_num])
  116. db_data = curs.fetchall()
  117. if db_data:
  118. return flask.jsonify({ 'length' : db_data[0][0] })
  119. else:
  120. return flask.jsonify({})
  121. data = curs.fetchall()
  122. if data:
  123. data_a = {}
  124. admin = admin_check(3)
  125. curs.execute(db_change("select ip from topic where code = ? order by id + 0 asc limit 1"), [topic_num])
  126. data_f = curs.fetchall()
  127. data_f = data_f[0][0] if data_f else ''
  128. data_a['data_main'] = {
  129. "ip_first" : ip_pas(data_f, 1),
  130. "admin" : str(admin)
  131. }
  132. data_a['data'] = []
  133. ip_a = ip_pas([i[3] for i in data])
  134. ip_a_2 = ip_pas([i[3] for i in data], 1)
  135. for i in data:
  136. data_v = i[1] if i[4] != 'O' or admin == 1 else ''
  137. if data_v != '':
  138. data_v = render_set(
  139. doc_data = data_v,
  140. data_type = 'api_thread',
  141. data_in = 'topic_' + topic_num + '_' + i[0]
  142. )
  143. else:
  144. data_v = ['', '']
  145. data_a['data'] += [{
  146. "id" : i[0],
  147. "data" : data_v,
  148. "date" : i[2],
  149. "ip" : ip_a_2[i[3]],
  150. "blind" : i[4],
  151. "ip_pas" : ip_a[i[3]],
  152. "data_pas" : data_v
  153. }]
  154. if render == '':
  155. return flask.jsonify(data_a)
  156. else:
  157. data_r = ''
  158. if 'data' in data_a:
  159. for for_a in data_a['data']:
  160. if tool == 'top':
  161. color = 'red'
  162. elif for_a["blind"] == '1':
  163. color = 'blue'
  164. elif data_a['data_main']["ip_first"] == for_a["ip"]:
  165. color = 'green'
  166. else:
  167. color = 'default'
  168. data_r += api_topic_thread_make(
  169. for_a["ip_pas"],
  170. '<a href="/thread/' + topic_num + '/comment/' + for_a["id"] + '/tool">(' + load_lang('tool') + ')</a> ' + for_a["date"],
  171. for_a["data_pas"][0] + '<script>' + for_a["data_pas"][1] + '</script>',
  172. for_a["id"],
  173. color = color,
  174. blind = for_a["blind"],
  175. add_style = '',
  176. admin_check = admin if tool == 'normal' else 0,
  177. topic_num = topic_num
  178. )
  179. if admin == 1 and tool == 'normal':
  180. data_r += '''
  181. <a href="javascript:opennamu_thread_blind();">(''' + load_lang('hide') + ''' | ''' + load_lang('hide_release') + ''')</a>
  182. <hr class="main_hr">
  183. '''
  184. return flask.jsonify({ "data" : data_r })
  185. else:
  186. return flask.jsonify({})
  187. else:
  188. return flask.jsonify({})