-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
524 additions
and
303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Content managed by Project Forge, see [projectforge.md] for details. | ||
package user | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/samber/lo" | ||
"golang.org/x/exp/slices" | ||
|
||
"github.com/kyleu/rituals/app/util" | ||
) | ||
|
||
type Accounts []*Account | ||
|
||
func (a Accounts) String() string { | ||
return strings.Join(lo.Map(a, func(x *Account, _ int) string { | ||
return x.String() | ||
}), ",") | ||
} | ||
|
||
func (a Accounts) TitleString() string { | ||
return strings.Join(lo.Map(a, func(x *Account, _ int) string { | ||
return x.TitleString() | ||
}), ",") | ||
} | ||
|
||
func (a Accounts) Images() []string { | ||
ret := make(util.KeyVals[string], 0, len(a)) | ||
lo.ForEach(a, func(x *Account, _ int) { | ||
if x.Picture != "" { | ||
ret = append(ret, &util.KeyVal[string]{Key: x.Provider, Val: x.Picture}) | ||
} | ||
}) | ||
return ret.Values() | ||
} | ||
|
||
func (a Accounts) Image() string { | ||
if is := a.Images(); len(is) > 0 { | ||
return is[0] | ||
} | ||
return "" | ||
} | ||
|
||
func (a Accounts) Sort() { | ||
slices.SortFunc(a, func(l *Account, r *Account) bool { | ||
if l.Provider == r.Provider { | ||
return strings.ToLower(l.Email) < strings.ToLower(r.Email) | ||
} | ||
return l.Provider < r.Provider | ||
}) | ||
} | ||
|
||
func (a Accounts) GetByProvider(p string) Accounts { | ||
return lo.Filter(a, func(x *Account, _ int) bool { | ||
return x.Provider == p | ||
}) | ||
} | ||
|
||
func (a Accounts) GetByProviderDomain(p string, d string) *Account { | ||
return lo.FindOrElse(a, nil, func(x *Account) bool { | ||
return x.Provider == p && x.Domain() == d | ||
}) | ||
} | ||
|
||
func (a Accounts) Matches(match string) bool { | ||
if match == "" || match == "*" { | ||
return true | ||
} | ||
if strings.Contains(match, ",") { | ||
return lo.ContainsBy(util.StringSplitAndTrim(match, ","), func(x string) bool { | ||
return a.Matches(x) | ||
}) | ||
} | ||
prv, acct := util.StringSplit(match, ':', true) | ||
for _, x := range a { | ||
if x.Provider == prv { | ||
if acct == "" { | ||
return true | ||
} | ||
return strings.HasSuffix(x.Email, acct) | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (a Accounts) Purge(keys ...string) Accounts { | ||
ret := make(Accounts, 0, len(a)) | ||
for _, ss := range a { | ||
hit := false | ||
for _, key := range keys { | ||
if ss.Provider == key { | ||
hit = true | ||
} | ||
} | ||
if !hit { | ||
ret = append(ret, ss) | ||
} | ||
} | ||
return ret | ||
} | ||
|
||
func AccountsFromString(s string) Accounts { | ||
return lo.Map(util.StringSplitAndTrim(s, ","), func(x string, _ int) *Account { | ||
return accountFromString(x) | ||
}) | ||
} |
Oops, something went wrong.