all database operations now take a context.Context, which is propagated through from sources

This commit is contained in:
2025-12-20 22:29:26 -07:00
parent 9e6bf2feda
commit 5c8bb8dd5e
39 changed files with 605 additions and 504 deletions
+28 -26
View File
@@ -41,7 +41,7 @@ func Conferences(ctxt ui.AmContext) (string, any, error) {
ctxt.VarMap().Set("commName", comm.Name)
ctxt.VarMap().Set("commAlias", comm.Alias)
ctxt.VarMap().Set("amsterdam_pageTitle", "Conference Listing: "+comm.Name)
clist, err := database.AmGetCommunityConferences(comm.Id,
clist, err := database.AmGetCommunityConferences(ctxt.Ctx(), comm.Id,
comm.TestPermission("Community.ShowHiddenObjects", ctxt.EffectiveLevel()))
if err != nil {
return ui.ErrorPage(ctxt, err)
@@ -59,7 +59,7 @@ func Conferences(ctxt ui.AmContext) (string, any, error) {
* Standard Go error status.
*/
func Topics(ctxt ui.AmContext) (string, any, error) {
prefs, err := ctxt.CurrentUser().Prefs()
prefs, err := ctxt.CurrentUser().Prefs(ctxt.Ctx())
if err != nil {
return ui.ErrorPage(ctxt, err)
}
@@ -94,7 +94,7 @@ func Topics(ctxt ui.AmContext) (string, any, error) {
sort = ctxt.QueryParamInt("sort", sort)
ctxt.SetSession("topic.sort", sort)
topics, err := database.AmListTopics(conf.ConfId, ctxt.CurrentUserId(), view, sort, false)
topics, err := database.AmListTopics(ctxt.Ctx(), conf.ConfId, ctxt.CurrentUserId(), view, sort, false)
if err != nil {
return ui.ErrorPage(ctxt, err)
}
@@ -137,12 +137,12 @@ func NewTopicForm(ctxt ui.AmContext) (string, any, error) {
ctxt.VarMap().Set("conferenceName", conf.Name)
ctxt.VarMap().Set("urlStem", fmt.Sprintf("/comm/%s/conf/%s", comm.Alias, ctxt.URLParam("confid")))
ctxt.VarMap().Set("topicName", "")
cs, err := conf.Settings(ctxt.CurrentUser())
cs, err := conf.Settings(ctxt.Ctx(), ctxt.CurrentUser())
if err != nil {
return ui.ErrorPage(ctxt, err)
}
if cs == nil || cs.DefaultPseud == nil {
ci, err := ctxt.CurrentUser().ContactInfo()
ci, err := ctxt.CurrentUser().ContactInfo(ctxt.Ctx())
if err != nil {
return ui.ErrorPage(ctxt, err)
}
@@ -178,7 +178,7 @@ func NewTopic(ctxt ui.AmContext) (string, any, error) {
}
if ctxt.FormFieldIsSet("preview") {
// start by escaping the title
checker, err := htmlcheck.AmNewHTMLChecker("escaper")
checker, err := htmlcheck.AmNewHTMLChecker(ctxt.Ctx(), "escaper")
if err != nil {
return ui.ErrorPage(ctxt, err)
}
@@ -203,7 +203,7 @@ func NewTopic(ctxt ui.AmContext) (string, any, error) {
ctxt.VarMap().Set("pb", v)
// run the preview
checker, err = htmlcheck.AmNewHTMLChecker("preview")
checker, err = htmlcheck.AmNewHTMLChecker(ctxt.Ctx(), "preview")
if err != nil {
return ui.ErrorPage(ctxt, err)
}
@@ -225,7 +225,7 @@ func NewTopic(ctxt ui.AmContext) (string, any, error) {
}
if ctxt.FormFieldIsSet("post1") {
// start by checking the title and pseud
checker, err := htmlcheck.AmNewHTMLChecker("post-pseud")
checker, err := htmlcheck.AmNewHTMLChecker(ctxt.Ctx(), "post-pseud")
if err != nil {
return ui.ErrorPage(ctxt, err)
}
@@ -238,7 +238,7 @@ func NewTopic(ctxt ui.AmContext) (string, any, error) {
zeroPostPseud, _ := checker.Value()
// now check the post data itself
checker, err = htmlcheck.AmNewHTMLChecker("post-body")
checker, err = htmlcheck.AmNewHTMLChecker(ctxt.Ctx(), "post-body")
if err != nil {
return ui.ErrorPage(ctxt, err)
}
@@ -249,7 +249,7 @@ func NewTopic(ctxt ui.AmContext) (string, any, error) {
lines, _ := checker.Lines()
// Add the topic!
topic, err := database.AmNewTopic(conf, ctxt.CurrentUser(), topicName, zeroPostPseud, zeroPost, int32(lines), ctxt.RemoteIP())
topic, err := database.AmNewTopic(ctxt.Ctx(), conf, ctxt.CurrentUser(), topicName, zeroPostPseud, zeroPost, int32(lines), ctxt.RemoteIP())
if err != nil {
return ui.ErrorPage(ctxt, err)
}
@@ -258,7 +258,7 @@ func NewTopic(ctxt ui.AmContext) (string, any, error) {
return "redirect", urlStem, nil // no attachment - just redisplay topic list
}
post, err := topic.GetPost(0) // get the initial post in the new topic
post, err := topic.GetPost(ctxt.Ctx(), 0) // get the initial post in the new topic
if err != nil {
return ui.ErrorPage(ctxt, err)
}
@@ -303,12 +303,12 @@ func AttachmentUpload(ctxt ui.AmContext) (string, any, error) {
if err == nil {
if file.Size <= (1024 * 1024) { // 1 Mb
var post *database.PostHeader
post, err = database.AmGetPost(postId)
post, err = database.AmGetPost(ctxt.Ctx(), postId)
if err == nil {
var data []byte
data, err = slurpFile(file)
if err == nil {
err = post.SetAttachment(file.Filename, file.Header.Get("Content-Type"), int32(file.Size), data)
err = post.SetAttachment(ctxt.Ctx(), file.Filename, file.Header.Get("Content-Type"), int32(file.Size), data)
if err == nil {
return "redirect", target, nil
}
@@ -370,7 +370,8 @@ func breakRange(topic *database.Topic, into []int32, param string, sep string) e
func templateExtractUserName(args jet.Arguments) reflect.Value {
rc := "<<ERROR>>"
post := args.Get(0).Convert(reflect.TypeFor[*database.PostHeader]()).Interface().(*database.PostHeader)
user, err := database.AmGetUser(post.CreatorUid)
ctxt := args.Get(1).Convert(reflect.TypeFor[ui.AmContext]()).Interface().(ui.AmContext)
user, err := database.AmGetUser(ctxt.Ctx(), post.CreatorUid)
if err == nil {
rc = user.Username
} else {
@@ -381,7 +382,8 @@ func templateExtractUserName(args jet.Arguments) reflect.Value {
func templatePostText(args jet.Arguments) reflect.Value {
post := args.Get(0).Convert(reflect.TypeFor[*database.PostHeader]()).Interface().(*database.PostHeader)
rc, err := post.Text()
ctxt := args.Get(1).Convert(reflect.TypeFor[ui.AmContext]()).Interface().(ui.AmContext)
rc, err := post.Text(ctxt.Ctx())
if err != nil {
log.Errorf("templatePostText could not get post text from post #%d: %v", post.PostId, err)
rc = ""
@@ -395,10 +397,10 @@ func templateOverrideLine(args jet.Arguments) reflect.Value {
rc := ""
if post.IsScribbled() {
scr_date := ""
scr_user, err := database.AmGetUser(*post.ScribbleUid)
scr_user, err := database.AmGetUser(ctxt.Ctx(), *post.ScribbleUid)
if err == nil {
var p *database.UserPrefs
p, err = ctxt.CurrentUser().Prefs()
p, err = ctxt.CurrentUser().Prefs(ctxt.Ctx())
if err == nil {
scr_date = p.Localizer().Strftime("%b %e, %Y %r", *post.ScribbleDate)
}
@@ -430,20 +432,20 @@ func ReadPosts(ctxt ui.AmContext) (string, any, error) {
rst := strings.Split(ctxt.Parameter("rst"), ",")
if len(rst) >= 2 {
user := ctxt.CurrentUser()
ampool.Submit(func(context.Context) {
ampool.Submit(func(ctx context.Context) {
topicId, e1 := strconv.ParseInt(rst[0], 10, 32)
lastRead, e2 := strconv.ParseInt(rst[1], 10, 32)
if e1 == nil && e2 == nil {
topic, _ := database.AmGetTopic(int32(topicId))
topic, _ := database.AmGetTopic(ctx, int32(topicId))
if topic != nil {
topic.SetLastRead(user, int32(lastRead))
topic.SetLastRead(ctx, user, int32(lastRead))
}
}
})
}
}
// Get user prefs.
prefs, err := ctxt.CurrentUser().Prefs()
prefs, err := ctxt.CurrentUser().Prefs(ctxt.Ctx())
if err != nil {
return ui.ErrorPage(ctxt, err)
}
@@ -452,7 +454,7 @@ func ReadPosts(ctxt ui.AmContext) (string, any, error) {
conf := ctxt.GetScratch("currentConference").(*database.Conference)
var topic *database.Topic = nil
if rawTopic, err := strconv.ParseInt(ctxt.URLParam("topic"), 10, 16); err == nil {
topic, err = database.AmGetTopicByNumber(conf, int16(rawTopic))
topic, err = database.AmGetTopicByNumber(ctxt.Ctx(), conf, int16(rawTopic))
}
if topic == nil {
ctxt.SetRC(http.StatusNotFound)
@@ -460,7 +462,7 @@ func ReadPosts(ctxt ui.AmContext) (string, any, error) {
}
// Determine the range of posts to display. The "pin" is the post number after which we display the horizontal line separating old and new posts.
lastRead, err := topic.GetLastRead(ctxt.CurrentUser())
lastRead, err := topic.GetLastRead(ctxt.Ctx(), ctxt.CurrentUser())
if err != nil {
ctxt.SetRC(http.StatusNotFound)
return ui.ErrorPage(ctxt, fmt.Errorf("posts not found in topic %d - %v", topic.Number, err))
@@ -495,7 +497,7 @@ func ReadPosts(ctxt ui.AmContext) (string, any, error) {
}
// Load the actual posts.
posts, err := database.AmGetPostRange(topic, postRange[0], postRange[1])
posts, err := database.AmGetPostRange(ctxt.Ctx(), topic, postRange[0], postRange[1])
if err != nil {
return ui.ErrorPage(ctxt, fmt.Errorf("internal error getting posts <%d:%d-%d> - %v", topic.Number, postRange[0], postRange[1], err))
}
@@ -533,8 +535,8 @@ func ReadPosts(ctxt ui.AmContext) (string, any, error) {
ctxt.VarMap().Set("amsterdam_pageTitle", fmt.Sprintf("%s: %s", topic.Name, summaryLine))
if resetLastRead {
user := ctxt.CurrentUser()
ampool.Submit(func(context.Context) {
topic.SetLastRead(user, topic.TopMessage)
ampool.Submit(func(ctx context.Context) {
topic.SetLastRead(ctx, user, topic.TopMessage)
})
}
return "framed_template", "posts.jet", nil