-
Notifications
You must be signed in to change notification settings - Fork 1
/
ai-topic-summary.js
57 lines (51 loc) · 1.63 KB
/
ai-topic-summary.js
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
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { inject as service } from "@ember/service";
export default class AiTopicSummaryComponent extends Component {
@service siteSettings;
@service currentUser;
@service router;
@tracked localDownVotes;
@tracked downVotes;
@tracked text;
@tracked topic_id;
@tracked voted;
constructor() {
super(...arguments);
this.topic_id = this.router.currentRoute.parent.params.id;
const topicAiSummaryDataPath = `/ai_topic_summary/ai_summary/${this.topic_id}.json`;
ajax(topicAiSummaryDataPath).then((response) => {
if (response.ai_summary !== null) {
this.text = response.ai_summary.text;
this.downVotes = response.ai_summary.downvoted;
}
});
this.localDownVotes = typeof(this.downVotes) !== 'undefined'? this.downVotes.length || 0 : 0;
if (this.currentUser) {
this.voted = typeof(this.downVotes) !== 'undefined'? this.downVotes.includes(this.currentUser.id) : false;
} else {
this.voted = true;
}
}
get show() {
return this.siteSettings.ai_topic_summary_enabled && this.text && this.currentUser
}
@action
downVote() {
this.localDownVotes++;
this.voted = true;
ajax("/ai_topic_summary/downvote", {
type: "POST",
data: {
username: this.currentUser.username,
topic_id: this.topic_id,
},
returnXHR: true,
}).catch(function (error) {
popupAjaxError(error);
});
}
}