Support getting info from forum field table.

This commit is contained in:
Gucheng Wang 2021-12-04 23:25:04 +08:00
parent 60c43edafd
commit c76c79736c
3 changed files with 89 additions and 5 deletions

57
discuzx/forum_field.go Normal file
View File

@ -0,0 +1,57 @@
// Copyright 2021 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package discuzx
type Field struct {
Fid int
Description string
Icon string
Moderators string
Rules string
}
func getFields() []*Field {
fields := []*Field{}
err := adapter.Engine.Table("pre_forum_forumfield").Find(&fields)
if err != nil {
panic(err)
}
return fields
}
func getField(id int) *Field {
field := Field{Fid: id}
existed, err := adapter.Engine.Table("pre_forum_forumfield").Get(&field)
if err != nil {
panic(err)
}
if existed {
return &field
} else {
return nil
}
}
func getFieldMap() map[int]*Field {
fields := getFields()
m := map[int]*Field{}
for _, field := range fields {
m[field.Fid] = field
}
return m
}

View File

@ -16,16 +16,34 @@ package discuzx
import (
"fmt"
"strings"
"github.com/casbin/casnode/object"
"github.com/casbin/casnode/util"
)
func getInfoFromField(field *Field) (string, string, []string) {
if field == nil {
return "", "", []string{}
}
desc := field.Description
extra := field.Rules
moderators := []string{}
if field.Moderators != "" {
moderators = strings.Split(field.Moderators, "\t")
}
return desc, extra, moderators
}
func addForums() {
tabs := []*object.Tab{}
nodes := []*object.Node{}
forumTree, _ := getForumTree()
forumFieldMap := getFieldMap()
for i, groupForum := range forumTree {
defaultNode := ""
if len(groupForum.Forums) != 0 {
@ -46,11 +64,15 @@ func addForums() {
fmt.Printf("[%d/%d]: Synced group forum: %s\n", i+1, len(forumTree), groupForum.Name)
for j, forum := range groupForum.Forums {
field := forumFieldMap[forum.Fid]
desc, extra, moderators := getInfoFromField(field)
forumNode := &object.Node{
Id: forum.Name,
Name: forum.Name,
CreatedTime: util.GetCurrentTime(),
Desc: forum.Name,
Desc: desc,
Extra: extra,
Image: "https://cdn.v2ex.com/navatar/3b8a/6142/215_xxlarge.png?m=1523190513",
TabId: groupForum.Name,
ParentNode: "",
@ -58,7 +80,7 @@ func addForums() {
Sorter: forum.Displayorder,
Ranking: forum.Fid,
Hot: forum.Threads,
Moderators: []string{},
Moderators: moderators,
MailingList: "",
GoogleGroupCookie: "",
IsHidden: forum.Status == 0,
@ -67,11 +89,15 @@ func addForums() {
fmt.Printf("\t[%d/%d]: Synced forum: %s\n", j+1, len(groupForum.Forums), forum.Name)
for k, subForum := range forum.Forums {
field2 := forumFieldMap[subForum.Fid]
desc2, extra2, moderators2 := getInfoFromField(field2)
subForumNode := &object.Node{
Id: subForum.Name,
Name: subForum.Name,
CreatedTime: util.GetCurrentTime(),
Desc: subForum.Name,
Desc: desc2,
Extra: extra2,
Image: "https://cdn.v2ex.com/navatar/3b8a/6142/215_xxlarge.png?m=1523190513",
TabId: groupForum.Name,
ParentNode: forum.Name,
@ -79,7 +105,7 @@ func addForums() {
Sorter: subForum.Displayorder,
Ranking: subForum.Fid,
Hot: subForum.Threads,
Moderators: []string{},
Moderators: moderators2,
MailingList: "",
GoogleGroupCookie: "",
IsHidden: subForum.Status == 0,

View File

@ -24,7 +24,8 @@ type Node struct {
Id string `xorm:"varchar(100) notnull pk" json:"id"`
Name string `xorm:"varchar(100)" json:"name"`
CreatedTime string `xorm:"varchar(40)" json:"createdTime"`
Desc string `xorm:"varchar(500)" json:"desc"`
Desc string `xorm:"mediumtext" json:"desc"`
Extra string `xorm:"mediumtext" json:"extra"`
Image string `xorm:"varchar(200)" json:"image"`
BackgroundImage string `xorm:"varchar(200)" json:"backgroundImage"`
HeaderImage string `xorm:"varchar(200)" json:"headerImage"`