Giter VIP home page Giter VIP logo

toolbox-devvit's Introduction

toolbox-devvit npm version

Helpers for working with /r/toolbox data from Devvit community apps. Read the documentation.

Installation

npm install --production toolbox-devvit

Usage Example

import {Devvit} from '@devvit/public-api';
import {ToolboxClient} from 'toolbox-devvit';

Devvit.configure({
	redditAPI: true,
	// ...
});

// A simple action that creates a usernote on a post's author
Devvit.addMenuItem({
	location: 'post',
	label: 'Create Test Usernote',
	description: 'Creates a Toolbox usernote for testing',
	onPress: async (event, {reddit, ui, postId}) => {
		const subredditName = (await reddit.getCurrentSubreddit()).name;
		const username = (await reddit.getPostById(postId!)).authorName;
		const text = 'Hihi i am a note';
		const wikiRevisionReason = 'Create note via my custom app';

		const toolbox = new ToolboxClient(reddit);
		await toolbox.addUsernote(subredditName, {
			username,
			text,
		}, wikiRevisionReason);

		ui.showToast({
			appearance: 'success',
			text: 'Note added!',
		});
	},
});

export default Devvit;

License

MIT © the toolbox team

toolbox-devvit's People

Contributors

dependabot[bot] avatar eritbh avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

toolbox-devvit's Issues

Subreddit config: Mod macros

Similar to removal reasons (#40), I think this will have to include some API for programmatically executing specific mod macros in addition to just a way to access the stored data.

Subreddit config: Domain tags

Both reading and writing these should be pretty simple to implement. The existing old subConfig.ts code was meant to do stuff like this before; the types it contains can be pulled out, but the rest will probably have to be reorganized since I've restructured this project since that file was relevant.

Usernotes: Get all usernotes across all users

Rather than only being able to get lists of usernotes from users, we should add helpers to return every usernote, either in an array or in the same Map<string, Usernote[]> structure that's used internally.

Set up eslint/dprint

this time with a config that doesn't include my deadname

At this point it might make sense to port the existing toolbox eslint config to a module that we can import from multiple repos. That feels like a bigger thing though...

Docs site

just chuck typedoc output into a gh-pages branch probably

Usernotes: Modifying and deleting existing usernotes

There's no real way to change or delete a note in a Usernotes object right now*. without any form of unique identifier on usernotes; timestamps sort of work for that, but not really. We'll have to come up with an API to record changes to usernote objects and delete them.

* You can modify existing notes, technically, because Usernotes.get() returns references to the same note objects it holds, so any changes made there will be serialized out. But the array that's returned isn't the internal array, so deletion isn't possible - that's intentional until we consider the API for that more thoroughly.

Subreddit config: Usernote types

Usernote type tags are exposed as part of the Usernote interface, but there's currently no way to get the name or color of a note type from that. We'll have to be careful when implementing this to make sure the behavior of the default note labels matches Toolbox's.

Subreddit config: Removal reasons

This one is tricky. We can give access to the underlying removal reasons data pretty quickly, but I think for it to be of any use we'll also need to implement our token substitutions within the library and come up with an API for that.

Get builds working again

Current build script is not properly creating new builds/* branches for new code branches. Main branch name change means no development builds are being published and every commit gets a failed build badge.

`notesForUser(username: string): PrettyUsernote[] | null` returns empty array sometimes

As per title, notesForUser(username: string): PrettyUsernote[] | null occasionally returns an empty array for users that do have usernotes. I couldn't find a pattern regarding which searches fail and which succeed, but it appeared only to be for usernotes newer than ~6 months that the issue occurred.

Searching for the usernotes for a user manually rather than using this function does not present the issue, therefore the issue is likely with this library.

Code that fails sometimes:

	public getUsernotesByName(user: string) : Promise<PrettyUsernote[]>{
		return new Promise<PrettyUsernote[]>( (resolve, reject) => {
			void this.getUsernotesPage().then((wiki: Snoowrap.WikiPage) => {
				const usernotes = new toolbox.UsernotesData(wiki.content_md);
				resolve( usernotes.notesForUser(user) ?? [] )
			});
		})
	}

Code that works appropriately:

	public getUsernotesByName(user: string): Promise<PrettyUsernote[]> {
		return new Promise<PrettyUsernote[]>((resolve, reject) => {
			void this.getUsernotesPage().then((wiki: Snoowrap.WikiPage) => {
				const usernotes = new toolbox.UsernotesData(wiki.content_md);

				// Find username case_insensitive
				Object.keys(usernotes.users).forEach((key) => {
					if (key.toLowerCase() == user.toLowerCase()) {
						resolve(
							usernotes.users[key].ns.map<PrettyUsernote>(
								(note) => {
									return <PrettyUsernote>{
										text: note.n,
										timestamp: note.t
											? new Date(note.t * 1000)
											: undefined,
										link: note.l && expandPermalink(note.l),
									};
								}
							)
						);
					}
				});

				resolve([]);
			});
		});
	}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.