Skip to content

Commit

Permalink
Merge pull request #6 from prplecake/dev-delete-bookmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
prplecake authored Apr 1, 2022
2 parents 134bb35 + 94f604e commit d9982fe
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
8 changes: 5 additions & 3 deletions bookmarks.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
)

type bookmark struct {
URL string
Content string
Account account
ID string
URL string
Content string
Account account
Visibility string
}

type account struct {
Expand Down
1 change: 1 addition & 0 deletions config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ instances:
# https://tools.splat.soy/fediverse-access-token
- accesstoken:
instanceurl: https://freeradical.zone
deletebookmarks: true
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type config struct {

type instanceConfig struct {
AccessToken, InstanceURL string
DeleteBookmarks bool
}

type pinboardConfig struct {
Expand Down
35 changes: 35 additions & 0 deletions mastodon-bookmark-sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func main() {
defer resp.Body.Close()

log.Printf("resp.StatusCode: %d", resp.StatusCode)

if debug {
buf := new(strings.Builder)
_, err = io.Copy(buf, resp.Body)
Expand All @@ -139,6 +140,40 @@ func main() {
}
log.Printf("resp.Body: %s", buf.String())
}

if instance.DeleteBookmarks && resp.StatusCode == 200 && bookmarks[i].Visibility != "private" && bookmarks[i].Visibility != "direct" {
// only delete bookmarks if successfully saved by Pinboard and
// don't delete bookmarks of private or direct statuses
instance.deleteBookmark(bookmarks[i])
}
}
}
}

func (instance *instanceConfig) deleteBookmark(status bookmark) {
apiURL := instance.InstanceURL + "/api/v1/statuses/" + status.ID + "/unbookmark"

req, err := http.NewRequest("POST", apiURL, nil)
if err != nil {
log.Fatal(err)
}

req.Header.Set("Authorization", "Bearer "+instance.AccessToken)

c := &http.Client{}

resp, err := c.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()

if debug {
buf := new(strings.Builder)
_, err = io.Copy(buf, resp.Body)
if err != nil {
log.Fatal(err)
}
log.Printf("resp.Body: %s", buf.String())
}
}

0 comments on commit d9982fe

Please sign in to comment.