Sort of implement personal feeds
This commit is contained in:
parent
1c216e415d
commit
8d2e008125
6 changed files with 47 additions and 5 deletions
|
@ -31,3 +31,6 @@ type Feed struct {
|
||||||
// Otherwise, use its own key
|
// Otherwise, use its own key
|
||||||
PublicKey sql.NullString
|
PublicKey sql.NullString
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Suffix added to feeds created as the default feed for a user
|
||||||
|
const FeedDefaultSuffix = "-default"
|
||||||
|
|
9
storage-new/models/FeedReason.go
Normal file
9
storage-new/models/FeedReason.go
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
package models
|
||||||
|
|
||||||
|
type FeedAppearanceReason string
|
||||||
|
|
||||||
|
const (
|
||||||
|
FeedAppearanceReasonBoost FeedAppearanceReason = "boost"
|
||||||
|
FeedAppearanceReasonFollowUser FeedAppearanceReason = "follow user"
|
||||||
|
FeedAppearanceReasonFollowTag FeedAppearanceReason = "follow tag"
|
||||||
|
)
|
|
@ -25,7 +25,7 @@ type NoteToFeed struct {
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
Note Note // The note being assigned
|
Note Note // The note being assigned
|
||||||
NoteId string
|
NoteId string
|
||||||
// Feed Feed
|
Feed *Feed
|
||||||
// FeedId uint64
|
FeedId uint64
|
||||||
// Reason AppearanceReason
|
Reason string
|
||||||
}
|
}
|
||||||
|
|
|
@ -183,6 +183,17 @@ func createLocalUser(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Error().Err(err).Msg("Failed to add links to new user")
|
log.Error().Err(err).Msg("Failed to add links to new user")
|
||||||
_ = webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
_ = webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
err = dbgen.Feed.Create(&models.Feed{
|
||||||
|
Owner: user,
|
||||||
|
OwnerId: user.ID,
|
||||||
|
Name: user.Username + models.FeedDefaultSuffix,
|
||||||
|
IsDefault: true,
|
||||||
|
PublicKey: sql.NullString{Valid: false},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("Failed to create default feed for new user")
|
||||||
|
_ = webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteUser(w http.ResponseWriter, r *http.Request) {
|
func deleteUser(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
@ -685,6 +685,7 @@ func handleCreate(w http.ResponseWriter, r *http.Request, object map[string]any)
|
||||||
log := hlog.FromRequest(r)
|
log := hlog.FromRequest(r)
|
||||||
activity := translators.ActivityCreate{}
|
activity := translators.ActivityCreate{}
|
||||||
err := mapstructure.Decode(object, &activity)
|
err := mapstructure.Decode(object, &activity)
|
||||||
|
targetUserId := r.PathValue("id")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().
|
log.Error().
|
||||||
Err(err).
|
Err(err).
|
||||||
|
@ -704,7 +705,7 @@ func handleCreate(w http.ResponseWriter, r *http.Request, object map[string]any)
|
||||||
}
|
}
|
||||||
switch val := activity.Object.(type) {
|
switch val := activity.Object.(type) {
|
||||||
case string:
|
case string:
|
||||||
actor, err := dbgen.User.GetById(r.PathValue("id"))
|
actor, err := dbgen.User.GetById(targetUserId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("Failed to get local actor for importing targeted note")
|
log.Error().Err(err).Msg("Failed to get local actor for importing targeted note")
|
||||||
_ = webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
_ = webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||||
|
@ -779,6 +780,13 @@ func handleCreate(w http.ResponseWriter, r *http.Request, object map[string]any)
|
||||||
dbNote.RepliesTo = sql.NullString{Valid: true, String: replyUrl}
|
dbNote.RepliesTo = sql.NullString{Valid: true, String: replyUrl}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
feed, err := dbgen.Feed.Where(dbgen.Feed.OwnerId.Eq(targetUserId), dbgen.Feed.IsDefault.Is(true)).
|
||||||
|
First()
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("Failed to get feed for targeted user inbox")
|
||||||
|
_ = webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||||
|
return false
|
||||||
|
}
|
||||||
tx := dbgen.Q.Begin()
|
tx := dbgen.Q.Begin()
|
||||||
err = tx.Note.Create(&dbNote)
|
err = tx.Note.Create(&dbNote)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -800,6 +808,17 @@ func handleCreate(w http.ResponseWriter, r *http.Request, object map[string]any)
|
||||||
_ = webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
_ = webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
err = tx.NoteToFeed.Create(&models.NoteToFeed{
|
||||||
|
Reason: string(models.FeedAppearanceReasonFollowUser),
|
||||||
|
NoteId: dbNote.ID,
|
||||||
|
FeedId: uint64(feed.ID),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
_ = tx.Rollback()
|
||||||
|
log.Error().Err(err).Any("note", dbNote).Msg("Failed to create note to feed relation in db")
|
||||||
|
_ = webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
|
||||||
|
return false
|
||||||
|
}
|
||||||
err = tx.Commit()
|
err = tx.Commit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Any("note", dbNote).Msg("Failed to submit note creation in db")
|
log.Error().Err(err).Any("note", dbNote).Msg("Failed to submit note creation in db")
|
||||||
|
|
|
@ -175,7 +175,7 @@ func applyBodyHash(headers http.Header, body []byte) error {
|
||||||
// If the request is POST, stores it in the db as not processed yet.
|
// If the request is POST, stores it in the db as not processed yet.
|
||||||
// This is to ensure data consistency
|
// This is to ensure data consistency
|
||||||
func prePostRequest(
|
func prePostRequest(
|
||||||
method, target string,
|
target string,
|
||||||
body []byte,
|
body []byte,
|
||||||
actor *models.User,
|
actor *models.User,
|
||||||
) (*uint64, error) {
|
) (*uint64, error) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue