github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/tequilapi/endpoints/feedback.go (about)

     1  /*
     2   * Copyright (C) 2019 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package endpoints
    19  
    20  import (
    21  	"encoding/json"
    22  	"net/http"
    23  
    24  	"github.com/gin-gonic/gin"
    25  	"github.com/mysteriumnetwork/go-rest/apierror"
    26  
    27  	"github.com/mysteriumnetwork/node/feedback"
    28  	"github.com/mysteriumnetwork/node/tequilapi/utils"
    29  	"github.com/rs/zerolog/log"
    30  )
    31  
    32  type feedbackAPI struct {
    33  	reporter *feedback.Reporter
    34  }
    35  
    36  func newFeedbackAPI(reporter *feedback.Reporter) *feedbackAPI {
    37  	return &feedbackAPI{reporter: reporter}
    38  }
    39  
    40  // ReportIssueGithubResponse response for github issue creation
    41  //
    42  // swagger:model ReportIssueGithubResponse
    43  type ReportIssueGithubResponse struct {
    44  	IssueID string `json:"issue_id"`
    45  }
    46  
    47  // ReportIssueGithub reports user issue to github
    48  // swagger:operation POST /feedback/issue Feedback reportIssueGithub
    49  //
    50  //	---
    51  //	summary: Reports user issue to github
    52  //	description: Reports user issue to github
    53  //	deprecated: true
    54  //	parameters:
    55  //	  - in: body
    56  //	    name: body
    57  //	    description: Bug report issue request
    58  //	    schema:
    59  //	      $ref: "#/definitions/BugReport"
    60  //	responses:
    61  //	  200:
    62  //	    description: Issue reported
    63  //	    schema:
    64  //	      "$ref": "#/definitions/ReportIssueGithubResponse"
    65  //	  400:
    66  //	    description: Failed to parse or request validation failed
    67  //	    schema:
    68  //	      "$ref": "#/definitions/APIError"
    69  //	  429:
    70  //	    description: Too many requests (max. 1/minute)
    71  //	    schema:
    72  //	      "$ref": "#/definitions/APIError"
    73  //	  500:
    74  //	    description: Internal server error
    75  //	    schema:
    76  //	      "$ref": "#/definitions/APIError"
    77  //	  503:
    78  //	    description: Unavailable
    79  //	    schema:
    80  //	      "$ref": "#/definitions/APIError"
    81  func (api *feedbackAPI) ReportIssueGithub(c *gin.Context) {
    82  	var req feedback.BugReport
    83  	err := json.NewDecoder(c.Request.Body).Decode(&req)
    84  	if err != nil {
    85  		c.Error(apierror.ParseFailed())
    86  		return
    87  	}
    88  
    89  	result, apiErr, err := api.reporter.NewIssue(req)
    90  	if err != nil {
    91  		log.Error().Stack().Err(err).Msg("Could not create an issue for feedback")
    92  	}
    93  	if apiErr != nil {
    94  		c.Error(apiErr)
    95  		return
    96  	} else if err != nil {
    97  		c.Error(err)
    98  		return
    99  	}
   100  
   101  	utils.WriteAsJSON(ReportIssueGithubResponse{
   102  		IssueID: result.IssueId,
   103  	}, c.Writer)
   104  }
   105  
   106  // ReportIssueIntercom reports user issue to intercom
   107  // swagger:operation POST /feedback/issue/intercom Feedback reportIssueIntercom
   108  //
   109  //	---
   110  //	summary: Reports user issue to intercom
   111  //	description: Reports user user to intercom
   112  //	deprecated: true
   113  //	parameters:
   114  //	  - in: body
   115  //	    name: body
   116  //	    description: Report issue request
   117  //	    schema:
   118  //	      $ref: "#/definitions/UserReport"
   119  //	responses:
   120  //	  201:
   121  //	    description: Issue reported
   122  //	  400:
   123  //	    description: Failed to parse or request validation failed
   124  //	    schema:
   125  //	      "$ref": "#/definitions/APIError"
   126  //	  429:
   127  //	    description: Too many requests (max. 1/minute)
   128  //	    schema:
   129  //	      "$ref": "#/definitions/APIError"
   130  //	  500:
   131  //	    description: Internal server error
   132  //	    schema:
   133  //	      "$ref": "#/definitions/APIError"
   134  //	  503:
   135  //	    description: Unavailable
   136  //	    schema:
   137  //	      "$ref": "#/definitions/APIError"
   138  func (api *feedbackAPI) ReportIssueIntercom(c *gin.Context) {
   139  	var req feedback.UserReport
   140  	err := json.NewDecoder(c.Request.Body).Decode(&req)
   141  	if err != nil {
   142  		c.Error(apierror.ParseFailed())
   143  		return
   144  	}
   145  
   146  	_, apiErr, err := api.reporter.NewIntercomIssue(req)
   147  	if err != nil {
   148  		log.Error().Stack().Err(err).Msg("Could not create an issue for feedback")
   149  	}
   150  	if apiErr != nil {
   151  		c.Error(apiErr)
   152  		return
   153  	} else if err != nil {
   154  		c.Error(err)
   155  		return
   156  	}
   157  
   158  	c.Status(http.StatusCreated)
   159  }
   160  
   161  // BugReport reports a bug with logs
   162  // swagger:operation POST /feedback/bug-report Feedback bugReport
   163  //
   164  //	---
   165  //	summary: Creates a bug report
   166  //	description: Creates a bug report with logs
   167  //	parameters:
   168  //	  - in: body
   169  //	    name: body
   170  //	    description: Report a bug
   171  //	    schema:
   172  //	      $ref: "#/definitions/BugReport"
   173  //	responses:
   174  //	  200:
   175  //	    description: Bug report response
   176  //	    schema:
   177  //	      "$ref": "#/definitions/CreateBugReportResponse"
   178  //	  400:
   179  //	    description: Failed to parse or request validation failed
   180  //	    schema:
   181  //	      "$ref": "#/definitions/APIError"
   182  //	  429:
   183  //	    description: Too many requests (max. 1/minute)
   184  //	    schema:
   185  //	      "$ref": "#/definitions/APIError"
   186  //	  500:
   187  //	    description: Internal server error
   188  //	    schema:
   189  //	      "$ref": "#/definitions/APIError"
   190  //	  503:
   191  //	    description: Unavailable
   192  //	    schema:
   193  //	      "$ref": "#/definitions/APIError"
   194  func (api *feedbackAPI) BugReport(c *gin.Context) {
   195  	var req feedback.BugReport
   196  	err := json.NewDecoder(c.Request.Body).Decode(&req)
   197  	if err != nil {
   198  		c.Error(apierror.ParseFailed())
   199  		return
   200  	}
   201  
   202  	result, apiErr, err := api.reporter.NewBugReport(req)
   203  	if err != nil {
   204  		log.Error().Stack().Err(err).Msg("Could not create a bug report")
   205  	}
   206  	if apiErr != nil {
   207  		c.Error(apiErr)
   208  		return
   209  	} else if err != nil {
   210  		c.Error(err)
   211  		return
   212  	}
   213  
   214  	utils.WriteAsJSON(*result, c.Writer)
   215  }
   216  
   217  // AddRoutesForFeedback registers feedback routes
   218  func AddRoutesForFeedback(
   219  	reporter *feedback.Reporter,
   220  ) func(*gin.Engine) error {
   221  	api := newFeedbackAPI(reporter)
   222  	return func(g *gin.Engine) error {
   223  		g.POST("/feedback/issue", api.ReportIssueGithub)
   224  		g.POST("/feedback/issue/intercom", api.ReportIssueIntercom)
   225  		g.POST("/feedback/bug-report", api.BugReport)
   226  		return nil
   227  	}
   228  }