Files
amsterdam/conferenceadmin.go
T

425 lines
14 KiB
Go

/*
* Amsterdam Web Communities System
* Copyright (c) 2025-2026 Erbosoft Metaverse Design Solutions, All Rights Reserved
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
// Package main contains the high-level Amsterdam logic.
package main
import (
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"git.erbosoft.com/amy/amsterdam/database"
"git.erbosoft.com/amy/amsterdam/ui"
"git.erbosoft.com/amy/amsterdam/util"
log "github.com/sirupsen/logrus"
)
/* EditConferenceForm displays the dialog for editing the conference properties.
* 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 EditConferenceForm(ctxt ui.AmContext) (string, any, error) {
comm := ctxt.CurrentCommunity()
conf := ctxt.GetScratch("currentConference").(*database.Conference)
myLevel := ctxt.GetScratch("levelInConference").(uint16)
if !conf.TestPermission("Conference.Change", myLevel) {
ctxt.SetRC(http.StatusForbidden)
return ui.ErrorPage(ctxt, ENOPERM)
}
dlg, err := ui.AmLoadDialog("edit_conference")
if err != nil {
return ui.ErrorPage(ctxt, err)
}
dlg.SetCommunity(comm)
dlg.SetConference(conf, ctxt.GetScratch("currentAlias").(string))
dlg.Field("name").Value = conf.Name
dlg.Field("descr").SetVal(conf.Description)
if comm.TestPermission("Community.Create", ctxt.EffectiveLevel()) {
f, err := conf.HiddenInList(ctxt.Ctx(), comm)
if err != nil {
return ui.ErrorPage(ctxt, err)
}
dlg.Field("hide").SetChecked(f)
} else {
dlg.Field("hide").Disabled = true
}
dlg.Field("read_lvl").SetLevel(conf.ReadLevel)
dlg.Field("post_lvl").SetLevel(conf.PostLevel)
dlg.Field("create_lvl").SetLevel(conf.CreateLevel)
dlg.Field("hide_lvl").SetLevel(conf.HideLevel)
dlg.Field("nuke_lvl").SetLevel(conf.NukeLevel)
dlg.Field("change_lvl").SetLevel(conf.ChangeLevel)
dlg.Field("delete_lvl").SetLevel(conf.DeleteLevel)
flags, err := conf.Flags(ctxt.Ctx())
if err != nil {
return ui.ErrorPage(ctxt, err)
}
dlg.Field("pic_in_post").SetChecked(flags.Get(database.ConferenceFlagPicturesInPosts))
return dlg.Render(ctxt)
}
/* EditConference saves the conference properties being edited.
* 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 EditConference(ctxt ui.AmContext) (string, any, error) {
comm := ctxt.CurrentCommunity()
conf := ctxt.GetScratch("currentConference").(*database.Conference)
myLevel := ctxt.GetScratch("levelInConference").(uint16)
if !conf.TestPermission("Conference.Change", myLevel) {
ctxt.SetRC(http.StatusForbidden)
return ui.ErrorPage(ctxt, ENOPERM)
}
dlg, err := ui.AmLoadDialog("edit_conference")
if err != nil {
return ui.ErrorPage(ctxt, err)
}
button := dlg.WhichButton(ctxt)
if button == "cancel" {
return "redirect", fmt.Sprintf("/comm/%s/conf/%s/manage", comm.Alias, ctxt.GetScratch("currentAlias")), nil
} else if button != "update" {
dlg.SetCommunity(comm)
dlg.SetConference(conf, ctxt.GetScratch("currentAlias").(string))
return dlg.RenderError(ctxt, "invalid button pressed")
}
dlg.LoadFromForm(ctxt)
if err = dlg.Validate(); err == nil {
if err = conf.SetInfo(ctxt.Ctx(), dlg.Field("name").Value, dlg.Field("descr").Value, dlg.Field("read_lvl").GetLevel(), dlg.Field("post_lvl").GetLevel(),
dlg.Field("create_lvl").GetLevel(), dlg.Field("hide_lvl").GetLevel(), dlg.Field("nuke_lvl").GetLevel(), dlg.Field("change_lvl").GetLevel(),
dlg.Field("delete_lvl").GetLevel()); err == nil {
if err = conf.SetHiddenInList(ctxt.Ctx(), comm, dlg.Field("hide").IsChecked()); err == nil {
var flags *util.OptionSet
flags, err = conf.Flags(ctxt.Ctx())
if err == nil {
flags.Set(database.ConferenceFlagPicturesInPosts, dlg.Field("pic_in_post").IsChecked())
err = conf.SaveFlags(ctxt.Ctx(), flags)
}
}
}
}
if err != nil {
dlg.SetCommunity(comm)
dlg.SetConference(conf, ctxt.GetScratch("currentAlias").(string))
return dlg.RenderError(ctxt, err.Error())
}
return "redirect", fmt.Sprintf("/comm/%s/conf/%s/manage", comm.Alias, ctxt.GetScratch("currentAlias")), nil
}
/* ConferenceAliasForm displays the form for managing conference aliases.
* 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 ConferenceAliasForm(ctxt ui.AmContext) (string, any, error) {
comm := ctxt.CurrentCommunity()
conf := ctxt.GetScratch("currentConference").(*database.Conference)
myLevel := ctxt.GetScratch("levelInConference").(uint16)
if !conf.TestPermission("Conference.Change", myLevel) {
ctxt.SetRC(http.StatusForbidden)
return ui.ErrorPage(ctxt, ENOPERM)
}
ctxt.VarMap().Set("newAlias", "")
ctxt.VarMap().Set("confName", conf.Name)
ctxt.VarMap().Set("backLink", fmt.Sprintf("/comm/%s/conf/%s/manage", comm.Alias, ctxt.GetScratch("currentAlias")))
ctxt.VarMap().Set("selfLink", fmt.Sprintf("/comm/%s/conf/%s/aliases", comm.Alias, ctxt.GetScratch("currentAlias")))
ctxt.VarMap().Set("amsterdam_pageTitle", fmt.Sprintf("Manage Conference Aliases: %s", conf.Name))
if ctxt.HasParameter("del") {
err := conf.RemoveAlias(ctxt.Ctx(), ctxt.Parameter("del"), ctxt.CurrentUser(), ctxt.RemoteIP())
if err != nil {
ctxt.VarMap().Set("errorMessage", err.Error())
}
}
aliases, err := conf.Aliases(ctxt.Ctx())
if err != nil {
return ui.ErrorPage(ctxt, err)
}
ctxt.VarMap().Set("aliases", aliases)
return "framed_template", "conf_aliases.jet", nil
}
/* ConferenceAliasAdd adds a new alias to the current conference.
* 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 ConferenceAliasAdd(ctxt ui.AmContext) (string, any, error) {
comm := ctxt.CurrentCommunity()
conf := ctxt.GetScratch("currentConference").(*database.Conference)
myLevel := ctxt.GetScratch("levelInConference").(uint16)
if !conf.TestPermission("Conference.Change", myLevel) {
ctxt.SetRC(http.StatusForbidden)
return ui.ErrorPage(ctxt, ENOPERM)
}
ctxt.VarMap().Set("confName", conf.Name)
ctxt.VarMap().Set("backLink", fmt.Sprintf("/comm/%s/conf/%s/manage", comm.Alias, ctxt.GetScratch("currentAlias")))
ctxt.VarMap().Set("selfLink", fmt.Sprintf("/comm/%s/conf/%s/aliases", comm.Alias, ctxt.GetScratch("currentAlias")))
ctxt.VarMap().Set("amsterdam_pageTitle", fmt.Sprintf("Manage Conference Aliases: %s", conf.Name))
newAlias := ctxt.FormField("na")
ctxt.VarMap().Set("newAlias", newAlias)
var err error = nil
if ctxt.FormFieldIsSet("add") {
if database.AmIsValidAmsterdamID(newAlias) {
err = conf.AddAlias(ctxt.Ctx(), newAlias, ctxt.CurrentUser(), ctxt.RemoteIP())
} else {
err = fmt.Errorf("value '%s' is not a valid Amsterdam id", newAlias)
}
} else {
err = errors.New("invalid button press")
}
if err != nil {
ctxt.VarMap().Set("errorMessage", err.Error())
}
aliases, err := conf.Aliases(ctxt.Ctx())
if err != nil {
return ui.ErrorPage(ctxt, err)
}
ctxt.VarMap().Set("newAlias", "")
ctxt.VarMap().Set("aliases", aliases)
return "framed_template", "conf_aliases.jet", nil
}
// CMData is the result data passed to the conference members page.
type CMData struct {
User *database.User
Level uint16
}
// fieldMap maps field names to search field indexes.
var fieldMap = map[string]int{
"name": database.SearchUserFieldName,
"descr": database.SearchUserFieldDescription,
"first": database.SearchUserFieldFirstName,
"last": database.SearchUserFieldLastName,
}
// operMap maps operator names to search operator indices.
var operMap = map[string]int{
"st": database.SearchUserOperPrefix,
"in": database.SearchUserOperSubstring,
"re": database.SearchUserOperRegex,
}
/* ConferenceMembers shows the conference members and allows their access levels to be adjusted.
* 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 ConferenceMembers(ctxt ui.AmContext) (string, any, error) {
comm := ctxt.CurrentCommunity()
conf := ctxt.GetScratch("currentConference").(*database.Conference)
myLevel := ctxt.GetScratch("levelInConference").(uint16)
if !conf.TestPermission("Conference.Change", myLevel) {
ctxt.SetRC(http.StatusForbidden)
return ui.ErrorPage(ctxt, ENOPERM)
}
// Set the first batch of page variables.
ctxt.VarMap().Set("commName", comm.Name)
ctxt.VarMap().Set("confName", conf.Name)
ctxt.VarMap().Set("backLink", fmt.Sprintf("/comm/%s/conf/%s/manage", comm.Alias, ctxt.GetScratch("currentAlias")))
ctxt.VarMap().Set("selfLink", fmt.Sprintf("/comm/%s/conf/%s/members", comm.Alias, ctxt.GetScratch("currentAlias")))
ctxt.VarMap().Set("roleList", database.AmRoleList("Conference.UserLevels"))
ctxt.VarMap().Set("amsterdam_pageTitle", fmt.Sprintf("Membership in Conference: %s", conf.Name))
// Get the search parameter values and adjust them.
mode := ctxt.Parameter("mode")
field := ctxt.Parameter("field")
oper := ctxt.Parameter("oper")
term := ctxt.Parameter("term")
offsetStr := ctxt.Parameter("ofs")
if mode == "" {
mode = "conf"
}
if field == "" {
field = "name"
}
if oper == "" {
oper = "st"
}
offset := 0
if offsetStr != "" {
var err error
offset, err = strconv.Atoi(offsetStr)
if err != nil {
offset = 0
}
}
maxPage := ctxt.Globals().MaxSearchPage
// Adjust the offset based on the page buttons.
if ctxt.HasParameter("prev") {
offset = max(0, offset-int(maxPage))
} else if ctxt.HasParameter("next") {
offset += int(maxPage)
}
// Write the search parameters back to the page variables.
ctxt.VarMap().Set("mode", mode)
ctxt.VarMap().Set("field", field)
ctxt.VarMap().Set("oper", oper)
ctxt.VarMap().Set("term", term)
ctxt.VarMap().Set("offset", offset)
ctxt.VarMap().Set("max", maxPage)
if ctxt.HasParameter("update") {
// TODO: update the levels
}
// Get the member list for the conference.
members, err := conf.Members(ctxt.Ctx())
if err != nil {
return ui.ErrorPage(ctxt, err)
}
// Generate the result list.
total := 0
var mr []CMData
switch mode {
case "conf":
total = len(members)
if offset > 0 {
members = members[offset:]
}
if len(members) > int(maxPage) {
members = members[:maxPage]
}
mr = make([]CMData, len(members))
for i := range members {
mr[i].User, _ = database.AmGetUser(ctxt.Ctx(), members[i].Uid)
mr[i].Level = members[i].Level
}
case "comm":
ulist, t, err := database.AmSearchCommunityMembers(ctxt.Ctx(), comm, fieldMap[field], operMap[oper], term, offset, int(maxPage))
if err != nil {
return ui.ErrorPage(ctxt, err)
}
total = t
mr = make([]CMData, len(ulist))
for i := range ulist {
mr[i].User = ulist[i]
mr[i].Level = 0
for j := range members {
if members[j].Uid == ulist[i].Uid {
mr[i].Level = members[j].Level
break
}
}
}
}
// Set the last few variables and return.
ctxt.VarMap().Set("resultList", mr)
ctxt.VarMap().Set("total", total)
ctxt.VarMap().Set("validUids", strings.Join(util.Map(mr, func(cd CMData) string {
return fmt.Sprintf("%d", cd.User.Uid)
}), ","))
if offset > 0 {
ctxt.VarMap().Set("showPrev", true)
}
if (offset + len(mr)) < total {
ctxt.VarMap().Set("showNext", true)
}
return "framed_template", "conf_members.jet", nil
}
/* CreateConferenceForm displays the dialog for creating a new conference.
* 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 CreateConferenceForm(ctxt ui.AmContext) (string, any, error) {
comm := ctxt.CurrentCommunity()
if !comm.TestPermission("Community.Create", ctxt.EffectiveLevel()) {
ctxt.SetRC(http.StatusForbidden)
return ui.ErrorPage(ctxt, ENOPERM)
}
dlg, err := ui.AmLoadDialog("create_conference")
if err != nil {
return ui.ErrorPage(ctxt, err)
}
dlg.SetCommunity(comm)
return dlg.Render(ctxt)
}
/* CreateConference creates a new conference.
* 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 CreateConference(ctxt ui.AmContext) (string, any, error) {
comm := ctxt.CurrentCommunity()
if !comm.TestPermission("Community.Create", ctxt.EffectiveLevel()) {
ctxt.SetRC(http.StatusForbidden)
return ui.ErrorPage(ctxt, ENOPERM)
}
dlg, err := ui.AmLoadDialog("create_conference")
if err != nil {
return ui.ErrorPage(ctxt, err)
}
button := dlg.WhichButton(ctxt)
if button == "cancel" {
return "redirect", fmt.Sprintf("/comm/%s/conf", comm.Alias), nil
} else if button != "create" {
dlg.SetCommunity(comm)
return dlg.RenderError(ctxt, "invalid button pressed")
}
dlg.LoadFromForm(ctxt)
alias := dlg.Field("alias").Value
conf, err := database.AmCreateConference(ctxt.Ctx(), comm, dlg.Field("name").Value, alias, dlg.Field("descr").Value,
dlg.Field("ctype").Value == "1", dlg.Field("hide").IsChecked(), ctxt.CurrentUser(), ctxt.RemoteIP())
if err != nil {
dlg.SetCommunity(comm)
return dlg.RenderError(ctxt, err.Error())
}
log.Infof("Created conference '%s'", conf.Name)
return "redirect", fmt.Sprintf("/comm/%s/conf/%s", comm.Alias, alias), nil
}