Editing user profile; Warning! Mastodon compatibility broken and editing may lost your bio

This commit is contained in:
localhost_frssoft 2022-11-04 20:55:42 +03:00
parent 53353c0e64
commit 1160d00ee1
6 changed files with 104 additions and 2 deletions

View File

@ -13,6 +13,9 @@ type AccountPleroma struct {
Relationship Relationship `json:"relationship"`
IsAdmin bool `json:"is_admin"`
IsModerator bool `json:"is_moderator"`
IsConfirmed bool `json:"is_confirmed"`
AcceptsChatMessages bool `json:"accepts_chat_messages"`
HideFavourites *bool `json:"hide_favorites"`
}
// Account hold information for mastodon account.
@ -37,6 +40,7 @@ type Account struct {
Fields []Field `json:"fields"`
Bot bool `json:"bot"`
Pleroma *AccountPleroma `json:"pleroma"`
MastodonAccount bool
}
// Field is a Mastodon account profile field.
@ -60,6 +64,7 @@ func (c *Client) GetAccount(ctx context.Context, id string) (*Account, error) {
var account Account
params := url.Values{}
if account.Pleroma != nil {
account.MastodonAccount = false
params.Set("with_relationships", "1")
}
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/accounts/%s", url.PathEscape(string(id))), params, &account, nil)
@ -73,9 +78,16 @@ func (c *Client) GetAccount(ctx context.Context, id string) (*Account, error) {
}
if len(rs) > 0 {
if account.Pleroma != nil {
account.Pleroma = &AccountPleroma{*rs[0], account.Pleroma.IsAdmin, account.Pleroma.IsModerator}
account.Pleroma = &AccountPleroma{*rs[0],
account.Pleroma.IsAdmin,
account.Pleroma.IsModerator,
account.Pleroma.IsConfirmed,
account.Pleroma.AcceptsChatMessages,
account.Pleroma.HideFavourites,
}
} else {
account.Pleroma = &AccountPleroma{*rs[0], false, false}
account.MastodonAccount = true
account.Pleroma = &AccountPleroma{*rs[0], false, false, false, false, nil}
}
}
}
@ -105,6 +117,23 @@ type Profile struct {
// Set the base64 encoded character string of the image.
Avatar string
Header string
//Other settings
Bot *bool
Pleroma *ProfilePleroma
}
type ProfilePleroma struct {
AcceptsChatMessages *bool
ActorType *string
AllowFollowingMove *bool
Discoverable *bool
HideFavourites *bool
HideFollowers *bool
HideFollows *bool
HideFollowersCount *bool
HideFollowsCount *bool
}
// AccountUpdate updates the information of the current user.
@ -142,6 +171,15 @@ func (c *Client) AccountUpdate(ctx context.Context, profile *Profile) (*Account,
if profile.Header != "" {
params.Set("header", profile.Header)
}
if profile.Bot != nil {
params.Set("bot", strconv.FormatBool(*profile.Bot))
}
if profile.Pleroma != nil {
if profile.Pleroma.AcceptsChatMessages != nil {
params.Set("accepts_chat_messages", strconv.FormatBool(*profile.Pleroma.AcceptsChatMessages))
}
}
var account Account
err := c.doAPI(ctx, http.MethodPatch, "/api/v1/accounts/update_credentials", params, &account, nil)

View File

@ -109,6 +109,11 @@ type UserData struct {
NextLink string
}
type UserEditData struct {
*CommonData
User *mastodon.Account
}
type UserSearchData struct {
*CommonData
User *mastodon.Account

View File

@ -34,6 +34,7 @@ const (
RetweetedByPage = "retweetedby.tmpl"
SearchPage = "search.tmpl"
SettingsPage = "settings.tmpl"
UserEditPage = "useredit.tmpl"
FiltersPage = "filters.tmpl"
)

View File

@ -858,6 +858,19 @@ func (s *service) SearchPage(c *client,
return s.renderer.Render(c.rctx, c.w, renderer.SearchPage, data)
}
func (s *service) UserEditPage(c *client) (err error) {
cdata := s.cdata(c, "useredit", 0, 0, "")
u, err := c.GetAccountCurrentUser(c.ctx)
if err != nil {
return
}
data := &renderer.UserEditData{
CommonData: cdata,
User: u,
}
return s.renderer.Render(c.rctx, c.w, renderer.UserEditPage, data)
}
func (s *service) SettingsPage(c *client) (err error) {
cdata := s.cdata(c, "settings", 0, 0, "")
data := &renderer.SettingsData{
@ -1141,6 +1154,11 @@ func (s *service) SaveSettings(c *client, settings *model.Settings) (err error)
return s.sessionRepo.Add(sess)
}
func (s *service) UserSave(c *client, usersettings mastodon.Profile) (err error) {
_, err = c.AccountUpdate(c.ctx, &usersettings)
return
}
func (s *service) MuteConversation(c *client, id string) (err error) {
_, err = c.MuteConversation(c.ctx, id)
return

View File

@ -243,6 +243,43 @@ func NewHandler(s *service, logger *log.Logger, staticDir string) http.Handler {
return s.SearchPage(c, sq, qType, offset)
}, SESSION, HTML)
userEditPage := handle(func(c *client) error {
return s.UserEditPage(c)
}, SESSION, HTML)
userEdit := handle(func(c *client) error {
displayName := c.r.FormValue("display-name")
note := c.r.FormValue("note")
locked := c.r.FormValue("locked") == "true"
bot := c.r.FormValue("bot") == "true"
acceptsChatMessages := c.r.FormValue("accepts-chat-messages") == "true"
hideFavourites := c.r.FormValue("hide-favourites") == "true"
pleromaProfile := mastodon.ProfilePleroma{
AcceptsChatMessages: &acceptsChatMessages,
HideFavourites: &hideFavourites,
}
usersettings := mastodon.Profile{
DisplayName: &displayName,
Note: &note,
Locked: &locked,
Fields: nil,
Source: nil,
Avatar: "",
Header: "",
Bot: &bot,
Pleroma: &pleromaProfile,
}
err := s.UserSave(c, usersettings)
if err != nil {
return err
}
redirect(c, "/user/"+c.r.FormValue("id"))
return nil
}, SESSION, HTML)
settingsPage := handle(func(c *client) error {
return s.SettingsPage(c)
}, SESSION, HTML)
@ -790,6 +827,8 @@ func NewHandler(s *service, logger *log.Logger, staticDir string) http.Handler {
r.HandleFunc("/aboutinstance", aboutInstance).Methods(http.MethodGet)
r.HandleFunc("/emojis", emojisPage).Methods(http.MethodGet)
r.HandleFunc("/search", searchPage).Methods(http.MethodGet)
r.HandleFunc("/useredit", userEditPage).Methods(http.MethodGet)
r.HandleFunc("/useredit", userEdit).Methods(http.MethodPost)
r.HandleFunc("/settings", settingsPage).Methods(http.MethodGet)
r.HandleFunc("/filters", filtersPage).Methods(http.MethodGet)
r.HandleFunc("/signin", signin).Methods(http.MethodPost)

View File

@ -125,6 +125,7 @@
- <a href="/user/{{.User.ID}}/mutes"> mutes </a>
- <a href="/user/{{.User.ID}}/blocks"> blocks </a>
{{if .User.Locked}}- <a href="/user/{{.User.ID}}/requests"> requests </a>{{end}}
- <a href="/useredit"> edit </a>
</div>
{{end}}
<div>