[F3] driver/users implementation

This commit is contained in:
Earl Warren 2024-02-03 12:18:06 +01:00
parent 2a56eecf69
commit 8dfeb1210c
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
4 changed files with 121 additions and 6 deletions

View file

@ -22,8 +22,23 @@ func (o *common) ListPage(ctx context.Context, page int) generic.ChildrenSlice {
return generic.NewChildrenSlice(0)
}
func (o *common) GetNativeID() string {
return ""
}
func (o *common) SetNative(native any) {
}
func (o *common) getPageSize() int {
return o.getTreeDriver().GetPageSize()
}
func (o *common) getKind() generic.Kind {
return o.GetNode().GetKind()
}
func (o *common) getTreeDriver() *treeDriver {
return o.GetTreeDriver().(*treeDriver)
}
func (o *common) IsNull() bool { return false }

View file

@ -26,14 +26,66 @@ func (o *treeDriver) Init() {
func (o *treeDriver) Factory(ctx context.Context, kind generic.Kind) generic.NodeDriverInterface {
switch kind {
case f3_tree.KindUsers:
return newUsers()
case f3_tree.KindOrganizations:
return newOrganizations()
case f3_tree.KindTopics:
return newTopics()
case f3_tree.KindForge:
return newForge()
case f3_tree.KindOrganizations:
return newOrganizations()
// case f3_tree.KindOrganization:
// return newOrganization()
case f3_tree.KindUsers:
return newUsers()
case f3_tree.KindUser:
return newUser()
case f3_tree.KindProjects:
return newProjects()
// case f3_tree.KindProject:
// return newProject()
// case f3_tree.KindIssues:
// return newIssues()
// case f3_tree.KindIssue:
// return newIssue()
// case f3_tree.KindComments:
// return newComments()
// case f3_tree.KindComment:
// return newComment()
// case f3_tree.KindAssets:
// return newAssets()
// case f3_tree.KindAsset:
// return newAsset()
// case f3_tree.KindLabels:
// return newLabels()
// case f3_tree.KindLabel:
// return newLabel()
// case f3_tree.KindReactions:
// return newReactions()
// case f3_tree.KindReaction:
// return newReaction()
// case f3_tree.KindReviews:
// return newReviews()
// case f3_tree.KindReview:
// return newReview()
// case f3_tree.KindReviewComments:
// return newReviewComments()
// case f3_tree.KindReviewComment:
// return newReviewComment()
// case f3_tree.KindMilestones:
// return newMilestones()
// case f3_tree.KindMilestone:
// return newMilestone()
// case f3_tree.KindPullRequests:
// return newPullRequests()
// case f3_tree.KindPullRequest:
// return newPullRequest()
// case f3_tree.KindReleases:
// return newReleases()
// case f3_tree.KindRelease:
// return newRelease()
case f3_tree.KindTopics:
return newTopics()
// case f3_tree.KindRepositories:
// return newRepositories()
// case f3_tree.KindRepository:
// return newRepository(ctx)
case generic.KindRoot:
return newRoot(o.GetTree().(f3_tree.TreeInterface).NewFormat(kind))
default:

View file

@ -0,0 +1,17 @@
// Copyright Earl Warren <contact@earl-warren.org>
// Copyright Loïc Dachary <loic@dachary.org>
// SPDX-License-Identifier: MIT
package driver
import (
"lab.forgefriends.org/friendlyforgeformat/gof3/tree/generic"
)
type user struct {
common
}
func newUser() generic.NodeDriverInterface {
return &user{}
}

View file

@ -5,6 +5,13 @@
package driver
import (
"context"
"fmt"
"code.gitea.io/gitea/models/db"
user_model "code.gitea.io/gitea/models/user"
f3_tree "lab.forgefriends.org/friendlyforgeformat/gof3/tree/f3"
"lab.forgefriends.org/friendlyforgeformat/gof3/tree/generic"
)
@ -12,6 +19,30 @@ type users struct {
container
}
func (o *users) ListPage(ctx context.Context, page int) generic.ChildrenSlice {
sess := db.GetEngine(ctx).In("type", user_model.UserTypeIndividual, user_model.UserTypeF3)
if page != 0 {
sess = db.SetSessionPagination(sess, &db.ListOptions{Page: page, PageSize: o.getPageSize()})
}
sess = sess.Select("`user`.*")
users := make([]*user_model.User, 0, o.getPageSize())
if err := sess.Find(&users); err != nil {
panic(fmt.Errorf("error while listing users: %v", err))
}
return f3_tree.ConvertListed(ctx, o.GetNode(), f3_tree.ConvertToAny(users...)...)
}
func (o *users) GetIDFromName(ctx context.Context, name string) generic.NodeID {
user, err := user_model.GetUserByName(ctx, name)
if err != nil {
panic(fmt.Errorf("GetUserByName: %v", err))
}
return generic.NodeID(fmt.Sprintf("%d", user.ID))
}
func newUsers() generic.NodeDriverInterface {
return &users{}
}