diff --git a/conference.go b/conference.go index a5b40d5..66a58fe 100644 --- a/conference.go +++ b/conference.go @@ -457,6 +457,7 @@ func ReadPosts(ctxt ui.AmContext) (string, any, error) { // Locate community, conference, and topic. comm := ctxt.CurrentCommunity() conf := ctxt.GetScratch("currentConference").(*database.Conference) + myLevel := ctxt.GetScratch("levelInConference").(uint16) var topic *database.Topic = nil if rawTopic, err := strconv.ParseInt(ctxt.URLParam("topic"), 10, 16); err == nil { topic, err = database.AmGetTopicByNumber(ctxt.Ctx(), conf, int16(rawTopic)) @@ -526,8 +527,46 @@ func ReadPosts(ctxt ui.AmContext) (string, any, error) { } ctxt.VarMap().Set("pseud", pseud) + // Set permission and status flags. + hidden, _ := topic.IsHidden(ctxt.Ctx(), ctxt.CurrentUser()) + ctxt.VarMap().Set("isTopicHidden", hidden) + confHidePerm := conf.TestPermission("Conference.Hide", myLevel) + ctxt.VarMap().Set("canFreeze", confHidePerm) + ctxt.VarMap().Set("canArchive", confHidePerm) + ctxt.VarMap().Set("canStick", confHidePerm) + ctxt.VarMap().Set("isFrozen", topic.Frozen) + ctxt.VarMap().Set("isArchived", topic.Archived) + ctxt.VarMap().Set("isSticky", topic.Sticky) + confNukePerm := conf.TestPermission("Conference.Nuke", myLevel) + ctxt.VarMap().Set("canDelete", confNukePerm) + ctxt.VarMap().Set("canPost", (!(topic.Frozen || topic.Archived) || confHidePerm) && conf.TestPermission("Conference.Post", myLevel)) + + // Set advanced controls. + advancedControls := ctxt.HasParameter("ac") && (len(posts) == 1) + if advancedControls { + isMyPost := (posts[0].CreatorUid == ctxt.CurrentUserId()) && !ctxt.CurrentUser().IsAnon + isScribbled := posts[0].IsScribbled() + canHide := !isScribbled && (isMyPost || confHidePerm) + ctxt.VarMap().Set("canHide", canHide) + ctxt.VarMap().Set("isPostHidden", posts[0].Hidden) + canScribble := !isScribbled && (isMyPost || confNukePerm) + ctxt.VarMap().Set("canScribble", canScribble) + ctxt.VarMap().Set("canNuke", confNukePerm) + canPublish := !isScribbled && database.AmTestPermission("Global.PublishFP", myLevel) + if canPublish { + published, _ := posts[0].IsPublished(ctxt.Ctx()) + if published { + canPublish = false + } + } + ctxt.VarMap().Set("canPublish", canPublish) + if !canHide && !canScribble && !confNukePerm && !canPublish { + advancedControls = false + } + } + ctxt.VarMap().Set("advancedControls", advancedControls) + // Render the output. - ctxt.VarMap().Set("advancedControls", ctxt.HasParameter("ac") && len(posts) == 1) ctxt.VarMap().Set("amsterdam_pageTitle", fmt.Sprintf("%s: %s", topic.Name, summaryLine)) ctxt.VarMap().Set("topicName", topic.Name) ctxt.VarMap().Set("summaryLine", summaryLine) diff --git a/database/post.go b/database/post.go index 1de6c99..74ff1c3 100644 --- a/database/post.go +++ b/database/post.go @@ -44,6 +44,20 @@ func (p *PostHeader) IsScribbled() bool { return p.ScribbleUid != nil && p.ScribbleDate != nil } +// IsPublished returns true if the post has been published to the front page. +func (p *PostHeader) IsPublished(ctx context.Context) (bool, error) { + rs, err := amdb.QueryContext(ctx, "SELECT COUNT(*) FROM postpublish WHERE postid = ?", p.PostId) + if err != nil { + return false, err + } + if !rs.Next() { + return false, errors.New("internal failure in IsPublished") + } + ct := 0 + err = rs.Scan(&ct) + return ct > 0, err +} + /* SetAttachment sets the attachment data for a post. * Parameters: * ctx - Standard Go context value. diff --git a/database/topic.go b/database/topic.go index 25ff2da..710a758 100644 --- a/database/topic.go +++ b/database/topic.go @@ -81,6 +81,19 @@ func (t *Topic) SetLastRead(ctx context.Context, u *User, postNum int32) error { return err } +// IsHidden tells us whether the user has the topic hidden. +func (t *Topic) IsHidden(ctx context.Context, u *User) (bool, error) { + rs, err := amdb.QueryContext(ctx, "SELECT hidden FROM topicsettings WHERE topicid = ? AND uid = ?", t.TopicId, u.Uid) + if err != nil { + return false, err + } + rc := false + if rs.Next() { + err = rs.Scan(&rc) + } + return rc, err +} + // TopicSettings contains per-user settings for topics, including the "last read" message pointer. type TopicSettings struct { TopicId int32 `db:"topicid"` // unique ID of the topic diff --git a/ui/views/posts.jet b/ui/views/posts.jet index 6c7a640..45f1bb9 100644 --- a/ui/views/posts.jet +++ b/ui/views/posts.jet @@ -20,22 +20,46 @@