@shaxpir/duiduidui-models 1.35.1 → 1.36.0

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.
@@ -11,6 +11,7 @@ export declare enum ContentKind {
11
11
  TERM = "term",
12
12
  USER = "user",
13
13
  IMAGE = "image",
14
+ USAGE = "usage",
14
15
  WORKSPACE = "workspace",
15
16
  MANIFEST = "manifest"
16
17
  }
@@ -17,6 +17,7 @@ var ContentKind;
17
17
  ContentKind["TERM"] = "term";
18
18
  ContentKind["USER"] = "user";
19
19
  ContentKind["IMAGE"] = "image";
20
+ ContentKind["USAGE"] = "usage";
20
21
  ContentKind["WORKSPACE"] = "workspace";
21
22
  // These are used in the ShareDB system, but for internal bookkeeping, not for Content subclasses.
22
23
  ContentKind["MANIFEST"] = "manifest";
@@ -31,6 +31,7 @@ export interface ManifestPayload {
31
31
  session: any;
32
32
  social: any;
33
33
  term: any;
34
+ usage: any;
34
35
  user: any;
35
36
  workspace: any;
36
37
  }
@@ -37,6 +37,7 @@ class Manifest extends Model_1.Model {
37
37
  session: {},
38
38
  social: {},
39
39
  term: {},
40
+ usage: {},
40
41
  user: {},
41
42
  workspace: {}
42
43
  }
@@ -292,13 +292,13 @@ class Model {
292
292
  return data.meta.owner === userId;
293
293
  }
294
294
  static doesUserHaveContentPermission(kind, body, userId, type) {
295
- // Users are allowed to read any of their own content, but they are not allowed to write their User, Billing, Social, or Chat objects.
295
+ // Users are allowed to read any of their own content, but they are not allowed to write their User, Billing, Social, Chat, or Usage objects.
296
296
  if (body.meta.owner === userId) {
297
297
  if (type === Permissions_1.PermissionType.READ) {
298
298
  return true;
299
299
  }
300
300
  else if (type === Permissions_1.PermissionType.WRITE) {
301
- return kind !== ContentKind_1.ContentKind.USER && kind !== ContentKind_1.ContentKind.BILLING && kind !== ContentKind_1.ContentKind.SOCIAL && kind !== ContentKind_1.ContentKind.CHAT;
301
+ return kind !== ContentKind_1.ContentKind.USER && kind !== ContentKind_1.ContentKind.BILLING && kind !== ContentKind_1.ContentKind.SOCIAL && kind !== ContentKind_1.ContentKind.CHAT && kind !== ContentKind_1.ContentKind.USAGE;
302
302
  }
303
303
  }
304
304
  // Shared content: participants can read but not write (server-controlled).
@@ -0,0 +1,14 @@
1
+ import { Doc } from '@shaxpir/sharedb/lib/client';
2
+ import { ShareSync } from '../repo';
3
+ import { ContentId } from "./Content";
4
+ import { Metric } from './Metric';
5
+ export declare enum UsageName {
6
+ AI_TOKENS = "ai_tokens"
7
+ }
8
+ export declare class Usage extends Metric {
9
+ static makeUsageId(userId: ContentId, usageName: UsageName): ContentId;
10
+ static createUsage(userId: ContentId, usageName: UsageName, entries?: {
11
+ [key: string]: number;
12
+ }): Usage;
13
+ constructor(doc: Doc, shouldAcquire: boolean, shareSync: ShareSync);
14
+ }
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Usage = exports.UsageName = void 0;
4
+ const shaxpir_common_1 = require("@shaxpir/shaxpir-common");
5
+ const repo_1 = require("../repo");
6
+ const ContentKind_1 = require("./ContentKind");
7
+ const Metric_1 = require("./Metric");
8
+ var UsageName;
9
+ (function (UsageName) {
10
+ UsageName["AI_TOKENS"] = "ai_tokens";
11
+ })(UsageName || (exports.UsageName = UsageName = {}));
12
+ class Usage extends Metric_1.Metric {
13
+ static makeUsageId(userId, usageName) {
14
+ return shaxpir_common_1.CachingHasher.makeMd5Base62Hash(userId + "-usage-" + usageName);
15
+ }
16
+ static createUsage(userId, usageName, entries = {}) {
17
+ const usageId = Usage.makeUsageId(userId, usageName);
18
+ const now = shaxpir_common_1.ClockService.getClock().now();
19
+ return repo_1.ShareSyncFactory.get().createContent({
20
+ meta: {
21
+ ref: usageId,
22
+ kind: ContentKind_1.ContentKind.USAGE,
23
+ id: usageId,
24
+ owner: userId,
25
+ created_at: now,
26
+ updated_at: now
27
+ },
28
+ payload: {
29
+ metric_name: usageName,
30
+ min_date: null,
31
+ max_date: null,
32
+ entries: entries
33
+ }
34
+ });
35
+ }
36
+ constructor(doc, shouldAcquire, shareSync) {
37
+ super(doc, shouldAcquire, shareSync);
38
+ }
39
+ }
40
+ exports.Usage = Usage;
@@ -31,5 +31,6 @@ export * from './SkillLevel';
31
31
  export * from './Social';
32
32
  export * from './Streaks';
33
33
  export * from './Term';
34
+ export * from './Usage';
34
35
  export * from './User';
35
36
  export * from './Workspace';
@@ -48,5 +48,6 @@ __exportStar(require("./SkillLevel"), exports);
48
48
  __exportStar(require("./Social"), exports);
49
49
  __exportStar(require("./Streaks"), exports);
50
50
  __exportStar(require("./Term"), exports);
51
+ __exportStar(require("./Usage"), exports);
51
52
  __exportStar(require("./User"), exports);
52
53
  __exportStar(require("./Workspace"), exports);
@@ -49,6 +49,7 @@ const ContentKind_1 = require("../models/ContentKind");
49
49
  const Device_1 = require("../models/Device");
50
50
  const Manifest_1 = require("../models/Manifest");
51
51
  const Session_1 = require("../models/Session");
52
+ const Usage_1 = require("../models/Usage");
52
53
  const User_1 = require("../models/User");
53
54
  // Register the ShareDB types
54
55
  const Json1 = __importStar(require("./PermissiveJson1"));
@@ -518,6 +519,9 @@ class ShareSync {
518
519
  else if (kind === ContentKind_1.ContentKind.TERM) {
519
520
  return new models_1.Term(doc, shouldAcquire, shareSync);
520
521
  }
522
+ else if (kind === ContentKind_1.ContentKind.USAGE) {
523
+ return new Usage_1.Usage(doc, shouldAcquire, shareSync);
524
+ }
521
525
  else if (kind === ContentKind_1.ContentKind.USER) {
522
526
  return new User_1.User(doc, shouldAcquire, shareSync);
523
527
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shaxpir/duiduidui-models",
3
- "version": "1.35.1",
3
+ "version": "1.36.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/shaxpir/duiduidui-models"