2
0

api_topic.py 9.5 KB

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