2017-09-13 15:09:13 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Gosora Topic Store
|
|
|
|
* Copyright Azareal 2017 - 2018
|
|
|
|
*
|
|
|
|
*/
|
2017-06-13 07:12:58 +00:00
|
|
|
package main
|
|
|
|
|
2017-09-15 22:20:01 +00:00
|
|
|
import (
|
|
|
|
"database/sql"
|
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
|
|
|
"errors"
|
|
|
|
"strings"
|
2017-09-15 22:20:01 +00:00
|
|
|
"sync"
|
2017-09-18 17:03:52 +00:00
|
|
|
"sync/atomic"
|
2017-09-15 22:20:01 +00:00
|
|
|
|
|
|
|
"./query_gen/lib"
|
|
|
|
)
|
2017-06-13 07:12:58 +00:00
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// TODO: Add the watchdog goroutine
|
2017-09-15 22:20:01 +00:00
|
|
|
// TODO: Add BulkGetMap
|
|
|
|
// TODO: Add some sort of update method
|
2017-09-18 17:03:52 +00:00
|
|
|
// ? - Should we add stick, lock, unstick, and unlock methods? These might be better on the Topics not the TopicStore
|
2017-06-13 07:12:58 +00:00
|
|
|
var topics TopicStore
|
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
|
|
|
var ErrNoTitle = errors.New("This message is missing a title")
|
|
|
|
var ErrNoBody = errors.New("This message is missing a body")
|
2017-06-13 07:12:58 +00:00
|
|
|
|
|
|
|
type TopicStore interface {
|
|
|
|
Get(id int) (*Topic, error)
|
|
|
|
BypassGet(id int) (*Topic, error)
|
2017-09-15 22:20:01 +00:00
|
|
|
Exists(id int) bool
|
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
|
|
|
Create(fid int, topicName string, content string, uid int, ipaddress string) (tid int, err error)
|
2017-09-18 17:03:52 +00:00
|
|
|
AddLastTopic(item *Topic, fid int) error // unimplemented
|
|
|
|
// TODO: Implement these two methods
|
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
|
|
|
//Replies(tid int) ([]*Reply, error)
|
|
|
|
//RepliesRange(tid int, lower int, higher int) ([]*Reply, error)
|
|
|
|
GlobalCount() int
|
2017-09-15 22:20:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type TopicCache interface {
|
|
|
|
CacheGet(id int) (*Topic, error)
|
2017-09-18 17:03:52 +00:00
|
|
|
CacheGetUnsafe(id int) (*Topic, error)
|
2017-09-15 22:20:01 +00:00
|
|
|
CacheSet(item *Topic) error
|
|
|
|
CacheAdd(item *Topic) error
|
|
|
|
CacheAddUnsafe(item *Topic) error
|
|
|
|
CacheRemove(id int) error
|
|
|
|
CacheRemoveUnsafe(id int) error
|
2017-09-18 17:03:52 +00:00
|
|
|
Flush()
|
2017-09-22 02:21:17 +00:00
|
|
|
Reload(id int) error
|
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
|
|
|
Length() int
|
2017-09-15 22:20:01 +00:00
|
|
|
SetCapacity(capacity int)
|
2017-06-13 07:12:58 +00:00
|
|
|
GetCapacity() int
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
type MemoryTopicStore struct {
|
2017-09-15 22:20:01 +00:00
|
|
|
items map[int]*Topic
|
2017-09-18 17:03:52 +00:00
|
|
|
length int64 // sync/atomic only lets us operate on int32s and int64s
|
2017-09-15 22:20:01 +00:00
|
|
|
capacity int
|
|
|
|
get *sql.Stmt
|
|
|
|
exists *sql.Stmt
|
|
|
|
topicCount *sql.Stmt
|
2017-06-13 07:12:58 +00:00
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2017-09-13 15:09:13 +00:00
|
|
|
// NewMemoryTopicStore gives you a new instance of MemoryTopicStore
|
2017-11-02 13:35:19 +00:00
|
|
|
func NewMemoryTopicStore(capacity int) (*MemoryTopicStore, error) {
|
2017-11-06 07:23:32 +00:00
|
|
|
acc := qgen.Builder.Accumulator()
|
2017-06-15 11:40:35 +00:00
|
|
|
return &MemoryTopicStore{
|
2017-09-15 22:20:01 +00:00
|
|
|
items: make(map[int]*Topic),
|
|
|
|
capacity: capacity,
|
2017-11-06 07:23:32 +00:00
|
|
|
get: acc.SimpleSelect("topics", "title, content, createdBy, createdAt, lastReplyAt, is_closed, sticky, parentID, ipaddress, postCount, likeCount, data", "tid = ?", "", ""),
|
|
|
|
exists: acc.SimpleSelect("topics", "tid", "tid = ?", "", ""),
|
|
|
|
topicCount: acc.SimpleCount("topics", "", ""),
|
|
|
|
}, acc.FirstError()
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-18 17:03:52 +00:00
|
|
|
func (mts *MemoryTopicStore) CacheGet(id int) (*Topic, error) {
|
|
|
|
mts.RLock()
|
|
|
|
item, ok := mts.items[id]
|
|
|
|
mts.RUnlock()
|
2017-06-13 07:12:58 +00:00
|
|
|
if ok {
|
|
|
|
return item, nil
|
|
|
|
}
|
2017-06-28 12:05:26 +00:00
|
|
|
return item, ErrNoRows
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-18 17:03:52 +00:00
|
|
|
func (mts *MemoryTopicStore) CacheGetUnsafe(id int) (*Topic, error) {
|
|
|
|
item, ok := mts.items[id]
|
2017-06-13 07:12:58 +00:00
|
|
|
if ok {
|
|
|
|
return item, nil
|
|
|
|
}
|
2017-06-28 12:05:26 +00:00
|
|
|
return item, ErrNoRows
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-18 17:03:52 +00:00
|
|
|
func (mts *MemoryTopicStore) Get(id int) (*Topic, error) {
|
|
|
|
mts.RLock()
|
|
|
|
topic, ok := mts.items[id]
|
|
|
|
mts.RUnlock()
|
2017-06-13 07:12:58 +00:00
|
|
|
if ok {
|
|
|
|
return topic, nil
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
topic = &Topic{ID: id}
|
2017-09-28 22:16:34 +00:00
|
|
|
err := mts.get.QueryRow(id).Scan(&topic.Title, &topic.Content, &topic.CreatedBy, &topic.CreatedAt, &topic.LastReplyAt, &topic.IsClosed, &topic.Sticky, &topic.ParentID, &topic.IPAddress, &topic.PostCount, &topic.LikeCount, &topic.Data)
|
2017-06-13 07:12:58 +00:00
|
|
|
if err == nil {
|
2017-09-03 04:50:31 +00:00
|
|
|
topic.Link = buildTopicURL(nameToSlug(topic.Title), id)
|
2017-09-18 17:03:52 +00:00
|
|
|
_ = mts.CacheAdd(topic)
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
return topic, err
|
|
|
|
}
|
|
|
|
|
2017-09-18 17:03:52 +00:00
|
|
|
// BypassGet will always bypass the cache and pull the topic directly from the database
|
|
|
|
func (mts *MemoryTopicStore) BypassGet(id int) (*Topic, error) {
|
2017-09-03 04:50:31 +00:00
|
|
|
topic := &Topic{ID: id}
|
2017-09-28 22:16:34 +00:00
|
|
|
err := mts.get.QueryRow(id).Scan(&topic.Title, &topic.Content, &topic.CreatedBy, &topic.CreatedAt, &topic.LastReplyAt, &topic.IsClosed, &topic.Sticky, &topic.ParentID, &topic.IPAddress, &topic.PostCount, &topic.LikeCount, &topic.Data)
|
2017-09-03 04:50:31 +00:00
|
|
|
topic.Link = buildTopicURL(nameToSlug(topic.Title), id)
|
2017-06-13 07:12:58 +00:00
|
|
|
return topic, err
|
|
|
|
}
|
|
|
|
|
2017-09-18 17:03:52 +00:00
|
|
|
func (mts *MemoryTopicStore) Reload(id int) error {
|
2017-09-03 04:50:31 +00:00
|
|
|
topic := &Topic{ID: id}
|
2017-09-28 22:16:34 +00:00
|
|
|
err := mts.get.QueryRow(id).Scan(&topic.Title, &topic.Content, &topic.CreatedBy, &topic.CreatedAt, &topic.LastReplyAt, &topic.IsClosed, &topic.Sticky, &topic.ParentID, &topic.IPAddress, &topic.PostCount, &topic.LikeCount, &topic.Data)
|
2017-06-13 07:12:58 +00:00
|
|
|
if err == nil {
|
2017-09-03 04:50:31 +00:00
|
|
|
topic.Link = buildTopicURL(nameToSlug(topic.Title), id)
|
2017-09-18 17:03:52 +00:00
|
|
|
_ = mts.CacheSet(topic)
|
2017-06-13 07:12:58 +00:00
|
|
|
} else {
|
2017-09-18 17:03:52 +00:00
|
|
|
_ = mts.CacheRemove(id)
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-18 17:03:52 +00:00
|
|
|
func (mts *MemoryTopicStore) Exists(id int) bool {
|
|
|
|
return mts.exists.QueryRow(id).Scan(&id) == nil
|
2017-09-15 22:20:01 +00:00
|
|
|
}
|
|
|
|
|
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
|
|
|
func (mts *MemoryTopicStore) Create(fid int, topicName string, content string, uid int, ipaddress string) (tid int, err error) {
|
|
|
|
topicName = strings.TrimSpace(topicName)
|
|
|
|
if topicName == "" {
|
|
|
|
return 0, ErrNoBody
|
|
|
|
}
|
|
|
|
|
|
|
|
content = strings.TrimSpace(content)
|
|
|
|
parsedContent := parseMessage(content, fid, "forums")
|
|
|
|
if strings.TrimSpace(parsedContent) == "" {
|
|
|
|
return 0, ErrNoBody
|
|
|
|
}
|
|
|
|
|
|
|
|
wcount := wordCount(content)
|
|
|
|
// TODO: Move this statement into the topic store
|
2017-11-05 09:55:34 +00:00
|
|
|
res, err := stmts.createTopic.Exec(fid, topicName, content, parsedContent, uid, ipaddress, wcount, uid)
|
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lastID, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = fstore.AddTopic(int(lastID), uid, fid)
|
|
|
|
return int(lastID), err
|
|
|
|
}
|
|
|
|
|
2017-09-18 17:03:52 +00:00
|
|
|
func (mts *MemoryTopicStore) CacheSet(item *Topic) error {
|
|
|
|
mts.Lock()
|
|
|
|
_, ok := mts.items[item.ID]
|
2017-06-13 07:12:58 +00:00
|
|
|
if ok {
|
2017-09-18 17:03:52 +00:00
|
|
|
mts.items[item.ID] = item
|
|
|
|
} else if int(mts.length) >= mts.capacity {
|
|
|
|
mts.Unlock()
|
2017-06-13 07:12:58 +00:00
|
|
|
return ErrStoreCapacityOverflow
|
|
|
|
} else {
|
2017-09-18 17:03:52 +00:00
|
|
|
mts.items[item.ID] = item
|
|
|
|
atomic.AddInt64(&mts.length, 1)
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
2017-09-18 17:03:52 +00:00
|
|
|
mts.Unlock()
|
2017-06-13 07:12:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-18 17:03:52 +00:00
|
|
|
func (mts *MemoryTopicStore) CacheAdd(item *Topic) error {
|
|
|
|
if int(mts.length) >= mts.capacity {
|
2017-06-13 07:12:58 +00:00
|
|
|
return ErrStoreCapacityOverflow
|
|
|
|
}
|
2017-09-18 17:03:52 +00:00
|
|
|
mts.Lock()
|
|
|
|
mts.items[item.ID] = item
|
|
|
|
mts.Unlock()
|
|
|
|
atomic.AddInt64(&mts.length, 1)
|
2017-06-13 07:12:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-15 22:20:01 +00:00
|
|
|
// TODO: Make these length increments thread-safe. Ditto for the other DataStores
|
2017-09-18 17:03:52 +00:00
|
|
|
func (mts *MemoryTopicStore) CacheAddUnsafe(item *Topic) error {
|
|
|
|
if int(mts.length) >= mts.capacity {
|
2017-06-13 07:12:58 +00:00
|
|
|
return ErrStoreCapacityOverflow
|
|
|
|
}
|
2017-09-18 17:03:52 +00:00
|
|
|
mts.items[item.ID] = item
|
|
|
|
atomic.AddInt64(&mts.length, 1)
|
2017-06-13 07:12:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-15 22:20:01 +00:00
|
|
|
// TODO: Make these length decrements thread-safe. Ditto for the other DataStores
|
2017-09-18 17:03:52 +00:00
|
|
|
func (mts *MemoryTopicStore) CacheRemove(id int) error {
|
|
|
|
mts.Lock()
|
|
|
|
delete(mts.items, id)
|
|
|
|
mts.Unlock()
|
|
|
|
atomic.AddInt64(&mts.length, -1)
|
2017-06-13 07:12:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-18 17:03:52 +00:00
|
|
|
func (mts *MemoryTopicStore) CacheRemoveUnsafe(id int) error {
|
|
|
|
delete(mts.items, id)
|
|
|
|
atomic.AddInt64(&mts.length, -1)
|
2017-06-13 07:12:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-15 22:20:01 +00:00
|
|
|
// ? - What is this? Do we need it? Should it be in the main store interface?
|
2017-09-18 17:03:52 +00:00
|
|
|
func (mts *MemoryTopicStore) AddLastTopic(item *Topic, fid int) error {
|
2017-06-13 07:12:58 +00:00
|
|
|
// Coming Soon...
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-18 17:03:52 +00:00
|
|
|
func (mts *MemoryTopicStore) Flush() {
|
|
|
|
mts.Lock()
|
|
|
|
mts.items = make(map[int]*Topic)
|
|
|
|
mts.length = 0
|
|
|
|
mts.Unlock()
|
|
|
|
}
|
|
|
|
|
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
|
|
|
// ! Is this concurrent?
|
|
|
|
// Length returns the number of topics in the memory cache
|
|
|
|
func (mts *MemoryTopicStore) Length() int {
|
2017-09-18 17:03:52 +00:00
|
|
|
return int(mts.length)
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-18 17:03:52 +00:00
|
|
|
func (mts *MemoryTopicStore) SetCapacity(capacity int) {
|
|
|
|
mts.capacity = capacity
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-18 17:03:52 +00:00
|
|
|
func (mts *MemoryTopicStore) GetCapacity() int {
|
|
|
|
return mts.capacity
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
|
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
|
|
|
// GlobalCount returns the total number of topics on these forums
|
|
|
|
func (mts *MemoryTopicStore) GlobalCount() int {
|
2017-09-15 22:20:01 +00:00
|
|
|
var tcount int
|
2017-09-18 17:03:52 +00:00
|
|
|
err := mts.topicCount.QueryRow().Scan(&tcount)
|
2017-09-15 22:20:01 +00:00
|
|
|
if err != nil {
|
|
|
|
LogError(err)
|
|
|
|
}
|
|
|
|
return tcount
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
type SQLTopicStore struct {
|
2017-09-15 22:20:01 +00:00
|
|
|
get *sql.Stmt
|
|
|
|
exists *sql.Stmt
|
|
|
|
topicCount *sql.Stmt
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
|
2017-11-02 13:35:19 +00:00
|
|
|
func NewSQLTopicStore() (*SQLTopicStore, error) {
|
2017-11-06 07:23:32 +00:00
|
|
|
acc := qgen.Builder.Accumulator()
|
2017-09-15 22:20:01 +00:00
|
|
|
return &SQLTopicStore{
|
2017-11-06 07:23:32 +00:00
|
|
|
get: acc.SimpleSelect("topics", "title, content, createdBy, createdAt, lastReplyAt, is_closed, sticky, parentID, ipaddress, postCount, likeCount, data", "tid = ?", "", ""),
|
|
|
|
exists: acc.SimpleSelect("topics", "tid", "tid = ?", "", ""),
|
|
|
|
topicCount: acc.SimpleCount("topics", "", ""),
|
|
|
|
}, acc.FirstError()
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
func (sts *SQLTopicStore) Get(id int) (*Topic, error) {
|
2017-09-03 04:50:31 +00:00
|
|
|
topic := Topic{ID: id}
|
2017-09-28 22:16:34 +00:00
|
|
|
err := sts.get.QueryRow(id).Scan(&topic.Title, &topic.Content, &topic.CreatedBy, &topic.CreatedAt, &topic.LastReplyAt, &topic.IsClosed, &topic.Sticky, &topic.ParentID, &topic.IPAddress, &topic.PostCount, &topic.LikeCount, &topic.Data)
|
2017-09-03 04:50:31 +00:00
|
|
|
topic.Link = buildTopicURL(nameToSlug(topic.Title), id)
|
2017-06-13 07:12:58 +00:00
|
|
|
return &topic, err
|
|
|
|
}
|
|
|
|
|
2017-09-18 17:03:52 +00:00
|
|
|
// BypassGet is an alias of Get(), as we don't have a cache for SQLTopicStore
|
2017-09-10 16:57:22 +00:00
|
|
|
func (sts *SQLTopicStore) BypassGet(id int) (*Topic, error) {
|
2017-09-03 04:50:31 +00:00
|
|
|
topic := &Topic{ID: id}
|
2017-09-28 22:16:34 +00:00
|
|
|
err := sts.get.QueryRow(id).Scan(&topic.Title, &topic.Content, &topic.CreatedBy, &topic.CreatedAt, &topic.LastReplyAt, &topic.IsClosed, &topic.Sticky, &topic.ParentID, &topic.IPAddress, &topic.PostCount, &topic.LikeCount, &topic.Data)
|
2017-09-03 04:50:31 +00:00
|
|
|
topic.Link = buildTopicURL(nameToSlug(topic.Title), id)
|
2017-06-13 07:12:58 +00:00
|
|
|
return topic, err
|
|
|
|
}
|
|
|
|
|
2017-09-15 22:20:01 +00:00
|
|
|
func (sts *SQLTopicStore) Exists(id int) bool {
|
|
|
|
return sts.exists.QueryRow(id).Scan(&id) == nil
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
2017-09-15 22:20:01 +00:00
|
|
|
|
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
|
|
|
func (sts *SQLTopicStore) Create(fid int, topicName string, content string, uid int, ipaddress string) (tid int, err error) {
|
|
|
|
topicName = strings.TrimSpace(topicName)
|
|
|
|
if topicName == "" {
|
|
|
|
return 0, ErrNoBody
|
|
|
|
}
|
|
|
|
|
|
|
|
content = strings.TrimSpace(content)
|
|
|
|
parsedContent := parseMessage(content, fid, "forums")
|
|
|
|
if strings.TrimSpace(parsedContent) == "" {
|
|
|
|
return 0, ErrNoBody
|
|
|
|
}
|
|
|
|
|
|
|
|
wcount := wordCount(content)
|
|
|
|
// TODO: Move this statement into the topic store
|
2017-11-05 09:55:34 +00:00
|
|
|
res, err := stmts.createTopic.Exec(fid, topicName, content, parsedContent, uid, ipaddress, wcount, uid)
|
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lastID, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = fstore.AddTopic(int(lastID), uid, fid)
|
|
|
|
return int(lastID), err
|
|
|
|
}
|
|
|
|
|
2017-09-18 17:03:52 +00:00
|
|
|
// ? - What're we going to do about this?
|
2017-09-10 16:57:22 +00:00
|
|
|
func (sts *SQLTopicStore) AddLastTopic(item *Topic, fid int) error {
|
2017-06-13 07:12:58 +00:00
|
|
|
// Coming Soon...
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
|
|
|
// GlobalCount returns the total number of topics on these forums
|
|
|
|
func (sts *SQLTopicStore) GlobalCount() int {
|
2017-09-15 22:20:01 +00:00
|
|
|
var tcount int
|
|
|
|
err := sts.topicCount.QueryRow().Scan(&tcount)
|
|
|
|
if err != nil {
|
|
|
|
LogError(err)
|
|
|
|
}
|
|
|
|
return tcount
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|