Something went wrong. Try again.
An implementation of the ATProto statusphere example app but in Go
Something went wrong. Try again.
statusphere-go home_handler.go
3.4 kB · 163 lines
Go
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164package statusphere
import ( "fmt" "log/slog" "net/http" "time")
var Availablestatus = []string{ "👍", "👎", "💙", "🥹", "😧", "😤", "🙃", "😉", "😎", "🤓", "🤨", "🥳", "😭", "😤", "🤯", "🫡", "💀", "✊", "🤘", "👀", "🧠", "👩💻", "🧑💻", "🥷", "🧌", "🦋", "🚀",}
type HomeData struct { DisplayName string AvailableStatus []string UsersStatus []UserStatus}
type UserStatus struct { Status string Handle string HandleURL string Date string IsToday bool}
func (s *Server) HandleHome(w http.ResponseWriter, r *http.Request) { tmpl := s.getTemplate("home.html") data := HomeData{ AvailableStatus: Availablestatus, }
did, _ := s.currentSessionDID(r) if did != nil { profile, err := s.getUserProfileForDid(did.String()) if err != nil { slog.Error("getting logged in users profile", "error", err) } data.DisplayName = profile.DisplayName }
today := time.Now().Format(time.DateOnly)
results, err := s.store.GetStatuses(10) if err != nil { slog.Error("get status'", "error", err) }
for _, status := range results { date := time.UnixMilli(status.CreatedAt).Format(time.DateOnly)
profile, err := s.getUserProfileForDid(status.Did) if err != nil { slog.Error("getting user profile for status - skipping", "error", err, "did", status.Did) continue }
data.UsersStatus = append(data.UsersStatus, UserStatus{ Status: status.Status, Handle: profile.Handle, HandleURL: fmt.Sprintf("https://bsky.app/profile/%s", status.Did), Date: date, IsToday: date == today, }) }
tmpl.Execute(w, data)}
func (s *Server) HandleStatus(w http.ResponseWriter, r *http.Request) { err := r.ParseForm() if err != nil { slog.Error("parsing form", "error", err) http.Error(w, "parsing form", http.StatusBadRequest) return }
status := r.FormValue("status") if status == "" { http.Error(w, "missing status", http.StatusBadRequest) return }
did, sessionID := s.currentSessionDID(r) if did == nil { http.Redirect(w, r, "/login", http.StatusFound) return }
oauthSess, err := s.oauthClient.ResumeSession(r.Context(), *did, sessionID) if err != nil { slog.Error("resuming session", "error", err, "did", *did, "session ID", sessionID)
// clear the session out sess, _ := s.sessionStore.Get(r, sessionStoreName) sess.Values = make(map[any]any) _ = sess.Save(r, w)
http.Redirect(w, r, "/login", http.StatusFound) return } c := oauthSess.APIClient()
createdAt := time.Now()
bodyReq := map[string]any{ "repo": c.AccountDID.String(), "collection": "xyz.statusphere.status", "record": map[string]any{ "status": status, "createdAt": createdAt, }, } var result CreateRecordResp err = c.Post(r.Context(), "com.atproto.repo.createRecord", bodyReq, &result) if err != nil { slog.Error("failed to create new status", "error", err) http.Redirect(w, r, "/", http.StatusFound) return }
statusToStore := Status{ URI: result.URI, Did: c.AccountDID.String(), Status: status, CreatedAt: createdAt.UnixMilli(), IndexedAt: time.Now().UnixMilli(), }
err = s.store.CreateStatus(statusToStore) if err != nil { slog.Error("failed to store status that has been created", "error", err) }
http.Redirect(w, r, "/", http.StatusFound)}