Skip to content

Commit e2e3c76

Browse files
committed
feat: implement posting
1 parent 91c2de4 commit e2e3c76

5 files changed

Lines changed: 75 additions & 20 deletions

File tree

bluesky/blueskyapi.go

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ type ThreadRoot struct {
158158

159159
// Reposting/Retweeting
160160
type CreateRecordPayload struct {
161-
Collection string `json:"collection"`
162-
Repo string `json:"repo"`
163-
Record RepostRecord `json:"record"`
161+
Collection string `json:"collection"`
162+
Repo string `json:"repo"`
163+
Record interface{} `json:"record"`
164164
}
165165

166166
type DeleteRecordPayload struct {
@@ -169,12 +169,18 @@ type DeleteRecordPayload struct {
169169
RKey string `json:"rkey"`
170170
}
171171

172-
type RepostRecord struct {
172+
type PostInteractionRecord struct {
173173
Type string `json:"$type"`
174174
CreatedAt string `json:"createdAt"`
175175
Subject Subject `json:"subject"`
176176
}
177177

178+
type CreatePostRecord struct {
179+
Type string `json:"$type"`
180+
Text string `json:"text"`
181+
CreatedAt time.Time `json:"createdAt"`
182+
}
183+
178184
type Subject struct {
179185
URI string `json:"uri"`
180186
CID string `json:"cid"`
@@ -422,7 +428,7 @@ func GetTimeline(token string, context string) (error, *Timeline) {
422428
func GetPost(token string, uri string, depth int, parentHeight int) (error, *ThreadRoot) {
423429
// Example URL at://did:plc:dqibjxtqfn6hydazpetzr2w4/app.bsky.feed.post/3lchbospvbc2j
424430

425-
url := "https://public.bsky.social/xrpc/app.bsky.feed.getPostThread?depth=" + fmt.Sprintf("%d", depth) + "&parentHeight=" + fmt.Sprintf("%d", parentHeight) + "&uri=" + uri
431+
url := "https://bsky.social/xrpc/app.bsky.feed.getPostThread?depth=" + fmt.Sprintf("%d", depth) + "&parentHeight=" + fmt.Sprintf("%d", parentHeight) + "&uri=" + uri
426432

427433
resp, err := SendRequest(&token, http.MethodGet, url, nil)
428434
if err != nil {
@@ -451,23 +457,60 @@ func GetPost(token string, uri string, depth int, parentHeight int) (error, *Thr
451457
return nil, &thread
452458
}
453459

454-
func UpdateStatus(token string, status string) error {
460+
// This handles both normal & replys
461+
func UpdateStatus(token string, my_did string, status string, in_reply_to *string) (*ThreadRoot, error) {
455462
url := "https://public.bsky.social/xrpc/com.atproto.repo.createRecord"
456463

457-
resp, err := SendRequest(&token, http.MethodPost, url, nil)
464+
reqBody := []byte{}
465+
var err error
466+
467+
if in_reply_to == nil || *in_reply_to == "" {
468+
469+
payload := CreateRecordPayload{
470+
Collection: "app.bsky.feed.post",
471+
Repo: my_did,
472+
Record: CreatePostRecord{
473+
Type: "app.bsky.feed.post",
474+
Text: status,
475+
CreatedAt: time.Now().UTC(),
476+
},
477+
}
478+
479+
reqBody, err = json.Marshal(payload)
480+
if err != nil {
481+
return nil, errors.New("failed to marshal payload")
482+
}
483+
484+
} else {
485+
return nil, errors.New("in_reply_to not implemented")
486+
}
487+
488+
resp, err := SendRequest(&token, http.MethodPost, url, bytes.NewReader(reqBody))
458489
if err != nil {
459-
return err
490+
return nil, errors.New("failed to post")
460491
}
492+
461493
defer resp.Body.Close()
462494

463495
if resp.StatusCode != http.StatusOK {
464496
bodyBytes, _ := io.ReadAll(resp.Body)
465497
bodyString := string(bodyBytes)
466498
fmt.Println("Response Status:", resp.StatusCode)
467499
fmt.Println("Response Body:", bodyString)
468-
return nil
500+
return nil, errors.New("failed to update status")
501+
}
502+
503+
postData := CreateRecordResult{}
504+
if err := json.NewDecoder(resp.Body).Decode(&postData); err != nil {
505+
return nil, err
469506
}
470-
return errors.New("failed to update status")
507+
508+
err, thread := GetPost(token, postData.URI, 0, 1)
509+
if err != nil {
510+
return nil, errors.New("failed to fetch made post")
511+
}
512+
513+
return thread, nil
471514
}
472515

473516
func ReTweet(token string, id string, my_did string) (error, *ThreadRoot, *string) {
@@ -481,7 +524,7 @@ func ReTweet(token string, id string, my_did string) (error, *ThreadRoot, *strin
481524
payload := CreateRecordPayload{
482525
Collection: "app.bsky.feed.repost",
483526
Repo: my_did,
484-
Record: RepostRecord{
527+
Record: PostInteractionRecord{
485528
Type: "app.bsky.feed.repost",
486529
CreatedAt: time.Now().UTC().Format(time.RFC3339),
487530
Subject: Subject{
@@ -529,7 +572,7 @@ func LikePost(token string, id string, my_did string) (error, *ThreadRoot) {
529572
payload := CreateRecordPayload{
530573
Collection: "app.bsky.feed.like",
531574
Repo: my_did,
532-
Record: RepostRecord{
575+
Record: PostInteractionRecord{
533576
Type: "app.bsky.feed.like",
534577
CreatedAt: time.Now().UTC().Format(time.RFC3339),
535578
Subject: Subject{

bridge/bridge.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Retweet struct {
3636
RetweetedStatus Tweet `json:"retweeted_status"`
3737
}
3838

39+
// https://web.archive.org/web/20120708212016/https://dev.twitter.com/docs/platform-objects/tweets
3940
type Tweet struct {
4041
Coordinates interface{} `json:"coordinates"`
4142
Favourited bool `json:"favorited"`

twitterv1/interaction.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,32 @@ import (
1212

1313
// https://web.archive.org/web/20120508224719/https://dev.twitter.com/docs/api/1/post/statuses/update
1414
func status_update(c *fiber.Ctx) error {
15-
_, _, oauthToken, err := GetAuthFromReq(c)
15+
my_did, _, oauthToken, err := GetAuthFromReq(c)
1616

1717
if err != nil {
1818
return c.Status(fiber.StatusUnauthorized).SendString("OAuth token not found in Authorization header")
1919
}
2020

2121
status := c.FormValue("status")
2222
trim_user := c.FormValue("trim_user")
23-
in_reply_to_status_id := c.FormValue("in_reply_to_status_ids")
23+
in_reply_to_status_id := c.FormValue("in_reply_to_status_id")
2424

2525
fmt.Println("Status:", status)
2626
fmt.Println("TrimUser:", trim_user)
2727
fmt.Println("InReplyToStatusID:", in_reply_to_status_id)
2828

29-
if err := blueskyapi.UpdateStatus(*oauthToken, status); err != nil {
29+
thread, err := blueskyapi.UpdateStatus(*oauthToken, *my_did, status, &in_reply_to_status_id)
30+
31+
if err != nil {
3032
fmt.Println("Error:", err)
3133
return c.Status(fiber.StatusInternalServerError).SendString("Failed to update status")
3234
}
3335

34-
// TODO: Implement this
35-
36-
return c.SendString("Not implemented")
36+
if thread.Thread.Parent == nil {
37+
return c.JSON(TranslatePostToTweet(thread.Thread.Post, "", "", nil, nil))
38+
} else {
39+
return c.JSON(TranslatePostToTweet(thread.Thread.Post, thread.Thread.Parent.URI, thread.Thread.Parent.Author.DID, &thread.Thread.Parent.Record.CreatedAt, nil))
40+
}
3741
}
3842

3943
// https://web.archive.org/web/20120407091252/https://dev.twitter.com/docs/api/1/post/statuses/retweet/%3Aid

twitterv1/post_viewing.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,12 @@ func GetStatusFromId(c *fiber.Ctx) error {
165165
return err
166166
}
167167

168-
return c.JSON(TranslatePostToTweet(thread.Thread.Post, "", "", nil, nil)) // TODO: Some things may be needed for reposts to show up correctly. thats a later problem :)
168+
// TODO: Some things may be needed for reposts to show up correctly. thats a later problem :)
169+
if thread.Thread.Parent == nil {
170+
return c.JSON(TranslatePostToTweet(thread.Thread.Post, "", "", nil, nil))
171+
} else {
172+
return c.JSON(TranslatePostToTweet(thread.Thread.Post, thread.Thread.Parent.URI, thread.Thread.Parent.Author.DID, &thread.Thread.Parent.Record.CreatedAt, nil))
173+
}
169174
}
170175

171176
func TranslatePostToTweet(tweet blueskyapi.Post, replyMsgBskyURI string, replyUserBskyId string, replyTimeStamp *time.Time, postReason *blueskyapi.PostReason) bridge.Tweet {

twitterv1/twitterv1.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ func InitServer() {
2828
// Auth
2929
app.Post("/oauth/access_token", access_token)
3030

31-
// Interactions
31+
// Tweeting
3232
app.Post("/1/statuses/update.json", status_update)
33+
34+
// Interactions
3335
app.Post("/1/statuses/retweet/:id.json", retweet)
3436
app.Post("/1/favorites/create/:id.json", favourite)
3537
app.Post("/1/favorites/destroy/:id.json", Unfavourite)

0 commit comments

Comments
 (0)