code.gitea.io/gitea@v1.21.7/routers/api/v1/notify/threads.go (about)

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package notify
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  
    10  	activities_model "code.gitea.io/gitea/models/activities"
    11  	"code.gitea.io/gitea/models/db"
    12  	issues_model "code.gitea.io/gitea/models/issues"
    13  	"code.gitea.io/gitea/modules/context"
    14  	"code.gitea.io/gitea/services/convert"
    15  )
    16  
    17  // GetThread get notification by ID
    18  func GetThread(ctx *context.APIContext) {
    19  	// swagger:operation GET /notifications/threads/{id} notification notifyGetThread
    20  	// ---
    21  	// summary: Get notification thread by ID
    22  	// consumes:
    23  	// - application/json
    24  	// produces:
    25  	// - application/json
    26  	// parameters:
    27  	// - name: id
    28  	//   in: path
    29  	//   description: id of notification thread
    30  	//   type: string
    31  	//   required: true
    32  	// responses:
    33  	//   "200":
    34  	//     "$ref": "#/responses/NotificationThread"
    35  	//   "403":
    36  	//     "$ref": "#/responses/forbidden"
    37  	//   "404":
    38  	//     "$ref": "#/responses/notFound"
    39  
    40  	n := getThread(ctx)
    41  	if n == nil {
    42  		return
    43  	}
    44  	if err := n.LoadAttributes(ctx); err != nil && !issues_model.IsErrCommentNotExist(err) {
    45  		ctx.InternalServerError(err)
    46  		return
    47  	}
    48  
    49  	ctx.JSON(http.StatusOK, convert.ToNotificationThread(ctx, n))
    50  }
    51  
    52  // ReadThread mark notification as read by ID
    53  func ReadThread(ctx *context.APIContext) {
    54  	// swagger:operation PATCH /notifications/threads/{id} notification notifyReadThread
    55  	// ---
    56  	// summary: Mark notification thread as read by ID
    57  	// consumes:
    58  	// - application/json
    59  	// produces:
    60  	// - application/json
    61  	// parameters:
    62  	// - name: id
    63  	//   in: path
    64  	//   description: id of notification thread
    65  	//   type: string
    66  	//   required: true
    67  	// - name: to-status
    68  	//   in: query
    69  	//   description: Status to mark notifications as
    70  	//   type: string
    71  	//   default: read
    72  	//   required: false
    73  	// responses:
    74  	//   "205":
    75  	//     "$ref": "#/responses/NotificationThread"
    76  	//   "403":
    77  	//     "$ref": "#/responses/forbidden"
    78  	//   "404":
    79  	//     "$ref": "#/responses/notFound"
    80  
    81  	n := getThread(ctx)
    82  	if n == nil {
    83  		return
    84  	}
    85  
    86  	targetStatus := statusStringToNotificationStatus(ctx.FormString("to-status"))
    87  	if targetStatus == 0 {
    88  		targetStatus = activities_model.NotificationStatusRead
    89  	}
    90  
    91  	notif, err := activities_model.SetNotificationStatus(ctx, n.ID, ctx.Doer, targetStatus)
    92  	if err != nil {
    93  		ctx.InternalServerError(err)
    94  		return
    95  	}
    96  	if err = notif.LoadAttributes(ctx); err != nil && !issues_model.IsErrCommentNotExist(err) {
    97  		ctx.InternalServerError(err)
    98  		return
    99  	}
   100  	ctx.JSON(http.StatusResetContent, convert.ToNotificationThread(ctx, notif))
   101  }
   102  
   103  func getThread(ctx *context.APIContext) *activities_model.Notification {
   104  	n, err := activities_model.GetNotificationByID(ctx, ctx.ParamsInt64(":id"))
   105  	if err != nil {
   106  		if db.IsErrNotExist(err) {
   107  			ctx.Error(http.StatusNotFound, "GetNotificationByID", err)
   108  		} else {
   109  			ctx.InternalServerError(err)
   110  		}
   111  		return nil
   112  	}
   113  	if n.UserID != ctx.Doer.ID && !ctx.Doer.IsAdmin {
   114  		ctx.Error(http.StatusForbidden, "GetNotificationByID", fmt.Errorf("only user itself and admin are allowed to read/change this thread %d", n.ID))
   115  		return nil
   116  	}
   117  	return n
   118  }