|
| 1 | +// Copyright 2016 The Gogs Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package user |
| 6 | + |
| 7 | +import ( |
| 8 | + api "code.gitea.io/sdk/gitea" |
| 9 | + |
| 10 | + "code.gitea.io/gitea/models" |
| 11 | + "code.gitea.io/gitea/modules/context" |
| 12 | +) |
| 13 | + |
| 14 | +// getStarredRepos returns the repos that the user with the specified userID has |
| 15 | +// starred |
| 16 | +func getStarredRepos(userID int64, private bool) ([]*api.Repository, error) { |
| 17 | + starredRepos, err := models.GetStarredRepos(userID, private) |
| 18 | + if err != nil { |
| 19 | + return nil, err |
| 20 | + } |
| 21 | + repos := make([]*api.Repository, len(starredRepos)) |
| 22 | + for i, starred := range starredRepos { |
| 23 | + repos[i] = starred.APIFormat(&api.Permission{true, true, true}) |
| 24 | + } |
| 25 | + return repos, nil |
| 26 | +} |
| 27 | + |
| 28 | +// GetStarredRepos returns the repos that the user specified by the APIContext |
| 29 | +// has starred |
| 30 | +func GetStarredRepos(ctx *context.APIContext) { |
| 31 | + user := GetUserByParams(ctx) |
| 32 | + private := user.ID == ctx.User.ID |
| 33 | + repos, err := getStarredRepos(user.ID, private) |
| 34 | + if err != nil { |
| 35 | + ctx.Error(500, "getStarredRepos", err) |
| 36 | + } |
| 37 | + ctx.JSON(200, &repos) |
| 38 | +} |
| 39 | + |
| 40 | +// GetMyStarredRepos returns the repos that the authenticated user has starred |
| 41 | +func GetMyStarredRepos(ctx *context.APIContext) { |
| 42 | + repos, err := getStarredRepos(ctx.User.ID, true) |
| 43 | + if err != nil { |
| 44 | + ctx.Error(500, "getStarredRepos", err) |
| 45 | + } |
| 46 | + ctx.JSON(200, &repos) |
| 47 | +} |
| 48 | + |
| 49 | +// IsStarring returns whether the authenticated is starring the repo |
| 50 | +func IsStarring(ctx *context.APIContext) { |
| 51 | + if models.IsStaring(ctx.User.ID, ctx.Repo.Repository.ID) { |
| 52 | + ctx.Status(204) |
| 53 | + } else { |
| 54 | + ctx.Status(404) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// Star the repo specified in the APIContext, as the authenticated user |
| 59 | +func Star(ctx *context.APIContext) { |
| 60 | + err := models.StarRepo(ctx.User.ID, ctx.Repo.Repository.ID, true) |
| 61 | + if err != nil { |
| 62 | + ctx.Error(500, "StarRepo", err) |
| 63 | + return |
| 64 | + } |
| 65 | + ctx.Status(204) |
| 66 | +} |
| 67 | + |
| 68 | +// Unstar the repo specified in the APIContext, as the authenticated user |
| 69 | +func Unstar(ctx *context.APIContext) { |
| 70 | + err := models.StarRepo(ctx.User.ID, ctx.Repo.Repository.ID, false) |
| 71 | + if err != nil { |
| 72 | + ctx.Error(500, "StarRepo", err) |
| 73 | + return |
| 74 | + } |
| 75 | + ctx.Status(204) |
| 76 | +} |
0 commit comments