Bin
2025-12-17 dcf780a91c16b6be28635b6e2e0e702060ee19f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { FF_DEV_3034, isFF } from "../utils/feature-flags";
 
export class CommentsSdk {
  constructor(lsf, dm) {
    this.lsf = lsf;
    this.dm = dm;
    this.bindEventHandlers();
  }
 
  bindEventHandlers() {
    ["comments:create", "comments:update", "comments:delete", "comments:list"].forEach((evt) => this.lsf.off(evt));
 
    this.lsf.on("comments:create", this.createComment);
    this.lsf.on("comments:update", this.updateComment);
    this.lsf.on("comments:delete", this.deleteComment);
    this.lsf.on("comments:list", this.listComments);
  }
 
  createComment = async (comment) => {
    const body = {
      is_resolved: comment.is_resolved,
      text: comment.text,
      region_ref: comment.region_ref,
      classifications: comment.classifications,
    };
 
    if (comment.annotation) {
      body.annotation = comment.annotation;
    } else if (isFF(FF_DEV_3034) && comment.draft) {
      body.draft = comment.draft;
    }
    const { $meta: _, ...newComment } = await this.dm.apiCall("createComment", undefined, {
      body,
    });
 
    return newComment;
  };
 
  updateComment = async (comment) => {
    if (!comment.id || comment.id < 0) return; // Don't allow an update with an incorrect id
 
    const res = await this.dm.apiCall("updateComment", { id: comment.id }, { body: comment });
 
    return res;
  };
 
  listComments = async (params) => {
    const listParams = {
      ordering: params.ordering || "-id",
      expand_created_by: true,
    };
 
    if (params.annotation) {
      listParams.annotation = params.annotation;
    } else if (isFF(FF_DEV_3034) && params.draft) {
      listParams.draft = params.draft;
    } else {
      return [];
    }
 
    const res = await this.dm.apiCall("listComments", listParams);
 
    // Ensure request is went through and res is an array
    if (!res?.length) {
      return [];
    }
 
    const commentUsers = [];
    const comments = res.map((comment) => {
      commentUsers.push(comment.created_by);
      return { ...comment, created_by: comment.created_by.id };
    });
 
    if (commentUsers.length && this.lsf?.store?.enrichUsers) {
      try {
        this.lsf.store.enrichUsers(commentUsers);
      } catch (error) {
        console.warn("Failed to enrich comment users:", error.message);
        return [];
      }
    }
 
    return comments;
  };
 
  deleteComment = async (comment) => {
    if (!comment.id || comment.id < 0) return; // Don't allow an update with an incorrect id
 
    const res = await this.dm.apiCall("deleteComment", { id: comment.id }, { body: comment });
 
    return res;
  };
}