From 2e8c6e6fb67c9832c5c2b6ad0ceaf48c979ee6bd Mon Sep 17 00:00:00 2001 From: Azareal Date: Sat, 1 Feb 2020 16:56:04 +1000 Subject: [PATCH] fix deleting like alerts when unliking a reply immediately delete alerts via websockets when a topic or reply is unliked Add AidsByParams method to ActivityStream. --- common/activity_stream.go | 19 +++++++++++++++++++ common/settings.go | 14 +++++++------- routes/reply.go | 12 ++++++++++-- routes/topic.go | 10 +++++++++- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/common/activity_stream.go b/common/activity_stream.go index a35d03f1..7fa745da 100644 --- a/common/activity_stream.go +++ b/common/activity_stream.go @@ -14,6 +14,7 @@ type ActivityStream interface { Delete(id int) error DeleteByParams(event string, targetID int, targetType string) error DeleteByParamsExtra(event string, targetID int, targetType, extra string) error + AidsByParams(event string, elementID int, elementType string) (aids []int, err error) AidsByParamsExtra(event string, elementID int, elementType, extra string) (aids []int, err error) Count() (count int) } @@ -24,6 +25,7 @@ type DefaultActivityStream struct { delete *sql.Stmt deleteByParams *sql.Stmt deleteByParamsExtra *sql.Stmt + aidsByParams *sql.Stmt aidsByParamsExtra *sql.Stmt count *sql.Stmt } @@ -36,6 +38,7 @@ func NewDefaultActivityStream(acc *qgen.Accumulator) (*DefaultActivityStream, er delete: acc.Delete(as).Where("asid=?").Prepare(), deleteByParams: acc.Delete(as).Where("event=? AND elementID=? AND elementType=?").Prepare(), deleteByParamsExtra: acc.Delete(as).Where("event=? AND elementID=? AND elementType=? AND extra=?").Prepare(), + aidsByParams: acc.Select(as).Columns("asid").Where("event=? AND elementID=? AND elementType=?").Prepare(), aidsByParamsExtra: acc.Select(as).Columns("asid").Where("event=? AND elementID=? AND elementType=? AND extra=?").Prepare(), count: acc.Count(as).Prepare(), }, acc.FirstError() @@ -71,6 +74,22 @@ func (s *DefaultActivityStream) DeleteByParamsExtra(event string, elementID int, return err } +func (s *DefaultActivityStream) AidsByParams(event string, elementID int, elementType string) (aids []int, err error) { + rows, err := s.aidsByParams.Query(event, elementID, elementType) + if err != nil { + return nil, err + } + defer rows.Close() + for rows.Next() { + var aid int + if err := rows.Scan(&aid); err != nil { + return nil, err + } + aids = append(aids, aid) + } + return aids, rows.Err() +} + func (s *DefaultActivityStream) AidsByParamsExtra(event string, elementID int, elementType, extra string) (aids []int, err error) { rows, err := s.aidsByParamsExtra.Query(event, elementID, elementType, extra) if err != nil { diff --git a/common/settings.go b/common/settings.go index f8491f7f..749777f7 100644 --- a/common/settings.go +++ b/common/settings.go @@ -7,7 +7,7 @@ import ( "strings" "sync/atomic" - "github.com/Azareal/Gosora/query_gen" + qgen "github.com/Azareal/Gosora/query_gen" ) var SettingBox atomic.Value // An atomic value pointing to a SettingBox @@ -16,7 +16,7 @@ var SettingBox atomic.Value // An atomic value pointing to a SettingBox type SettingMap map[string]interface{} type SettingStore interface { - ParseSetting(sname string, scontent string, stype string, sconstraint string) string + ParseSetting(sname, scontent, stype, sconstraint string) string BypassGet(name string) (*Setting, error) BypassGetAll(name string) ([]*Setting, error) } @@ -47,9 +47,9 @@ func init() { DbInits.Add(func(acc *qgen.Accumulator) error { s := "settings" settingStmts = SettingStmts{ - getAll: acc.Select(s).Columns("name, content, type, constraints").Prepare(), - get: acc.Select(s).Columns("content, type, constraints").Where("name = ?").Prepare(), - update: acc.Update(s).Set("content = ?").Where("name = ?").Prepare(), + getAll: acc.Select(s).Columns("name,content,type,constraints").Prepare(), + get: acc.Select(s).Columns("content,type,constraints").Where("name=?").Prepare(), + update: acc.Update(s).Set("content=?").Where("name=?").Prepare(), } return acc.FirstError() }) @@ -80,7 +80,7 @@ func LoadSettings() error { } // TODO: Add better support for HTML attributes (html-attribute). E.g. Meta descriptions. -func (sBox SettingMap) ParseSetting(sname string, scontent string, stype string, constraint string) (err error) { +func (sBox SettingMap) ParseSetting(sname, scontent, stype, constraint string) (err error) { ssBox := map[string]interface{}(sBox) switch stype { case "bool": @@ -146,7 +146,7 @@ func (sBox SettingMap) BypassGetAll() (settingList []*Setting, err error) { return settingList, rows.Err() } -func (sBox SettingMap) Update(name string, content string) RouteError { +func (sBox SettingMap) Update(name, content string) RouteError { s, err := sBox.BypassGet(name) if err == ErrNoRows { return FromError(err) diff --git a/routes/reply.go b/routes/reply.go index 39fa87ed..ac8dd5cf 100644 --- a/routes/reply.go +++ b/routes/reply.go @@ -566,8 +566,16 @@ func ReplyUnlikeSubmit(w http.ResponseWriter, r *http.Request, user c.User, srid if err != nil { return c.InternalErrorJSQ(err, w, r, js) } - // TODO: Push dismiss-event alerts to the users. - err = c.Activity.DeleteByParams("like", topic.ID, "reply") + + // TODO: Better coupling between the two params queries + aids, err := c.Activity.AidsByParams("like", reply.ID, "post") + if err != nil { + return c.InternalErrorJSQ(err, w, r, js) + } + for _, aid := range aids { + c.DismissAlert(reply.CreatedBy, aid) + } + err = c.Activity.DeleteByParams("like", reply.ID, "post") if err != nil { return c.InternalErrorJSQ(err, w, r, js) } diff --git a/routes/topic.go b/routes/topic.go index 2f6fcb9c..6ac9531d 100644 --- a/routes/topic.go +++ b/routes/topic.go @@ -993,7 +993,15 @@ func UnlikeTopicSubmit(w http.ResponseWriter, r *http.Request, user c.User, stid if err != nil { return c.InternalErrorJSQ(err, w, r, js) } - // TODO: Push dismiss-event alerts to the users. + + // TODO: Better coupling between the two params queries + aids, err := c.Activity.AidsByParams("like", topic.ID, "topic") + if err != nil { + return c.InternalErrorJSQ(err, w, r, js) + } + for _, aid := range aids { + c.DismissAlert(topic.CreatedBy, aid) + } err = c.Activity.DeleteByParams("like", topic.ID, "topic") if err != nil { return c.InternalErrorJSQ(err, w, r, js)