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

     1  // Copyright 2014 The Gogs Authors. All rights reserved.
     2  // Copyright 2020 The Gitea Authors.
     3  // SPDX-License-Identifier: MIT
     4  
     5  package user
     6  
     7  import (
     8  	"net/http"
     9  
    10  	activities_model "code.gitea.io/gitea/models/activities"
    11  	user_model "code.gitea.io/gitea/models/user"
    12  	"code.gitea.io/gitea/modules/context"
    13  	"code.gitea.io/gitea/routers/api/v1/utils"
    14  	"code.gitea.io/gitea/services/convert"
    15  )
    16  
    17  // Search search users
    18  func Search(ctx *context.APIContext) {
    19  	// swagger:operation GET /users/search user userSearch
    20  	// ---
    21  	// summary: Search for users
    22  	// produces:
    23  	// - application/json
    24  	// parameters:
    25  	// - name: q
    26  	//   in: query
    27  	//   description: keyword
    28  	//   type: string
    29  	// - name: uid
    30  	//   in: query
    31  	//   description: ID of the user to search for
    32  	//   type: integer
    33  	//   format: int64
    34  	// - name: page
    35  	//   in: query
    36  	//   description: page number of results to return (1-based)
    37  	//   type: integer
    38  	// - name: limit
    39  	//   in: query
    40  	//   description: page size of results
    41  	//   type: integer
    42  	// responses:
    43  	//   "200":
    44  	//     description: "SearchResults of a successful search"
    45  	//     schema:
    46  	//       type: object
    47  	//       properties:
    48  	//         ok:
    49  	//           type: boolean
    50  	//         data:
    51  	//           type: array
    52  	//           items:
    53  	//             "$ref": "#/definitions/User"
    54  
    55  	listOptions := utils.GetListOptions(ctx)
    56  
    57  	uid := ctx.FormInt64("uid")
    58  	var users []*user_model.User
    59  	var maxResults int64
    60  	var err error
    61  
    62  	switch uid {
    63  	case user_model.GhostUserID:
    64  		maxResults = 1
    65  		users = []*user_model.User{user_model.NewGhostUser()}
    66  	case user_model.ActionsUserID:
    67  		maxResults = 1
    68  		users = []*user_model.User{user_model.NewActionsUser()}
    69  	default:
    70  		users, maxResults, err = user_model.SearchUsers(ctx, &user_model.SearchUserOptions{
    71  			Actor:       ctx.Doer,
    72  			Keyword:     ctx.FormTrim("q"),
    73  			UID:         uid,
    74  			Type:        user_model.UserTypeIndividual,
    75  			ListOptions: listOptions,
    76  		})
    77  		if err != nil {
    78  			ctx.JSON(http.StatusInternalServerError, map[string]any{
    79  				"ok":    false,
    80  				"error": err.Error(),
    81  			})
    82  			return
    83  		}
    84  	}
    85  
    86  	ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
    87  	ctx.SetTotalCountHeader(maxResults)
    88  
    89  	ctx.JSON(http.StatusOK, map[string]any{
    90  		"ok":   true,
    91  		"data": convert.ToUsers(ctx, ctx.Doer, users),
    92  	})
    93  }
    94  
    95  // GetInfo get user's information
    96  func GetInfo(ctx *context.APIContext) {
    97  	// swagger:operation GET /users/{username} user userGet
    98  	// ---
    99  	// summary: Get a user
   100  	// produces:
   101  	// - application/json
   102  	// parameters:
   103  	// - name: username
   104  	//   in: path
   105  	//   description: username of user to get
   106  	//   type: string
   107  	//   required: true
   108  	// responses:
   109  	//   "200":
   110  	//     "$ref": "#/responses/User"
   111  	//   "404":
   112  	//     "$ref": "#/responses/notFound"
   113  
   114  	if !user_model.IsUserVisibleToViewer(ctx, ctx.ContextUser, ctx.Doer) {
   115  		// fake ErrUserNotExist error message to not leak information about existence
   116  		ctx.NotFound("GetUserByName", user_model.ErrUserNotExist{Name: ctx.Params(":username")})
   117  		return
   118  	}
   119  	ctx.JSON(http.StatusOK, convert.ToUser(ctx, ctx.ContextUser, ctx.Doer))
   120  }
   121  
   122  // GetAuthenticatedUser get current user's information
   123  func GetAuthenticatedUser(ctx *context.APIContext) {
   124  	// swagger:operation GET /user user userGetCurrent
   125  	// ---
   126  	// summary: Get the authenticated user
   127  	// produces:
   128  	// - application/json
   129  	// responses:
   130  	//   "200":
   131  	//     "$ref": "#/responses/User"
   132  
   133  	ctx.JSON(http.StatusOK, convert.ToUser(ctx, ctx.Doer, ctx.Doer))
   134  }
   135  
   136  // GetUserHeatmapData is the handler to get a users heatmap
   137  func GetUserHeatmapData(ctx *context.APIContext) {
   138  	// swagger:operation GET /users/{username}/heatmap user userGetHeatmapData
   139  	// ---
   140  	// summary: Get a user's heatmap
   141  	// produces:
   142  	// - application/json
   143  	// parameters:
   144  	// - name: username
   145  	//   in: path
   146  	//   description: username of user to get
   147  	//   type: string
   148  	//   required: true
   149  	// responses:
   150  	//   "200":
   151  	//     "$ref": "#/responses/UserHeatmapData"
   152  	//   "404":
   153  	//     "$ref": "#/responses/notFound"
   154  
   155  	heatmap, err := activities_model.GetUserHeatmapDataByUser(ctx, ctx.ContextUser, ctx.Doer)
   156  	if err != nil {
   157  		ctx.Error(http.StatusInternalServerError, "GetUserHeatmapDataByUser", err)
   158  		return
   159  	}
   160  	ctx.JSON(http.StatusOK, heatmap)
   161  }
   162  
   163  func ListUserActivityFeeds(ctx *context.APIContext) {
   164  	// swagger:operation GET /users/{username}/activities/feeds user userListActivityFeeds
   165  	// ---
   166  	// summary: List a user's activity feeds
   167  	// produces:
   168  	// - application/json
   169  	// parameters:
   170  	// - name: username
   171  	//   in: path
   172  	//   description: username of user
   173  	//   type: string
   174  	//   required: true
   175  	// - name: only-performed-by
   176  	//   in: query
   177  	//   description: if true, only show actions performed by the requested user
   178  	//   type: boolean
   179  	// - name: date
   180  	//   in: query
   181  	//   description: the date of the activities to be found
   182  	//   type: string
   183  	//   format: date
   184  	// - name: page
   185  	//   in: query
   186  	//   description: page number of results to return (1-based)
   187  	//   type: integer
   188  	// - name: limit
   189  	//   in: query
   190  	//   description: page size of results
   191  	//   type: integer
   192  	// responses:
   193  	//   "200":
   194  	//     "$ref": "#/responses/ActivityFeedsList"
   195  	//   "404":
   196  	//     "$ref": "#/responses/notFound"
   197  
   198  	includePrivate := ctx.IsSigned && (ctx.Doer.IsAdmin || ctx.Doer.ID == ctx.ContextUser.ID)
   199  	listOptions := utils.GetListOptions(ctx)
   200  
   201  	opts := activities_model.GetFeedsOptions{
   202  		RequestedUser:   ctx.ContextUser,
   203  		Actor:           ctx.Doer,
   204  		IncludePrivate:  includePrivate,
   205  		OnlyPerformedBy: ctx.FormBool("only-performed-by"),
   206  		Date:            ctx.FormString("date"),
   207  		ListOptions:     listOptions,
   208  	}
   209  
   210  	feeds, count, err := activities_model.GetFeeds(ctx, opts)
   211  	if err != nil {
   212  		ctx.Error(http.StatusInternalServerError, "GetFeeds", err)
   213  		return
   214  	}
   215  	ctx.SetTotalCountHeader(count)
   216  
   217  	ctx.JSON(http.StatusOK, convert.ToActivities(ctx, feeds, ctx.Doer))
   218  }