Browse Source

imlemented base forum logistics

TooManySugar 3 years ago
parent
commit
d060f25adb
4 changed files with 215 additions and 3 deletions
  1. 128 0
      cmd/web/connector.go
  2. 82 0
      cmd/web/handlers.go
  3. 2 0
      cmd/web/main.go
  4. 3 3
      ui/templates/newindex.html

+ 128 - 0
cmd/web/connector.go

@@ -32,6 +32,20 @@ type Torrent struct {
 	Deleted bool
 }
 
+type Folder struct {
+	Uuid 		string
+	ParentUuid 	string
+	Type		int
+	Title		string
+	Snake		string
+}
+
+type ForumMessage struct {
+	Author	string
+	Time 	string
+	Text 	string
+}
+
 func (c *connection) Init(filepath string) {
 	if NOMYSQL {
 		return
@@ -344,6 +358,120 @@ func (c connection) GetLastNews(amount int64) (news []NewsItem, count int64, err
 
 }
 
+func (c connection) TakeChildFolders(parentUuid string, amount int64) (folders []Folder, err error){
+	db, err := sql.Open("mysql", c.dBOpenStr)
+	if err != nil {
+		return
+	}
+	defer db.Close()
+
+	var act_query string
+
+	act_query = fmt.Sprintf("SELECT * FROM `%s`.`folders` as `folders`", c.connProperties.Db)
+	act_query += fmt.Sprintf(" WHERE `folders`.`parent_uuid` = '%s'", parentUuid)
+	act_query += " ORDER BY `id` DESC"
+	if amount != 0 {
+		act_query += fmt.Sprintf(" LIMIT %d",  amount)
+	}
+
+	fmt.Println(act_query)
+	rows, err := db.Query(act_query)
+	if err != nil {
+		return
+	}
+	defer rows.Close()
+
+	for rows.Next() {
+		var folder Folder
+		var id interface{}
+		if err := rows.Scan(
+			&id,
+			&folder.Uuid,
+			&folder.ParentUuid,
+			&folder.Type,
+			&folder.Title,
+			&folder.Snake,
+		); err != nil {
+			log.Fatal(err)
+		}
+
+		folders = append(folders, folder)
+	}
+
+	return
+}
+
+func (c connection) TakeChildFolderBySnake(parentUuid string, snake string) (folder Folder, err error){
+	db, err := sql.Open("mysql", c.dBOpenStr)
+	if err != nil {
+		return
+	}
+	defer db.Close()
+
+	var act_query string
+
+	act_query = fmt.Sprintf("SELECT * FROM `%s`.`folders` as `folders`", c.connProperties.Db)
+	act_query += fmt.Sprintf(" WHERE `folders`.`parent_uuid` = '%s'", parentUuid)
+	act_query += fmt.Sprintf(" AND `folders`.`snake` = '%s'", snake)
+	fmt.Println(act_query)
+	rows, err := db.Query(act_query)
+	if err != nil {
+		return
+	}
+	defer rows.Close()
+
+	for rows.Next() {
+		var id interface{}
+		if err := rows.Scan(
+			&id,
+			&folder.Uuid,
+			&folder.ParentUuid,
+			&folder.Type,
+			&folder.Title,
+			&folder.Snake,
+		); err != nil {
+			log.Fatal(err)
+		}
+	}
+	return
+}
+
+func (c connection) TakeTopicMessages(parentUuid string, amount int64) (messages []ForumMessage, err error) {
+	db, err := sql.Open("mysql", c.dBOpenStr)
+	if err != nil {
+		return
+	}
+	defer db.Close()
+
+	var act_query string
+
+	act_query = fmt.Sprintf("SELECT `users`.`username`, `messages`.`time`, `messages`.`text`  FROM `%s`.`messages` as `messages`", c.connProperties.Db)
+	act_query += fmt.Sprintf(" INNER JOIN `%s`.`users` as `users` ON `users`.`idusers` = `messages`.`author`", c.connProperties.Db)
+	act_query += fmt.Sprintf(" WHERE `messages`.`parent_uuid` = '%s'", parentUuid)
+	if amount != 0 {
+		act_query += fmt.Sprintf(" LIMIT %d",  amount)
+	}
+	fmt.Println(act_query)
+	rows, err := db.Query(act_query)
+	if err != nil {
+		return
+	}
+	defer rows.Close()
+
+	for rows.Next() {
+		var message ForumMessage
+		if err := rows.Scan(
+			&message.Author,
+			&message.Time,
+			&message.Text,
+		); err != nil {
+			log.Fatal(err)
+		}
+		messages = append(messages, message)
+	}
+	return
+}
+
 /*
 func main() {
 

+ 82 - 0
cmd/web/handlers.go

@@ -6,6 +6,7 @@ import (
 
 	"net/http"
 	"strconv"
+	"strings"
 
 	"github.com/gorilla/mux"
 
@@ -274,3 +275,84 @@ func DownloadTorrentHandler(w http.ResponseWriter, r *http.Request) {
 	w.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s.torrent\"", torrent.Name))
 	http.ServeFile(w, r, fmt.Sprintf("files/torrents/%s.torrent", torrent.Hash))
 }
+
+
+func ForumHandler(w http.ResponseWriter, r *http.Request) {
+	forumpath := mux.Vars(r)["rest"]
+	w.Write([]byte("now at forum\n"))
+	fmt.Println(len(forumpath))
+	if len(forumpath) == 0 {
+		// Serve forum root
+		w.Write([]byte("forum root\n"))
+		rootUuid := "7e92ebbd-750f-4a34-ad62-bc6a4f47cbfa"
+		folders, err := dBConnector.TakeChildFolders(rootUuid, 10)
+		if err != nil {
+			w.Write([]byte("error getting folders from server"))
+			fmt.Println("ERROR:", err)
+		}
+		for _, folder := range folders {
+			w.Write([]byte(fmt.Sprintf("<a href=%s>%s</a>\n", (r.URL.String()+"/"+folder.Snake), folder.Title)))
+		}
+		return
+	}
+	if len(forumpath) > 0 {
+		// Serve subfolders
+		dirs := strings.Split(forumpath,"/")
+
+		//var currentUuid string
+		var parentUuid string
+		parentUuid = "7e92ebbd-750f-4a34-ad62-bc6a4f47cbfa"
+
+		var folder Folder
+		for _, dir :=range dirs {
+			var err error
+			folder, err = dBConnector.TakeChildFolderBySnake(parentUuid, dir)
+			if err != nil {
+				fmt.Println("ERROR:", err)
+				return
+			}
+			if folder == (Folder{}) {
+				// Child not found
+				w.Write([]byte(fmt.Sprintf("\nNot found %v", dir)))
+				return
+			}
+			parentUuid = folder.Uuid
+
+			w.Write([]byte(fmt.Sprintf("/%v", dir)))
+		}
+
+		w.Write([]byte(fmt.Sprintf("\nType of %v is: %d\n", folder.Title, folder.Type)))
+
+		if folder.Type == 1 {
+			// Load subfolders
+			folders, err := dBConnector.TakeChildFolders(folder.Uuid, 10)
+			if err != nil {
+				w.Write([]byte("error getting folders from server"))
+				fmt.Println("ERROR:", err)
+			}
+			for _, folder := range folders {
+				w.Write([]byte(fmt.Sprintf("<a href=%s>%s type:%d</a>\n", (r.URL.String()+"/"+folder.Snake), folder.Title, folder.Type)))
+			}
+		}
+
+		if folder.Type == 2 {
+			// Load messages
+			messages, err := dBConnector.TakeTopicMessages(folder.Uuid, 0)
+			if err != nil {
+				w.Write([]byte("error getting messages from server"))
+				fmt.Println("ERROR:", err)
+			}
+
+			if len(messages) == 0 {
+				// Should be imposible
+				w.Write([]byte("No messages in this topic"))
+				return
+			}
+
+			for _, message := range messages {
+				w.Write([]byte(fmt.Sprintf("Author:\n%s\n Time:\n %s\n Text:\n %s\n\n", message.Author, message.Time, message.Text)))
+			}
+		}
+		return
+	}
+}

+ 2 - 0
cmd/web/main.go

@@ -89,6 +89,8 @@ func main() {
 	requestRouter.HandleFunc("/game/", gamePostHandler).Methods("POST")
 
 	requestRouter.HandleFunc("/files/torrents/{hash}.torrent", DownloadTorrentHandler).Methods("GET")
+	requestRouter.HandleFunc("/forum", ForumHandler).Methods("GET")
+	requestRouter.HandleFunc(`/forum/{rest:[a-zA-Zа-яА-Я0-9=\-\/\_\.]+}`, ForumHandler).Methods("GET")
 
 	fs := justFilesFilesystem{http.Dir("resources/")}
 	http.Handle("/resources/", http.StripPrefix("/resources/", http.FileServer(fs)))

+ 3 - 3
ui/templates/newindex.html

@@ -28,7 +28,7 @@
                     </div>
                     <div class="container h-full">
                         <div class="container h-full p-2">
-                            <a href="torrents.html">
+                            <a href="/forum/torrents">
                                 <div class="container h-full rounded-md hover:bg-indigo-600 hover:bg-opacity-20 hover:text-gray-200">
                                     <div class="container py-2">
                                         Торренты
@@ -50,7 +50,7 @@
                      </div>
                      <div class="container h-full">
                         <div class="container h-full p-2">
-                            <a href="news.html">
+                            <a href="/forum/news">
                                 <div class="container h-full rounded-md hover:bg-indigo-600 hover:bg-opacity-20 hover:text-gray-200">
                                     <div class="container py-2">
                                         Новости
@@ -61,7 +61,7 @@
                      </div>
                      <div class="container h-full">
                         <div class="container h-full p-2">
-                            <a href="forum.html">
+                            <a href="forum">
                                 <div class="container h-full rounded-md hover:bg-indigo-600 hover:bg-opacity-20 hover:text-gray-200">
                                     <div class="container py-2">
                                         Форум