Compare commits

..

No commits in common. "federation" and "task-602" have entirely different histories.

6 changed files with 86 additions and 195 deletions

32
CODEOWNERS Normal file
View file

@ -0,0 +1,32 @@
# This file describes the expected reviewers for a PR based on the changed
# files. Unlike what the name of the file suggests they don't own the code, but
# merely have a good understanding of that area of the codebase and therefore
# are usually suited as a reviewer.
# Please mind the alphabetic order of reviewers.
# Files related to the CI of the Forgejo project.
.forgejo/.* @dachary @earl-warren
# Files related to frontend development.
# Javascript and CSS code.
web_src/.* @caesar @crystal @gusted
# HTML templates used by the backend.
templates/.* @caesar @crystal @gusted
# Files related to Go development.
# The modules usually don't require much knowledge about Forgejo and could
# be reviewed by Go developers.
modules/.* @dachary @earl-warren @gusted
# Models has code related to SQL queries, general database knowledge and XORM.
models/.* @dachary @earl-warren @gusted
# The routers directory contains the most amount code that requires a good grasp
# of how Forgejo comes together. It's tedious to write good integration testing
# for code that lives in here.
routers/.* @dachary @earl-warren @gusted

View file

@ -54,70 +54,23 @@ type FederatedUser struct {
FederationHostID int64 `xorm:"INDEX"`
}
func CreateFederatedUser(ctx context.Context, alias string, website string, hostname string) (*user.User, error) {
func CreateFederatedUser(ctx context.Context, u *user.User, host *FederatedHost) error {
engine := db.GetEngine(ctx)
// create FederatedHost
exists, err := FederatedHostExists(ctx, hostname)
if err != nil {
return nil, err
}
var federatedHost FederatedHost
if exists {
x, err := GetFederatdHost(ctx, hostname)
federatedHost = *x
if err != nil {
return nil, err
}
} else {
federatedHost := new(FederatedHost)
federatedHost.HostFqdn = hostname
if err = federatedHost.Save(ctx); err != nil {
return nil, err
}
}
// create user.User
u := new(user.User)
u.Name = "@" + alias + "@" + hostname
//panic(u.Name)
u.Email = alias + "@" + hostname
u.Website = website
u.KeepEmailPrivate = true
exist, err := user.GetUser(ctx, u)
if err != nil {
return nil, err
}
if exist {
return u, nil // TODO: must also check for federatedUser existence
}
if err = createUser(ctx, u); err != nil {
return nil, err
}
// _, err := engine.
// Insert(u)
// if err != nil {
// return err
// }
federatedUser := new(FederatedUser)
federatedUser.ExternalID = u.Name
federatedUser.UserID = u.ID
federatedUser.FederationHostID = federatedHost.ID
exist, err = engine.Get(federatedUser)
if err != nil {
return nil, err
}
if !exist {
_, err = engine.Insert(federatedUser)
if err != nil {
return nil, err
}
}
return u, nil
federatedUser.FederationHostID = host.ID
_, err := engine.Insert(federatedUser)
return err
}
func createUser(ctx context.Context, u *user.User) error {
func CreatUser(ctx context.Context, u *user.User) error {
// set system defaults
u.Visibility = setting.Service.DefaultUserVisibilityMode
u.AllowCreateOrganization = setting.Service.DefaultAllowCreateOrganization && !setting.Admin.DisableRegularOrgCreation
@ -232,28 +185,3 @@ func GetRemoteUsersWithNoLocalFollowers(ctx context.Context, olderThan time.Dura
return users, nil
}
func GetRemotePersons(ctx context.Context, page int) ([]FederatedUser, error) {
limit := 1
offset := page * limit
var federatedUsers []FederatedUser
err := db.GetEngine(ctx).
Table("federated_user").
Limit(limit, offset).
Find(&federatedUsers)
// TODO: this doesn't work, so fetching federated_user and then getting user. How to make this work?
// var users []user.User
// err := db.GetEngine(ctx).
// Table("user").
// Join("inner", "federated_user", "federated_user.user_id = user.id").
// Limit(limit, offset).
// Find(&users)
if err != nil {
log.Trace("Error: GetRemotePersons: %w", err)
return nil, err
}
return federatedUsers, nil
}

View file

@ -2922,7 +2922,6 @@ dashboard.sync_branch.started = Branches Sync started
dashboard.sync_tag.started = Tags Sync started
dashboard.rebuild_issue_indexer = Rebuild issue indexer
dashboard.remote_actor_cleanup = Clean remote actors with no local followers
dashboard.remote_actor_update = Update remote actors' data
users.user_manage_panel = Manage user accounts
users.new_account = Create User Account

View file

@ -93,6 +93,13 @@ func RenderUserSearch(ctx *context.Context, opts *user_model.SearchUserOptions,
opts.Keyword = ctx.FormTrim("q")
opts.OrderBy = orderBy
if len(opts.Keyword) == 0 || isKeywordValid(opts.Keyword) {
users, count, err = user_model.SearchUsers(ctx, opts)
if err != nil {
ctx.ServerError("SearchUsers", err)
return
}
}
if len(opts.Keyword) > 0 && forgefed.IsFingerable(opts.Keyword) {
webfingerRes, err := forgefed.WebFingerLookup(opts.Keyword)
@ -118,14 +125,6 @@ func RenderUserSearch(ctx *context.Context, opts *user_model.SearchUserOptions,
// }
}
if len(opts.Keyword) == 0 || isKeywordValid(opts.Keyword) {
users, count, err = user_model.SearchUsers(ctx, opts)
if err != nil {
ctx.ServerError("SearchUsers", err)
return
}
}
if isSitemap {
m := sitemap.NewSitemap()
for _, item := range users {

View file

@ -195,7 +195,6 @@ func initBasicTasks() {
if setting.Federation.Enabled {
registerCleanupRemotePersonsWithNoFollowers()
registerUpdateRemotePersons()
}
}
@ -213,15 +212,3 @@ func registerCleanupRemotePersonsWithNoFollowers() {
return forgefed_service.CleanUpRemotePersons(ctx, acConfig.OlderThan)
})
}
func registerUpdateRemotePersons() {
RegisterTaskFatal("remote_actor_update", &OlderThanConfig{
BaseConfig: BaseConfig{
Enabled: true,
RunAtStart: true,
Schedule: "@every 6h",
},
}, func(ctx context.Context, _ *user_model.User, config Config) error {
return forgefed_service.UpdatePersonActor(ctx)
})
}

View file

@ -43,59 +43,61 @@ func GetActor(id string) (*ap.Actor, error) {
return actorObj, nil
}
func GetPersonAvatar(ctx context.Context, person *ap.Person) ([]byte, error) {
avatarObj := new(ap.Image)
ap.CopyItemProperties(avatarObj, person.Icon)
log.Info("Getting avatar from link : %s", avatarObj.URL.GetLink().String())
r, err := http.Get(avatarObj.URL.GetLink().String())
if err != nil {
log.Error("Got error while fetching avatar fn: %w", err)
return nil, err
}
defer r.Body.Close()
return io.ReadAll(r.Body)
}
func SavePerson(ctx context.Context, person *ap.Person) (*user.User, error) {
fmt.Println(person.ID.String())
hostname, err := GetHostnameFromResource(person.ID.String())
u, err := federation.CreateFederatedUser(ctx, person.PreferredUsername.String(), person.URL.GetID().String(), hostname)
exists, err := federation.FederatedHostExists(ctx, hostname)
if err != nil {
log.Error("Got error while saving person: %w", err)
return nil, err
}
avatar, err := GetPersonAvatar(ctx, person)
var federatedHost federation.FederatedHost
if exists {
x, err := federation.GetFederatdHost(ctx, hostname)
federatedHost = *x
if err != nil {
return nil, err
}
} else {
federatedHost := new(federation.FederatedHost)
federatedHost.HostFqdn = hostname
if err = federatedHost.Save(ctx); err != nil {
return nil, err
}
}
if err != nil {
log.Error("Got error while fetching avatar: %w", err)
return nil, err
}
if u.IsUploadAvatarChanged(avatar) {
_ = user_service.UploadAvatar(ctx, u, avatar)
u := new(user.User)
u.Name = "@" + person.PreferredUsername.String() + "@" + hostname
//panic(u.Name)
u.Email = person.PreferredUsername.String() + "@" + hostname
u.Website = person.URL.GetID().String()
u.KeepEmailPrivate = true
exist, err := user.GetUser(ctx, u)
if err != nil {
return nil, err
}
if exist {
return u, nil // TODO: must also check for federatedUser existence
}
if err = federation.CreatUser(ctx, u); err != nil {
return nil, err
}
if err = federation.CreateFederatedUser(ctx, u, &federatedHost); err != nil {
return nil, err
}
return u, nil
}
func GetActorFromUser(ctx context.Context, u *user.User) (*ap.Actor, error) {
alias := u.Name
webfingerRes, err := WebFingerLookup(alias)
if err != nil {
return nil, err
}
actorID := webfingerRes.GetActorLink().Href
return GetActor(actorID)
}
// Clean up remote actors (persons) without any followers in local instance
func CleanUpRemotePersons(ctx context.Context, olderThan time.Duration) error {
page := 0
@ -120,59 +122,3 @@ func CleanUpRemotePersons(ctx context.Context, olderThan time.Duration) error {
}
return nil
}
func UpdatePersonActor(ctx context.Context) error {
// NOTE: change of any of these don't matter at this point since we are
// ignoring actor's PreferredUsername and using their address to generate
// username and email. Ask suggestions from other devs.
//
// fmt.Println(person.ID.String())
// hostname, err := GetHostnameFromResource(person.ID.String())
//
//
// u := new(user.User)
// u.Name = "@" + person.PreferredUsername.String() + "@" + hostname
// //panic(u.Name)
// u.Email = person.PreferredUsername.String() + "@" + hostname
// u.Website = person.URL.GetID().String()
// u.KeepEmailPrivate = true
page := 0
for {
federatedUsers, err := federation.GetRemotePersons(ctx, page)
if len(federatedUsers) == 0 {
break
}
if err != nil {
log.Trace("Error: UpdatePersonActor: %v", err)
return err
}
for _, f := range federatedUsers {
log.Info("Updating users, got %s", f.ExternalID)
u, err := user.GetUserByName(ctx, f.ExternalID)
if err != nil {
log.Error("Got error while getting user: %w", err)
return err
}
person, err := GetActorFromUser(ctx, u)
if err != nil {
log.Error("Got error while fetching actor: %w", err)
return err
}
avatar, err := GetPersonAvatar(ctx, person)
if err != nil {
log.Error("Got error while fetching avatar: %w", err)
return err
}
if u.IsUploadAvatarChanged(avatar) {
_ = user_service.UploadAvatar(ctx, u, avatar)
}
}
page += 1
}
return nil
}