follow requests prettymuch working

This commit is contained in:
tsmethurst 2021-05-14 23:46:14 +02:00
commit d68c64505b
15 changed files with 520 additions and 16 deletions

View file

@ -112,6 +112,8 @@ type DB interface {
HANDY SHORTCUTS
*/
AcceptFollowRequest(originAccountID string, targetAccountID string) error
// CreateInstanceAccount creates an account in the database with the same username as the instance host value.
// Ie., if the instance is hosted at 'example.org' the instance user will have a username of 'example.org'.
// This is needed for things like serving files that belong to the instance and not an individual user/account.
@ -148,6 +150,8 @@ type DB interface {
// In case of no entries, a 'no entries' error will be returned
GetFollowersByAccountID(accountID string, followers *[]gtsmodel.Follow) error
GetFavesByAccountID(accountID string, faves *[]gtsmodel.StatusFave) error
// GetStatusesByAccountID is a shortcut for the common action of fetching a list of statuses produced by accountID.
// The given slice 'statuses' will be set to the result of the query, whatever it is.
// In case of no entries, a 'no entries' error will be returned

View file

@ -307,6 +307,32 @@ func (ps *postgresService) DeleteWhere(key string, value interface{}, i interfac
HANDY SHORTCUTS
*/
func (ps *postgresService) AcceptFollowRequest(originAccountID string, targetAccountID string) error {
fr := &gtsmodel.FollowRequest{}
if err := ps.conn.Model(fr).Where("account_id = ?", originAccountID).Where("target_account_id = ?", targetAccountID).Select(); err != nil {
if err == pg.ErrMultiRows {
return db.ErrNoEntries{}
}
return err
}
follow := &gtsmodel.Follow{
AccountID: originAccountID,
TargetAccountID: targetAccountID,
URI: fr.URI,
}
if _, err := ps.conn.Model(follow).Insert(); err != nil {
return err
}
if _, err := ps.conn.Model(&gtsmodel.FollowRequest{}).Where("account_id = ?", originAccountID).Where("target_account_id = ?", targetAccountID).Delete(); err != nil {
return err
}
return nil
}
func (ps *postgresService) CreateInstanceAccount() error {
username := ps.config.Host
key, err := rsa.GenerateKey(rand.Reader, 2048)
@ -393,7 +419,7 @@ func (ps *postgresService) GetLocalAccountByUsername(username string, account *g
func (ps *postgresService) GetFollowRequestsForAccountID(accountID string, followRequests *[]gtsmodel.FollowRequest) error {
if err := ps.conn.Model(followRequests).Where("target_account_id = ?", accountID).Select(); err != nil {
if err == pg.ErrNoRows {
return db.ErrNoEntries{}
return nil
}
return err
}
@ -403,7 +429,7 @@ func (ps *postgresService) GetFollowRequestsForAccountID(accountID string, follo
func (ps *postgresService) GetFollowingByAccountID(accountID string, following *[]gtsmodel.Follow) error {
if err := ps.conn.Model(following).Where("account_id = ?", accountID).Select(); err != nil {
if err == pg.ErrNoRows {
return db.ErrNoEntries{}
return nil
}
return err
}
@ -413,7 +439,17 @@ func (ps *postgresService) GetFollowingByAccountID(accountID string, following *
func (ps *postgresService) GetFollowersByAccountID(accountID string, followers *[]gtsmodel.Follow) error {
if err := ps.conn.Model(followers).Where("target_account_id = ?", accountID).Select(); err != nil {
if err == pg.ErrNoRows {
return db.ErrNoEntries{}
return nil
}
return err
}
return nil
}
func (ps *postgresService) GetFavesByAccountID(accountID string, faves *[]gtsmodel.StatusFave) error {
if err := ps.conn.Model(faves).Where("account_id = ?", accountID).Select(); err != nil {
if err == pg.ErrNoRows {
return nil
}
return err
}