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

     1  // Copyright 2015 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  	user_model "code.gitea.io/gitea/models/user"
    11  	"code.gitea.io/gitea/modules/context"
    12  	api "code.gitea.io/gitea/modules/structs"
    13  	"code.gitea.io/gitea/routers/api/v1/utils"
    14  	"code.gitea.io/gitea/services/convert"
    15  )
    16  
    17  func responseAPIUsers(ctx *context.APIContext, users []*user_model.User) {
    18  	apiUsers := make([]*api.User, len(users))
    19  	for i := range users {
    20  		apiUsers[i] = convert.ToUser(ctx, users[i], ctx.Doer)
    21  	}
    22  	ctx.JSON(http.StatusOK, &apiUsers)
    23  }
    24  
    25  func listUserFollowers(ctx *context.APIContext, u *user_model.User) {
    26  	users, count, err := user_model.GetUserFollowers(ctx, u, ctx.Doer, utils.GetListOptions(ctx))
    27  	if err != nil {
    28  		ctx.Error(http.StatusInternalServerError, "GetUserFollowers", err)
    29  		return
    30  	}
    31  
    32  	ctx.SetTotalCountHeader(count)
    33  	responseAPIUsers(ctx, users)
    34  }
    35  
    36  // ListMyFollowers list the authenticated user's followers
    37  func ListMyFollowers(ctx *context.APIContext) {
    38  	// swagger:operation GET /user/followers user userCurrentListFollowers
    39  	// ---
    40  	// summary: List the authenticated user's followers
    41  	// parameters:
    42  	// - name: page
    43  	//   in: query
    44  	//   description: page number of results to return (1-based)
    45  	//   type: integer
    46  	// - name: limit
    47  	//   in: query
    48  	//   description: page size of results
    49  	//   type: integer
    50  	// produces:
    51  	// - application/json
    52  	// responses:
    53  	//   "200":
    54  	//     "$ref": "#/responses/UserList"
    55  
    56  	listUserFollowers(ctx, ctx.Doer)
    57  }
    58  
    59  // ListFollowers list the given user's followers
    60  func ListFollowers(ctx *context.APIContext) {
    61  	// swagger:operation GET /users/{username}/followers user userListFollowers
    62  	// ---
    63  	// summary: List the given user's followers
    64  	// produces:
    65  	// - application/json
    66  	// parameters:
    67  	// - name: username
    68  	//   in: path
    69  	//   description: username of user
    70  	//   type: string
    71  	//   required: true
    72  	// - name: page
    73  	//   in: query
    74  	//   description: page number of results to return (1-based)
    75  	//   type: integer
    76  	// - name: limit
    77  	//   in: query
    78  	//   description: page size of results
    79  	//   type: integer
    80  	// responses:
    81  	//   "200":
    82  	//     "$ref": "#/responses/UserList"
    83  	//   "404":
    84  	//     "$ref": "#/responses/notFound"
    85  
    86  	listUserFollowers(ctx, ctx.ContextUser)
    87  }
    88  
    89  func listUserFollowing(ctx *context.APIContext, u *user_model.User) {
    90  	users, count, err := user_model.GetUserFollowing(ctx, u, ctx.Doer, utils.GetListOptions(ctx))
    91  	if err != nil {
    92  		ctx.Error(http.StatusInternalServerError, "GetUserFollowing", err)
    93  		return
    94  	}
    95  
    96  	ctx.SetTotalCountHeader(count)
    97  	responseAPIUsers(ctx, users)
    98  }
    99  
   100  // ListMyFollowing list the users that the authenticated user is following
   101  func ListMyFollowing(ctx *context.APIContext) {
   102  	// swagger:operation GET /user/following user userCurrentListFollowing
   103  	// ---
   104  	// summary: List the users that the authenticated user is following
   105  	// parameters:
   106  	// - name: page
   107  	//   in: query
   108  	//   description: page number of results to return (1-based)
   109  	//   type: integer
   110  	// - name: limit
   111  	//   in: query
   112  	//   description: page size of results
   113  	//   type: integer
   114  	// produces:
   115  	// - application/json
   116  	// responses:
   117  	//   "200":
   118  	//     "$ref": "#/responses/UserList"
   119  
   120  	listUserFollowing(ctx, ctx.Doer)
   121  }
   122  
   123  // ListFollowing list the users that the given user is following
   124  func ListFollowing(ctx *context.APIContext) {
   125  	// swagger:operation GET /users/{username}/following user userListFollowing
   126  	// ---
   127  	// summary: List the users that the given user is following
   128  	// produces:
   129  	// - application/json
   130  	// parameters:
   131  	// - name: username
   132  	//   in: path
   133  	//   description: username of user
   134  	//   type: string
   135  	//   required: true
   136  	// - name: page
   137  	//   in: query
   138  	//   description: page number of results to return (1-based)
   139  	//   type: integer
   140  	// - name: limit
   141  	//   in: query
   142  	//   description: page size of results
   143  	//   type: integer
   144  	// responses:
   145  	//   "200":
   146  	//     "$ref": "#/responses/UserList"
   147  	//   "404":
   148  	//     "$ref": "#/responses/notFound"
   149  
   150  	listUserFollowing(ctx, ctx.ContextUser)
   151  }
   152  
   153  func checkUserFollowing(ctx *context.APIContext, u *user_model.User, followID int64) {
   154  	if user_model.IsFollowing(ctx, u.ID, followID) {
   155  		ctx.Status(http.StatusNoContent)
   156  	} else {
   157  		ctx.NotFound()
   158  	}
   159  }
   160  
   161  // CheckMyFollowing whether the given user is followed by the authenticated user
   162  func CheckMyFollowing(ctx *context.APIContext) {
   163  	// swagger:operation GET /user/following/{username} user userCurrentCheckFollowing
   164  	// ---
   165  	// summary: Check whether a user is followed by the authenticated user
   166  	// parameters:
   167  	// - name: username
   168  	//   in: path
   169  	//   description: username of followed user
   170  	//   type: string
   171  	//   required: true
   172  	// responses:
   173  	//   "204":
   174  	//     "$ref": "#/responses/empty"
   175  	//   "404":
   176  	//     "$ref": "#/responses/notFound"
   177  
   178  	checkUserFollowing(ctx, ctx.Doer, ctx.ContextUser.ID)
   179  }
   180  
   181  // CheckFollowing check if one user is following another user
   182  func CheckFollowing(ctx *context.APIContext) {
   183  	// swagger:operation GET /users/{username}/following/{target} user userCheckFollowing
   184  	// ---
   185  	// summary: Check if one user is following another user
   186  	// parameters:
   187  	// - name: username
   188  	//   in: path
   189  	//   description: username of following user
   190  	//   type: string
   191  	//   required: true
   192  	// - name: target
   193  	//   in: path
   194  	//   description: username of followed user
   195  	//   type: string
   196  	//   required: true
   197  	// responses:
   198  	//   "204":
   199  	//     "$ref": "#/responses/empty"
   200  	//   "404":
   201  	//     "$ref": "#/responses/notFound"
   202  
   203  	target := GetUserByParamsName(ctx, ":target")
   204  	if ctx.Written() {
   205  		return
   206  	}
   207  	checkUserFollowing(ctx, ctx.ContextUser, target.ID)
   208  }
   209  
   210  // Follow follow a user
   211  func Follow(ctx *context.APIContext) {
   212  	// swagger:operation PUT /user/following/{username} user userCurrentPutFollow
   213  	// ---
   214  	// summary: Follow a user
   215  	// parameters:
   216  	// - name: username
   217  	//   in: path
   218  	//   description: username of user to follow
   219  	//   type: string
   220  	//   required: true
   221  	// responses:
   222  	//   "204":
   223  	//     "$ref": "#/responses/empty"
   224  	//   "404":
   225  	//     "$ref": "#/responses/notFound"
   226  
   227  	if err := user_model.FollowUser(ctx, ctx.Doer.ID, ctx.ContextUser.ID); err != nil {
   228  		ctx.Error(http.StatusInternalServerError, "FollowUser", err)
   229  		return
   230  	}
   231  	ctx.Status(http.StatusNoContent)
   232  }
   233  
   234  // Unfollow unfollow a user
   235  func Unfollow(ctx *context.APIContext) {
   236  	// swagger:operation DELETE /user/following/{username} user userCurrentDeleteFollow
   237  	// ---
   238  	// summary: Unfollow a user
   239  	// parameters:
   240  	// - name: username
   241  	//   in: path
   242  	//   description: username of user to unfollow
   243  	//   type: string
   244  	//   required: true
   245  	// responses:
   246  	//   "204":
   247  	//     "$ref": "#/responses/empty"
   248  	//   "404":
   249  	//     "$ref": "#/responses/notFound"
   250  
   251  	if err := user_model.UnfollowUser(ctx, ctx.Doer.ID, ctx.ContextUser.ID); err != nil {
   252  		ctx.Error(http.StatusInternalServerError, "UnfollowUser", err)
   253  		return
   254  	}
   255  	ctx.Status(http.StatusNoContent)
   256  }