mention notifications working

This commit is contained in:
tsmethurst
2021-05-27 15:44:00 +02:00
parent 6eaeaa4d18
commit 4d5c150f9f
8 changed files with 88 additions and 17 deletions

View File

@ -26,6 +26,69 @@ import (
)
func (p *processor) notifyStatus(status *gtsmodel.Status) error {
// if there are no mentions in this status then just bail
if len(status.Mentions) == 0 {
return nil
}
if status.GTSMentions == nil {
// there are mentions but they're not fully populated on the status yet so do this
menchies := []*gtsmodel.Mention{}
for _, m := range status.Mentions {
gtsm := &gtsmodel.Mention{}
if err := p.db.GetByID(m, gtsm); err != nil {
return fmt.Errorf("notifyStatus: error getting mention with id %s from the db: %s", m, err)
}
menchies = append(menchies, gtsm)
}
status.GTSMentions = menchies
}
// now we have mentions as full gtsmodel.Mention structs on the status we can continue
for _, m := range status.GTSMentions {
// make sure this is a local account, otherwise we don't need to create a notification for it
if m.GTSAccount == nil {
a := &gtsmodel.Account{}
if err := p.db.GetByID(m.TargetAccountID, a); err != nil {
// we don't have the account or there's been an error
return fmt.Errorf("notifyStatus: error getting account with id %s from the db: %s", m.TargetAccountID, err)
}
m.GTSAccount = a
}
if m.GTSAccount.Domain != "" {
// not a local account so skip it
continue
}
// make sure a notif doesn't already exist for this mention
err := p.db.GetWhere([]db.Where{
{Key: "notification_type", Value: gtsmodel.NotificationMention},
{Key: "target_account_id", Value: m.TargetAccountID},
{Key: "origin_account_id", Value: status.AccountID},
{Key: "status_id", Value: status.ID},
}, &gtsmodel.Notification{})
if err == nil {
// notification exists already so just continue
continue
}
if _, ok := err.(db.ErrNoEntries); !ok {
// there's a real error in the db
return fmt.Errorf("notifyStatus: error checking existence of notification for mention with id %s : %s", m.ID, err)
}
// if we've reached this point we know the mention is for a local account, and the notification doesn't exist, so create it
notif := &gtsmodel.Notification{
NotificationType: gtsmodel.NotificationMention,
TargetAccountID: m.TargetAccountID,
OriginAccountID: status.AccountID,
StatusID: status.ID,
}
if err := p.db.Put(notif); err != nil {
return fmt.Errorf("notifyStatus: error putting notification in database: %s", err)
}
}
return nil
}