# 预设回复应要求标题唯一

**URL:** https://meta.discourse.org/t/canned-replies-should-require-titles-to-be-unique/103996
**Category:** Feature
**Tags:** pr-welcome, canned-replies
**Created:** [2018年十二月11日 11:59 UTC](https://meta.discourse.org/t/canned-replies-should-require-titles-to-be-unique/103996 "2018-12-11T11:59:56Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Chaboi\_3000](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chaboi_3000/32/163015_2.png) [@Chaboi\_3000](https://meta.discourse.org/u/Chaboi_3000)
#### Post date: [2018年十二月11日 11:59 UTC](https://meta.discourse.org/t/canned-replies-should-require-titles-to-be-unique/103996/1 "2018-12-11T11:59:56Z")

</div>

It’s really confusing when you have two canned replies with the same title. Can anyone prevent this from happening?

 ![35%20PM](https://global.discourse-cdn.com/meta/original/3X/f/0/f07bbde8944c3ce10fecbf5feb9ac171dcfa969e.png)

---

<div class="post-metadata">

### Author: ![nsuchy](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nsuchy/32/166530_2.png) [@nsuchy](https://meta.discourse.org/u/nsuchy)
#### Post date: [2018年十二月11日 23:26 UTC](https://meta.discourse.org/t/canned-replies-should-require-titles-to-be-unique/103996/2 "2018-12-11T23:26:27Z")

</div>

I agree, that could be confusing. Working on a PR for this now 🙂

---

<div class="post-metadata">

### Author: ![Chaboi\_3000](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chaboi_3000/32/163015_2.png) [@Chaboi\_3000](https://meta.discourse.org/u/Chaboi_3000)
#### Post date: [2018年十二月12日 01:06 UTC](https://meta.discourse.org/t/canned-replies-should-require-titles-to-be-unique/103996/3 "2018-12-12T01:06:52Z")

</div>

Is it ok if the plugin returns an error message like: `Error: Duplicate title`?

---

<div class="post-metadata">

### Author: ![nsuchy](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nsuchy/32/166530_2.png) [@nsuchy](https://meta.discourse.org/u/nsuchy)
#### Post date: [2018年十二月12日 01:53 UTC](https://meta.discourse.org/t/canned-replies-should-require-titles-to-be-unique/103996/4 "2018-12-12T01:53:30Z")

</div>

My intent is for the API to return an error upon submitting the new reply and for the UI Components to parse the error and return a pretty error message.

Currently I was thinking of: “Um oh! This title has already been used for another canned reply on your account. Please choose a different title.”

---

<div class="post-metadata">

### Author: ![nsuchy](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nsuchy/32/166530_2.png) [@nsuchy](https://meta.discourse.org/u/nsuchy)
#### Post date: [2018年十二月12日 02:09 UTC](https://meta.discourse.org/t/canned-replies-should-require-titles-to-be-unique/103996/5 "2018-12-12T02:09:26Z")

</div>

Just an update on my progress:

1. It’s my understanding that I need to fetch the list of shared canned replies, loop over every reply, and throw an error to the user if the title matches any of the replies. This seems inefficient in my mind, but computers are very fast, and it’s unlikely a forum will have 1000s of canned replies so at worst this is an extra database query on add reply and edit reply.
2. I need to modify the UI to detect the API rejecting to insert the reply and to display my error in a pretty way.
3. Still allow adding normal canned replies.

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [2018年十二月12日 04:07 UTC](https://meta.discourse.org/t/canned-replies-should-require-titles-to-be-unique/103996/6 "2018-12-12T04:07:08Z")

</div>

> [@Good first issue?](https://meta.discourse.org/t/good-first-issue/104054/15):
>
> I need to fetch the list of shared canned replies, loop over every reply, and throw an error to the user if the title matches any of the replies.

That’s one way you could do it. But if you make a field UNIQUE any attempt to INSERT a duplicate value would cause a fail.

---

<div class="post-metadata">

### Author: ![nsuchy](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nsuchy/32/166530_2.png) [@nsuchy](https://meta.discourse.org/u/nsuchy)
#### Post date: [2018年十二月12日 04:53 UTC](https://meta.discourse.org/t/canned-replies-should-require-titles-to-be-unique/103996/7 "2018-12-12T04:53:39Z")

</div>

That’s true but what about customers who already have two entries with duplicate titles. The migration for them would fail.

---

<div class="post-metadata">

### Author: ![justin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/justin/32/157614_2.png) [@justin](https://meta.discourse.org/u/justin)
#### Post date: [2018年十二月12日 14:38 UTC](https://meta.discourse.org/t/canned-replies-should-require-titles-to-be-unique/103996/8 "2018-12-12T14:38:58Z")

</div>

An approach may be to use a Sidekiq job to do the checking and make any duplicates unique and send a PM notifying the site admin of the changes. Though I should note I haven’t done this when changing a field to unique, so I can’t say how well it’ll mesh.

---

<div class="post-metadata">

### Author: ![nsuchy](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nsuchy/32/166530_2.png) [@nsuchy](https://meta.discourse.org/u/nsuchy)
#### Post date: [2018年十二月12日 14:50 UTC](https://meta.discourse.org/t/canned-replies-should-require-titles-to-be-unique/103996/9 "2018-12-12T14:50:19Z")

</div>

We still have to address any sites who have two canned replies with the same title before setting a unique key.

---

<div class="post-metadata">

### Author: ![jtbayly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jtbayly/32/119510_2.png) [@jtbayly](https://meta.discourse.org/u/jtbayly)
#### Post date: [2018年十二月12日 15:22 UTC](https://meta.discourse.org/t/canned-replies-should-require-titles-to-be-unique/103996/10 "2018-12-12T15:22:41Z")

</div>

I think that’s what @justin was trying to suggest a fix for. On update, prior to making the field unique,

1. find non-unique titles.
2. Add a numeral or something
3. PM the admin the list of canned replies that had their titles modified.
4. Make the field unique.

---

<div class="post-metadata">

### Author: ![nsuchy](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nsuchy/32/166530_2.png) [@nsuchy](https://meta.discourse.org/u/nsuchy)
#### Post date: [2018年十二月12日 22:06 UTC](https://meta.discourse.org/t/canned-replies-should-require-titles-to-be-unique/103996/11 "2018-12-12T22:06:21Z")

</div>

An update on my progress:

The API now only adds the reply if the title isn’t already in use.  
[https://github.com/nsuchy/discourse-canned-replies/commit/6fe13c5b868e94d273c3abda1d6b3d006c0a7efd](https://github.com/nsuchy/discourse-canned-replies/commit/6fe13c5b868e94d273c3abda1d6b3d006c0a7efd)

I’m not exactly sure where to start with the UI and displaying an error. Currently (this can be easily changed), the API returns `{error: 'Title already in use!'}` but the UI doesn’t react to it and the status code is still 200. While debugging a few times I got status code 500s and the UI did react to that as expected how can I go about sending a specific status code then changing the error message? Any guidance on that would be appreciated.

@jtbayly I’ll look into the actual migration steps and implementation soon 🙂

---

<div class="post-metadata">

### Author: ![nsuchy](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nsuchy/32/166530_2.png) [@nsuchy](https://meta.discourse.org/u/nsuchy)
#### Post date: [2018年十二月13日 18:29 UTC](https://meta.discourse.org/t/canned-replies-should-require-titles-to-be-unique/103996/12 "2018-12-13T18:29:00Z")

</div>

@sam I had an idea, how do you feel about the new reply model checking if the title is taken already and disabling the button if it is? Similar to what Discourse registration does.

 ![26%20PM](https://global.discourse-cdn.com/meta/original/3X/8/b/8b9a743b27e94ae358abf8fbd492ae6bb8cf3c7b.png)

---

<div class="post-metadata">

### Author: ![nsuchy](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nsuchy/32/166530_2.png) [@nsuchy](https://meta.discourse.org/u/nsuchy)
#### Post date: [2018年十二月13日 18:52 UTC](https://meta.discourse.org/t/canned-replies-should-require-titles-to-be-unique/103996/13 "2018-12-13T18:52:51Z")

</div>

Another progress update 😃

 ![14%20PM](https://global.discourse-cdn.com/meta/original/3X/1/8/18e302c39e83d401b61f03dfbc967c939140c557.png)  
The controller returns a proper error message now. ([https://github.com/nsuchy/discourse-canned-replies/commit/3da1023aa7a9577b6ec6dd9b2b4e4e8b0b6154be](https://github.com/nsuchy/discourse-canned-replies/commit/3da1023aa7a9577b6ec6dd9b2b4e4e8b0b6154be))

Can someone review my pull request at [Fix "Canned replies should require titles to be unique" by elaine-jackson · Pull Request #45 · discourse/discourse-canned-replies · GitHub](https://github.com/discourse/discourse-canned-replies/pull/45)? Thanks 🙂

**On setting unique keys**  
There was some discussion on setting a unique key. Everything is currently stored in a single row in the database which is updated. Since most forums will only use a handful of canned replies this does not introduce a huge performance penalty yet. However this does prevent us from setting a unique key in the database. Are plugins “allowed” to create their own tables in the database? This would require a major change to the plugin if we want to utilize unique keys which are enforced by the database server and would be another discussion on if/when/how that should be done.

Screenshot of query:

 ![33%20PM](https://global.discourse-cdn.com/meta/original/3X/a/e/ae36a5ec2dad036cdb6791a90d2a58f7db1b7879.png)
