allow users to select follow settings

This commit is contained in:
Connor Turland 2017-03-08 18:50:39 +00:00
parent e3b4dac1e1
commit 8483b62603
14 changed files with 118 additions and 17 deletions

View file

@ -23,11 +23,12 @@ class UsersController < ApplicationController
if user_params[:password] == '' && user_params[:password_confirmation] == ''
# not trying to change the password
if @user.update_attributes(user_params.except(:password, :password_confirmation))
update_follow_settings(@user, params[:settings])
@user.image = nil if params[:remove_image] == '1'
@user.save
sign_in(@user, bypass: true)
respond_to do |format|
format.html { redirect_to root_url, notice: 'Account updated!' }
format.html { redirect_to root_url, notice: 'Settings updated' }
end
else
sign_in(@user, bypass: true)
@ -40,11 +41,12 @@ class UsersController < ApplicationController
correct_pass = @user.valid_password?(params[:current_password])
if correct_pass && @user.update_attributes(user_params)
update_follow_settings(@user, params[:settings])
@user.image = nil if params[:remove_image] == '1'
@user.save
sign_in(@user, bypass: true)
respond_to do |format|
format.html { redirect_to root_url, notice: 'Account updated!' }
format.html { redirect_to root_url, notice: 'Settings updated' }
end
else
respond_to do |format|
@ -104,9 +106,16 @@ class UsersController < ApplicationController
private
def update_follow_settings(user, settings)
user.settings.follow_topic_on_created = settings[:follow_topic_on_created]
user.settings.follow_topic_on_contributed = settings[:follow_topic_on_contributed]
user.settings.follow_map_on_created = settings[:follow_map_on_created]
user.settings.follow_map_on_contributed = settings[:follow_map_on_contributed]
end
def user_params
params.require(:user).permit(
:name, :email, :image, :password, :password_confirmation, :emails_allowed
:name, :email, :image, :password, :password_confirmation, :emails_allowed, :settings
)
end
end

View file

@ -39,7 +39,7 @@ class Map < ApplicationRecord
# Validate the attached image is image/jpg, image/png, etc
validates_attachment_content_type :screenshot, content_type: %r{\Aimage/.*\Z}
after_create :after_created_async
after_create :after_created
after_update :after_updated
after_save :update_deferring_topics_and_synapses, if: :permission_changed?
@ -140,11 +140,10 @@ class Map < ApplicationRecord
protected
def after_created_async
def after_created
FollowService.follow(self, self.user, 'created')
# notify users following the map creator
end
handle_asynchronously :after_created_async
def after_updated
return unless ATTRS_TO_WATCH.any? { |k| changed_attributes.key?(k) }

View file

@ -62,6 +62,12 @@ class User < ApplicationRecord
maps: following.where(followed_type: 'Map').to_a.map(&:followed_id)
}
end
if (_options[:follow_settings])
json['follow_topic_on_created'] = settings.follow_topic_on_created == "1"
json['follow_topic_on_contributed'] = settings.follow_topic_on_contributed == "1"
json['follow_map_on_created'] = settings.follow_map_on_created == "1"
json['follow_map_on_contributed'] = settings.follow_map_on_contributed == "1"
end
if (_options[:email])
json['email'] = email
end
@ -127,8 +133,10 @@ class User < ApplicationRecord
end
def settings
# make sure we always return a UserPreference instance
self[:settings] = UserPreference.new if self[:settings].nil?
if not self[:settings].respond_to?(:follow_topic_on_created)
self[:settings].initialize_follow_settings
end
self[:settings]
end

View file

@ -1,6 +1,7 @@
# frozen_string_literal: true
class UserPreference
attr_accessor :metacodes, :metacode_focus
attr_accessor :metacodes, :metacode_focus, :follow_topic_on_created, :follow_topic_on_contributed,
:follow_map_on_created, :follow_map_on_contributed
def initialize
array = []
@ -16,5 +17,13 @@ class UserPreference
end
@metacodes = array
@metacode_focus = array[0]
initialize_follow_settings
end
def initialize_follow_settings
@follow_topic_on_created = false
@follow_topic_on_contributed = false
@follow_map_on_created = false
@follow_map_on_contributed = false
end
end

View file

