2017-11-11 04:06:16 +00:00
package common
2017-11-11 23:34:27 +00:00
import (
"database/sql"
"sync/atomic"
2019-11-08 21:46:50 +00:00
qgen "github.com/Azareal/Gosora/query_gen"
2017-11-11 23:34:27 +00:00
)
2017-11-11 04:06:16 +00:00
2018-08-04 11:46:36 +00:00
// TODO: Move some features into methods on this?
2017-11-11 04:06:16 +00:00
type WordFilter struct {
2019-11-10 02:37:53 +00:00
ID int
Find string
Replace string
}
type WordFilterDiff struct {
BeforeFind string
BeforeReplace string
AfterFind string
AfterReplace string
2017-11-11 04:06:16 +00:00
}
2018-08-04 11:46:36 +00:00
var WordFilters WordFilterStore
2017-11-11 04:06:16 +00:00
2018-08-04 11:46:36 +00:00
type WordFilterStore interface {
ReloadAll ( ) error
GetAll ( ) ( filters map [ int ] * WordFilter , err error )
2019-11-08 21:46:50 +00:00
Get ( id int ) ( * WordFilter , error )
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
Create ( find , replace string ) ( int , error )
2018-08-04 11:46:36 +00:00
Delete ( id int ) error
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
Update ( id int , find , replace string ) error
2018-08-04 11:46:36 +00:00
Length ( ) int
EstCount ( ) int
2019-06-01 12:31:48 +00:00
Count ( ) ( count int )
2017-11-11 23:34:27 +00:00
}
2018-08-04 11:46:36 +00:00
type DefaultWordFilterStore struct {
box atomic . Value // An atomic value holding a WordFilterMap
2017-11-11 23:34:27 +00:00
2018-08-04 11:46:36 +00:00
getAll * sql . Stmt
2019-11-08 21:46:50 +00:00
get * sql . Stmt
2018-08-04 11:46:36 +00:00
create * sql . Stmt
delete * sql . Stmt
update * sql . Stmt
count * sql . Stmt
2017-11-11 04:06:16 +00:00
}
2018-08-04 11:46:36 +00:00
func NewDefaultWordFilterStore ( acc * qgen . Accumulator ) ( * DefaultWordFilterStore , error ) {
2019-10-28 07:46:14 +00:00
wf := "word_filters"
2018-08-04 11:46:36 +00:00
store := & DefaultWordFilterStore {
2019-10-28 07:46:14 +00:00
getAll : acc . Select ( wf ) . Columns ( "wfid,find,replacement" ) . Prepare ( ) ,
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
get : acc . Select ( wf ) . Columns ( "find,replacement" ) . Where ( "wfid=?" ) . Prepare ( ) ,
2019-10-28 07:46:14 +00:00
create : acc . Insert ( wf ) . Columns ( "find,replacement" ) . Fields ( "?,?" ) . Prepare ( ) ,
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
delete : acc . Delete ( wf ) . Where ( "wfid=?" ) . Prepare ( ) ,
update : acc . Update ( wf ) . Set ( "find=?,replacement=?" ) . Where ( "wfid=?" ) . Prepare ( ) ,
2019-10-28 07:46:14 +00:00
count : acc . Count ( wf ) . Prepare ( ) ,
2018-08-04 11:46:36 +00:00
}
// TODO: Should we initialise this elsewhere?
if acc . FirstError ( ) == nil {
acc . RecordError ( store . ReloadAll ( ) )
}
return store , acc . FirstError ( )
}
// ReloadAll drops all the items in the memory cache and replaces them with fresh copies from the database
2019-06-01 12:31:48 +00:00
func ( s * DefaultWordFilterStore ) ReloadAll ( ) error {
2019-10-06 05:32:08 +00:00
wordFilters := make ( map [ int ] * WordFilter )
2019-06-01 12:31:48 +00:00
filters , err := s . bypassGetAll ( )
2017-11-11 04:06:16 +00:00
if err != nil {
return err
}
2017-11-23 05:37:08 +00:00
for _ , filter := range filters {
wordFilters [ filter . ID ] = filter
}
2019-06-01 12:31:48 +00:00
s . box . Store ( wordFilters )
2017-11-23 05:37:08 +00:00
return nil
}
2018-08-04 11:46:36 +00:00
// ? - Return pointers to word filters intead to save memory? -- A map is a pointer.
2019-06-01 12:31:48 +00:00
func ( s * DefaultWordFilterStore ) bypassGetAll ( ) ( filters [ ] * WordFilter , err error ) {
rows , err := s . getAll . Query ( )
2017-11-23 05:37:08 +00:00
if err != nil {
return nil , err
}
defer rows . Close ( )
2017-11-11 04:06:16 +00:00
for rows . Next ( ) {
2019-10-06 05:32:08 +00:00
f := & WordFilter { ID : 0 }
2019-11-10 02:37:53 +00:00
err := rows . Scan ( & f . ID , & f . Find , & f . Replace )
2017-11-11 04:06:16 +00:00
if err != nil {
2017-11-23 05:37:08 +00:00
return filters , err
2017-11-11 04:06:16 +00:00
}
2019-10-06 05:32:08 +00:00
filters = append ( filters , f )
2017-11-11 04:06:16 +00:00
}
2017-11-23 05:37:08 +00:00
return filters , rows . Err ( )
2017-11-11 04:06:16 +00:00
}
2018-08-04 11:46:36 +00:00
// GetAll returns all of the word filters in a map. Do note mutate this map (or maps returned from any store not explicitly noted as copies) as multiple threads may be accessing it at once
2019-06-01 12:31:48 +00:00
func ( s * DefaultWordFilterStore ) GetAll ( ) ( filters map [ int ] * WordFilter , err error ) {
return s . box . Load ( ) . ( map [ int ] * WordFilter ) , nil
2018-08-04 11:46:36 +00:00
}
2019-11-08 21:46:50 +00:00
func ( s * DefaultWordFilterStore ) Get ( id int ) ( * WordFilter , error ) {
wf := & WordFilter { ID : id }
2019-11-10 02:37:53 +00:00
err := s . get . QueryRow ( id ) . Scan ( & wf . Find , & wf . Replace )
2019-11-08 21:46:50 +00:00
return wf , err
}
2018-08-04 11:46:36 +00:00
// Create adds a new word filter to the database and refreshes the memory cache
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
func ( s * DefaultWordFilterStore ) Create ( find , replace string ) ( int , error ) {
2019-11-08 21:46:50 +00:00
res , err := s . create . Exec ( find , replace )
2018-08-04 11:46:36 +00:00
if err != nil {
2019-11-08 21:46:50 +00:00
return 0 , err
}
id64 , err := res . LastInsertId ( )
if err != nil {
return 0 , err
2018-08-04 11:46:36 +00:00
}
2019-11-08 21:46:50 +00:00
return int ( id64 ) , s . ReloadAll ( )
2018-08-04 11:46:36 +00:00
}
// Delete removes a word filter from the database and refreshes the memory cache
2019-06-01 12:31:48 +00:00
func ( s * DefaultWordFilterStore ) Delete ( id int ) error {
_ , err := s . delete . Exec ( id )
2018-08-04 11:46:36 +00:00
if err != nil {
return err
}
2019-06-01 12:31:48 +00:00
return s . ReloadAll ( )
2018-08-04 11:46:36 +00:00
}
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
func ( s * DefaultWordFilterStore ) Update ( id int , find , replace string ) error {
2019-10-06 05:32:08 +00:00
_ , err := s . update . Exec ( find , replace , id )
2018-08-04 11:46:36 +00:00
if err != nil {
return err
}
2019-06-01 12:31:48 +00:00
return s . ReloadAll ( )
2018-08-04 11:46:36 +00:00
}
// Length gets the number of word filters currently in memory, for the DefaultWordFilterStore, this should be all of them
2019-06-01 12:31:48 +00:00
func ( s * DefaultWordFilterStore ) Length ( ) int {
return len ( s . box . Load ( ) . ( map [ int ] * WordFilter ) )
2018-08-04 11:46:36 +00:00
}
// EstCount provides the same result as Length(), intended for alternate implementations of WordFilterStore, so that Length is the number of items in cache, if only a subset is held there and EstCount is the total count
2019-06-01 12:31:48 +00:00
func ( s * DefaultWordFilterStore ) EstCount ( ) int {
return len ( s . box . Load ( ) . ( map [ int ] * WordFilter ) )
2018-08-04 11:46:36 +00:00
}
2019-06-01 12:31:48 +00:00
// Count gets the total number of word filters directly from the database
func ( s * DefaultWordFilterStore ) Count ( ) ( count int ) {
err := s . count . QueryRow ( ) . Scan ( & count )
2018-08-04 11:46:36 +00:00
if err != nil {
LogError ( err )
}
return count
2017-11-11 04:06:16 +00:00
}