summary refs log blame commit diff stats
path: root/svc/post.go
blob: 90508591c43a6c79347831aa96f919dc9e13dcd2 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
                                                      














                                                             
                                                                                                         

                      
 


                                       
                                                                                           






                                                           
                                                                                                              


                      

                               


                                                                           
 
                                                                            


                                                                                                                                    
                 
 


                                                                         
 



                                                                                                                       
 






                                                                          
 
package svc // import "github.com/getwtxt/getwtxt/svc"

import (
	"fmt"
	"net/http"

	"github.com/getwtxt/registry"
)

// Requests to apiEndpointPOSTHandler are passed off to this
// function. apiPostUser then fetches the twtxt data, then if
// it's an individual user's file, adds it. If it's registry
// output, it scrapes the users/urls/statuses from the remote
// registry before adding each user to the local cache.
func apiPostUser(w http.ResponseWriter, r *http.Request) {
	if err := r.ParseForm(); err != nil {
		errHTTP(w, r, fmt.Errorf("error parsing values: %v", err.Error()), http.StatusBadRequest)
		return
	}

	nick := r.FormValue("nickname")
	urls := r.FormValue("url")
	if nick == "" || urls == "" {
		errHTTP(w, r, fmt.Errorf("nickname or URL missing"), http.StatusBadRequest)
		return
	}

	uip := getIPFromCtx(r.Context())

	out, remoteRegistry, err := registry.GetTwtxt(urls)
	if err != nil {
		errHTTP(w, r, fmt.Errorf("error fetching twtxt Data: %v", err.Error()), http.StatusBadRequest)
		return
	}

	switch remoteRegistry {
	case true:
		remoteRegistries.Mu.Lock()
		remoteRegistries.List = append(remoteRegistries.List, urls)
		remoteRegistries.Mu.Unlock()

		if err := twtxtCache.CrawlRemoteRegistry(urls); err != nil {
			errHTTP(w, r, fmt.Errorf("error crawling remote registry: %v", err.Error()), http.StatusInternalServerError)
		} else {
			log200(r)
		}

	case false:
		statuses, err := registry.ParseUserTwtxt(out, nick, urls)
		errLog("Error Parsing User Data: ", err)

		if err := twtxtCache.AddUser(nick, urls, "", uip, statuses); err != nil {
			errHTTP(w, r, fmt.Errorf("error adding user to cache: %v", err.Error()), http.StatusBadRequest)
			return
		}

		_, err = w.Write([]byte(fmt.Sprintf("200 OK\n")))
		if err != nil {
			errHTTP(w, r, err, http.StatusInternalServerError)
		} else {
			log200(r)
		}
	}
}