Skip to content

Commit

Permalink
enable chant edit by default
Browse files Browse the repository at this point in the history
  • Loading branch information
ichorid committed Dec 13, 2018
1 parent f347b64 commit 8794bdf
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 104 deletions.
6 changes: 0 additions & 6 deletions Tribler/Core/Config/tribler_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,6 @@ def get_chant_channels_dir(self):
path = self.config['chant']['channels_dir']
return path if os.path.isabs(path) else os.path.join(self.get_state_dir(), path)

def set_chant_channel_edit(self, value):
self.config['chant']['channel_edit'] = bool(value)

def get_chant_channel_edit(self):
return self.config['chant']['channel_edit']

# General
def set_family_filter_enabled(self, value):
self.config['general']['family_filter'] = bool(value)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
from __future__ import absolute_import

from pony.orm import db_session
from twisted.web import http

import Tribler.Core.Utilities.json_util as json
from Tribler.Core.Modules.restapi.channels.base_channels_endpoint import BaseChannelsEndpoint
from Tribler.Core.Modules.restapi.channels.channels_playlists_endpoint import ChannelsPlaylistsEndpoint
from Tribler.Core.Modules.restapi.channels.channels_rss_endpoint import ChannelsRssFeedsEndpoint, \
ChannelsRecheckFeedsEndpoint
from Tribler.Core.Modules.restapi.channels.channels_torrents_endpoint import ChannelsTorrentsEndpoint
from Tribler.Core.Modules.restapi.util import convert_db_channel_to_json, convert_channel_metadata_to_tuple
from Tribler.Core.exceptions import DuplicateChannelNameError
import Tribler.Core.Utilities.json_util as json


class ChannelsDiscoveredEndpoint(BaseChannelsEndpoint):
"""
This class is responsible for requests regarding the discovered channels.
"""

def getChild(self, path, request):
return ChannelsDiscoveredSpecificEndpoint(self.session, path)

Expand Down Expand Up @@ -51,35 +52,21 @@ def render_PUT(self, request):
if 'description' not in parameters or len(parameters['description']) == 0:
description = u''
else:
description = unicode(parameters['description'][0], 'utf-8')

if self.session.config.get_chant_channel_edit():
my_key = self.session.trustchain_keypair
my_channel_id = my_key.pub().key_to_bin()

# Do not allow to add a channel twice
if self.session.lm.mds.get_my_channel():
request.setResponseCode(http.INTERNAL_SERVER_ERROR)
return json.dumps({"error": "channel already exists"})

title = unicode(parameters['name'][0], 'utf-8')
self.session.lm.mds.ChannelMetadata.create_channel(title, description)
return json.dumps({
"added": str(my_channel_id).encode("hex"),
})

if 'mode' not in parameters or len(parameters['mode']) == 0:
# By default, the mode of the new channel is closed.
mode = u'closed'
else:
mode = unicode(parameters['mode'][0], 'utf-8')
description = str(parameters['description'][0]).encode('utf-8')

my_key = self.session.trustchain_keypair
my_channel_id = my_key.pub().key_to_bin()

try:
channel_id = self.session.create_channel(unicode(parameters['name'][0], 'utf-8'), description, mode)
except DuplicateChannelNameError as ex:
return BaseChannelsEndpoint.return_500(self, request, ex)
# Do not allow to add a channel twice
if self.session.lm.mds.get_my_channel():
request.setResponseCode(http.INTERNAL_SERVER_ERROR)
return json.dumps({"error": "channel already exists"})

return json.dumps({"added": channel_id})
title = str(parameters['name'][0]).encode('utf-8')
self.session.lm.mds.ChannelMetadata.create_channel(title, description)
return json.dumps({
"added": str(my_channel_id).encode("hex"),
})


class ChannelsDiscoveredSpecificEndpoint(BaseChannelsEndpoint):
Expand Down
96 changes: 31 additions & 65 deletions Tribler/Core/Modules/restapi/channels/my_channel_endpoint.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from binascii import hexlify

