Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-25 19:26:09 +01:00

unify logging & minor improvements

This commit is contained in:
Michael Jerger 2023-12-15 14:45:20 +01:00
parent 42854c9003
commit e1d7db178c
3 changed files with 83 additions and 72 deletions

View file

@ -37,8 +37,17 @@ TAGS="sqlite" make build generate-swagger
# launch local # launch local
```bash ```bash
# cleanup
./gitea admin user delete --purge -id 8
./gitea admin user delete --purge -id 9
./gitea admin user delete --purge -id 10
# create a user # create a user
./gitea admin user create --username me --password me --email "buero@meissa.de" ./gitea admin user create --username me --password me --email "buero@meissa.de" --admin --must-change-password false
./gitea admin user create --username stargoose1 --random-password --email "stargoose1@meissa.de"
./gitea admin user create --username stargoose2 --random-password --email "stargoose2@meissa.de"
./gitea admin user create --username stargoose3 --random-password --email "stargoose3@meissa.de"
./gitea admin user list
# create a token # create a token
./gitea admin user generate-access-token -u me -t token --scopes write:activitypub,write:repository,write:user ./gitea admin user generate-access-token -u me -t token --scopes write:activitypub,write:repository,write:user
@ -72,8 +81,8 @@ curl -X 'POST' \
"id": "http://localhost:3000/api/v1/activitypub/user-id/1/outbox/12345", "id": "http://localhost:3000/api/v1/activitypub/user-id/1/outbox/12345",
"type": "Star", "type": "Star",
"source": "forgejo", "source": "forgejo",
"actor": "http://localhost:3000/api/v1/activitypub/user-id/1", "actor": "http://localhost:3000/api/v1/activitypub/user-id/5",
"object": "http://localhost:3000/api/v1/activitypub/repository-id/1" "object": "http://localhost:3000/api/v1/activitypub/repository-id/2"
} }
``` ```

View file

