tangled
alpha
login
or
join now
tom.sherman.is
/
piper
0
fork
atom
A fork of https://github.com/teal-fm/piper
0
fork
atom
overview
issues
pulls
pipelines
Grabs the current record for swap on put
baileytownsend.dev
5 months ago
e75c522b
7102bf1f
+40
-6
1 changed file
expand all
collapse all
unified
split
service
playingnow
playingnow.go
+40
-6
service/playingnow/playingnow.go
···
11
"github.com/bluesky-social/indigo/api/atproto"
12
lexutil "github.com/bluesky-social/indigo/lex/util"
13
"github.com/bluesky-social/indigo/xrpc"
0
14
"github.com/spf13/viper"
15
"github.com/teal-fm/piper/api/teal"
16
"github.com/teal-fm/piper/db"
···
85
Item: playView,
86
}
87
0
0
0
0
0
0
0
0
88
// Create the record input
89
input := atproto.RepoPutRecord_Input{
90
Collection: "fm.teal.alpha.actor.status",
91
Repo: sess.DID,
92
Rkey: "self", // Use "self" as the record key for current status
93
Record: &lexutil.LexiconTypeDecoder{Val: status},
0
94
}
95
-
96
-
authArgs := db.AtpSessionToAuthArgs(sess)
97
98
// Submit to PDS
99
var out atproto.RepoPutRecord_Output
···
157
Item: emptyPlayView,
158
}
159
0
0
0
0
0
0
0
0
160
// Update the record
161
-
//TODO this is failing with InvalidSwap: Record was at "prevouis cid"
162
-
//2025/09/22 08:03:29 spotify: Updated!
163
input := atproto.RepoPutRecord_Input{
164
Collection: "fm.teal.alpha.actor.status",
165
Repo: sess.DID,
166
Rkey: "self",
167
Record: &lexutil.LexiconTypeDecoder{Val: status},
0
168
}
169
170
-
authArgs := db.AtpSessionToAuthArgs(sess)
171
-
172
var out atproto.RepoPutRecord_Output
173
if err := xrpcClient.Do(ctx, authArgs, xrpc.Procedure, "application/json", "com.atproto.repo.putRecord", nil, input, &out); err != nil {
174
p.logger.Printf("Error clearing playing now status for DID %s: %v", did, err)
···
250
251
return playView, nil
252
}
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
···
11
"github.com/bluesky-social/indigo/api/atproto"
12
lexutil "github.com/bluesky-social/indigo/lex/util"
13
"github.com/bluesky-social/indigo/xrpc"
14
+
oauth "github.com/haileyok/atproto-oauth-golang"
15
"github.com/spf13/viper"
16
"github.com/teal-fm/piper/api/teal"
17
"github.com/teal-fm/piper/db"
···
86
Item: playView,
87
}
88
89
+
var swapRecord *string
90
+
authArgs := db.AtpSessionToAuthArgs(sess)
91
+
92
+
swapRecord, err = p.getStatusSwapRecord(ctx, xrpcClient, sess, authArgs)
93
+
if err != nil {
94
+
return err
95
+
}
96
+
97
// Create the record input
98
input := atproto.RepoPutRecord_Input{
99
Collection: "fm.teal.alpha.actor.status",
100
Repo: sess.DID,
101
Rkey: "self", // Use "self" as the record key for current status
102
Record: &lexutil.LexiconTypeDecoder{Val: status},
103
+
SwapRecord: swapRecord,
104
}
0
0
105
106
// Submit to PDS
107
var out atproto.RepoPutRecord_Output
···
165
Item: emptyPlayView,
166
}
167
168
+
authArgs := db.AtpSessionToAuthArgs(sess)
169
+
170
+
var swapRecord *string
171
+
swapRecord, err = p.getStatusSwapRecord(ctx, xrpcClient, sess, authArgs)
172
+
if err != nil {
173
+
return err
174
+
}
175
+
176
// Update the record
0
0
177
input := atproto.RepoPutRecord_Input{
178
Collection: "fm.teal.alpha.actor.status",
179
Repo: sess.DID,
180
Rkey: "self",
181
Record: &lexutil.LexiconTypeDecoder{Val: status},
182
+
SwapRecord: swapRecord,
183
}
184
0
0
185
var out atproto.RepoPutRecord_Output
186
if err := xrpcClient.Do(ctx, authArgs, xrpc.Procedure, "application/json", "com.atproto.repo.putRecord", nil, input, &out); err != nil {
187
p.logger.Printf("Error clearing playing now status for DID %s: %v", did, err)
···
263
264
return playView, nil
265
}
266
+
267
+
// getStatusSwapRecord retrieves the current swap record (CID) for the actor status record.
268
+
// Returns (nil, nil) if the record does not exist yet.
269
+
func (p *PlayingNowService) getStatusSwapRecord(ctx context.Context, xrpcClient *oauth.XrpcClient, sess *models.ATprotoAuthSession, authArgs *oauth.XrpcAuthedRequestArgs) (*string, error) {
270
+
getOutput := atproto.RepoGetRecord_Output{}
271
+
if err := xrpcClient.Do(ctx, authArgs, xrpc.Query, "application/json", "com.atproto.repo.getRecord", map[string]any{
272
+
"repo": sess.DID,
273
+
"collection": "fm.teal.alpha.actor.status",
274
+
"rkey": "self",
275
+
}, nil, &getOutput); err != nil {
276
+
xErr, ok := err.(*xrpc.Error)
277
+
if !ok {
278
+
return nil, fmt.Errorf("could not get record: %w", err)
279
+
}
280
+
if xErr.StatusCode != 400 { // 400 means not found in this API
281
+
return nil, fmt.Errorf("could not get record: %w", err)
282
+
}
283
+
return nil, nil
284
+
}
285
+
return getOutput.Cid, nil
286
+
}