fixed concurrency in users & communities, implemented Communities sidebox
This commit is contained in:
+42
-6
@@ -11,6 +11,7 @@ package database
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
lru "github.com/hashicorp/golang-lru"
|
lru "github.com/hashicorp/golang-lru"
|
||||||
@@ -18,6 +19,7 @@ import (
|
|||||||
|
|
||||||
// Community struct contains the high level data for a community.
|
// Community struct contains the high level data for a community.
|
||||||
type Community struct {
|
type Community struct {
|
||||||
|
Mutex sync.RWMutex
|
||||||
Id int32 `db:"commid"`
|
Id int32 `db:"commid"`
|
||||||
CreateDate time.Time `db:"createdate"`
|
CreateDate time.Time `db:"createdate"`
|
||||||
LastAccess *time.Time `db:"lastaccess"`
|
LastAccess *time.Time `db:"lastaccess"`
|
||||||
@@ -25,6 +27,7 @@ type Community struct {
|
|||||||
ReadLevel uint16 `db:"read_lvl"`
|
ReadLevel uint16 `db:"read_lvl"`
|
||||||
WriteLevel uint16 `db:"write_lvl"`
|
WriteLevel uint16 `db:"write_lvl"`
|
||||||
CreateLevel uint16 `db:"create_lvl"`
|
CreateLevel uint16 `db:"create_lvl"`
|
||||||
|
DeleteLevel uint16 `db:"delete_lvl"`
|
||||||
JoinLevel uint16 `db:"join_lvl"`
|
JoinLevel uint16 `db:"join_lvl"`
|
||||||
ContactId int32 `db:"contactid"`
|
ContactId int32 `db:"contactid"`
|
||||||
HostUid *int32 `db:"host_uid"`
|
HostUid *int32 `db:"host_uid"`
|
||||||
@@ -45,6 +48,18 @@ type Community struct {
|
|||||||
// communityCache is the cache for Community objects.
|
// communityCache is the cache for Community objects.
|
||||||
var communityCache *lru.TwoQueueCache = nil
|
var communityCache *lru.TwoQueueCache = nil
|
||||||
|
|
||||||
|
// getCommunityMutex is a mutex on AmGetCommunity.
|
||||||
|
var getCommunityMutex sync.Mutex
|
||||||
|
|
||||||
|
// init initializes the community cache.
|
||||||
|
func init() {
|
||||||
|
var err error
|
||||||
|
communityCache, err = lru.New2Q(50)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* AmGetCommunity returns a reference to the specified community.
|
/* AmGetCommunity returns a reference to the specified community.
|
||||||
* Parameters:
|
* Parameters:
|
||||||
* id - The ID of the community.
|
* id - The ID of the community.
|
||||||
@@ -54,12 +69,8 @@ var communityCache *lru.TwoQueueCache = nil
|
|||||||
*/
|
*/
|
||||||
func AmGetCommunity(id int32) (*Community, error) {
|
func AmGetCommunity(id int32) (*Community, error) {
|
||||||
var err error = nil
|
var err error = nil
|
||||||
if communityCache == nil {
|
getCommunityMutex.Lock()
|
||||||
communityCache, err = lru.New2Q(50)
|
defer getCommunityMutex.Unlock()
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
rc, ok := communityCache.Get(id)
|
rc, ok := communityCache.Get(id)
|
||||||
if !ok {
|
if !ok {
|
||||||
var dbdata []Community
|
var dbdata []Community
|
||||||
@@ -75,3 +86,28 @@ func AmGetCommunity(id int32) (*Community, error) {
|
|||||||
}
|
}
|
||||||
return rc.(*Community), err
|
return rc.(*Community), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* AmGetCommunitiesForUser returns a list of communities the user is a member of.
|
||||||
|
* Parameters:
|
||||||
|
* uid - The ID of the user.
|
||||||
|
* Returns:
|
||||||
|
* Array of pointers to communities for the user
|
||||||
|
* Standard Go error status
|
||||||
|
*/
|
||||||
|
func AmGetCommunitiesForUser(uid int32) ([]*Community, error) {
|
||||||
|
var rc []*Community = make([]*Community, 0)
|
||||||
|
rows, err := amdb.Queryx("SELECT commid FROM commmember WHERE uid = ?", uid)
|
||||||
|
if err == nil {
|
||||||
|
defer rows.Close()
|
||||||
|
for err == nil && rows.Next() {
|
||||||
|
var cid int32
|
||||||
|
var c *Community
|
||||||
|
rows.Scan(&cid)
|
||||||
|
c, err = AmGetCommunity(cid)
|
||||||
|
if err == nil {
|
||||||
|
rc = append(rc, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return rc, err
|
||||||
|
}
|
||||||
|
|||||||
+45
-17
@@ -10,8 +10,8 @@
|
|||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
lru "github.com/hashicorp/golang-lru"
|
lru "github.com/hashicorp/golang-lru"
|
||||||
@@ -19,6 +19,7 @@ import (
|
|||||||
|
|
||||||
// User represents a user in the Amsterdam database.
|
// User represents a user in the Amsterdam database.
|
||||||
type User struct {
|
type User struct {
|
||||||
|
Mutex sync.RWMutex
|
||||||
Uid int32 `db:"uid"`
|
Uid int32 `db:"uid"`
|
||||||
Username string `db:"username"`
|
Username string `db:"username"`
|
||||||
Passhash string `db:"passhash"`
|
Passhash string `db:"passhash"`
|
||||||
@@ -40,9 +41,21 @@ type User struct {
|
|||||||
// userCache is the cache for User objects.
|
// userCache is the cache for User objects.
|
||||||
var userCache *lru.TwoQueueCache = nil
|
var userCache *lru.TwoQueueCache = nil
|
||||||
|
|
||||||
|
// getUserMutex is a mutex on AmGetUser.
|
||||||
|
var getUserMutex sync.Mutex
|
||||||
|
|
||||||
// anonUid is the UID of the "anonymous" user.
|
// anonUid is the UID of the "anonymous" user.
|
||||||
var anonUid int32 = -1
|
var anonUid int32 = -1
|
||||||
|
|
||||||
|
// init initializes the user cache.
|
||||||
|
func init() {
|
||||||
|
var err error
|
||||||
|
userCache, err = lru.New2Q(100)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* AmGetUser returns a reference to the specified user.
|
/* AmGetUser returns a reference to the specified user.
|
||||||
* Parameters:
|
* Parameters:
|
||||||
* uid - The UID of the user.
|
* uid - The UID of the user.
|
||||||
@@ -52,12 +65,8 @@ var anonUid int32 = -1
|
|||||||
*/
|
*/
|
||||||
func AmGetUser(uid int32) (*User, error) {
|
func AmGetUser(uid int32) (*User, error) {
|
||||||
var err error = nil
|
var err error = nil
|
||||||
if userCache == nil {
|
getUserMutex.Lock()
|
||||||
userCache, err = lru.New2Q(100)
|
defer getUserMutex.Unlock()
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
rc, ok := userCache.Get(uid)
|
rc, ok := userCache.Get(uid)
|
||||||
if !ok {
|
if !ok {
|
||||||
var dbdata []User
|
var dbdata []User
|
||||||
@@ -74,16 +83,10 @@ func AmGetUser(uid int32) (*User, error) {
|
|||||||
return rc.(*User), err
|
return rc.(*User), err
|
||||||
}
|
}
|
||||||
|
|
||||||
/* AmGetAnonUser returns a reference to the anonymous user.
|
// getAnonUserID retrieves the UID of the "anonymous" user from the database.
|
||||||
* Returns:
|
func getAnonUserID() (int32, error) {
|
||||||
* Pointer to User containing anonymous user data, or nil
|
|
||||||
* Standard Go error status
|
|
||||||
*/
|
|
||||||
func AmGetAnonUser() (*User, error) {
|
|
||||||
var err error = nil
|
|
||||||
if anonUid < 0 {
|
if anonUid < 0 {
|
||||||
var rows *sql.Rows
|
rows, err := amdb.Query("SELECT uid FROM users WHERE is_anon = 1")
|
||||||
rows, err = amdb.Query("SELECT uid FROM users WHERE is_anon = 1")
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
if rows.Next() {
|
if rows.Next() {
|
||||||
@@ -95,10 +98,35 @@ func AmGetAnonUser() (*User, error) {
|
|||||||
err = fmt.Errorf("no anonymous user in Amsterdam database")
|
err = fmt.Errorf("no anonymous user in Amsterdam database")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if err != nil {
|
||||||
|
return -1, err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return anonUid, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
/* AmIsUserAnon returns true if the specified user ID is the anonymous one.
|
||||||
|
* Parameters:
|
||||||
|
* uid = The user ID to test.
|
||||||
|
* Returns:
|
||||||
|
* true if the user is anonymous, false if not
|
||||||
|
* Standard Go error status
|
||||||
|
*/
|
||||||
|
func AmIsUserAnon(uid int32) (bool, error) {
|
||||||
|
auid, err := getAnonUserID()
|
||||||
|
return (uid == auid), err
|
||||||
|
}
|
||||||
|
|
||||||
|
/* AmGetAnonUser returns a reference to the anonymous user.
|
||||||
|
* Returns:
|
||||||
|
* Pointer to User containing anonymous user data, or nil
|
||||||
|
* Standard Go error status
|
||||||
|
*/
|
||||||
|
func AmGetAnonUser() (*User, error) {
|
||||||
var rc *User = nil
|
var rc *User = nil
|
||||||
|
auid, err := getAnonUserID()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
rc, err = AmGetUser(anonUid)
|
rc, err = AmGetUser(auid)
|
||||||
}
|
}
|
||||||
return rc, err
|
return rc, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ type RenderedSidebox struct {
|
|||||||
Items []RenderedSideboxItem
|
Items []RenderedSideboxItem
|
||||||
}
|
}
|
||||||
|
|
||||||
/* buildFeaturedCommunities creates the data for the "Featured Communities" sidebox.
|
/* buildCommunitiesSidebox creates the data for the "My/Featured Communities" sidebox.
|
||||||
* Parameters:
|
* Parameters:
|
||||||
* uid - UID of the user rendering the page.
|
* uid - UID of the user rendering the page.
|
||||||
* out - The RenderedSidebox to be built.
|
* out - The RenderedSidebox to be built.
|
||||||
@@ -39,9 +39,30 @@ type RenderedSidebox struct {
|
|||||||
* Returns:
|
* Returns:
|
||||||
* Standard Go error status.
|
* Standard Go error status.
|
||||||
*/
|
*/
|
||||||
func buildFeaturedCommunities(uid int32, out *RenderedSidebox, in *database.Sidebox) error {
|
func buildCommunitiesSidebox(uid int32, out *RenderedSidebox, in *database.Sidebox) error {
|
||||||
|
var err error
|
||||||
|
var anon bool
|
||||||
|
anon, err = database.AmIsUserAnon(uid)
|
||||||
|
if err == nil {
|
||||||
|
if anon {
|
||||||
|
out.Title = "Featured Communities"
|
||||||
|
} else {
|
||||||
|
out.Title = "Your Communities"
|
||||||
|
}
|
||||||
|
var l []*database.Community
|
||||||
|
l, err = database.AmGetCommunitiesForUser(uid)
|
||||||
|
if err == nil {
|
||||||
|
out.Items = make([]RenderedSideboxItem, len(l))
|
||||||
|
for i, c := range l {
|
||||||
|
out.Items[i].Text = c.Name
|
||||||
|
out.Items[i].Link = new(string)
|
||||||
|
*out.Items[i].Link = "/TODO/community/" + c.Alias
|
||||||
|
out.Items[i].Flags = make([]string, 0)
|
||||||
|
}
|
||||||
out.TemplateName = "sb_ftrcomm.jet"
|
out.TemplateName = "sb_ftrcomm.jet"
|
||||||
return nil
|
}
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
/* buildFeaturedConferences creates the data for the "Featured Conferences" sidebox.
|
/* buildFeaturedConferences creates the data for the "Featured Conferences" sidebox.
|
||||||
@@ -81,7 +102,7 @@ func buildUsersOnline(uid int32, out *RenderedSidebox, in *database.Sidebox) err
|
|||||||
func buildRenderedSidebox(uid int32, out *RenderedSidebox, in *database.Sidebox) error {
|
func buildRenderedSidebox(uid int32, out *RenderedSidebox, in *database.Sidebox) error {
|
||||||
switch in.Boxid {
|
switch in.Boxid {
|
||||||
case 1:
|
case 1:
|
||||||
return buildFeaturedCommunities(uid, out, in)
|
return buildCommunitiesSidebox(uid, out, in)
|
||||||
case 2:
|
case 2:
|
||||||
return buildFeaturedConferences(uid, out, in)
|
return buildFeaturedConferences(uid, out, in)
|
||||||
case 3:
|
case 3:
|
||||||
|
|||||||
+8
-1
@@ -28,11 +28,11 @@ type AmContext interface {
|
|||||||
RC() int
|
RC() int
|
||||||
OutputType() string
|
OutputType() string
|
||||||
Render(string) error
|
Render(string) error
|
||||||
Scratchpad() map[string]any
|
|
||||||
SubRender(string) ([]byte, error)
|
SubRender(string) ([]byte, error)
|
||||||
Session() *sessions.Session
|
Session() *sessions.Session
|
||||||
SetOutputType(string)
|
SetOutputType(string)
|
||||||
SetRC(int)
|
SetRC(int)
|
||||||
|
GetScratch(string) any
|
||||||
SetScratch(string, any)
|
SetScratch(string, any)
|
||||||
URLPath() string
|
URLPath() string
|
||||||
VarMap() jet.VarMap
|
VarMap() jet.VarMap
|
||||||
@@ -117,6 +117,13 @@ func (c *amContext) SetRC(rc int) {
|
|||||||
c.httprc = rc
|
c.httprc = rc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *amContext) GetScratch(name string) any {
|
||||||
|
if c.scratchpad == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return c.scratchpad[name]
|
||||||
|
}
|
||||||
|
|
||||||
func (c *amContext) SetScratch(name string, val any) {
|
func (c *amContext) SetScratch(name string, val any) {
|
||||||
if c.scratchpad == nil {
|
if c.scratchpad == nil {
|
||||||
c.scratchpad = make(map[string]any)
|
c.scratchpad = make(map[string]any)
|
||||||
|
|||||||
+12
-8
@@ -6,20 +6,24 @@
|
|||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* 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/.
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
*}
|
*}
|
||||||
<!-- Featured Communities -->
|
<!-- Communities sidebox -->
|
||||||
|
{{ sb := .GetScratch("__sidebox") }}
|
||||||
<div class="mb-4">
|
<div class="mb-4">
|
||||||
<div class="bg-blue-600 px-2 py-1 rounded-t">
|
<div class="bg-blue-600 px-2 py-1 rounded-t">
|
||||||
{{ if .CurrentUser().IsAnon }}
|
<h3 class="text-white font-bold text-base">{{ sb.Title }}</h3>
|
||||||
<h3 class="text-white font-bold text-base">Featured Communities:</h3>
|
|
||||||
{{ else }}
|
|
||||||
<h3 class="text-white font-bold text-base">Your Communities:</h3>
|
|
||||||
{{ end }}
|
|
||||||
</div>
|
</div>
|
||||||
<div class="bg-blue-400 px-2 py-2 rounded-b">
|
<div class="bg-blue-400 px-2 py-2 rounded-b">
|
||||||
<div class="flex items-center">
|
<div class="flex items-center">
|
||||||
|
{{ if len(sb.Items) > 0 }}
|
||||||
|
{{ range sb.Items }}
|
||||||
|
<div class="text-small">
|
||||||
<span class="mr-2">🟣</span>
|
<span class="mr-2">🟣</span>
|
||||||
<a href="http://necrovenice:8080/venice/community/Piazza"
|
<a href="{{ .Link }}" class="text-blue-700 hover:text-blue-900 font-bold text-sm">{{ .Text }}</a>
|
||||||
class="text-blue-700 hover:text-blue-900 font-bold text-sm">La Piazza</a>
|
</div>
|
||||||
|
{{ end }}
|
||||||
|
{{ else }}
|
||||||
|
<div class="text-small">You are not a member of any communities.</div>
|
||||||
|
{{ end }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user