@fsvreddit/fsv-devvit-web-helpers 0.2.2 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,28 +1,28 @@
1
- BSD 3-Clause License
2
-
3
- Copyright (c) 2024, fsv
4
-
5
- Redistribution and use in source and binary forms, with or without
6
- modification, are permitted provided that the following conditions are met:
7
-
8
- 1. Redistributions of source code must retain the above copyright notice, this
9
- list of conditions and the following disclaimer.
10
-
11
- 2. Redistributions in binary form must reproduce the above copyright notice,
12
- this list of conditions and the following disclaimer in the documentation
13
- and/or other materials provided with the distribution.
14
-
15
- 3. Neither the name of the copyright holder nor the names of its
16
- contributors may be used to endorse or promote products derived from
17
- this software without specific prior written permission.
18
-
19
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
- SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
- OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2024, fsv
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -1,2 +1,4 @@
1
1
  export * from "./crowdControl.js";
2
2
  export * from "./extendedDevvit.js";
3
+ export * from "./userExtended.js";
4
+ export * from "./userSocialLinks.js";
@@ -16,3 +16,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./crowdControl.js"), exports);
18
18
  __exportStar(require("./extendedDevvit.js"), exports);
19
+ __exportStar(require("./userExtended.js"), exports);
20
+ __exportStar(require("./userSocialLinks.js"), exports);
@@ -1,2 +1,10 @@
1
- import { User } from "@devvit/web/server";
1
+ import { ModeratorPermission, User } from "@devvit/web/server";
2
2
  export declare function getUserOrUndefined(username: string): Promise<User | undefined>;
3
+ export declare function isModerator(subredditName: string, username: string): Promise<boolean>;
4
+ export declare function isBanned(subredditName: string, username: string): Promise<boolean>;
5
+ export declare function isContributor(subredditName: string, username: string): Promise<boolean>;
6
+ export declare function hasPermissions(opts: {
7
+ subredditName: string;
8
+ username: string;
9
+ requiredPerms: ModeratorPermission[];
10
+ }): Promise<boolean>;
@@ -1,6 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getUserOrUndefined = getUserOrUndefined;
4
+ exports.isModerator = isModerator;
5
+ exports.isBanned = isBanned;
6
+ exports.isContributor = isContributor;
7
+ exports.hasPermissions = hasPermissions;
4
8
  const server_1 = require("@devvit/web/server");
5
9
  async function getUserOrUndefined(username) {
6
10
  let user;
@@ -12,3 +16,32 @@ async function getUserOrUndefined(username) {
12
16
  }
13
17
  return user;
14
18
  }
19
+ async function isModerator(subredditName, username) {
20
+ const modList = await server_1.reddit.getModerators({
21
+ subredditName,
22
+ username,
23
+ }).all();
24
+ return modList.length > 0;
25
+ }
26
+ async function isBanned(subredditName, username) {
27
+ const banList = await server_1.reddit.getBannedUsers({
28
+ subredditName,
29
+ username,
30
+ }).all();
31
+ return banList.length > 0;
32
+ }
33
+ async function isContributor(subredditName, username) {
34
+ const contributorList = await server_1.reddit.getApprovedUsers({
35
+ subredditName,
36
+ username,
37
+ }).all();
38
+ return contributorList.length > 0;
39
+ }
40
+ async function hasPermissions(opts) {
41
+ const user = await server_1.reddit.getUserByUsername(opts.username);
42
+ const permissions = await user?.getModPermissionsForSubreddit(opts.subredditName);
43
+ if (!permissions) {
44
+ return false;
45
+ }
46
+ return permissions.includes("all") || opts.requiredPerms.every(perm => permissions.includes(perm));
47
+ }
@@ -0,0 +1,10 @@
1
+ interface TriggerLockOptions {
2
+ expiration?: Date;
3
+ verboseLogs?: boolean;
4
+ }
5
+ export declare function hasEventBeenHandled(identifier: string, opts?: TriggerLockOptions): Promise<boolean>;
6
+ /**
7
+ * @deprecated Use `hasEventBeenHandled` instead.
8
+ */
9
+ export declare function hasTriggerBeenHandled(identifier: string, opts?: TriggerLockOptions): Promise<boolean>;
10
+ export {};
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.hasEventBeenHandled = hasEventBeenHandled;
4
+ exports.hasTriggerBeenHandled = hasTriggerBeenHandled;
5
+ const server_1 = require("@devvit/web/server");
6
+ const date_fns_1 = require("date-fns");
7
+ const redis_1 = require("../redis");
8
+ async function hasEventBeenHandled(identifier, opts) {
9
+ const redisKey = `triggerLock:${identifier}`;
10
+ const expiration = opts?.expiration ?? (0, date_fns_1.addDays)(new Date(), 1);
11
+ const newVal = await server_1.redis.incrBy(redisKey, 1);
12
+ await (0, redis_1.expireKeyAt)(redisKey, expiration);
13
+ return newVal > 1;
14
+ }
15
+ /**
16
+ * @deprecated Use `hasEventBeenHandled` instead.
17
+ */
18
+ async function hasTriggerBeenHandled(identifier, opts) {
19
+ return hasEventBeenHandled(identifier, opts);
20
+ }
@@ -1,2 +1,2 @@
1
- export * from "./duplicateTriggerMitigation.js";
1
+ export * from "./duplicateEventMitigation.js";
2
2
  export * from "./postsAndComments.js";
@@ -14,5 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./duplicateTriggerMitigation.js"), exports);
17
+ __exportStar(require("./duplicateEventMitigation.js"), exports);
18
18
  __exportStar(require("./postsAndComments.js"), exports);
package/eslint.config.mjs CHANGED
@@ -1,3 +1,3 @@
1
- import fsvconfig from "@fsvreddit/eslint-config";
2
-
3
- export default fsvconfig;
1
+ import fsvconfig from "@fsvreddit/eslint-config";
2
+
3
+ export default fsvconfig;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fsvreddit/fsv-devvit-web-helpers",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "Helper functions and utilities for Devvit Web apps on Reddit",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -1,15 +1,15 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "exclude": [
4
- "vitest.config.ts",
5
- "node_modules",
6
- "tests",
7
- "dist",
8
- "eslint.config.mjs"
9
- ],
10
- "compilerOptions": {
11
- "rootDir": "src",
12
- "outDir": "dist",
13
- "skipLibCheck": true
14
- }
15
- }
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "exclude": [
4
+ "vitest.config.ts",
5
+ "node_modules",
6
+ "tests",
7
+ "dist",
8
+ "eslint.config.mjs"
9
+ ],
10
+ "compilerOptions": {
11
+ "rootDir": "src",
12
+ "outDir": "dist",
13
+ "skipLibCheck": true
14
+ }
15
+ }