Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ type DirectMessages interface {
LookUpAllOneToOneDM(ctx context.Context, participantID string, opt ...*DirectMessageOption) (*LookUpAllOneToOneDMResponse, error)
LookUpDM(ctx context.Context, dmConversationID string, opt ...*DirectMessageOption) (*LookUpDMResponse, error)
LookUpAllDM(ctx context.Context, opt ...*DirectMessageOption) (*LookUpAllDMResponse, error)
// DM Blocks
PostDMBlocking(ctx context.Context, userID string, targetUserID string) (*PostDMBlockingResponse, error)
UndoDMBlocking(ctx context.Context, userID string, targetUserID string) (*UndoDMBlockingResponse, error)
}

type CommunityNotes interface {
Expand Down Expand Up @@ -677,3 +680,13 @@ func (c *Client) FinalizeChunkedUpload(ctx context.Context, req *MediaUploadFina
func (c *Client) CheckUploadStatus(ctx context.Context, req *MediaUploadStatusRequest) (*MediaUploadResponse, error) {
return checkUploadStatus(ctx, c.client, req)
}

// PostDMBlocking blocks DMs from a specific user.
func (c *Client) PostDMBlocking(ctx context.Context, userID string, targetUserID string) (*PostDMBlockingResponse, error) {
return postDMBlocking(ctx, c.client, userID, targetUserID)
}

// UndoDMBlocking unblocks DMs from a specific user.
func (c *Client) UndoDMBlocking(ctx context.Context, userID string, targetUserID string) (*UndoDMBlockingResponse, error) {
return undoDMBlocking(ctx, c.client, userID, targetUserID)
}
28 changes: 28 additions & 0 deletions direct_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,31 @@ type LookUpAllDMResponse struct {
Detail string `json:"detail,omitempty"`
Type string `json:"type,omitempty"`
}

// DMBlocking represents the DM blocking data
type DMBlocking struct {
DMBlocking bool `json:"dm_blocking"`
}

// PostDMBlockingResponse represents the response for blocking DMs from a user
type PostDMBlockingResponse struct {
DMBlocking *DMBlocking `json:"data,omitempty"`
Errors []*APIResponseError `json:"errors,omitempty"`
Title string `json:"title,omitempty"`
Detail string `json:"detail,omitempty"`
Type string `json:"type,omitempty"`
}

// UndoDMBlockingResponse represents the response for unblocking DMs from a user
type UndoDMBlockingResponse struct {
DMBlocking *DMBlocking `json:"data,omitempty"`
Errors []*APIResponseError `json:"errors,omitempty"`
Title string `json:"title,omitempty"`
Detail string `json:"detail,omitempty"`
Type string `json:"type,omitempty"`
}

// DMBlockingBody represents the request body for DM blocking operations
type DMBlockingBody struct {
TargetUserID string `json:"target_user_id"`
}
110 changes: 110 additions & 0 deletions dm_blocks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package gotwtr

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
)

func postDMBlocking(ctx context.Context, c *client, userID string, targetUserID string) (*PostDMBlockingResponse, error) {
if userID == "" {
return nil, errors.New("post dm blocking: userID parameter is required")
}
if targetUserID == "" {
return nil, errors.New("post dm blocking: targetUserID parameter is required")
}

ep := fmt.Sprintf(postDMBlockingURL, userID)

body := &DMBlockingBody{
TargetUserID: targetUserID,
}

j, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("post dm blocking json marshal: %w", err)
}

req, err := http.NewRequestWithContext(ctx, http.MethodPost, ep, bytes.NewReader(j))
if err != nil {
return nil, fmt.Errorf("post dm blocking new request with ctx: %w", err)
}

req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.bearerToken))
req.Header.Set("Content-Type", "application/json")

resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("post dm blocking response: %w", err)
}

defer func() { _ = resp.Body.Close() }()

var pdbr PostDMBlockingResponse
if err := json.NewDecoder(resp.Body).Decode(&pdbr); err != nil {
return nil, fmt.Errorf("post dm blocking decode: %w", err)
}

if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
return &pdbr, &HTTPError{
APIName: "post dm blocking",
Status: resp.Status,
URL: req.URL.String(),
}
}

return &pdbr, nil
}

func undoDMBlocking(ctx context.Context, c *client, userID string, targetUserID string) (*UndoDMBlockingResponse, error) {
if userID == "" {
return nil, errors.New("undo dm blocking: userID parameter is required")
}
if targetUserID == "" {
return nil, errors.New("undo dm blocking: targetUserID parameter is required")
}

ep := fmt.Sprintf(undoDMBlockingURL, userID)

body := &DMBlockingBody{
TargetUserID: targetUserID,
}

j, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("undo dm blocking json marshal: %w", err)
}

req, err := http.NewRequestWithContext(ctx, http.MethodDelete, ep, bytes.NewReader(j))
if err != nil {
return nil, fmt.Errorf("undo dm blocking new request with ctx: %w", err)
}

req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.bearerToken))
req.Header.Set("Content-Type", "application/json")

resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("undo dm blocking response: %w", err)
}

defer func() { _ = resp.Body.Close() }()

var udbr UndoDMBlockingResponse
if err := json.NewDecoder(resp.Body).Decode(&udbr); err != nil {
return nil, fmt.Errorf("undo dm blocking decode: %w", err)
}

if resp.StatusCode != http.StatusOK {
return &udbr, &HTTPError{
APIName: "undo dm blocking",
Status: resp.Status,
URL: req.URL.String(),
}
}

return &udbr, nil
}
Loading
Loading