api_topic.py 8.8 KB

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