wrote the code to handle attachment upload (and link from new-topic post)

This commit is contained in:
2025-11-17 21:50:53 -07:00
parent bf7504b6e9
commit 43bd810942
5 changed files with 169 additions and 15 deletions
+51
View File
@@ -0,0 +1,51 @@
/*
* Amsterdam Web Communities System
* Copyright (c) 2025 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/.
*/
// The database package contains database management and storage logic.
package database
import (
"errors"
"fmt"
"time"
)
type PostHeader struct {
PostId int64 `db:"postid"` // ID of the post
Parent int64 `db:"parent"` // ID of parent (unused?)
TopicId int32 `db:"topicid"` // topic containing the post
Num int32 `db:"num"` // post number
LineCount *int32 `db:"linecount"` // number of lines
CreatorUid int32 `db:"creator_uid"` // UID creating post
Posted time.Time `db:"posted"` // date posted
Hidden bool `db:"hidden"` // is post hidden?
ScribbleUid *int32 `db:"scribble_uid"` // UID of who scribbled it
ScribbleDate *time.Time `db:"scribble_date"` // when was it scribbled?
Pseud *string `db:"pseud"` // post's "pseud" (name/header)
}
func (p *PostHeader) SetAttachment(fileName string, mimeType string, length int32, data []byte) error {
_, err := amdb.Exec("INSERT INTO postattach (postid, datalen, filename, mimetype, data) VALUES (?, ?, ?, ?, ?)",
p.PostId, length, fileName, mimeType, data)
return err
}
func AmGetPost(postId int64) (*PostHeader, error) {
var dbdata []PostHeader
err := amdb.Select(&dbdata, "SELECT * FROM posts WHERE postid = ?", postId)
if err != nil {
return nil, err
}
if len(dbdata) == 0 {
return nil, errors.New("post not found")
}
if len(dbdata) > 1 {
return nil, fmt.Errorf("AmGetPost: too many entries (%d) for post ID %d", len(dbdata), postId)
}
return &(dbdata[0]), nil
}
+36 -10
View File
@@ -31,6 +31,25 @@ type Topic struct {
Name string `db:"name"` // topic name
}
// GetPost returns a post in the topic by number.
func (t *Topic) GetPost(num int32) (*PostHeader, error) {
if num > t.TopMessage {
return nil, fmt.Errorf("no post %d in topic %d", num, t.TopicId)
}
var dbdata []PostHeader
err := amdb.Select(&dbdata, "SELECT * FROM posts WHERE topicid = ? AND num = ?", t.TopicId, num)
if err == nil {
if len(dbdata) == 0 {
err = fmt.Errorf("no post %d in topic %d", num, t.TopicId)
} else if len(dbdata) > 1 {
err = fmt.Errorf("topic.GetPost: too many entries (%d) for post %d in topic %d", len(dbdata), num, t.TopicId)
} else {
return &(dbdata[0]), nil
}
}
return nil, 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
@@ -44,18 +63,25 @@ type TopicSettings struct {
// TopicSummary is a smaller data structure that gets topic information to create the topic list display.
type TopicSummary struct {
TopicID int32
Number int16
Name string
Unread int32
Total int32
LastUpdate time.Time
Frozen bool
Archived bool
Subscribed bool
Hidden bool
TopicID int32 // the topic ID
Number int16 // the number of the topic
Name string // the topic name
Unread int32 // number of unread messages
Total int32 // total number of messages
LastUpdate time.Time // last update timestamp
Frozen bool // is topic frozen?
Archived bool // is topic archived?
Subscribed bool // is topic subscribed?
Hidden bool // is topic hidden?
}
/* AmGetTopic retrieves a topic by ID.
* Parameters:
* topicId - ID of the topic to retrieve.
* Returns:
* The topic pointer, or nil.
* Standard Go error status.
*/
func AmGetTopic(topicId int32) (*Topic, error) {
var dbdata []Topic
err := amdb.Select(&dbdata, "SELECT * FROM topics WHERE topicid = ?", topicId)