2017-11-10 03:33:11 +00:00
package common
2016-12-04 06:16:59 +00:00
2018-09-30 00:08:27 +00:00
import (
2022-02-21 03:32:53 +00:00
"database/sql"
"encoding/json"
2018-09-30 00:08:27 +00:00
2022-02-21 03:32:53 +00:00
qgen "github.com/Azareal/Gosora/query_gen"
2018-09-30 00:08:27 +00:00
)
2017-11-11 04:06:16 +00:00
2017-09-15 22:20:01 +00:00
var blankGroup = Group { ID : 0 , Name : "" }
2017-03-18 07:23:02 +00:00
2017-09-03 04:50:31 +00:00
type GroupAdmin struct {
2022-02-21 03:32:53 +00:00
ID int
Name string
Rank string
RankClass string
CanEdit bool
CanDelete bool
2017-03-18 07:23:02 +00:00
}
2017-11-02 13:35:19 +00:00
// ! Fix the data races in the fperms
2017-09-03 04:50:31 +00:00
type Group struct {
2022-02-21 03:32:53 +00:00
ID int
Name string
IsMod bool
IsAdmin bool
IsBanned bool
Tag string
Perms Perms
PermissionsText [ ] byte
PluginPerms map [ string ] bool // Custom permissions defined by plugins. What if two plugins declare the same permission, but they handle them in incompatible ways? Very unlikely, we probably don't need to worry about this, the plugin authors should be aware of each other to some extent
PluginPermsText [ ] byte
CanSee [ ] int // The IDs of the forums this group can see
UserCount int // ! Might be temporary as I might want to lean on the database instead for this
2016-12-04 06:16:59 +00:00
}
2017-09-28 22:16:34 +00:00
2017-11-11 04:06:16 +00:00
type GroupStmts struct {
2022-02-21 03:32:53 +00:00
updateGroup * sql . Stmt
updateGroupRank * sql . Stmt
updateGroupPerms * sql . Stmt
2017-11-11 04:06:16 +00:00
}
var groupStmts GroupStmts
func init ( ) {
2022-02-21 03:32:53 +00:00
DbInits . Add ( func ( acc * qgen . Accumulator ) error {
set := func ( s string ) * sql . Stmt {
return acc . Update ( "users_groups" ) . Set ( s ) . Where ( "gid=?" ) . Prepare ( )
}
groupStmts = GroupStmts {
updateGroup : set ( "name=?,tag=?" ) ,
updateGroupRank : set ( "is_admin=?,is_mod=?,is_banned=?" ) ,
updateGroupPerms : set ( "permissions=?" ) ,
}
return acc . FirstError ( )
} )
2017-11-11 04:06:16 +00:00
}
2020-01-31 10:48:55 +00:00
func ( g * Group ) ChangeRank ( isAdmin , isMod , isBanned bool ) ( err error ) {
2022-02-21 03:32:53 +00:00
_ , err = groupStmts . updateGroupRank . Exec ( isAdmin , isMod , isBanned , g . ID )
if err != nil {
return err
}
_ = Groups . Reload ( g . ID )
return nil
2017-10-21 00:27:47 +00:00
}
2020-01-31 10:48:55 +00:00
func ( g * Group ) Update ( name , tag string ) ( err error ) {
2022-02-21 03:32:53 +00:00
_ , err = groupStmts . updateGroup . Exec ( name , tag , g . ID )
if err != nil {
return err
}
_ = Groups . Reload ( g . ID )
return nil
2018-09-30 00:08:27 +00:00
}
// Please don't pass arbitrary inputs to this method
2019-08-31 22:34:43 +00:00
func ( g * Group ) UpdatePerms ( perms map [ string ] bool ) ( err error ) {
2022-02-21 03:32:53 +00:00
pjson , err := json . Marshal ( perms )
if err != nil {
return err
}
_ , err = groupStmts . updateGroupPerms . Exec ( pjson , g . ID )
if err != nil {
return err
}
return Groups . Reload ( g . ID )
2018-09-30 00:08:27 +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
// Copy gives you a non-pointer concurrency safe copy of the group
2019-08-31 22:34:43 +00:00
func ( g * Group ) Copy ( ) Group {
2022-02-21 03:32:53 +00:00
return * g
2017-09-28 22:16:34 +00:00
}
2017-11-02 13:35:19 +00:00
2019-08-31 22:34:43 +00:00
func ( g * Group ) CopyPtr ( ) ( co * Group ) {
2022-02-21 03:32:53 +00:00
co = new ( Group )
* co = * g
return co
2018-09-30 00:08:27 +00:00
}
2017-11-02 13:35:19 +00:00
// TODO: Replace this sorting mechanism with something a lot more efficient
// ? - Use sort.Slice instead?
type SortGroup [ ] * Group
func ( sg SortGroup ) Len ( ) int {
2022-02-21 03:32:53 +00:00
return len ( sg )
2017-11-02 13:35:19 +00:00
}
func ( sg SortGroup ) Swap ( i , j int ) {
2022-02-21 03:32:53 +00:00
sg [ i ] , sg [ j ] = sg [ j ] , sg [ i ]
2017-11-02 13:35:19 +00:00
}
func ( sg SortGroup ) Less ( i , j int ) bool {
2022-02-21 03:32:53 +00:00
return sg [ i ] . ID < sg [ j ] . ID
2017-11-02 13:35:19 +00:00
}