diff --git a/conference_ops.go b/conference_ops.go index 734ccee..f99b7b3 100644 --- a/conference_ops.go +++ b/conference_ops.go @@ -16,6 +16,7 @@ import ( "fmt" "io" "mime/multipart" + "net/http" "strconv" "strings" @@ -23,6 +24,8 @@ import ( "git.erbosoft.com/amy/amsterdam/ui" ) +var ENOPERM error = errors.New("you are not permitted to perform this operation") + // slurpFile reads the contrents of a multipart.File into memory. func slurpFile(file *multipart.FileHeader) ([]byte, error) { f, err := file.Open() @@ -139,3 +142,40 @@ func HideTopic(ctxt ui.AmContext) (string, any, error) { } return "redirect", fmt.Sprintf("/comm/%s/conf/%s/r/%d", ctxt.CurrentCommunity().Alias, ctxt.GetScratch("currentAlias"), topic.Number), nil } + +/* HideMessage hides or shows a topic message. + * Parameters: + * ctxt - The AmContext for the request. + * Returns: + * Command string dictating what to be rendered. + * Data as a parameter for the command string. + * Standard Go error status. + */ +func HideMessage(ctxt ui.AmContext) (string, any, error) { + if ctxt.CurrentUser().IsAnon { + ctxt.SetRC(http.StatusForbidden) + return ui.ErrorPage(ctxt, ENOPERM) + } + conf := ctxt.GetScratch("currentConference").(*database.Conference) + myLevel := ctxt.GetScratch("levelInConference").(uint16) + topic := ctxt.GetScratch("currentTopic").(*database.Topic) + msgNum, err := strconv.Atoi(ctxt.URLParam("msg")) + if err != nil { + return ui.ErrorPage(ctxt, err) + } + hdrs, err := database.AmGetPostRange(ctxt.Ctx(), topic, int32(msgNum), int32(msgNum)) + if err != nil { + return ui.ErrorPage(ctxt, err) + } else if len(hdrs) != 1 { + return ui.ErrorPage(ctxt, errors.New("internal error getting post reference")) + } + if (hdrs[0].CreatorUid != ctxt.CurrentUserId()) && !conf.TestPermission("Conference.Hide", myLevel) { + ctxt.SetRC(http.StatusForbidden) + return ui.ErrorPage(ctxt, ENOPERM) + } + err = hdrs[0].SetHidden(ctxt.Ctx(), ctxt.CurrentUser(), !(hdrs[0].Hidden), ctxt.RemoteIP()) + if err != nil { + return ui.ErrorPage(ctxt, err) + } + return "redirect", fmt.Sprintf("/comm/%s/conf/%s/r/%d?r=%d&ac=1", ctxt.CurrentCommunity().Alias, ctxt.GetScratch("currentAlias"), topic.Number, hdrs[0].Num), nil +} diff --git a/main.go b/main.go index 98f4acc..d02386c 100644 --- a/main.go +++ b/main.go @@ -102,6 +102,7 @@ func setupEcho() *echo.Echo { confGroup.POST("/r/:topic", ui.AmWrap(PostInTopic), ui.SetTopic) opsGroup := confGroup.Group("/op/:topic", ui.SetTopic) opsGroup.GET("/hide", ui.AmWrap(HideTopic)) + opsGroup.GET("/hide/:msg", ui.AmWrap(HideMessage)) return e } diff --git a/ui/views/posts.jet b/ui/views/posts.jet index f6c526f..4825ae5 100644 --- a/ui/views/posts.jet +++ b/ui/views/posts.jet @@ -118,7 +118,7 @@ {{ if advancedControls }}
{{ if canHide }} - {{ if isPostHidden }}Show{{ else }}Hide{{ end }}