diff --git a/conference_ops.go b/conference_ops.go index e21d023..94d71b8 100644 --- a/conference_ops.go +++ b/conference_ops.go @@ -429,6 +429,13 @@ func TopicManage(ctxt ui.AmContext) (string, any, error) { } ctxt.VarMap().Set("canInvite", member) + // Get the E-mail subscription status. + sub, err := topic.IsSubscribed(ctxt.Ctx(), ctxt.CurrentUser()) + if err != nil { + return ui.ErrorPage(ctxt, err) + } + ctxt.VarMap().Set("subscribed", sub) + // Get the filtered users list. bozos, err := topic.GetBozos(ctxt.Ctx(), ctxt.CurrentUser()) if err != nil { @@ -436,12 +443,36 @@ func TopicManage(ctxt ui.AmContext) (string, any, error) { } ctxt.VarMap().Set("bozos", bozos) - ctxt.VarMap().Set("subscribed", false) // TODO - ctxt.VarMap().Set("amsterdam_pageTitle", "Manage Topic: "+topic.Name) return "framed_template", "manage_topic.jet", nil } +/* TopicSetSubscribe toggles the "subscription" flag on the current topic for the current user. + * 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 TopicSetSubscribe(ctxt ui.AmContext) (string, any, error) { + if ctxt.CurrentUser().IsAnon { + ctxt.SetRC(http.StatusForbidden) + return ui.ErrorPage(ctxt, ENOPERM) + } + comm := ctxt.CurrentCommunity() + topic := ctxt.GetScratch("currentTopic").(*database.Topic) + flag, err := topic.IsSubscribed(ctxt.Ctx(), ctxt.CurrentUser()) + if err != nil { + return ui.ErrorPage(ctxt, err) + } + err = topic.SetSubscribed(ctxt.Ctx(), ctxt.CurrentUser(), !flag) + if err != nil { + return ui.ErrorPage(ctxt, err) + } + return "redirect", fmt.Sprintf("/comm/%s/conf/%s/op/%d/manage", comm.Alias, ctxt.GetScratch("currentAlias"), topic.Number), nil +} + /* TopicRemoveBozo removes filtering from a specified user in the topic. * Parameters: * ctxt - The AmContext for the request. diff --git a/database/topic.go b/database/topic.go index 2e7d111..f95abb9 100644 --- a/database/topic.go +++ b/database/topic.go @@ -240,6 +240,36 @@ func (t *Topic) GetBozos(ctx context.Context, u *User) ([]TopicBozo, error) { return rc, nil } +// IsSubscribed returns true if the given user is subscribed to receive E-mails of topic posts. +func (t *Topic) IsSubscribed(ctx context.Context, u *User) (bool, error) { + row := amdb.QueryRowContext(ctx, "SELECT subscribe FROM topicsettings WHERE topicid = ? AND uid = ?", t.TopicId, u.Uid) + var rc bool + err := row.Scan(&rc) + switch err { + case nil: + return rc, nil + case sql.ErrNoRows: + return false, nil + } + return false, err +} + +// SetSubscribed sets the "subscribed" flag for the given user. +func (t *Topic) SetSubscribed(ctx context.Context, u *User, flag bool) error { + if u.IsAnon { + return nil + } + rs, err := amdb.ExecContext(ctx, "UPDATE topicsettings SET subscribe = ? WHERE topicid = ? AND uid = ?", flag, t.TopicId, u.Uid) + if err == nil { + var rows int64 + rows, err = rs.RowsAffected() + if err == nil && rows == 0 { + _, err = amdb.ExecContext(ctx, "INSERT INTO topicsettings (topicid, uid, subscribe)", t.TopicId, u.Uid, flag) + } + } + return err +} + // GetSubscribers returns an array of UIDs of every user that subscribed to the topic. func (t *Topic) GetSubscribers(ctx context.Context) ([]int32, error) { rs, err := amdb.QueryContext(ctx, "SELECT uid FROM topicsettings WHERE topicid = ? AND subscribe <> 0", t.TopicId) diff --git a/docs/MISSINGFUNCS.md b/docs/MISSINGFUNCS.md index c57ff35..2c4c7d3 100644 --- a/docs/MISSINGFUNCS.md +++ b/docs/MISSINGFUNCS.md @@ -33,7 +33,7 @@ _(italicized items can be deferred)_ - Posts view: - Find - Manage: - - Subscribe to Topic + - ~~Subscribe to Topic~~ - Send invite - ~~Filtered Users (list/remove)~~ - ~~Stick/Unstick~~ diff --git a/main.go b/main.go index e1917dd..9004eec 100644 --- a/main.go +++ b/main.go @@ -114,6 +114,7 @@ func setupEcho() *echo.Echo { opsGroup.GET("/scribble/:msg", ui.AmWrap(ScribbleMessage)) opsGroup.GET("/nuke/:msg", ui.AmWrap(NukeMessage)) opsGroup.GET("/manage", ui.AmWrap(TopicManage)) + opsGroup.GET("/subscribe", ui.AmWrap(TopicSetSubscribe)) opsGroup.GET("/rmbozo/:uid", ui.AmWrap(TopicRemoveBozo)) return e diff --git a/ui/views/manage_topic.jet b/ui/views/manage_topic.jet index 62d08e7..669c5b4 100644 --- a/ui/views/manage_topic.jet +++ b/ui/views/manage_topic.jet @@ -29,7 +29,7 @@ {{ end }}
- + {{ if subscribed }} Click Here To Stop Subscribing To This Topic {{ else }}