# Make members of a group watch a category by default

**URL:** <https://meta.discourse.org/t/make-members-of-a-group-watch-a-category-by-default/22127>\
**Category:** Feature\
**Tags:** completed\
**Created:** [2014年十一月13日 08:27 UTC](https://meta.discourse.org/t/make-members-of-a-group-watch-a-category-by-default/22127 "2014-11-13T08:27:01Z")\
**Posts on this page:** 20\
**Page:** 3

<div class="post-metadata">

**Author:** ![alehandrof](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alehandrof/32/119526_2.png) [@alehandrof](https://meta.discourse.org/u/alehandrof)\
**Post date:** [2016年六月29日 07:27 UTC](https://meta.discourse.org/t/make-members-of-a-group-watch-a-category-by-default/22127/43 "2016-06-29T07:27:34Z")

</div>

I revised the “Watch Category” plugin for my own site, incorporating code from both [Jared Needell](https://github.com/jaredneedell/discourse-watch-category-mcneel) and [Thomas Purchas](https://github.com/WarwickFilmSoc/discourse-watch-category).

You can find it here:  
[http://github.com/amical/discourse-watch-category](http://github.com/amical/discourse-watch-category)

This version allows you to automatically watch a category or sub-category for all the users in the system or all the users in a particular group.

Features that are still missing:

- Support for tags
- Admin UI. This looks tricky as I don’t see any UI that supports nested arrays, which you’d probably need.
- Users don’t automatically stop watching categories if they’re removed from a group. Should this be an (optional) feature? What happens if a user is removed from a group that prevents them from having access to a category that they’re still watching?

I don’t know Ruby and just cobbled together this for my purposes. I hope that more skilful people than myself can work on the above.

---

<div class="post-metadata">

**Author:** ![watchmanmonitor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/watchmanmonitor/32/430970_2.png) [@watchmanmonitor](https://meta.discourse.org/u/watchmanmonitor)\
**Post date:** [2016年七月1日 12:25 UTC](https://meta.discourse.org/t/make-members-of-a-group-watch-a-category-by-default/22127/44 "2016-07-01T12:25:05Z")

</div>

Wow, this is very exciting.. hope to try it out soon!

> [@alehandrof](#):
>
> Users don’t automatically stop watching categories if they’re removed from a group. Should this be an (optional) feature? What happens if a user is removed from a group that prevents them from having access to a category that they’re still watching?

In the official user defaults, I have every user watch all relevant categories by default. If they aren’t in a group which gives them permission to view - they simply don’t get the messages. No problem.

This way, if they are added, they automatically start getting the right messages.

I don’t think it’s important that you have users auto-stop watching categories if they are removed from a group. If they auto-start again when re-added to a group then it’s kinda 6-one-way, 1/2 dozen-the-other, however, the logic as to which groups to remove them from based on what membership could get really ichy if two groups each grant access to a given category, and a user is removed from just one group.

---

<div class="post-metadata">

**Author:** ![alehandrof](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alehandrof/32/119526_2.png) [@alehandrof](https://meta.discourse.org/u/alehandrof)\
**Post date:** [2016年七月1日 18:37 UTC](https://meta.discourse.org/t/make-members-of-a-group-watch-a-category-by-default/22127/45 "2016-07-01T18:37:48Z")

</div>

> [@watchmanmonitor](#):
>
> In the official user defaults, I have every user watch all relevant categories by default. If they aren’t in a group which gives them permission to view - they simply don’t get the messages. No problem.

Thank you for clarifying this! I’ve wanted to try it, but [`mailcatcher` doesn’t work](https://meta.discourse.org/t/mini-racer-installation-issues-on-ubuntu-vagrant-osx/44681/27) on my development server. And thus had no way of testing email notifications, except on production 🙀

> [@watchmanmonitor](#):
>
> however, the logic as to which groups to remove them from based on what membership could get really ichy if two groups each grant access to a given category, and a user is removed from just one group.

Ah, true. I hadn’t thought of this case. It could get really hairy. Like Sasquatch hairy. 🐒

I should note that I haven’t tried my plugin in production, as I wanted to figure out the the permission stuff first. I’ll try it over the weekend. People are less likely to be looking at the forum then – in case it explodes.

---

<div class="post-metadata">

**Author:** ![watchmanmonitor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/watchmanmonitor/32/430970_2.png) [@watchmanmonitor](https://meta.discourse.org/u/watchmanmonitor)\
**Post date:** [2016年七月2日 17:27 UTC](https://meta.discourse.org/t/make-members-of-a-group-watch-a-category-by-default/22127/46 "2016-07-02T17:27:17Z")

</div>

I think you’re 100% fine not attempting to auto-unwatch Groups. As for Production.. I’ll be in line to deploy this right after you :troll: (One of my 4 Discourse is good for testing.. only 15 users in “production”)

---

<div class="post-metadata">

**Author:** ![watchmanmonitor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/watchmanmonitor/32/430970_2.png) [@watchmanmonitor](https://meta.discourse.org/u/watchmanmonitor)\
**Post date:** [2016年七月3日 20:44 UTC](https://meta.discourse.org/t/make-members-of-a-group-watch-a-category-by-default/22127/47 "2016-07-03T20:44:22Z")

</div>

It’s worth noting that Watching a category doesn’t watch the _category_ by default

it only watches the topic which are created after a member joins.

> [@When changing category watch state, prompt to also change state of all topics in that category at the time of change](https://meta.discourse.org/t/when-changing-category-watch-state-prompt-to-also-change-state-of-all-topics-in-that-category-at-the-time-of-change/46767/5):
>
> Yes – otherwise, the change affects only new topics, while you still won’t be notified to replies in old topics.

---

<div class="post-metadata">

**Author:** ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)\
**Post date:** [2016年七月4日 01:25 UTC](https://meta.discourse.org/t/make-members-of-a-group-watch-a-category-by-default/22127/48 "2016-07-04T01:25:39Z")

</div>

> [@watchmanmonitor](#):
>
> it only watches the topic which are created after a member joins.

it only watches topics that are created after the member starts watching the category. Join date has nothing to do with it.

---

<div class="post-metadata">

**Author:** ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)\
**Post date:** [2016年七月4日 01:48 UTC](https://meta.discourse.org/t/make-members-of-a-group-watch-a-category-by-default/22127/49 "2016-07-04T01:48:28Z")

</div>

Yes but that is a bit of a technicality as the user _did not exist at all_ prior to that point. The account creation and watch happen “at the same time”.

---

<div class="post-metadata">

**Author:** ![watchmanmonitor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/watchmanmonitor/32/430970_2.png) [@watchmanmonitor](https://meta.discourse.org/u/watchmanmonitor)\
**Post date:** [2016年七月4日 02:07 UTC](https://meta.discourse.org/t/make-members-of-a-group-watch-a-category-by-default/22127/50 "2016-07-04T02:07:06Z")

</div>

Join date is relevant when I thought my user-pref to default watch key categories when they sign up.

~~As it is, there is no Watch a category - that entire phrase is a misconception\*~~

~~There is only Automatically watch new topics in this category. And this is not what I need or expect.~~

EDIT - This was addressed in

> [@Watching and tracking implementation for tags and categories](https://meta.discourse.org/t/watching-and-tracking-implementation-for-tags-and-categories/46987):
>
> I just revised the implementation of “tracking” and “watching” of tags and categories: Watching categories and tags has always been a rather limited feature. When you started watching a category you would only watch topics in that category from that time on. (with an edge case that allowed you to also watch recategorized old topics) This limitation was … hmm … limiting. Start watching #Bug and you would not be notified on a large amount of current #Bug discussion on historic topics. T…

* * *

\*It states this clear enough in user preferences.

> You will automatically watch all new topics in these categories. You will be notified of all new posts and topics, and a count of new posts will also appear next to the topic.

---

<div class="post-metadata">

**Author:** ![tobiaseigen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tobiaseigen/32/539204_2.png) [@tobiaseigen](https://meta.discourse.org/u/tobiaseigen)\
**Post date:** [2016年七月4日 08:20 UTC](https://meta.discourse.org/t/make-members-of-a-group-watch-a-category-by-default/22127/51 "2016-07-04T08:20:24Z")

</div>

I’m psyched to see this improved code for this plugin and will try it out next time I have to make adjustments to the groups that are to automatically watch categories. For now the old code is working fine and I don’t want to break it. 😉 But adding a new group and category was error prone so this is a vast improvement.

Pretty much agree with all of @watchmanmonitor’s input.

---

<div class="post-metadata">

**Author:** ![alehandrof](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alehandrof/32/119526_2.png) [@alehandrof](https://meta.discourse.org/u/alehandrof)\
**Post date:** [2016年七月4日 08:35 UTC](https://meta.discourse.org/t/make-members-of-a-group-watch-a-category-by-default/22127/52 "2016-07-04T08:35:57Z")

</div>

AFAICT, it works fine. It’s mostly just a cleaner way to do the same ol’ stuff.

But, as @watchmanmonitor pointed out on GitHub, you should fork the repository in order to customize it. This will prevent conflicts since the customization happens right in the code.

BTW, @Jared_Needell had an interesting bit about `user.staged` that I discarded, as I wasn’t sure what it was doing. My guess is that it ignored staged users?

I also agree with all of @watchmanmonitor’s input, but I’ll reply in @fefrei’s thread.

---

<div class="post-metadata">

**Author:** ![alehandrof](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alehandrof/32/119526_2.png) [@alehandrof](https://meta.discourse.org/u/alehandrof)\
**Post date:** [2016年七月15日 08:16 UTC](https://meta.discourse.org/t/make-members-of-a-group-watch-a-category-by-default/22127/53 "2016-07-15T08:16:07Z")

</div>

I’m wondering if any of the mailing list crowd have played around with the updated group PMs.

I’m thinking of using a category for inter-group communication and using the group PM to allow non-group users to contact them. (I thought of doing it the other way around, but it wouldn’t make sense in my case. Non-group users might not want their messages publicly viewable.)

I don’t know if group PMs are covered by Sam’s recent changes to the watching feature: [Watching and tracking implementation for tags and categories](https://meta.discourse.org/t/watching-and-tracking-implementation-for-tags-and-categories/46987)

In any case, if they’re not now, I assume they would be eventually.

Can someone suggest how to programatically change the watch state for group PMs?

I’m looking for the group PM equivalent for this: [discourse-watch-category/plugin.rb at master · amical/discourse-watch-category · GitHub](https://github.com/amical/discourse-watch-category/blob/master/plugin.rb#L37-L38)

---

<div class="post-metadata">

**Author:** ![alehandrof](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alehandrof/32/119526_2.png) [@alehandrof](https://meta.discourse.org/u/alehandrof)\
**Post date:** [2016年九月4日 09:11 UTC](https://meta.discourse.org/t/make-members-of-a-group-watch-a-category-by-default/22127/54 "2016-09-04T09:11:32Z")

</div>

@techAPJ: Can you point me to some example of how to script users watching group PMs and tags?

---

<div class="post-metadata">

**Author:** ![rriemann](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rriemann/32/115238_2.png) [@rriemann](https://meta.discourse.org/u/rriemann)\
**Post date:** [2017年二月2日 09:27 UTC](https://meta.discourse.org/t/make-members-of-a-group-watch-a-category-by-default/22127/57 "2017-02-02T09:27:29Z")

</div>

@alehandrof, for the record:

```ruby
# based on https://github.com/discourse/discourse-watch-category-mcneel/blob/master/plugin.rb
groups = {
  "Group_1" => {},
  "Group_2" => {category: "News"},
  "Group_3" => {tag: "content"},
  "Group_4" => {tag: "content", category: "News"}
}

# levels: :watching :watching_first_post etc.
level = :watching

groups.each do |group_name, subscriptions|
  group = Group.find_by_name(group_name)
  next if group.nil? # skip if group not found
  puts "group: #{group.name}"
  
  # watch group inbox
  # found in discourse/spec/services/post_alerter_spec.rb
  # GroupUser.where(user_id: user.id, group_id: group.id).update_all(notification_level: level) # change for one user
  GroupUser.where(group_id: group.id).update_all(notification_level: GroupUser.notification_levels[level]) # change for whole group
  
  # watch tag
  tag_name = subscriptions[:tag]
  if tag_name
    tag = Tag.find_by_name(tag_name)
    unless tag.nil?
      puts "tag: #{tag.name}"
      # TagUser.where(tag_id: tag.id).update_all(notification_level: GroupUser.notification_levels[:level])
      group.users.each do |user|
        watched_tags = TagUser.lookup(user, level).pluck(:tag_id)
        TagUser.change(user.id, tag.id, TagUser.notification_levels[level]) unless watched_tags.include?(tag.id)
      end
    end
  end  
  
  # watch category
  category_slug = subscriptions[:category]
  if category_slug
    category = Category.find_by_slug(category_slug)
    unless category.nil?
      puts "category: #{category.name}"
      group.users.each do |user|
        watched_categories = CategoryUser.lookup(user, level).pluck(:category_id)
        CategoryUser.set_notification_level_for_category(user, CategoryUser.notification_levels[level], category.id) unless watched_categories.include?(category.id)
      end
    end    
  end
end

```

---

<div class="post-metadata">

**Author:** ![tophee](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tophee/32/73406_2.png) [@tophee](https://meta.discourse.org/u/tophee)\
**Post date:** [2017年三月24日 00:24 UTC](https://meta.discourse.org/t/make-members-of-a-group-watch-a-category-by-default/22127/58 "2017-03-24T00:24:07Z")

</div>

> [@alehandrof](#):
>
> [GitHub - amical/discourse-watch-category: Watches a category for all the users in a particular group · GitHub](http://github.com/amical/discourse-watch-category)

What will happen if I uninstall/deactivate this plugin? Will all watch settings stay as they are or will all group-members be “unsubscribed” from their category?

---

<div class="post-metadata">

**Author:** ![alehandrof](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alehandrof/32/119526_2.png) [@alehandrof](https://meta.discourse.org/u/alehandrof)\
**Post date:** [2017年三月24日 13:34 UTC](https://meta.discourse.org/t/make-members-of-a-group-watch-a-category-by-default/22127/59 "2017-03-24T13:34:09Z")

</div>

The settings will stay the same if you disable/uninstall the plugin.

This post explains how to change settings for multiple users if, for example, you want to have them stop watching a category (i.e., “unsubscribe”):

> [@(Obsolete) Set category tracking level defaults historically](https://meta.discourse.org/t/how-do-i-set-category-tracking-level-defaults-historically/53165/28):
>
> That sounds about right slight_smile Thanks to everyone here for helping me. For those playing at home, here’s a summary of the last 15 posts or so. How to make all users watch (or unwatch) a specific category through the rails console Set your parameters: # Levels → :tracking :watching :watching\_first\_post :muted level = :watching category\_slug = "announcements" And then, to make everyone watch (or whatever level you set) this category: User.all.each do |user| CategoryUser.batch\_set(user…

---

<div class="post-metadata">

**Author:** ![alehandrof](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alehandrof/32/119526_2.png) [@alehandrof](https://meta.discourse.org/u/alehandrof)\
**Post date:** [2017年三月24日 13:36 UTC](https://meta.discourse.org/t/make-members-of-a-group-watch-a-category-by-default/22127/60 "2017-03-24T13:36:37Z")

</div>

@rriemann Your code looks interesting, but I haven’t had a chance to play around with it. Sorry for taking so long to respond. Your code, if it does what it seems to, is much better than mine. I will happily fold it into my plugin, if you (or someone else) doesn’t want to take over this rickety plugin 🙂

---

<div class="post-metadata">

**Author:** ![rriemann](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rriemann/32/115238_2.png) [@rriemann](https://meta.discourse.org/u/rriemann)\
**Post date:** [2017年三月24日 14:46 UTC](https://meta.discourse.org/t/make-members-of-a-group-watch-a-category-by-default/22127/61 "2017-03-24T14:46:45Z")

</div>

I cannot work on this at the moment. Plugin? I think there should be some setting in vanilla Discourse.

---

<div class="post-metadata">

**Author:** ![alehandrof](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alehandrof/32/119526_2.png) [@alehandrof](https://meta.discourse.org/u/alehandrof)\
**Post date:** [2017年三月24日 15:45 UTC](https://meta.discourse.org/t/make-members-of-a-group-watch-a-category-by-default/22127/62 "2017-03-24T15:45:06Z")

</div>

Agreed 🙂 But in the meantime, I’d like to have a UI to configure this behavior, and I’m tight on time (and with limited skills). I won’t be able to dig in to see how plugin config UIs work until end of May probably, but I might have time then.

---

<div class="post-metadata">

**Author:** ![tophee](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tophee/32/73406_2.png) [@tophee](https://meta.discourse.org/u/tophee)\
**Post date:** [2017年四月9日 11:14 UTC](https://meta.discourse.org/t/make-members-of-a-group-watch-a-category-by-default/22127/63 "2017-04-09T11:14:07Z")

</div>

Is your plugin still working as intended? I forked it yesterday and adopted it but after 12 hours the people it the respective groups are still not watching the respective categories…

I changed ` :watching` to `:watching_first_post`, maybe there is a problem with that?

No errors logged.

* * *

Update: Stupid me: I used the category names instead if the slugs. It should work now, I guess (any way of knowing at what times the script is executed?) Edit: yes there is: check the WatchCategory job at `http://<your-site>/sidekiq/scheduler`

---

<div class="post-metadata">

**Author:** ![tobiaseigen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tobiaseigen/32/539204_2.png) [@tobiaseigen](https://meta.discourse.org/u/tobiaseigen)\
**Post date:** [2017年四月10日 15:18 UTC](https://meta.discourse.org/t/make-members-of-a-group-watch-a-category-by-default/22127/64 "2017-04-10T15:18:27Z")

</div>

You can always go to siteurl/sidekiq/scheduler and run it yourself.

[上一頁](https://meta.discourse.org/t/make-members-of-a-group-watch-a-category-by-default/22127.md?page=2)

[下一頁](https://meta.discourse.org/t/make-members-of-a-group-watch-a-category-by-default/22127.md?page=4)
