github.com/Ptt-official-app/go-bbs@v0.12.0/serverlet/route_users.go (about)

     1  package main
     2  
     3  import (
     4  	// "github.com/Ptt-official-app/go-bbs"
     5  	// "github.com/Ptt-official-app/go-bbs/crypt"
     6  	"encoding/json"
     7  	"fmt"
     8  	"log"
     9  	"net/http"
    10  	"strings"
    11  	"time"
    12  )
    13  
    14  func routeUsers(w http.ResponseWriter, r *http.Request) {
    15  	if r.Method == "GET" {
    16  		getUsers(w, r)
    17  		return
    18  	}
    19  
    20  }
    21  
    22  func getUsers(w http.ResponseWriter, r *http.Request) {
    23  	userID, item, err := parseUserPath(r.URL.Path)
    24  
    25  	if item == "information" {
    26  		getUserInformation(w, r, userID)
    27  		return
    28  	} else if item == "favorites" {
    29  		getUserFavorites(w, r, userID)
    30  		return
    31  	}
    32  	// else
    33  
    34  	log.Println(userID, item, err)
    35  
    36  	w.WriteHeader(http.StatusNotFound)
    37  }
    38  
    39  func getUserInformation(w http.ResponseWriter, r *http.Request, userID string) {
    40  	token := getTokenFromRequest(r)
    41  	err := checkTokenPermission(token,
    42  		[]permission{PermissionReadUserInformation},
    43  		map[string]string{
    44  			"user_id": userID,
    45  		})
    46  
    47  	if err != nil {
    48  		// TODO: record unauthorized access
    49  		w.WriteHeader(http.StatusUnauthorized)
    50  		return
    51  	}
    52  
    53  	userrec, err := findUserecByID(userID)
    54  	if err != nil {
    55  		// TODO: record error
    56  
    57  		w.WriteHeader(http.StatusInternalServerError)
    58  		m := map[string]string{
    59  			"error":             "find_userrec_error",
    60  			"error_description": "get userrec for " + userID + " failed",
    61  		}
    62  		b, _ := json.MarshalIndent(m, "", "  ")
    63  		w.Write(b)
    64  		return
    65  	}
    66  
    67  	// TODO: Check Etag or Not-Modified for cache
    68  
    69  	dataMap := map[string]interface{}{
    70  		"user_id":              userrec.UserID(),
    71  		"nickname":             userrec.Nickname(),
    72  		"realname":             userrec.RealName(),
    73  		"number_of_login_days": fmt.Sprintf("%d", userrec.NumLoginDays()),
    74  		"number_of_posts":      fmt.Sprintf("%d", userrec.NumPosts()),
    75  		// "number_of_badposts":   fmt.Sprintf("%d", userrec.NumLoginDays),
    76  		"money":           fmt.Sprintf("%d", userrec.Money()),
    77  		"last_login_time": userrec.LastLogin().Format(time.RFC3339),
    78  		"last_login_ipv4": userrec.LastHost(),
    79  		"last_login_ip":   userrec.LastHost(),
    80  		// "last_login_country": fmt.Sprintf("%d", userrec.NumLoginDays),
    81  		"chess_status": map[string]interface{}{},
    82  		"plan":         map[string]interface{}{},
    83  	}
    84  
    85  	responseMap := map[string]interface{}{
    86  		"data": dataMap,
    87  	}
    88  
    89  	responseByte, _ := json.MarshalIndent(responseMap, "", "  ")
    90  
    91  	w.Write(responseByte)
    92  }
    93  func getUserFavorites(w http.ResponseWriter, r *http.Request, userID string) {
    94  	w.WriteHeader(http.StatusNotImplemented)
    95  }
    96  
    97  func parseUserPath(path string) (userID string, item string, err error) {
    98  	pathSegment := strings.Split(path, "/")
    99  	// /{{version}}/users/{{user_id}}/{{item}}
   100  	if len(pathSegment) == 4 {
   101  		// /{{version}}/users/{{user_id}}
   102  		return pathSegment[3], "", nil
   103  	}
   104  
   105  	return pathSegment[3], pathSegment[4], nil
   106  
   107  }