DocsPlugins

Feeds

Feeds of posts with replies, likes, follows, tags, search and pictures, each feed with its own rules for who reads and who writes.

The social plugin gives your app feeds, like a school’s news or a team’s channel. Every post is in a feed, and the feed’s rules decide who may read it, post in it, reply, like and pin. To add it:

sluurp plugin add social

Features include:

  • Posts, replies in threads, likes and pinned posts
  • Following people, and a feed of the people you follow
  • Tags, a search by words and a search by meaning
  • Pictures and video uploads, and a preview of a post’s first link
  • New posts arriving live, and a count of what is unread

Set up a feed

Open Feeds in the admin and choose New feed. Give it a name, a short description and an icon, then answer five questions: who may read it, post in it, reply, like, and pin posts to its top.

Each answer is one of three:

  • Anybody signed in, the default.
  • Anybody at all, signed in or not: for news anyone may read.
  • A rule, written as a collection’s rules are. For example, only the staff:
@request.auth.staff = true

The admin is where feeds are set up. Reading and moderating posts happens in your app.

Show a feed

Import the plugin once, in your app’s code. Then sluurp.social has everything a feed page needs.

app.ts
import "plugin:social";

const { items: feeds } = await sluurp.social.feeds();
const page = await sluurp.social.feed({ in: "school", sort: "latest" });

feeds() lists the feeds the person may read, each with what they may do in it. feed() returns a page of posts: from one feed with in, by one author, with one tag, matching words with q, or only from people they follow with only: "following". The first page of a feed also has its pinned posts. To get the next page, pass back next.

A post names its author by id: your app shows their name.

Write, reply and like

app.ts
const post = await sluurp.social.post({ body: "Museum trip on Friday #trips", feed: "school" });
await sluurp.social.post({ body: "Can we bring snacks?", replyTo: post.id });
await sluurp.social.like(post.id);

Words starting with # become the post’s tags. To add pictures or videos, upload each file first with upload(file), then pass the ids as media; mediaUrl(id) gives the address to show one.

Live

app.ts
const stop = sluurp.social.live(["school"], (event, data, feed) => {
  if (event === "joined") return reload();
  // "post", "edited" and "deleted" come with the post; "liked" with { id, likes, by, on }
});

joined comes on the first connection and after every reconnection. Read the feed again then: whatever was posted while the page was offline was not sent to it.

Call stop() when the page closes. When someone opens a feed, call seen(feed), which sets its unread count back to nothing.

To search by meaning rather than by words, pass near to feed(): the posts closest in meaning come first, each with its _score.

View as Markdown