start work on notifications

This commit is contained in:
tsmethurst
2021-05-25 17:42:17 +02:00
parent e670c32a91
commit 5853179728
17 changed files with 437 additions and 5 deletions

View File

@ -102,6 +102,15 @@ type Followable interface {
withObject
}
// Likeable represents the minimum interface for an activitystreams 'like' activity.
type Likeable interface {
withJSONLDId
withTypeName
withActor
withObject
}
type withJSONLDId interface {
GetJSONLDId() vocab.JSONLDIdProperty
}

View File

@ -380,6 +380,48 @@ func (c *converter) ASFollowToFollow(followable Followable) (*gtsmodel.Follow, e
return follow, nil
}
func (c *converter) ASLikeToFave(likeable Likeable) (*gtsmodel.StatusFave, error) {
idProp := likeable.GetJSONLDId()
if idProp == nil || !idProp.IsIRI() {
return nil, errors.New("no id property set on like, or was not an iri")
}
uri := idProp.GetIRI().String()
origin, err := extractActor(likeable)
if err != nil {
return nil, errors.New("error extracting actor property from like")
}
originAccount := &gtsmodel.Account{}
if err := c.db.GetWhere([]db.Where{{Key: "uri", Value: origin.String()}}, originAccount); err != nil {
return nil, fmt.Errorf("error extracting account with uri %s from the database: %s", origin.String(), err)
}
target, err := extractObject(likeable)
if err != nil {
return nil, errors.New("error extracting object property from like")
}
targetStatus := &gtsmodel.Status{}
if err := c.db.GetWhere([]db.Where{{Key: "uri", Value: target.String()}}, targetStatus); err != nil {
return nil, fmt.Errorf("error extracting status with uri %s from the database: %s", target.String(), err)
}
targetAccount := &gtsmodel.Account{}
if err := c.db.GetByID(targetStatus.AccountID, targetAccount); err != nil {
return nil, fmt.Errorf("error extracting account with id %s from the database: %s", targetStatus.AccountID, err)
}
return &gtsmodel.StatusFave{
TargetAccountID: targetAccount.ID,
StatusID: targetStatus.ID,
AccountID: originAccount.ID,
URI: uri,
GTSStatus: targetStatus,
GTSTargetAccount: targetAccount,
GTSFavingAccount: originAccount,
}, nil
}
func isPublic(tos []*url.URL) bool {
for _, entry := range tos {
if strings.EqualFold(entry.String(), "https://www.w3.org/ns/activitystreams#Public") {

View File

@ -84,6 +84,8 @@ type TypeConverter interface {
// RelationshipToMasto converts a gts relationship into its mastodon equivalent for serving in various places
RelationshipToMasto(r *gtsmodel.Relationship) (*model.Relationship, error)
NotificationToMasto(n *gtsmodel.Notification) (*model.Notification, error)
/*
FRONTEND (mastodon) MODEL TO INTERNAL (gts) MODEL
*/
@ -107,6 +109,8 @@ type TypeConverter interface {
ASFollowToFollowRequest(followable Followable) (*gtsmodel.FollowRequest, error)
// ASFollowToFollowRequest converts a remote activitystreams `follow` representation into gts model follow.
ASFollowToFollow(followable Followable) (*gtsmodel.Follow, error)
// ASLikeToFave converts a remote activitystreams 'like' representation into a gts model status fave.
ASLikeToFave(likeable Likeable) (*gtsmodel.StatusFave, error)
/*
INTERNAL (gts) MODEL TO ACTIVITYSTREAMS MODEL

View File

@ -138,6 +138,9 @@ func (c *converter) AccountToMastoPublic(a *gtsmodel.Account) (*model.Account, e
fields = append(fields, mField)
}
emojis := []model.Emoji{}
// TODO: account emojis
var acct string
if a.Domain != "" {
// this is a remote user
@ -165,7 +168,7 @@ func (c *converter) AccountToMastoPublic(a *gtsmodel.Account) (*model.Account, e
FollowingCount: followingCount,
StatusesCount: statusesCount,
LastStatusAt: lastStatusAt,
Emojis: nil, // TODO: implement this
Emojis: emojis, // TODO: implement this
Fields: fields,
}, nil
}
@ -594,3 +597,60 @@ func (c *converter) RelationshipToMasto(r *gtsmodel.Relationship) (*model.Relati
Note: r.Note,
}, nil
}
func (c *converter) NotificationToMasto(n *gtsmodel.Notification) (*model.Notification, error) {
if n.GTSTargetAccount == nil {
tAccount := &gtsmodel.Account{}
if err := c.db.GetByID(n.TargetAccountID, tAccount); err != nil {
return nil, fmt.Errorf("NotificationToMasto: error getting target account with id %s from the db: %s", n.TargetAccountID, err)
}
n.GTSTargetAccount = tAccount
}
if n.GTSOriginAccount == nil {
ogAccount := &gtsmodel.Account{}
if err := c.db.GetByID(n.OriginAccountID, ogAccount); err != nil {
return nil, fmt.Errorf("NotificationToMasto: error getting origin account with id %s from the db: %s", n.OriginAccountID, err)
}
n.GTSOriginAccount = ogAccount
}
mastoAccount, err := c.AccountToMastoPublic(n.GTSOriginAccount)
if err != nil {
return nil, fmt.Errorf("NotificationToMasto: error converting account to masto: %s", err)
}
var mastoStatus *model.Status
if n.StatusID != "" {
if n.GTSStatus == nil {
status := &gtsmodel.Status{}
if err := c.db.GetByID(n.StatusID, status); err != nil {
return nil, fmt.Errorf("NotificationToMasto: error getting status with id %s from the db: %s", n.StatusID, err)
}
n.GTSStatus = status
}
var replyToAccount *gtsmodel.Account
if n.GTSStatus.InReplyToAccountID != "" {
r := &gtsmodel.Account{}
if err := c.db.GetByID(n.GTSStatus.InReplyToAccountID, r); err != nil {
return nil, fmt.Errorf("NotificationToMasto: error getting replied to account with id %s from the db: %s", n.GTSStatus.InReplyToAccountID, err)
}
replyToAccount = r
}
var err error
mastoStatus, err = c.StatusToMasto(n.GTSStatus, n.GTSTargetAccount, n.GTSTargetAccount, nil, replyToAccount, nil)
if err != nil {
return nil, fmt.Errorf("NotificationToMasto: error converting status to masto: %s", err)
}
}
return &model.Notification{
ID: n.ID,
Type: string(n.NotificationType),
CreatedAt: n.CreatedAt.Format(time.RFC3339),
Account: mastoAccount,
Status: mastoStatus,
}, nil
}