Skip to content

Commit

Permalink
Merge pull request #164 from ppolariss/main
Browse files Browse the repository at this point in the history
feat: add BanUserForever api; fix: replace email with @danta.tech
  • Loading branch information
ppolariss authored Oct 10, 2024
2 parents dd8c570 + 22a8926 commit d89fee1
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 26 deletions.
2 changes: 1 addition & 1 deletion CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ representative at an online or offline event.

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported to the community leaders responsible for enforcement at
dev@fduhole.com.
dev@danta.tech.
All complaints will be reviewed and investigated promptly and fairly.

All community leaders are obligated to respect the privacy and security of the
Expand Down
140 changes: 137 additions & 3 deletions apis/penalty/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import (
"fmt"
"time"

"github.com/opentreehole/go-common"

"treehole_next/config"
. "treehole_next/models"
"treehole_next/utils"

"github.com/opentreehole/go-common"
"gorm.io/gorm"
"gorm.io/gorm/clause"

"github.com/gofiber/fiber/v2"
)
Expand All @@ -19,6 +23,10 @@ type PostBody struct {
Reason string `json:"reason"` // optional
}

type ForeverPostBody struct {
Reason string `json:"reason"` // optional
}

// BanUser
//
// @Summary Ban publisher of a floor
Expand Down Expand Up @@ -103,7 +111,132 @@ func BanUser(c *fiber.Ctx) error {
Data: floor,
Recipients: []int{floor.UserID},
Description: fmt.Sprintf(
"您因为违反社区公约被禁言。时间:%d天,原因:%s\n如有异议,请联系admin@fduhole.com。",
"您因为违反社区公约被禁言。时间:%d天,原因:%s\n如有异议,请联系admin@danta.tech。",
days,
body.Reason,
),
Title: "处罚通知",
Type: MessageTypePermission,
URL: fmt.Sprintf("/api/floors/%d", floor.ID),
}

// send
_, err = message.Send()
if err != nil {
return err
}

return c.JSON(user)
}

