aboutsummaryrefslogtreecommitdiff
path: root/activitypub
diff options
context:
space:
mode:
authorFChannel <>2022-06-18 13:57:30 -0700
committerFChannel <>2022-06-19 12:53:29 -0700
commit25829d2d0e379c323b8f2ae6e7c2aad7548f0a30 (patch)
treec828eb5bb04ce04141c717c26b637a59b32ab979 /activitypub
parentf3d1683d6562afb30522afb98fb3a22456473275 (diff)
sticky and lock implemented
Diffstat (limited to 'activitypub')
-rw-r--r--activitypub/actor.go89
-rw-r--r--activitypub/object.go99
-rw-r--r--activitypub/structs.go (renamed from activitypub/activityPubStruct.go)2
3 files changed, 187 insertions, 3 deletions
diff --git a/activitypub/actor.go b/activitypub/actor.go
index 9996abd..4142685 100644
--- a/activitypub/actor.go
+++ b/activitypub/actor.go
@@ -214,11 +214,18 @@ func (actor Actor) GetCatalogCollection() (Collection, error) {
var err error
var rows *sql.Rows
- query := `select x.id, x.name, x.content, x.type, x.published, x.updated, x.attributedto, x.attachment, x.preview, x.actor, x.tripcode, x.sensitive from (select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode, sensitive from activitystream where actor=$1 and id in (select id from replies where inreplyto='') and type='Note' union select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode, sensitive from activitystream where actor in (select following from following where id=$1) and id in (select id from replies where inreplyto='') and type='Note' union select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode, sensitive from cacheactivitystream where actor in (select following from following where id=$1) and id in (select id from replies where inreplyto='') and type='Note') as x order by x.updated desc limit 165`
+ query := `select x.id, x.name, x.content, x.type, x.published, x.updated, x.attributedto, x.attachment, x.preview, x.actor, x.tripcode, x.sensitive from (select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode, sensitive from activitystream where actor=$1 and id in (select id from replies where inreplyto='') and type='Note' and id not in (select activity_id from sticky where actor_id=$1) union select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode, sensitive from activitystream where actor in (select following from following where id=$1) and id in (select id from replies where inreplyto='') and type='Note' and id not in (select activity_id from sticky where actor_id=$1) union select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode, sensitive from cacheactivitystream where actor in (select following from following where id=$1) and id in (select id from replies where inreplyto='') and type='Note' and id not in (select activity_id from sticky where actor_id=$1)) as x order by x.updated desc limit 165`
+
if rows, err = config.DB.Query(query, actor.Id); err != nil {
return nColl, util.MakeError(err, "GetCatalogCollection")
}
+ stickies, _ := actor.GetStickies()
+
+ for _, e := range stickies.OrderedItems {
+ result = append(result, e)
+ }
+
defer rows.Close()
for rows.Next() {
var post ObjectBase
@@ -236,6 +243,7 @@ func (actor Actor) GetCatalogCollection() (Collection, error) {
return nColl, util.MakeError(err, "GetCatalogCollection")
}
+ post.Locked, _ = post.IsLocked()
post.Actor = actor.Id
var replies CollectionBase
@@ -277,9 +285,22 @@ func (actor Actor) GetCollectionPage(page int) (Collection, error) {
var err error
var rows *sql.Rows
- query := `select count (x.id) over(), x.id, x.name, x.content, x.type, x.published, x.updated, x.attributedto, x.attachment, x.preview, x.actor, x.tripcode, x.sensitive from (select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode, sensitive from activitystream where actor=$1 and id in (select id from replies where inreplyto='') and type='Note' union select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode, sensitive from activitystream where actor in (select following from following where id=$1) and id in (select id from replies where inreplyto='') and type='Note' union select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode, sensitive from cacheactivitystream where actor in (select following from following where id=$1) and id in (select id from replies where inreplyto='') and type='Note') as x order by x.updated desc limit 15 offset $2`
+ query := `select count (x.id) over(), x.id, x.name, x.content, x.type, x.published, x.updated, x.attributedto, x.attachment, x.preview, x.actor, x.tripcode, x.sensitive from (select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode, sensitive from activitystream where actor=$1 and id in (select id from replies where inreplyto='') and type='Note' and id not in (select activity_id from sticky where actor_id=$1) union select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode, sensitive from activitystream where actor in (select following from following where id=$1) and id in (select id from replies where inreplyto='') and type='Note' and id not in (select activity_id from sticky where actor_id=$1) union select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode, sensitive from cacheactivitystream where id not in (select activity_id from sticky where actor_id=$1) and actor in (select following from following where id=$1) and id in (select id from replies where inreplyto='') and type='Note') as x order by x.updated desc limit $2 offset $3`
+
+ limit := 15
+
+ if page == 0 {
+ stickies, _ := actor.GetStickies()
+ limit = limit - stickies.TotalItems
+
+ for _, e := range stickies.OrderedItems {
+ result = append(result, e)
+ }
+ }
- if rows, err = config.DB.Query(query, actor.Id, page*15); err != nil {
+ offset := page * limit
+
+ if rows, err = config.DB.Query(query, actor.Id, limit, offset); err != nil {
return nColl, util.MakeError(err, "GetCollectionPage")
}
@@ -301,6 +322,7 @@ func (actor Actor) GetCollectionPage(page int) (Collection, error) {
return nColl, util.MakeError(err, "GetCollectionPage")
}
+ post.Locked, _ = post.IsLocked()
post.Actor = actor.Id
post.Replies, post.Replies.TotalItems, post.Replies.TotalImgs, err = post.GetRepliesLimit(5)
@@ -359,6 +381,9 @@ func (actor Actor) GetCollection() (Collection, error) {
return nColl, util.MakeError(err, "GetCollection")
}
+ post.Sticky, _ = post.IsSticky()
+ post.Locked, _ = post.IsLocked()
+
post.Actor = actor.Id
post.Replies, post.Replies.TotalItems, post.Replies.TotalImgs, err = post.GetReplies()
@@ -1181,6 +1206,10 @@ func (actor Actor) ProcessInboxCreate(activity Activity) error {
return util.MakeError(errors.New("Object does not exist"), "ActorInbox")
}
+ if locked, _ := activity.Object.InReplyTo[0].IsLocked(); locked {
+ return util.MakeError(errors.New("Object locked"), "ActorInbox")
+ }
+
if wantToCache, err := activity.Object.WantToCache(actor); !wantToCache {
return util.MakeError(err, "ActorInbox")
}
@@ -1197,3 +1226,57 @@ func (actor Actor) ProcessInboxCreate(activity Activity) error {
return nil
}
+
+func (actor Actor) GetStickies() (Collection, error) {
+ var nColl Collection
+ var result []ObjectBase
+
+ query := `select count (x.id) over(), x.id, x.name, x.content, x.type, x.published, x.updated, x.attributedto, x.attachment, x.preview, x.actor, x.tripcode, x.sensitive from (select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode, sensitive from activitystream where actor=$1 and id in (select id from replies where inreplyto='') and type='Note' and id in (select activity_id from sticky where actor_id=$1) union select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode, sensitive from activitystream where actor in (select following from following where id=$1) and id in (select id from replies where inreplyto='') and type='Note' union select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode, sensitive from cacheactivitystream where actor in (select following from following where id=$1) and id in (select id from replies where inreplyto='') and type='Note' and id in (select activity_id from sticky where actor_id=$1)) as x order by x.updated desc limit 15`
+
+ rows, err := config.DB.Query(query, actor.Id)
+
+ if err != nil {
+ return nColl, util.MakeError(err, "GetStickies")
+ }
+
+ var count int
+ defer rows.Close()
+ for rows.Next() {
+ var post ObjectBase
+ var actor Actor
+
+ var attch ObjectBase
+ post.Attachment = append(post.Attachment, attch)
+
+ var prev NestedObjectBase
+ post.Preview = &prev
+
+ err = rows.Scan(&count, &post.Id, &post.Name, &post.Content, &post.Type, &post.Published, &post.Updated, &post.AttributedTo, &post.Attachment[0].Id, &post.Preview.Id, &actor.Id, &post.TripCode, &post.Sensitive)
+
+ if err != nil {
+ return nColl, util.MakeError(err, "GetStickies")
+ }
+
+ post.Sticky = true
+ post.Locked, _ = post.IsLocked()
+ post.Actor = actor.Id
+
+ var postCnt int
+ var imgCnt int
+ post.Replies, postCnt, imgCnt, _ = post.GetRepliesLimit(5)
+
+ post.Replies.TotalItems = postCnt
+ post.Replies.TotalImgs = imgCnt
+
+ post.Attachment, _ = post.Attachment[0].GetAttachment()
+
+ post.Preview, _ = post.Preview.GetPreview()
+
+ result = append(result, post)
+ }
+
+ nColl.TotalItems = count
+ nColl.OrderedItems = result
+
+ return nColl, nil
+}
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
+}
diff --git a/activitypub/activityPubStruct.go b/activitypub/structs.go
index b8e3180..c42d175 100644
--- a/activitypub/activityPubStruct.go
+++ b/activitypub/structs.go
@@ -145,6 +145,8 @@ type ObjectBase struct {
Duration string `json:"duration,omitempty"`
Size int64 `json:"size,omitempty"`
Sensitive bool `json:"sensitive,omitempty"`
+ Sticky bool `json:"sticky,omitempty"`
+ Locked bool `json:"locked,omitempty"`
}
type CryptoCur struct {