6
1
mirror of https://git.mills.io/saltyim/saltyim.git synced 2024-06-28 09:41:02 +00:00
prologic-saltyim/internal/api.go
mlctrez 53e79b449b chat_window_formatting (#102)
![image](/attachments/8d560ad1-bcf7-44c6-940e-3e386f9cc2b6)

trying out message formatting in the style of text message chatting where incoming is left justified and send messages are right justified

only the last message will have a timestamp

WIP until onClick displays timestamp of message

Co-authored-by: mlctrez <mlctrez@gmail.com>
Reviewed-on: https://git.mills.io/saltyim/saltyim/pulls/102
Reviewed-by: xuu <xuu@noreply@mills.io>
Co-authored-by: mlctrez <mlctrez@noreply@mills.io>
Co-committed-by: mlctrez <mlctrez@noreply@mills.io>
2022-03-31 21:38:34 +00:00

81 lines
1.9 KiB
Go

package internal
import (
"net/http"
"github.com/julienschmidt/httprouter"
log "github.com/sirupsen/logrus"
"github.com/unrolled/render"
"go.mills.io/saltyim"
)
// API ...
type API struct {
router *Router
config *Config
db Store
r *render.Render
}
// NewAPI ...
func NewAPI(router *Router, config *Config, db Store) *API {
api := &API{router, config, db, render.New()}
api.initRoutes()
return api
}
func (a *API) initRoutes() {
router := a.router.Group("/api/v1")
router.GET("/ping", a.PingEndpoint())
router.GET("/lookup/:addr", a.LookupEndpoint())
router.POST("/send", a.SendEndpoint())
}
// PingEndpoint ...
func (a *API) PingEndpoint() httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{}`))
}
}
// LookupEndpoint ...
func (a *API) LookupEndpoint() httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
addr, err := saltyim.LookupAddr(p.ByName("addr"))
if err != nil {
http.Error(w, "Lookup Error", http.StatusBadRequest)
return
}
a.r.JSON(w, http.StatusOK, addr)
}
}
// SendEndpoint ...
func (a *API) SendEndpoint() httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
req, signer, err := saltyim.NewSendRequest(r.Body)
if err != nil {
log.WithError(err).Error("error parsing send request")
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
// TODO: Do something with signer?
log.Debugf("request signed by %s", signer)
// TODO: Queue up an internal retry and return immediately on failure?
if err := saltyim.Send(req.Endpoint, req.Message, req.Capabilities); err != nil {
log.WithError(err).Errorf("error sending message to %s", req.Endpoint)
http.Error(w, "Send Error", http.StatusInternalServerError)
return
}
http.Error(w, "Message Accepted", http.StatusAccepted)
}
}