@ -31,9 +31,10 @@ type RepositoryId struct {
} }
func NewPersonId(uri string, source string) (PersonId, error) { func NewPersonId(uri string, source string) (PersonId, error) {
if !validation.IsValidExternalURL(uri) { // TODO: remove after test
return PersonId{}, fmt.Errorf("uri %s is not a valid external url", uri) //if !validation.IsValidExternalURL(uri) {
} // return PersonId{}, fmt.Errorf("uri %s is not a valid external url", uri)
//}
validatedUri, _ := url.Parse(uri) validatedUri, _ := url.Parse(uri)
pathWithActorID := strings.Split(validatedUri.Path, "/") pathWithActorID := strings.Split(validatedUri.Path, "/")
@ -99,11 +100,21 @@ func (id ActorId) AsUri() string {
return result return result
} }
func (id ActorId) AsWebfinger() string { func (id PersonId) AsWebfinger() string {
result := fmt.Sprintf("@%s@%s", strings.ToLower(id.Id), strings.ToLower(id.Host)) result := fmt.Sprintf("@%s@%s", strings.ToLower(id.Id), strings.ToLower(id.Host))
return result return result
} }
func (id PersonId) AsLoginName() string {
result := fmt.Sprintf("%s%s", strings.ToLower(id.Id), id.HostSuffix())
return result
}
func (id PersonId) HostSuffix() string {
result := fmt.Sprintf("-%s", strings.ToLower(id.Host))
return result
}
/* /*
Validate collects error strings in a slice and returns this Validate collects error strings in a slice and returns this
*/ */

View file

@ -26,31 +26,6 @@ import (
pwd_gen "github.com/sethvargo/go-password/password" pwd_gen "github.com/sethvargo/go-password/password"
) )
// TODO: Move this to model.user.search ? or to model.user.externalLoginUser ?
func SearchUsersByLoginName(loginName string) ([]*user_model.User, error) {
var actionsUser = user_model.NewActionsUser()
actionsUser.IsAdmin = true
options := &user_model.SearchUserOptions{
LoginName: loginName,
Actor: actionsUser,
Type: user_model.UserTypeRemoteUser,
OrderBy: db.SearchOrderByAlphabetically,
ListOptions: db.ListOptions{PageSize: 1},
IsActive: util.OptionalBoolFalse,
IncludeReserved: true,
}
users, _, err := user_model.SearchUsers(db.DefaultContext, options)
if err != nil {
return []*user_model.User{}, fmt.Errorf("search failed: %v", err)
}
log.Info("local found users: %v", len(users))
return users, nil
}
// Repository function returns the Repository actor for a repo // Repository function returns the Repository actor for a repo
func Repository(ctx *context.APIContext) { func Repository(ctx *context.APIContext) {
// swagger:operation GET /activitypub/repository-id/{repository-id} activitypub activitypubRepository // swagger:operation GET /activitypub/repository-id/{repository-id} activitypub activitypubRepository
@ -105,10 +80,10 @@ func RepositoryInbox(ctx *context.APIContext) {
var user *user_model.User var user *user_model.User
repository := ctx.Repo.Repository repository := ctx.Repo.Repository
log.Info("RepositoryInbox: repo: %v, owned by: %v", repository.Name, repository.OwnerName) log.Info("RepositoryInbox: repo: %v", repository)
activity := web.GetForm(ctx).(*forgefed.Star) activity := web.GetForm(ctx).(*forgefed.Star)
log.Info("RepositoryInbox: Activity.Source: %v, Activity.Actor %v, Activity.Actor.Id %v", activity.Source, activity.Actor, activity.Actor.GetID().String()) log.Info("RepositoryInbox: activity:%v", activity)
// parse actorId (person) // parse actorId (person)
actorId, err := forgefed.NewPersonId(activity.Actor.GetID().String(), string(activity.Source)) actorId, err := forgefed.NewPersonId(activity.Actor.GetID().String(), string(activity.Source))
@ -129,64 +104,48 @@ func RepositoryInbox(ctx *context.APIContext) {
} }
log.Info("RepositoryInbox: objectId validated: %v", objectId) log.Info("RepositoryInbox: objectId validated: %v", objectId)
adctorAsWebfinger := actorId.AsWebfinger() // used as LoginName in newly created user actorAsLoginId := actorId.AsLoginName() // used as LoginName in newly created user
log.Info("remotStargazer: %v", adctorAsWebfinger) log.Info("RepositoryInbox: remotStargazer: %v", actorAsLoginId)
// Check if user already exists // Check if user already exists
users, err := SearchUsersByLoginName(adctorAsWebfinger) users, err := SearchUsersByLoginName(actorAsLoginId)
if err != nil { if err != nil {
ctx.ServerError(fmt.Sprintf("Searching for user failed: %v"), err) ctx.ServerError(fmt.Sprintf("Searching for user failed: %v"), err)
return return
} }
log.Info("RepositoryInbox: local found users: %v", len(users))
switch len(users) { switch len(users) {
case 0: case 0:
{ {
user, err := createUserFromAP(ctx, actorId) user, err = createUserFromAP(ctx, actorId)
if err != nil { if err != nil {
ctx.ServerError(fmt.Sprintf("Searching for user failed: %v"), err) ctx.ServerError(fmt.Sprintf("Searching for user failed: %v"), err)
return return
} }
log.Info("created user from ap: %v", user) log.Info("RepositoryInbox: created user from ap: %v", user)
} }
case 1: case 1:
{ {
user = users[0] user = users[0]
log.Info("found user: %v", user) log.Info("RepositoryInbox: found user: %v", user)
} }
default: default:
{ {
ctx.Error(http.StatusInternalServerError, "StarRepo", fmt.Errorf("found more than one matches for federated users")) ctx.Error(http.StatusInternalServerError, "StarRepo",
fmt.Errorf("found more than one matches for federated users"))
return return
} }
} }
// TODO: why should we search user for a second time from db? // execute the activity if the repo was not stared already
remoteUser, err := user_model.GetUserByEmail(ctx, user.Email) alreadyStared := repo_model.IsStaring(ctx, user.ID, repository.ID)
if !alreadyStared {
err = repo_model.StarRepo(ctx, user.ID, repository.ID, true)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "StarRepo", err) ctx.Error(http.StatusInternalServerError, "StarRepo", err)
return return
} }
// check if already starred by this user
alreadyStared := repo_model.IsStaring(ctx, remoteUser.ID, repository.ID)
switch alreadyStared {
case true: // execute unstar action
{
err = repo_model.StarRepo(ctx, remoteUser.ID, repository.ID, false)
if err != nil {
ctx.Error(http.StatusInternalServerError, "StarRepo", err)
return
}
}
case false: // execute star action
{
err = repo_model.StarRepo(ctx, remoteUser.ID, repository.ID, true)
if err != nil {
ctx.Error(http.StatusInternalServerError, "StarRepo", err)
return
}
}
} }
// wait 5 sec. // wait 5 sec.
@ -195,14 +154,37 @@ func RepositoryInbox(ctx *context.APIContext) {
ctx.Status(http.StatusNoContent) ctx.Status(http.StatusNoContent)
} }
func createUserFromAP(ctx *context.APIContext, actorId forgefed.PersonId) (*user_model.User, error) { // TODO: Move this to model.user.search ? or to model.user.externalLoginUser ?
func SearchUsersByLoginName(loginName string) ([]*user_model.User, error) {
var actionsUser = user_model.NewActionsUser()
actionsUser.IsAdmin = true
options := &user_model.SearchUserOptions{
LoginName: loginName,
Actor: actionsUser,
Type: user_model.UserTypeRemoteUser,
OrderBy: db.SearchOrderByAlphabetically,
ListOptions: db.ListOptions{PageSize: 1},
IsActive: util.OptionalBoolFalse,
IncludeReserved: true,
}
users, _, err := user_model.SearchUsers(db.DefaultContext, options)
if err != nil {
return []*user_model.User{}, fmt.Errorf("search failed: %v", err)
}
return users, nil
}
func createUserFromAP(ctx *context.APIContext, personId forgefed.PersonId) (*user_model.User, error) {
// ToDo: Do we get a publicKeyId from server, repo or owner or repo? // ToDo: Do we get a publicKeyId from server, repo or owner or repo?
var actionsUser = user_model.NewActionsUser() var actionsUser = user_model.NewActionsUser()
client, err := api.NewClient(ctx, actionsUser, "no ide where to get key material.") client, err := api.NewClient(ctx, actionsUser, "no ide where to get key material.")
if err != nil { if err != nil {
return &user_model.User{}, err return &user_model.User{}, err
} }
response, err := client.Get(actorId.AsUri()) response, err := client.Get(personId.AsUri())
if err != nil { if err != nil {
return &user_model.User{}, err return &user_model.User{}, err
} }
@ -211,20 +193,29 @@ func createUserFromAP(ctx *context.APIContext, actorId forgefed.PersonId) (*user
if err != nil { if err != nil {
return &user_model.User{}, err return &user_model.User{}, err
} }
log.Info("RepositoryInbox: got body: %v", string(body))
person := ap.Person{} person := ap.Person{}
err = person.UnmarshalJSON(body) err = person.UnmarshalJSON(body)
if err != nil { if err != nil {
return &user_model.User{}, err return &user_model.User{}, err
} }
email := fmt.Sprintf("%v@%v", uuid.New().String(), actorId.Host) log.Info("RepositoryInbox: got person by ap: %v", person)
loginName := actorId.AsWebfinger() email := fmt.Sprintf("%v@%v", uuid.New().String(), personId.Host)
loginName := personId.AsLoginName()
name := fmt.Sprintf("%v%v", person.PreferredUsername.String(), personId.HostSuffix())
log.Info("RepositoryInbox: person.Name: %v", person.Name)
fullName := person.Name.String()
if len(person.Name) == 0 {
fullName = name
}
password, err := pwd_gen.Generate(32, 10, 10, false, true) password, err := pwd_gen.Generate(32, 10, 10, false, true)
if err != nil { if err != nil {
return &user_model.User{}, err return &user_model.User{}, err
} }
user := &user_model.User{ user := &user_model.User{
LowerName: strings.ToLower(person.Name.String()), LowerName: strings.ToLower(person.PreferredUsername.String()),
Name: person.Name.String(), Name: name,
FullName: fullName,
Email: email, Email: email,
EmailNotificationsPreference: "disabled", EmailNotificationsPreference: "disabled",
Passwd: password, Passwd: password,