@ -5,6 +5,8 @@ class FollowService
return unless is_tester(user)
return unless should_auto_follow(entity, user, reason)
follow = Follow.where(followed: entity, user: user).first_or_create
if FollowReason::REASONS.include?(reason) && !follow.follow_reason.read_attribute(reason)
follow.follow_reason.update_attribute(reason, true)
@ -28,6 +30,22 @@ class FollowService
protected
def should_auto_follow(entity, user, reason)
if entity.class == Topic
if reason == 'created'
return user.settings.follow_topic_on_created == '1'
elsif reason == 'contributed'
return user.settings.follow_topic_on_contributed == '1'
end
elsif entity.class == Map
if reason == 'created'
return user.settings.follow_map_on_created == '1'
elsif reason == 'contributed'
return user.settings.follow_map_contributed == '1'
end
end
end
def is_tester(user)
%w(connorturland@gmail.com devin@callysto.com chessscholar@gmail.com solaureum@gmail.com ishanshapiro@gmail.com).include?(user.email)
end

View file

@ -10,7 +10,7 @@
<ul>
<li class="accountListItem accountSettings">
<div class="accountIcon"></div>
<%= link_to "Account", edit_user_url(account) %>
<%= link_to "Settings", edit_user_url(account) %>
</li>
<% if account.admin %>
<li class="accountListItem accountAdmin">

View file

@ -3,7 +3,7 @@
<%= render :partial => 'shared/metacodeBgColors' %>
<script type="text/javascript" charset="utf-8">
<% if current_user %>
Metamaps.ServerData.ActiveMapper = <%= current_user.to_json({follows: true, email: true}).html_safe %>
Metamaps.ServerData.ActiveMapper = <%= current_user.to_json({follows: true, email: true, follow_settings: true}).html_safe %>
<% else %>
Metamaps.ServerData.ActiveMapper = null
<% end %>

View file

@ -8,7 +8,7 @@
<% content_for :mobile_title, "Account Settings" %>
<div id="yield">
<%= form_for @user, url: user_url, :html =>{ :multipart => true, :class => "edit_user centerGreyForm"} do |form| %>
<h3>Edit Account</h3>
<h3>Edit Settings</h3>
<div class="userImage">
<div class="userImageDiv" onclick="Metamaps.Account.toggleChangePicture()">
<%= image_tag @user.image.url(:ninetysix), :size => "84x84" %>
@ -45,6 +45,24 @@
<%= form.check_box :emails_allowed, class: 'inline' %>
Send Metamaps notifications to my email.
<% end %>
<%= fields_for :settings, @user.settings do |settings| %>
<%= settings.label :follow_topic_on_created, class: 'firstFieldText' do %>
<%= settings.check_box :follow_topic_on_created, class: 'inline' %>
Auto-follow topics you create.
<% end %>
<%= settings.label :follow_topic_on_contributed, class: 'firstFieldText' do %>
<%= settings.check_box :follow_topic_on_contributed, class: 'inline' %>
Auto-follow topics you edit.
<% end %>
<%= settings.label :follow_map_on_created, class: 'firstFieldText' do %>
<%= settings.check_box :follow_map_on_created, class: 'inline' %>
Auto-follow maps you create.
<% end %>
<%= settings.label :follow_map_on_contributed, class: 'firstFieldText' do %>
<%= settings.check_box :follow_map_on_contributed, class: 'inline' %>
Auto-follow maps you edit.
<% end %>
<% end %>
</div>
<div class="changePass" onclick="Metamaps.Account.showPass()">Change Password</div>
<div class="toHide">

View file

