Delete records and stuff
This commit is contained in:
48
data/repo.go
48
data/repo.go
@@ -23,12 +23,10 @@ package data
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"git.bullercodeworks.com/brian/expds/data/models"
|
||||
"github.com/bluesky-social/indigo/atproto/syntax"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
@@ -66,52 +64,6 @@ func NewRepo() (*Repo, error) {
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (r *Repo) fetchPds(atId string) (*models.Pds, error) {
|
||||
p, err := models.NewPdsFromDid(atId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.LoadedPDSs[atId] = p
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (r *Repo) ReloadPds(atId string) (*models.Pds, error) {
|
||||
return r.fetchPds(atId)
|
||||
}
|
||||
|
||||
func (r *Repo) GetPDS(atId string) (*models.Pds, error) {
|
||||
if p, ok := r.LoadedPDSs[atId]; ok && time.Since(p.RefreshTime) < r.BestBy {
|
||||
return p, nil
|
||||
}
|
||||
return r.fetchPds(atId)
|
||||
}
|
||||
|
||||
func (r *Repo) SendToPDS(did syntax.DID) error {
|
||||
session, err := r.Auth.GetSession(did)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c := session.APIClient()
|
||||
body := map[string]any{
|
||||
"repo": c.AccountDID.String(),
|
||||
"collection": "com.bullercodeworks.expds.status",
|
||||
"record": map[string]any{
|
||||
"$type": "com.bullercodeworks.expds.status",
|
||||
"text": "writeable",
|
||||
"createdAt": syntax.DatetimeNow(),
|
||||
},
|
||||
}
|
||||
var resp struct {
|
||||
Uri syntax.ATURI `json:"uri"`
|
||||
}
|
||||
r.Logger.Debug("posting expds status...")
|
||||
if err := c.Post(r.context, "com.atproto.repo.CreateRecord", body, &resp); err != nil {
|
||||
return err
|
||||
}
|
||||
r.Logger.Debug(fmt.Sprintf("posted: %s :: %s", resp.Uri.Authority(), resp.Uri.RecordKey()))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Repo) SetLogFunc(l func(string, ...any)) {
|
||||
r.logFunc = l
|
||||
r.handler = NewAppLogHandler(r.logFunc)
|
||||
|
||||
@@ -26,6 +26,7 @@ type AuthRepo struct {
|
||||
context context.Context
|
||||
|
||||
session *oauth.ClientSessionData
|
||||
authUrl string
|
||||
authError error
|
||||
|
||||
Logger *slog.Logger
|
||||
@@ -58,6 +59,7 @@ func (r *AuthRepo) buildOAuthClient() (*oauth.ClientConfig, *oauth.ClientApp, *S
|
||||
SessionExpiryDuration: time.Hour * 24 * 90,
|
||||
SessionInactivityDuration: time.Hour * 24 * 14,
|
||||
AuthRequestExpiryDuration: time.Minute * 30,
|
||||
Logger: r.Logger,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
@@ -68,19 +70,20 @@ func (r *AuthRepo) buildOAuthClient() (*oauth.ClientConfig, *oauth.ClientApp, *S
|
||||
}
|
||||
|
||||
func (r *AuthRepo) StartAuthFlow(port int, identifier string, callbackRes chan url.Values) (string, error) {
|
||||
var err error
|
||||
r.oauthConfig.CallbackURL = fmt.Sprintf("http://127.0.0.1:%d/callback", port)
|
||||
authUrl, err := r.oauthClient.StartAuthFlow(r.context, identifier)
|
||||
r.authUrl, err = r.oauthClient.StartAuthFlow(r.context, identifier)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error logging in: %w", err)
|
||||
}
|
||||
if !strings.HasPrefix(authUrl, "https://") {
|
||||
if !strings.HasPrefix(r.authUrl, "https://") {
|
||||
return "", fmt.Errorf("non-https authUrl")
|
||||
}
|
||||
go func() {
|
||||
exec.Command("xdg-open", authUrl).Start()
|
||||
exec.Command("xdg-open", r.authUrl).Start()
|
||||
r.session, r.authError = r.oauthClient.ProcessCallback(r.context, <-callbackRes)
|
||||
}()
|
||||
return authUrl, nil
|
||||
return r.authUrl, nil
|
||||
}
|
||||
|
||||
// Follows XDG conventions and creates the directories if necessary.
|
||||
@@ -101,6 +104,12 @@ func (r *AuthRepo) ListenForCallback(res chan url.Values) (int, error) {
|
||||
Handler: mux,
|
||||
}
|
||||
|
||||
mux.HandleFunc("/auth", func(w http.ResponseWriter, req *http.Request) {
|
||||
w.Header().Set("Location", r.authUrl)
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.WriteHeader(303)
|
||||
})
|
||||
|
||||
mux.HandleFunc("/callback", func(w http.ResponseWriter, req *http.Request) {
|
||||
res <- req.URL.Query()
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
@@ -130,6 +139,6 @@ func (r *AuthRepo) GetSession(did syntax.DID) (*oauth.ClientSession, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error getting most recent session: %w", err)
|
||||
}
|
||||
r.Logger.Warn(fmt.Sprintf("GetSession(): Resuming Session: %s (%s)", sess.SessionID, sess.AccountDID))
|
||||
r.Logger.Debug(fmt.Sprintf("GetSession(): Resuming Session: %s (%s)", sess.SessionID, sess.AccountDID))
|
||||
return r.oauthClient.ResumeSession(r.context, sess.AccountDID, sess.SessionID)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,9 @@ package data
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"log/slog"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/bluesky-social/indigo/atproto/auth/oauth"
|
||||
@@ -10,6 +13,7 @@ import (
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
// Taken from https://github.com/bluesky-social/cookbook/blob/main/go-oauth-cli-app/sqlitestore.go
|
||||
@@ -21,6 +25,7 @@ type SqliteStoreConfig struct {
|
||||
SessionExpiryDuration time.Duration // duration since session creation
|
||||
SessionInactivityDuration time.Duration // duration since last session update
|
||||
AuthRequestExpiryDuration time.Duration // duration since auth request creation
|
||||
Logger *slog.Logger
|
||||
}
|
||||
|
||||
// Implements the [oauth.ClientAuthStore] interface, backed by sqlite via gorm
|
||||
@@ -65,7 +70,14 @@ func NewSqliteStore(cfg *SqliteStoreConfig) (*SqliteStore, error) {
|
||||
return nil, fmt.Errorf("missing AuthRequestExpiryDuration")
|
||||
}
|
||||
|
||||
db, err := gorm.Open(sqlite.Open(cfg.DatabasePath), &gorm.Config{})
|
||||
db, err := gorm.Open(sqlite.Open(cfg.DatabasePath), &gorm.Config{
|
||||
Logger: logger.New(
|
||||
log.New(os.Stdout, "\r\n", log.LstdFlags),
|
||||
logger.Config{
|
||||
IgnoreRecordNotFoundError: true,
|
||||
},
|
||||
),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed opening db: %w", err)
|
||||
}
|
||||
|
||||
77
data/repo_pds.go
Normal file
77
data/repo_pds.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.bullercodeworks.com/brian/expds/data/models"
|
||||
"github.com/bluesky-social/indigo/atproto/syntax"
|
||||
)
|
||||
|
||||
func (r *Repo) fetchPds(atId string) (*models.Pds, error) {
|
||||
p, err := models.NewPdsFromDid(atId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.LoadedPDSs[atId] = p
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (r *Repo) ReloadPds(atId string) (*models.Pds, error) {
|
||||
return r.fetchPds(atId)
|
||||
}
|
||||
|
||||
func (r *Repo) GetPDS(atId string) (*models.Pds, error) {
|
||||
if p, ok := r.LoadedPDSs[atId]; ok && time.Since(p.RefreshTime) < r.BestBy {
|
||||
return p, nil
|
||||
}
|
||||
return r.fetchPds(atId)
|
||||
}
|
||||
|
||||
func (r *Repo) SendToPDS(did syntax.DID) error {
|
||||
session, err := r.Auth.GetSession(did)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c := session.APIClient()
|
||||
body := map[string]any{
|
||||
"repo": c.AccountDID.String(),
|
||||
"collection": "com.bullercodeworks.expds.status",
|
||||
"record": map[string]any{
|
||||
"$type": "com.bullercodeworks.expds.status",
|
||||
"text": "writeable",
|
||||
"createdAt": syntax.DatetimeNow(),
|
||||
},
|
||||
}
|
||||
var resp struct {
|
||||
Uri syntax.ATURI `json:"uri"`
|
||||
}
|
||||
r.Logger.Debug("posting expds status...")
|
||||
if err := c.Post(r.context, "com.atproto.repo.createRecord", body, &resp); err != nil {
|
||||
return err
|
||||
}
|
||||
r.Logger.Debug("posted: %s :: %s", resp.Uri.Authority(), resp.Uri.RecordKey())
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Repo) DeleteRecord(did syntax.DID, collection syntax.NSID, rkey string) error {
|
||||
session, err := r.Auth.GetSession(did)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c := session.APIClient()
|
||||
body := map[string]any{
|
||||
"repo": c.AccountDID,
|
||||
"collection": collection,
|
||||
"rkey": rkey,
|
||||
}
|
||||
var resp struct {
|
||||
Cid syntax.CID `json:"cid"`
|
||||
Rev syntax.TID `json:"rev"`
|
||||
}
|
||||
r.Logger.Debug("deleting record (%s)", rkey)
|
||||
if err := c.Post(r.context, "com.atproto.repo.deleteRecord", body, &resp); err != nil {
|
||||
return err
|
||||
}
|
||||
r.Logger.Debug("posted: %s :: %s", resp.Cid.String(), resp.Rev.String())
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user