// BanUserForever
//
// @Summary Ban publisher of a floor forever
// @Tags Penalty
// @Produce json
// @Router /penalty/{floor_id}/_forever [post]
// @Param json body ForeverPostBody true "json"
// @Success 201 {object} User
func BanUserForever(c *fiber.Ctx) error {
// validate body
var body ForeverPostBody
err := common.ValidateBody(c, &body)
if err != nil {
return err
}

floorID, err := c.ParamsInt("id")
if err != nil {
return err
}

// get user
user, err := GetUser(c)
if err != nil {
return err
}

// permission
if !user.IsAdmin {
return common.Forbidden()
}

var floor Floor
err = DB.Take(&floor, floorID).Error
if err != nil {
return err
}

// var hole Hole
// err = DB.Take(&hole, floor.HoleID).Error
// if err != nil {
// return err
// }

days := 3650
duration := time.Duration(days) * 24 * time.Hour

var punishments Punishments
var punishment *Punishment
var divisionIDs []int
madeBy := user.ID
err = DB.Transaction(func(tx *gorm.DB) (err error) {
err = tx.Clauses(clause.Locking{Strength: "UPDATE"}).Take(&user, floor.UserID).Error
if err != nil {
return err
}

err = tx.Clauses(clause.Locking{Strength: "UPDATE"}).Model(&Division{}).Select("id").Scan(&divisionIDs).Error
if err != nil {
return err
}

ExcludeBanForeverDivisionIds := config.Config.ExcludeBanForeverDivisionIds

divisionIDs = utils.Difference(divisionIDs, ExcludeBanForeverDivisionIds)

for _, divisionID := range divisionIDs {
punishment = &Punishment{
UserID: floor.UserID,
MadeBy: madeBy,
FloorID: &floor.ID,
DivisionID: divisionID,
Duration: &duration,
Day: days,
Reason: body.Reason,
StartTime: time.Now(),
EndTime: time.Now().Add(duration),
}

if user.BanDivision[divisionID] == nil {
user.BanDivision[divisionID] = &punishment.EndTime
} else {
user.BanDivision[divisionID].Add(*punishment.Duration)
}

punishments = append(punishments, punishment)
}
user.OffenceCount += len(divisionIDs)

err = tx.Create(&punishments).Error
if err != nil {
return err
}

err = tx.Select("BanDivision", "OffenceCount").Save(&user).Error
if err != nil {
return err
}

return nil
})

// construct message for user
message := Notification{
Data: floor,
Recipients: []int{floor.UserID},
Description: fmt.Sprintf(
"您因为违反社区公约被禁言。时间:%d天,原因:%s\n如有异议,请联系admin@danta.tech。",
days,
body.Reason,
),
Expand Down Expand Up @@ -187,6 +320,7 @@ func listPunishmentsByUserID(userID int) ([]Punishment, error) {

func RegisterRoutes(app fiber.Router) {
app.Post("/penalty/:id", BanUser)
app.Post("/penalty/:id/_forever", BanUserForever)
app.Get("/users/me/punishments", ListMyPunishments)
app.Get("/users/:id/punishments", ListPunishmentsByUserID)
}
7 changes: 4 additions & 3 deletions apis/report/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package report

import (
"fmt"
"github.com/opentreehole/go-common"
"github.com/rs/zerolog/log"
"time"
. "treehole_next/models"
. "treehole_next/utils"

"github.com/opentreehole/go-common"
"github.com/rs/zerolog/log"

"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -251,7 +252,7 @@ func BanReporter(c *fiber.Ctx) error {
Data: report,
Recipients: []int{report.UserID},
Description: fmt.Sprintf(
"您因违反社区公约被禁止举报。时间:%d天,原因:%s\n如有异议,请联系admin@fduhole.com。",
"您因违反社区公约被禁止举报。时间:%d天,原因:%s\n如有异议,请联系admin@danta.tech。",
days,
body.Reason,
),
Expand Down
29 changes: 15 additions & 14 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,21 @@ var Config struct {
HolePurgeDays int `env:"HOLE_PURGE_DAYS" envDefault:"30"`
OpenSensitiveCheck bool `env:"OPEN_SENSITIVE_CHECK" envDefault:"true"`

YiDunBusinessIdText string `env:"YI_DUN_BUSINESS_ID_TEXT" envDefault:""`
YiDunBusinessIdImage string `env:"YI_DUN_BUSINESS_ID_IMAGE" envDefault:""`
YiDunSecretId string `env:"YI_DUN_SECRET_ID" envDefault:""`
YiDunSecretKey string `env:"YI_DUN_SECRET_KEY" envDefault:""`
YiDunAccessKeyId string `env:"YI_DUN_ACCESS_KEY_ID" envDefault:""`
YiDunAccessKeySecret string `env:"YI_DUN_ACCESS_KEY_SECRET" envDefault:""`
ValidImageUrl []string `env:"VALID_IMAGE_URL"`
UrlHostnameWhitelist []string `env:"URL_HOSTNAME_WHITELIST"`
ExternalImageHost string `env:"EXTERNAL_IMAGE_HOSTNAME" envDefault:""`
NotifiableAdminIds []int `env:"NOTIFIABLE_ADMIN_IDS"`
ProxyUrl *url.URL `env:"PROXY_URL"`
QQBotGroupID *int64 `env:"GROUP_ID"`
QQBotUserID *int64 `env:"USER_ID"`
QQBotUrl *string `env:"QQ_BOT_URL"`
YiDunBusinessIdText string `env:"YI_DUN_BUSINESS_ID_TEXT" envDefault:""`
YiDunBusinessIdImage string `env:"YI_DUN_BUSINESS_ID_IMAGE" envDefault:""`
YiDunSecretId string `env:"YI_DUN_SECRET_ID" envDefault:""`
YiDunSecretKey string `env:"YI_DUN_SECRET_KEY" envDefault:""`
YiDunAccessKeyId string `env:"YI_DUN_ACCESS_KEY_ID" envDefault:""`
YiDunAccessKeySecret string `env:"YI_DUN_ACCESS_KEY_SECRET" envDefault:""`
ValidImageUrl []string `env:"VALID_IMAGE_URL"`
UrlHostnameWhitelist []string `env:"URL_HOSTNAME_WHITELIST"`
ExternalImageHost string `env:"EXTERNAL_IMAGE_HOSTNAME" envDefault:""`
NotifiableAdminIds []int `env:"NOTIFIABLE_ADMIN_IDS"`
ExcludeBanForeverDivisionIds []int `env:"EXCLUDE_BAN_FOREVER_DIVISION_IDS"`
ProxyUrl *url.URL `env:"PROXY_URL"`
QQBotGroupID *int64 `env:"GROUP_ID"`
QQBotUserID *int64 `env:"USER_ID"`
QQBotUrl *string `env:"QQ_BOT_URL"`
}

var DynamicConfig struct {
Expand Down
2 changes: 1 addition & 1 deletion data/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"repository": "https://github.com/OpenTreeHole/treehole_next",
"author": "hasbai",
"maintainer": "jingyijun",
"email": "dev@fduhole.com",
"email": "dev@danta.tech",
"license": "Apache-2.0"
}
41 changes: 40 additions & 1 deletion docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const docTemplate = `{
"title": "{{.Title}}",
"contact": {
"name": "Maintainer Ke Chen",
"email": "dev@fduhole.com"
"email": "dev@danta.tech"
},
"license": {
"name": "Apache 2.0",
Expand Down Expand Up @@ -1672,6 +1672,36 @@ const docTemplate = `{
}
}
},
"/penalty/{floor_id}/_forever": {
"post": {
"produces": [
"application/json"
],
"tags": [
"Penalty"
],
"summary": "Ban publisher of a floor forever",
"parameters": [
{
"description": "json",
"name": "json",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/penalty.ForeverPostBody"
}
}
],
"responses": {
"201": {
"description": "Created",
"schema": {
"$ref": "#/definitions/models.User"
}
}
}
}
},
"/reports": {
"get": {
"produces": [
Expand Down Expand Up @@ -4024,6 +4054,15 @@ const docTemplate = `{
}
}
},
"penalty.ForeverPostBody": {
"type": "object",
"properties": {
"reason": {
"description": "optional",
"type": "string"
}
}
},
"penalty.PostBody": {
"type": "object",
"properties": {
Expand Down
41 changes: 40 additions & 1 deletion docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"title": "Open Tree Hole",
"contact": {
"name": "Maintainer Ke Chen",
"email": "dev@fduhole.com"
"email": "dev@danta.tech"
},
"license": {
"name": "Apache 2.0",
Expand Down Expand Up @@ -1665,6 +1665,36 @@
}
}
},
"/penalty/{floor_id}/_forever": {
"post": {
"produces": [
"application/json"
],
"tags": [
"Penalty"
],
"summary": "Ban publisher of a floor forever",
"parameters": [
{
"description": "json",
"name": "json",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/penalty.ForeverPostBody"
}
}
],
"responses": {
"201": {
"description": "Created",
"schema": {
"$ref": "#/definitions/models.User"
}
}
}
}
},
"/reports": {
"get": {
"produces": [
Expand Down Expand Up @@ -4017,6 +4047,15 @@
}
}
},
"penalty.ForeverPostBody": {
"type": "object",
"properties": {
"reason": {
"description": "optional",
"type": "string"
}
}
},
"penalty.PostBody": {
"type": "object",
"properties": {
Expand Down
Loading

0 comments on commit d89fee1

Please sign in to comment.