@ -123,10 +123,14 @@ const Control = {
const authorized = Active.Map.authorizeToEdit(Active.Mapper)
if (!authorized) {
GlobalUI.notifyUser('Cannot edit Public map.')
GlobalUI.notifyUser('Cannot edit this map.')
return
}
if (Active.Mapper.get('follow_map_on_contributed')) {
Active.Mapper.followMap(Active.Map.id)
}
for (let i = l - 1; i >= 0; i -= 1) {
const node = Selected.Nodes[i]
Control.removeNode(node.id)
@ -139,10 +143,14 @@ const Control = {
var node = Visualize.mGraph.graph.getNode(nodeid)
if (!authorized) {
GlobalUI.notifyUser('Cannot edit Public map.')
GlobalUI.notifyUser('Cannot edit this map.')
return
}
if (Active.Mapper.get('follow_map_on_contributed')) {
Active.Mapper.followMap(Active.Map.id)
}
var topic = node.getData('topic')
var mapping = node.getData('mapping')
mapping.destroy()
@ -284,10 +292,14 @@ const Control = {
var authorized = Active.Map.authorizeToEdit(Active.Mapper)
if (!authorized) {
GlobalUI.notifyUser('Cannot edit Public map.')
GlobalUI.notifyUser('Cannot edit this map.')
return
}
if (Active.Mapper.get('follow_map_on_contributed')) {
Active.Mapper.followMap(Active.Map.id)
}
for (let i = l - 1; i >= 0; i -= 1) {
const edge = Selected.Edges[i]
Control.removeEdge(edge)
@ -300,10 +312,14 @@ const Control = {
var authorized = Active.Map.authorizeToEdit(Active.Mapper)
if (!authorized) {
GlobalUI.notifyUser('Cannot edit Public map.')
GlobalUI.notifyUser('Cannot edit this map.')
return
}
if (Active.Mapper.get('follow_map_on_contributed')) {
Active.Mapper.followMap(Active.Map.id)
}
if (edge.getData('mappings').length - 1 === 0) {
Control.hideEdge(edge)
}

View file

@ -17,14 +17,16 @@ const Mapper = Backbone.Model.extend({
</li>`
},
followMap: function(id) {
this.get('follows').maps.push(id)
const idIndex = this.get('follows').maps.indexOf(id)
if (idIndex < 0) this.get('follows').maps.push(id)
},
unfollowMap: function(id) {
const idIndex = this.get('follows').maps.indexOf(id)
if (idIndex > -1) this.get('follows').maps.splice(idIndex, 1)
},
followTopic: function(id) {
this.get('follows').topics.push(id)
const idIndex = this.get('follows').topics.indexOf(id)
if (idIndex < 0) this.get('follows').topics.push(id)
},
unfollowTopic: function(id) {
const idIndex = this.get('follows').topics.indexOf(id)

View file

@ -99,6 +99,9 @@ const CreateMap = {
success: function(model) {
// push the new map onto the collection of 'my maps'
DataModel.Maps.Mine.add(model)
if (Active.Mapper.get('follow_map_on_created')) {
Active.Mapper.followMap(model.id)
}
GlobalUI.clearNotify()
$('#wrapper').append(outdent`

View file

@ -977,6 +977,9 @@ const JIT = {
}
if (checkWhetherToSave()) {
if (Active.Mapper.get('follow_map_on_contributed')) {
Active.Mapper.followMap(Active.Map.id)
}
mapping = node.getData('mapping')
mapping.save({
xloc: node.getPos().x,

View file

@ -42,6 +42,11 @@ const Synapse = {
var synapseSuccessCallback = function(synapseModel, response) {
if (Active.Map) {
mapping.save({ mappable_id: synapseModel.id }, {
success: function(model, response) {
if (Active.Mapper.get('follow_map_on_contributed')) {
Active.Mapper.followMap(Active.Map.id)
}
},
error: function(model, response) {
console.log('error saving mapping to database')
}
@ -59,6 +64,11 @@ const Synapse = {
})
} else if (!synapse.isNew() && Active.Map) {
mapping.save(null, {
success: function(model, response) {
if (Active.Mapper.get('follow_map_on_contributed')) {
Active.Mapper.followMap(Active.Map.id)
}
},
error: function(model, response) {
console.log('error saving mapping to database')
}

View file

@ -242,12 +242,18 @@ const Topic = {
}
var mappingSuccessCallback = function(mappingModel, response, topicModel) {
if (Active.Mapper.get('follow_map_on_contributed')) {
Active.Mapper.followMap(Active.Map.id)
}
// call a success callback if provided
if (opts.success) {
opts.success(topicModel)
}
}
var topicSuccessCallback = function(topicModel, response) {
if (Active.Mapper.get('follow_topic_on_created')) {
Active.Mapper.followTopic(topicModel.id)
}
if (Active.Map) {
mapping.save({ mappable_id: topicModel.id }, {
success: function(model, response) {