...

Source file src/jschan/app/manage.go

Documentation: jschan/app

     1  package jschan
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"jschan/app/models"
     7  	"net/http"
     8  	"net/url"
     9  	"strconv"
    10  )
    11  
    12  type GetManageRecentOptions struct {
    13  	Board string
    14  }
    15  
    16  type GetManageRecentResponse []models.Post
    17  
    18  func (c *Client) GetManageRecent(ctx context.Context, options *GetManageRecentOptions) (GetManageRecentResponse, error) {
    19  
    20  	url := "/globalmanage/recent.json"
    21  	if options != nil && options.Board != "" {
    22  		url = fmt.Sprintf("%s/%s/manage/recent.json", c.BaseURL, options.Board)
    23  	}
    24  
    25  	req, err := http.NewRequest(http.MethodGet, url, nil)
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  
    30  	req = req.WithContext(ctx)
    31  
    32  	res := GetManageRecentResponse{}
    33  	if err := c.sendRequest(req, &res, nil); err != nil {
    34  		return nil, err
    35  	}
    36  
    37  	return res, nil
    38  
    39  }
    40  
    41  type GetManageReportsOptions struct {
    42  	Page  int
    43  	IP    string
    44  	Board string
    45  }
    46  
    47  type GetManageReportsResponse struct {
    48  	Reports     []models.Post `json:"reports"`
    49  	Page        int           `json:"page"`
    50  	IP          string        `json:"ip"`
    51  	QueryString string        `json:"queryString"`
    52  }
    53  
    54  func (c *Client) GetManageReports(ctx context.Context, options *GetManageReportsOptions) (*GetManageReportsResponse, error) {
    55  
    56  	query := url.Values{}
    57  	if options != nil {
    58  		if options.IP != "" {
    59  			query.Set("ip", options.IP)
    60  		}
    61  		if options.Page != 0 {
    62  			query.Set("page", strconv.Itoa(options.Page))
    63  		}
    64  	}
    65  
    66  	url := fmt.Sprintf("%s/globalmanage/reports.json", c.BaseURL)
    67  	if options != nil && options.Board != "" {
    68  		url = fmt.Sprintf("%s/%s/manage/reports.json", c.BaseURL, options.Board)
    69  	}
    70  	if len(query.Encode()) > 0 {
    71  		url = fmt.Sprintf("%s?%s", url, query.Encode())
    72  	}
    73  
    74  	req, err := http.NewRequest(http.MethodGet, url, nil)
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	req = req.WithContext(ctx)
    80  
    81  	res := GetManageReportsResponse{}
    82  	if err := c.sendRequest(req, &res, nil); err != nil {
    83  		return nil, err
    84  	}
    85  
    86  	return &res, nil
    87  
    88  }
    89  
    90  type GetManageBoardsOptions struct {
    91  	Search          string `json:"search"`
    92  	Sort            string `json:"sort"`
    93  	SortDirection   string `json:"direction"`
    94  	Page            int    `json:"page"`
    95  	FilterUnlisted  bool   `json:"filter_unlisted"`
    96  	FilterSfw       bool   `json:"filter_sfw"`
    97  	FilterAbandoned bool   `json:"filter_abandoned"`
    98  }
    99  
   100  func (c *Client) GetManageBoards(ctx context.Context, options *GetManageBoardsOptions) (*GetBoardsResponse, error) {
   101  
   102  	page := 1
   103  	search := ""
   104  	sort := "popularity"
   105  	direction := "desc"
   106  	filter_unlisted := false
   107  	filter_sfw := false
   108  	filter_abandoned := false
   109  	if options != nil {
   110  		search = options.Search
   111  		sort = options.Sort
   112  		direction = options.SortDirection
   113  		filter_unlisted = options.FilterUnlisted
   114  		filter_sfw = options.FilterSfw
   115  		filter_abandoned = options.FilterAbandoned
   116  		page = options.Page
   117  	}
   118  
   119  	query := url.Values{}
   120  	query.Set("search", search)
   121  	query.Set("page", fmt.Sprintf("%d", page))
   122  	query.Set("sort", sort)
   123  	query.Set("direction", direction)
   124  	if filter_unlisted {
   125  		query.Set("filter_unlisted", "true")
   126  	}
   127  	if filter_sfw {
   128  		query.Set("filter_sfw", "true")
   129  	}
   130  	if filter_abandoned {
   131  		query.Set("filter_abandoned", "true")
   132  	}
   133  
   134  	url := fmt.Sprintf("%s/boards.json?%s", c.BaseURL, query.Encode())
   135  
   136  	req, err := http.NewRequest(http.MethodGet, url, nil)
   137  
   138  	if err != nil {
   139  		return nil, err
   140  	}
   141  
   142  	req = req.WithContext(ctx)
   143  
   144  	res := GetBoardsResponse{}
   145  	if err := c.sendRequest(req, &res, nil); err != nil {
   146  		return nil, err
   147  	}
   148  
   149  	return &res, nil
   150  
   151  }
   152  

View as plain text