Просмотр исходного кода

게시판 API Golang으로 이전

잉여개발기 1 год назад
Родитель
Сommit
bbd9987f0d

+ 3 - 2
app.py

@@ -725,7 +725,7 @@ app.route('/api/random')(api_w_random)
 
 app.route('/api/bbs/w/<sub_code>')(api_bbs_w_post)
 app.route('/api/bbs/w/comment/<sub_code>')(api_bbs_w_comment)
-app.route('/api/bbs/w/comment_one/<sub_code>')(api_bbs_w_comment)
+app.route('/api/bbs/w/comment_one/<sub_code>')(api_bbs_w_comment_one)
 
 app.route('/api/version', defaults = { 'version_list' : version_list })(api_version)
 app.route('/api/skin_info')(api_skin_info)
@@ -778,7 +778,8 @@ app.route('/api/v2/topic/<int:num>/<set_type>/<everything:name>')(api_topic_list
 app.route('/api/v2/bbs')(api_bbs_list)
 app.route('/api/v2/bbs/main')(api_bbs)
 app.route('/api/v2/bbs/in/<int:bbs_num>/<int:page>')(api_bbs)
-app.route('/api/v2/bbs/w/comment/<int:bbs_num>/<int:post_num>/<tool>')(api_bbs_w_comment_n)
+app.route('/api/v2/bbs/w/comment/<sub_code>/<tool>', defaults = { 'legacy' : '' })(api_bbs_w_comment)
+app.route('/api/v2/bbs/w/comment_one/<sub_code>/<tool>', defaults = { 'legacy' : '' })(api_bbs_w_comment_one)
 
 app.route('/api/v2/doc_star_doc/<int:num>/<everything:name>', defaults = { 'do_type' : 'star_doc' })(api_w_watch_list)
 app.route('/api/v2/doc_watch_list/<int:num>/<everything:name>')(api_w_watch_list)

+ 2 - 3
route/__init__.py

@@ -4,8 +4,6 @@ from route.api_skin_info import api_skin_info
 from route.api_user_info import api_user_info
 from route.api_version import api_version
 from route.api_bbs_w_post import api_bbs_w_post
-from route.api_bbs_w_comment import api_bbs_w_comment
-from route.api_bbs_w_comment_one import api_bbs_w_comment_one
 
 from route.bbs_w_edit import bbs_w_edit
 from route.bbs_make import bbs_make
@@ -204,7 +202,8 @@ from route.go_api_list_title_index import api_list_title_index
 
 from route.go_api_bbs import api_bbs
 from route.go_api_bbs_list import api_bbs_list
-from route.go_api_bbs_w_comment import api_bbs_w_comment_n
+from route.go_api_bbs_w_comment import api_bbs_w_comment
+from route.go_api_bbs_w_comment_one import api_bbs_w_comment_one
 
 from route.go_api_setting import api_setting
 

+ 0 - 29
route/api_bbs_w_comment.py

@@ -1,29 +0,0 @@
-from .tool.func import *
-
-def api_bbs_w_comment(sub_code = ''):
-    with get_db_connect() as conn:
-        curs = conn.cursor()
-
-        curs.execute(db_change('select set_name, set_data, set_code, set_id from bbs_data where (set_name = "comment" or set_name = "comment_date" or set_name = "comment_user_id") and set_id = ? order by set_code + 0 asc'), [sub_code])
-        db_data = curs.fetchall()
-        if not db_data:
-            return flask.jsonify({})
-        else:
-            temp_id = ''
-            temp_dict = {}
-            temp_list = []
-
-            for for_a in db_data:
-                if temp_id != for_a[2]:
-                    if temp_dict != {}:
-                        temp_list += [dict(temp_dict)]
-
-                    temp_id = for_a[2]
-                    temp_dict['code'] = for_a[2]
-
-                temp_dict[for_a[0]] = for_a[1]
-
-            if temp_dict != {}:
-                temp_list += [dict(temp_dict)]
-
-            return flask.jsonify(temp_list)

+ 0 - 30
route/api_bbs_w_comment_one.py

@@ -1,30 +0,0 @@
-from .tool.func import *
-
-def api_bbs_w_comment_one(sub_code = ''):
-    with get_db_connect() as conn:
-        curs = conn.cursor()
-        
-        sub_code = sub_code.split('-')
-        sub_code_last = ''
-        if len(sub_code) > 2:
-            sub_code_last = sub_code[len(sub_code) - 1]
-            del sub_code[len(sub_code) - 1]
-            
-        sub_code = '-'.join(sub_code)
-
-        curs.execute(db_change('select set_name, set_data, set_code, set_id from bbs_data where (set_name = "comment" or set_name = "comment_date" or set_name = "comment_user_id") and set_id = ? and set_code = ?'), [sub_code, sub_code_last])
-        db_data = curs.fetchall()
-        if not db_data:
-            return flask.jsonify({})
-        else:
-            temp_id = ''
-            temp_dict = {}
-
-            for for_a in db_data:
-                if temp_id != for_a[2]:
-                    temp_id = for_a[2]
-                    temp_dict['code'] = for_a[2]
-
-                temp_dict[for_a[0]] = for_a[1]
-
-            return flask.jsonify(temp_dict)

+ 4 - 4
route/bbs_w_edit.py

@@ -1,11 +1,11 @@
 from .tool.func import *
 
 from .api_bbs_w_post import api_bbs_w_post
-from .api_bbs_w_comment_one import api_bbs_w_comment_one
+from .go_api_bbs_w_comment_one import api_bbs_w_comment_one
 
 from .edit import edit_editor
 
-def bbs_w_edit(bbs_num = '', post_num = '', comment_num = ''):
+async def bbs_w_edit(bbs_num = '', post_num = '', comment_num = ''):
     with get_db_connect() as conn:
         curs = conn.cursor()
 
@@ -19,7 +19,7 @@ def bbs_w_edit(bbs_num = '', post_num = '', comment_num = ''):
             return redirect(conn, '/bbs/main')
         
         if comment_num != '':
-            temp_dict = orjson.loads(api_bbs_w_comment_one(bbs_num_str + '-' + post_num_str + '-' + comment_num).data)
+            temp_dict = orjson.loads((await api_bbs_w_comment_one(bbs_num_str + '-' + post_num_str + '-' + comment_num)).get_data(as_text = True))
             if 'comment_user_id' in temp_dict:
                 if not temp_dict['comment_user_id'] == ip and acl_check('', 'owner_auth', '', '') == 1:
                     return re_error(conn, 0)
@@ -92,7 +92,7 @@ def bbs_w_edit(bbs_num = '', post_num = '', comment_num = ''):
             option_display = ''
 
             if comment_num != '':
-                temp_dict = orjson.loads(api_bbs_w_comment_one(bbs_num_str + '-' + post_num_str + '-' + comment_num).data)
+                temp_dict = orjson.loads((await api_bbs_w_comment_one(bbs_num_str + '-' + post_num_str + '-' + comment_num)).get_data(as_text = True))
 
                 title = ''
                 data = temp_dict['comment']

+ 12 - 17
route/bbs_w_post.py

@@ -1,23 +1,20 @@
 from .tool.func import *
 
 from .api_bbs_w_post import api_bbs_w_post
-from .api_bbs_w_comment import api_bbs_w_comment
+from .go_api_bbs_w_comment import api_bbs_w_comment
 
 from .go_api_topic import api_topic_thread_make, api_topic_thread_pre_render
 
 from .edit import edit_editor
 
-def bbs_w_post_comment(conn, user_id, sub_code, comment_num, bbs_num_str, post_num_str):
+async def bbs_w_post_comment(conn, user_id, sub_code, comment_num, bbs_num_str, post_num_str):
     comment_data = ''
     comment_select = ''
 
     comment_count = 0
     comment_add_count = 0
 
-    thread_data = orjson.loads(api_bbs_w_comment(sub_code).data)
-    
-    comment_count += len(thread_data)
-    comment_add_count += comment_count
+    thread_data = orjson.loads((await api_bbs_w_comment(sub_code)).get_data(as_text = True))
 
     for temp_dict in thread_data:
         if temp_dict['comment_user_id'] != '':
@@ -25,9 +22,14 @@ def bbs_w_post_comment(conn, user_id, sub_code, comment_num, bbs_num_str, post_n
             if user_id == temp_dict['comment_user_id']:
                 color = 'green'
 
-            sub_code_check = re.sub(r'^[0-9]+-[0-9]+-', '', sub_code + '-' + temp_dict['code'])
+            sub_code_check = re.sub(r'^[0-9]+-[0-9]+-', '', temp_dict['id'] + '-' + temp_dict['code'])
             margin_count = sub_code_check.count('-')
 
+            if margin_count == 0:
+                comment_count += 1
+            else:
+                comment_add_count += 1
+
             date = ''
             date += '<a href="javascript:opennamu_change_comment(\'' + sub_code_check + '\');">(' + get_lang(conn, 'comment') + ')</a> '
             date += '<a href="/bbs/tool/' + bbs_num_str + '/' + post_num_str + '/' + sub_code_check + '">(' + get_lang(conn, 'tool') + ')</a> '
@@ -49,15 +51,9 @@ def bbs_w_post_comment(conn, user_id, sub_code, comment_num, bbs_num_str, post_n
 
             comment_select += '<option value="' + sub_code_check + '" ' + comment_default + '>' + sub_code_check + '</option>'
 
-        temp_data = bbs_w_post_comment(conn, user_id, sub_code + '-' + temp_dict['code'], comment_num, bbs_num_str, post_num_str)
-
-        comment_data += temp_data[0]
-        comment_select += temp_data[1]
-        comment_add_count += temp_data[3]
-
     return (comment_data, comment_select, comment_count, comment_add_count)
 
-def bbs_w_post(bbs_num = '', post_num = ''):
+async def bbs_w_post(bbs_num = '', post_num = ''):
     with get_db_connect() as conn:
         curs = conn.cursor()
 
@@ -134,7 +130,7 @@ def bbs_w_post(bbs_num = '', post_num = ''):
                 user_id = temp_dict['user_id']
                 count = 0
 
-                thread_data = orjson.loads(api_bbs_w_comment(bbs_num_str + '-' + post_num_str).data)
+                thread_data = orjson.loads((await api_bbs_w_comment(bbs_num_str + '-' + post_num_str)).get_data(as_text = True))
                 for temp_dict in thread_data:
                     count += 1
                     if user_id == temp_dict['comment_user_id']:
@@ -258,13 +254,12 @@ def bbs_w_post(bbs_num = '', post_num = ''):
                 comment_count = 0
                 comment_add_count = 0
 
-                temp_data = bbs_w_post_comment(conn, user_id, bbs_num_str + '-' + post_num_str, comment_num, bbs_num_str, post_num_str)
+                temp_data = await bbs_w_post_comment(conn, user_id, bbs_num_str + '-' + post_num_str, comment_num, bbs_num_str, post_num_str)
 
                 comment_data += temp_data[0]
                 comment_select += temp_data[1]
                 comment_count += temp_data[2]
                 comment_add_count += temp_data[3]
-                comment_add_count -= comment_count
 
                 if comment_data != '':
                     data += '<hr>'

+ 3 - 3
route/go_api_bbs_w_comment.py

@@ -1,9 +1,9 @@
 from .tool.func import *
 
-async def api_bbs_w_comment_n(bbs_num = "", post_num = "", tool = "length"):
+async def api_bbs_w_comment(sub_code = '', tool = "", legacy = 'on'):
     other_set = {}
-    other_set["bbs_num"] = str(bbs_num)
-    other_set["post_num"] = str(post_num)
+    other_set["sub_code"] = sub_code
     other_set["tool"] = tool
+    other_set["legacy"] = legacy
 
     return flask.Response(response = (await python_to_golang(sys._getframe().f_code.co_name, other_set)), status = 200, mimetype = 'application/json')

+ 9 - 0
route/go_api_bbs_w_comment_one.py

@@ -0,0 +1,9 @@
+from .tool.func import *
+
+async def api_bbs_w_comment_one(sub_code = '', legacy = 'on', tool = ''):
+    other_set = {}
+    other_set["sub_code"] = sub_code
+    other_set["legacy"] = legacy
+    other_set["tool"] = tool
+
+    return flask.Response(response = (await python_to_golang(sys._getframe().f_code.co_name, other_set)), status = 200, mimetype = 'application/json')

+ 1 - 1
route/tool/func.py

@@ -1230,7 +1230,7 @@ def skin_check(conn, set_n = 0):
         return skin
     
 def cache_v():
-    return '.cache_v271'
+    return '.cache_v272'
 
 def wiki_css(data):
     with class_temp_db() as m_conn:

+ 3 - 3
route/view_raw.py

@@ -1,9 +1,9 @@
 from .tool.func import *
 
 from .api_bbs_w_post import api_bbs_w_post
-from .api_bbs_w_comment_one import api_bbs_w_comment_one
+from .go_api_bbs_w_comment_one import api_bbs_w_comment_one
 
-def view_raw(name = '', topic_num = '', num = '', doc_acl = 0, bbs_num = '', post_num = '', comment_num = ''):
+async def view_raw(name = '', topic_num = '', num = '', doc_acl = 0, bbs_num = '', post_num = '', comment_num = ''):
     with get_db_connect() as conn:
         curs = conn.cursor()
         
@@ -67,7 +67,7 @@ def view_raw(name = '', topic_num = '', num = '', doc_acl = 0, bbs_num = '', pos
 
         if bbs_num != '' and post_num != '':
             if comment_num != '':
-                data = orjson.loads(api_bbs_w_comment_one(bbs_num_str + '-' + post_num_str + '-' + comment_num).data)
+                data = orjson.loads((await api_bbs_w_comment_one(bbs_num_str + '-' + post_num_str + '-' + comment_num)).get_data(as_text = True))
                 sub_data = orjson.loads(api_bbs_w_post(bbs_num_str + '-' + post_num_str).data)
             else:
                 data = orjson.loads(api_bbs_w_post(bbs_num_str + '-' + post_num_str).data)

BIN
route_go/bin/main.amd64.bin


BIN
route_go/bin/main.amd64.exe


BIN
route_go/bin/main.arm64.bin


BIN
route_go/bin/main.arm64.exe


+ 4 - 2
route_go/main.go

@@ -55,8 +55,6 @@ func main() {
 		route_data = route.Api_list_old_page(call_arg[1:])
 	} else if call_arg[0] == "api_topic_list" {
 		route_data = route.Api_topic_list(call_arg[1:])
-	} else if call_arg[0] == "api_bbs_w_comment_n" {
-		route_data = route.Api_bbs_w_comment(call_arg[1:])
 	} else if call_arg[0] == "api_bbs_w_n" {
 		route_data = route.Api_bbs_w(call_arg[1:])
 	} else if call_arg[0] == "api_w_set_reset" {
@@ -95,6 +93,10 @@ func main() {
 		route_data = route.Api_list_auth(call_arg[1:])
 	} else if call_arg[0] == "api_w_page_view" {
 		route_data = route.Api_w_page_view(call_arg[1:])
+	} else if call_arg[0] == "api_bbs_w_comment_one" {
+		route_data = route.Api_bbs_w_comment_one(call_arg[1:])
+	} else if call_arg[0] == "api_bbs_w_comment" {
+		route_data = route.Api_bbs_w_comment(call_arg[1:])
 	} else {
 		log.Fatal(call_arg[0] + " is 404")
 	}

+ 42 - 2
route_go/route/api_bbs_w_comment.go

@@ -2,6 +2,7 @@ package route
 
 import (
 	"database/sql"
+	"encoding/json"
 	"log"
 	"opennamu/route/tool"
 	"strconv"
@@ -9,6 +10,34 @@ import (
 	jsoniter "github.com/json-iterator/go"
 )
 
+func Api_bbs_w_comment_all(sub_code string) []map[string]string {
+	end_data := []map[string]string{}
+
+	inter_other_set := map[string]string{}
+	inter_other_set["sub_code"] = sub_code
+	inter_other_set["tool"] = "around"
+	inter_other_set["legacy"] = "on"
+
+	json_data, _ := json.Marshal(inter_other_set)
+	return_data := Api_bbs_w_comment_one([]string{string(json_data)})
+
+	return_data_api := []map[string]string{}
+	json.Unmarshal([]byte(return_data), &return_data_api)
+
+	for for_a := 0; for_a < len(return_data_api); for_a++ {
+		end_data = append(end_data, return_data_api[for_a])
+
+		temp := Api_bbs_w_comment_all(sub_code + "-" + return_data_api[for_a]["code"])
+		if len(temp) > 0 {
+			for for_b := 0; for_b < len(temp); for_b++ {
+				end_data = append(end_data, temp[for_b])
+			}
+		}
+	}
+
+	return end_data
+}
+
 func Api_bbs_w_comment(call_arg []string) string {
 	var json = jsoniter.ConfigCompatibleWithStandardLibrary
 
@@ -26,7 +55,7 @@ func Api_bbs_w_comment(call_arg []string) string {
 		defer stmt.Close()
 
 		var comment_length string
-		bbs_and_post_num := other_set["bbs_num"] + "-" + other_set["post_num"]
+		bbs_and_post_num := other_set["sub_code"]
 
 		err = stmt.QueryRow(bbs_and_post_num).Scan(&comment_length)
 		if err != nil {
@@ -69,6 +98,17 @@ func Api_bbs_w_comment(call_arg []string) string {
 		json_data, _ := json.Marshal(data_list)
 		return string(json_data)
 	} else {
-		return "{}"
+		temp := Api_bbs_w_comment_all(other_set["sub_code"])
+
+		if other_set["legacy"] != "" {
+			json_data, _ := json.Marshal(temp)
+			return string(json_data)
+		} else {
+			return_data := make(map[string]interface{})
+			return_data["data"] = temp
+
+			json_data, _ := json.Marshal(return_data)
+			return string(json_data)
+		}
 	}
 }

+ 107 - 0
route_go/route/api_bbs_w_comment_one.go

@@ -0,0 +1,107 @@
+package route
+
+import (
+	"database/sql"
+	"log"
+	"opennamu/route/tool"
+	"strings"
+
+	jsoniter "github.com/json-iterator/go"
+)
+
+func Api_bbs_w_comment_one(call_arg []string) string {
+	var json = jsoniter.ConfigCompatibleWithStandardLibrary
+
+	other_set := map[string]string{}
+	json.Unmarshal([]byte(call_arg[0]), &other_set)
+
+	db := tool.DB_connect()
+	defer db.Close()
+
+	sub_code := other_set["sub_code"]
+	sub_code_parts := strings.Split(sub_code, "-")
+	sub_code_last := ""
+	new_sub_code := ""
+
+	if other_set["tool"] == "around" {
+		new_sub_code = other_set["sub_code"]
+	} else {
+		if len(sub_code_parts) > 2 {
+			sub_code_last = sub_code_parts[len(sub_code_parts)-1]
+			sub_code_parts = sub_code_parts[:len(sub_code_parts)-1]
+
+			new_sub_code = strings.Join(sub_code_parts, "-")
+		}
+	}
+
+	var rows *sql.Rows
+	if other_set["tool"] == "around" {
+		stmt, err := db.Prepare(tool.DB_change("select set_name, set_data, set_code, set_id from bbs_data where (set_name = 'comment' or set_name like 'comment%') and set_id = ?"))
+		if err != nil {
+			log.Fatal(err)
+		}
+		defer stmt.Close()
+
+		rows, err = stmt.Query(new_sub_code)
+		if err != nil {
+			log.Fatal(err)
+		}
+	} else {
+		stmt, err := db.Prepare(tool.DB_change("select set_name, set_data, set_code, set_id from bbs_data where (set_name = 'comment' or set_name like 'comment%') and set_id = ? and set_code = ?"))
+		if err != nil {
+			log.Fatal(err)
+		}
+		defer stmt.Close()
+
+		rows, err = stmt.Query(new_sub_code, sub_code_last)
+		if err != nil {
+			log.Fatal(err)
+		}
+	}
+	defer rows.Close()
+
+	data_list := []map[string]string{}
+	temp_dict := map[string]string{}
+	before_set_code := ""
+
+	for rows.Next() {
+		var set_name string
+		var set_data string
+		var set_code string
+		var set_id string
+
+		err := rows.Scan(&set_name, &set_data, &set_code, &set_id)
+		if err != nil {
+			log.Fatal(err)
+		}
+
+		if before_set_code != set_code {
+			if before_set_code != "" {
+				data_list = append(data_list, temp_dict)
+			}
+
+			temp_dict = map[string]string{}
+			temp_dict["id"] = set_id
+			temp_dict["code"] = set_code
+
+			before_set_code = set_code
+		}
+
+		temp_dict[set_name] = set_data
+	}
+
+	if before_set_code != "" {
+		data_list = append(data_list, temp_dict)
+	}
+
+	if other_set["legacy"] != "" {
+		json_data, _ := json.Marshal(data_list)
+		return string(json_data)
+	} else {
+		return_data := make(map[string]interface{})
+		return_data["data"] = data_list
+
+		json_data, _ := json.Marshal(return_data)
+		return string(json_data)
+	}
+}

+ 1 - 1
version.json

@@ -1,6 +1,6 @@
 {
     "beta" : {
-        "r_ver" : "v3.5.0-v171",
+        "r_ver" : "v3.5.0-v172",
         "c_ver" : "20240513",
         "s_ver" : "20240426"
     }

+ 1 - 1
views/main_css/js/route/bbs_in.js

@@ -48,7 +48,7 @@ function opennamu_bbs_in() {
         document.getElementById('opennamu_bbs_in').innerHTML = data_html;
 
         for(let for_a = 0; for_a < data.length; for_a++) {
-            fetch('/api/v2/bbs/w/comment/' + data[for_a]['set_id'] + '/' + data[for_a]['set_code'] + '/length').then(function(res) {
+            fetch('/api/v2/bbs/w/comment/' + data[for_a]['set_id'] + '-' + data[for_a]['set_code'] + '/length').then(function(res) {
                 return res.json();
             }).then(function(comment_data) {
                 if(comment_data) {

+ 1 - 1
views/main_css/js/route/bbs_main.js

@@ -69,7 +69,7 @@ function opennamu_bbs_main() {
             document.getElementById('opennamu_bbs_main').innerHTML = data_html;
 
             for(let for_a = 0; for_a < data.length; for_a++) {
-                fetch('/api/v2/bbs/w/comment/' + data[for_a]['set_id'] + '/' + data[for_a]['set_code'] + '/length').then(function(res) {
+                fetch('/api/v2/bbs/w/comment/' + data[for_a]['set_id'] + '-' + data[for_a]['set_code'] + '/length').then(function(res) {
                     return res.json();
                 }).then(function(comment_data) {
                     if(comment_data) {