From 25829d2d0e379c323b8f2ae6e7c2aad7548f0a30 Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Sat, 18 Jun 2022 13:57:30 -0700 Subject: sticky and lock implemented --- activitypub/object.go | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) (limited to 'activitypub/object.go') diff --git a/activitypub/object.go b/activitypub/object.go index 5eb8e67..acbe4a1 100644 --- a/activitypub/object.go +++ b/activitypub/object.go @@ -367,6 +367,9 @@ func (obj ObjectBase) GetCollectionLocal() (Collection, error) { return nColl, util.MakeError(err, "GetCollectionLocal") } + post.Sticky, _ = post.IsSticky() + post.Locked, _ = post.IsLocked() + post.Actor = actor.Id if post.InReplyTo, err = post.GetInReplyTo(); err != nil { @@ -462,6 +465,9 @@ func (obj ObjectBase) GetCollectionFromPath() (Collection, error) { return nColl, nil } + post.Sticky, _ = post.IsSticky() + post.Locked, _ = post.IsLocked() + post.Actor = actor.Id if post.InReplyTo, err = post.GetInReplyTo(); err != nil { @@ -1361,3 +1367,96 @@ func (obj ObjectBase) WriteWithAttachment(attachment ObjectBase) { panic(e) } } + +func (obj ObjectBase) MarkSticky(actorID string) error { + var count int + + var query = `select count(id) from replies where inreplyto='' and id=$1` + if err := config.DB.QueryRow(query, obj.Id).Scan(&count); err != nil { + return util.MakeError(err, "MarkSticky") + } + + if count == 1 { + var nCount int + query = `select count(activity_id) from sticky where activity_id=$1` + if err := config.DB.QueryRow(query, obj.Id).Scan(&nCount); err != nil { + return util.MakeError(err, "MarkSticky") + } + + if nCount > 0 { + query = `delete from sticky where activity_id=$1` + if _, err := config.DB.Exec(query, obj.Id); err != nil { + return util.MakeError(err, "MarkSticky") + } + } else { + query = `insert into sticky (actor_id, activity_id) values ($1, $2)` + if _, err := config.DB.Exec(query, actorID, obj.Id); err != nil { + return util.MakeError(err, "MarkSticky") + } + } + } + + return nil +} + +func (obj ObjectBase) MarkLocked(actorID string) error { + var count int + + var query = `select count(id) from replies where inreplyto='' and id=$1` + if err := config.DB.QueryRow(query, obj.Id).Scan(&count); err != nil { + return util.MakeError(err, "MarkLocked") + } + + if count == 1 { + var nCount int + + query = `select count(activity_id) from locked where activity_id=$1` + if err := config.DB.QueryRow(query, obj.Id).Scan(&nCount); err != nil { + return util.MakeError(err, "MarkLocked") + } + + if nCount > 0 { + query = `delete from locked where activity_id=$1` + if _, err := config.DB.Exec(query, obj.Id); err != nil { + return util.MakeError(err, "MarkLocked") + } + } else { + query = `insert into locked (actor_id, activity_id) values ($1, $2)` + if _, err := config.DB.Exec(query, actorID, obj.Id); err != nil { + return util.MakeError(err, "MarkLocked") + } + } + } + + return nil +} + +func (obj ObjectBase) IsSticky() (bool, error) { + var count int + + query := `select count(activity_id) from sticky where activity_id=$1 ` + if err := config.DB.QueryRow(query, obj.Id).Scan(&count); err != nil { + return false, util.MakeError(err, "IsSticky") + } + + if count != 0 { + return true, nil + } + + return false, nil +} + +func (obj ObjectBase) IsLocked() (bool, error) { + var count int + + query := `select count(activity_id) from locked where activity_id=$1 ` + if err := config.DB.QueryRow(query, obj.Id).Scan(&count); err != nil { + return false, util.MakeError(err, "IsSticky") + } + + if count != 0 { + return true, nil + } + + return false, nil +} -- cgit v1.2.3