2
0

api_topic.py 8.6 KB

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