offline-twitter/internal/webserver/handler_follow_unfollow.go
Alessio 24364a26b0 REFACTOR: rework the rendering helpers
- rendering helpers moved to their own file (separate from response helpers)
- create a unified render helper instead of "buffered_render_basic_X" and "buffered_render_tweet_X"
	- this helper takes 2 data objects: one with global data (tweet trove, logged in user, etc) and one page-specific
	- this lets us remove the disgusting interface type
- modify the User List template to use UserIDs indexing into a global data object instead of a list of Users
2023-12-31 15:56:12 -06:00

56 lines
1.3 KiB
Go

package webserver
import (
"net/http"
"strings"
"gitlab.com/offline-twitter/twitter_offline_engine/pkg/scraper"
)
func (app *Application) UserFollow(w http.ResponseWriter, r *http.Request) {
app.traceLog.Printf("'UserFollow' handler (path: %q)", r.URL.Path)
if r.Method != "POST" {
http.Error(w, "Method not allowed", 405)
return
}
parts := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
if len(parts) != 2 {
app.error_400_with_message(w, "Bad URL: "+r.URL.Path)
return
}
user, err := app.Profile.GetUserByHandle(scraper.UserHandle(parts[1]))
if err != nil {
app.error_404(w)
return
}
app.Profile.SetUserFollowed(&user, true)
app.buffered_render_htmx(w, "following-button", PageGlobalData{}, user)
}
func (app *Application) UserUnfollow(w http.ResponseWriter, r *http.Request) {
app.traceLog.Printf("'UserUnfollow' handler (path: %q)", r.URL.Path)
if r.Method != "POST" {
http.Error(w, "Method not allowed", 405)
return
}
parts := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
if len(parts) != 2 {
app.error_400_with_message(w, "Bad URL: "+r.URL.Path)
return
}
user, err := app.Profile.GetUserByHandle(scraper.UserHandle(parts[1]))
if err != nil {
app.error_404(w)
return
}
app.Profile.SetUserFollowed(&user, false)
app.buffered_render_htmx(w, "following-button", PageGlobalData{}, user)
}