from pony.orm import db_session
from twisted.web import http
Expand Down Expand Up @@ -42,33 +43,20 @@ def render_GET(self, request):
:statuscode 404: if your channel has not been created (yet).
"""
if self.session.config.get_chant_channel_edit():
my_channel_id = self.session.trustchain_keypair.pub().key_to_bin()
with db_session:
my_channel = self.session.lm.mds.ChannelMetadata.get_channel_with_id(my_channel_id)

if not my_channel:
request.setResponseCode(http.NOT_FOUND)
return json.dumps({"error": NO_CHANNEL_CREATED_RESPONSE_MSG})

my_channel = my_channel.to_dict()
return json.dumps({
'mychannel': {
'identifier': str(my_channel["public_key"]).encode('hex'),
'name': my_channel["title"],
'description': my_channel["tags"],
'chant': True
}})
else:
my_channel_id = self.channel_db_handler.getMyChannelId()
if my_channel_id is None:
my_channel_id = self.session.trustchain_keypair.pub().key_to_bin()
with db_session:
my_channel = self.session.lm.mds.ChannelMetadata.get_channel_with_id(my_channel_id)

if not my_channel:
request.setResponseCode(http.NOT_FOUND)
return json.dumps({"error": NO_CHANNEL_CREATED_RESPONSE_MSG})

my_channel = self.channel_db_handler.getChannel(my_channel_id)

return json.dumps({'mychannel': {'identifier': my_channel[1].encode('hex'), 'name': my_channel[2],
'description': my_channel[3]}})
my_channel = my_channel.to_dict()
return json.dumps({
'mychannel': {
'identifier': hexlify(str(my_channel["public_key"])),
'name': my_channel["title"],
'description': my_channel["tags"]}})

def render_POST(self, request):
"""
Expand Down Expand Up @@ -100,50 +88,28 @@ def render_POST(self, request):
request.setResponseCode(http.BAD_REQUEST)
return json.dumps({"error": 'channel name cannot be empty'})

if self.session.config.get_chant_channel_edit():
with db_session:
modified = False
my_key = self.session.trustchain_keypair
my_channel_id = my_key.pub().key_to_bin()
my_channel = self.session.lm.mds.ChannelMetadata.get_channel_with_id(my_channel_id)

if not my_channel:
request.setResponseCode(http.NOT_FOUND)
return json.dumps({"error": NO_CHANNEL_CREATED_RESPONSE_MSG})

if get_parameter(parameters, 'name'):
my_channel.update_metadata(update_dict={
"tags": unicode(get_parameter(parameters, 'description'), 'utf-8'),
"title": unicode(get_parameter(parameters, 'name'), 'utf-8')
})
modified = True

if get_parameter(parameters, 'commit_changes') and my_channel.staged_entries_list:
# Update torrent if we have uncommitted content in the channel
my_channel.commit_channel_torrent()
torrent_path = os.path.join(self.session.lm.mds.channels_dir, my_channel.dir_name + ".torrent")
self.session.lm.updated_my_channel(torrent_path)
modified = True
with db_session:
modified = False
my_key = self.session.trustchain_keypair
my_channel_id = my_key.pub().key_to_bin()
my_channel = self.session.lm.mds.ChannelMetadata.get_channel_with_id(my_channel_id)

return json.dumps({'modified': modified})
else:
my_channel_id = self.channel_db_handler.getMyChannelId()
if my_channel_id is None:
if not my_channel:
request.setResponseCode(http.NOT_FOUND)
return json.dumps({"error": NO_CHANNEL_CREATED_RESPONSE_MSG})

channel_community = self.get_community_for_channel_id(my_channel_id)
if channel_community is None:
return BaseChannelsEndpoint.return_404(request,
message="the community for the your channel cannot be found")
if get_parameter(parameters, 'name'):
my_channel.update_metadata(update_dict={
"tags": str(get_parameter(parameters, 'description')).encode('utf-8'),
"title": str(get_parameter(parameters, 'name')).encode('utf-8')
})
modified = True

my_channel = self.channel_db_handler.getChannel(my_channel_id)
changes = {}
if my_channel[2] != get_parameter(parameters, 'name'):
changes['name'] = unicode(get_parameter(parameters, 'name'), 'utf-8')
if my_channel[3] != get_parameter(parameters, 'description'):
changes['description'] = unicode(get_parameter(parameters, 'description'), 'utf-8')
if get_parameter(parameters, 'commit_changes') and my_channel.staged_entries_list:
# Update torrent if we have uncommitted content in the channel
my_channel.commit_channel_torrent()
torrent_path = os.path.join(self.session.lm.mds.channels_dir, my_channel.dir_name + ".torrent")
self.session.lm.updated_my_channel(torrent_path)
modified = True

channel_community.modifyChannel(changes)

return json.dumps({'modified': True})
return json.dumps({'modified': modified})
2 changes: 0 additions & 2 deletions Tribler/Test/Core/Config/test_tribler_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,6 @@ def test_get_set_chant_methods(self):
"""
self.tribler_config.set_chant_enabled(False)
self.assertFalse(self.tribler_config.get_chant_enabled())
self.tribler_config.set_chant_channel_edit(True)
self.assertTrue(self.tribler_config.get_chant_channel_edit())
self.tribler_config.set_chant_channels_dir('test')
self.assertEqual(self.tribler_config.get_chant_channels_dir(),
os.path.join(self.tribler_config.get_state_dir(), 'test'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ def setUpPreSession(self):
super(AbstractTestChantEndpoint, self).setUpPreSession()
self.config.set_libtorrent_enabled(True)
self.config.set_chant_enabled(True)
self.config.set_chant_channel_edit(True)

@db_session
def create_my_channel(self, name, description):
Expand Down

0 comments on commit 8794bdf

Please sign in to comment.