76 lines
2.0 KiB
Go
76 lines
2.0 KiB
Go
package reward
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/wangjia/pangolin/server/internal/apierr"
|
|
"github.com/wangjia/pangolin/server/internal/auth"
|
|
)
|
|
|
|
const inviteLinkBase = "https://pangolin.yanmeiai.com/i/"
|
|
|
|
type Handler struct {
|
|
svc *Service
|
|
st *Store
|
|
tgEnabled bool
|
|
channel string
|
|
}
|
|
|
|
func NewHandler(s *Service, st *Store, tgEnabled bool, channel string) *Handler {
|
|
return &Handler{svc: s, st: st, tgEnabled: tgEnabled, channel: channel}
|
|
}
|
|
|
|
func (h *Handler) GetInvite(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
uid, ok := auth.UserIDFromContext(ctx)
|
|
if !ok {
|
|
apierr.WriteJSON(w, http.StatusUnauthorized, apierr.ErrUnauthorized)
|
|
return
|
|
}
|
|
code, err := h.svc.EnsureCode(ctx, uid)
|
|
if err != nil {
|
|
apierr.WriteJSON(w, http.StatusInternalServerError, apierr.ErrInternal)
|
|
return
|
|
}
|
|
invited, converted, earned, err := h.st.Summary(ctx, uid)
|
|
if err != nil {
|
|
apierr.WriteJSON(w, http.StatusInternalServerError, apierr.ErrInternal)
|
|
return
|
|
}
|
|
joined, _ := h.st.TelegramClaimed(ctx, uid)
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
|
"invite_code": code,
|
|
"invite_link": inviteLinkBase + code,
|
|
"invited": invited,
|
|
"converted": converted,
|
|
"earned_days": earned,
|
|
"telegram": map[string]any{
|
|
"enabled": h.tgEnabled,
|
|
"joined": joined,
|
|
"channel": h.channel,
|
|
},
|
|
})
|
|
}
|
|
|
|
func (h *Handler) TelegramStart(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
uid, ok := auth.UserIDFromContext(ctx)
|
|
if !ok {
|
|
apierr.WriteJSON(w, http.StatusUnauthorized, apierr.ErrUnauthorized)
|
|
return
|
|
}
|
|
if !h.tgEnabled {
|
|
apierr.WriteJSON(w, http.StatusNotFound, apierr.ErrNotFound)
|
|
return
|
|
}
|
|
tok, err := h.svc.IssueTelegramToken(ctx, uid)
|
|
if err != nil {
|
|
apierr.WriteJSON(w, http.StatusInternalServerError, apierr.ErrInternal)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"deep_link": h.svc.TelegramDeepLink(tok)})
|
|
}
|