Skip to content

Commit

Permalink
Merge branch 'master' into c/react-template
Browse files Browse the repository at this point in the history
  • Loading branch information
CianLR authored Feb 18, 2019
2 parents ab75bf6 + 697b837 commit d1cfe3b
Show file tree
Hide file tree
Showing 20 changed files with 478 additions and 67 deletions.
40 changes: 31 additions & 9 deletions chump/src/components/account_edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import * as React from "react";

import { Redirect } from "react-router-dom";
import * as config from "../../rabble_config.json";
import {EditUserPromise, IEditUserResult} from "../models/edit_user";
import {EditUserPromise, GetUserInfo, IEditUserResult, IUserDetails} from "../models/edit_user";

interface IAccountEditState {
bio: string;
displayName: string;
currentPassword: string;
displayName: string;
newPassword: string;
postBodyCss: string;
postTitleCss: string;
privateAccount: boolean;
cancel: boolean;
redirect: boolean;
}

interface IAccountEditProps {
Expand All @@ -25,13 +25,13 @@ export class AccountEdit extends React.Component<IAccountEditProps, IAccountEdit

this.state = {
bio: "",
cancel: false,
currentPassword: "",
displayName: "",
newPassword: "",
postBodyCss: "",
postTitleCss: "",
privateAccount: false,
redirect: false,
};

this.handlePassword = this.handlePassword.bind(this);
Expand All @@ -43,10 +43,14 @@ export class AccountEdit extends React.Component<IAccountEditProps, IAccountEdit
this.handlePrivate = this.handlePrivate.bind(this);
this.handleUpdate = this.handleUpdate.bind(this);
this.handleCancel = this.handleCancel.bind(this);
this.handleUserInfo = this.handleUserInfo.bind(this);
this.handleGetError = this.handleGetError.bind(this);

GetUserInfo().then(this.handleUserInfo).catch(this.handleGetError);
}

public render() {
if (this.state.cancel) {
if (this.state.redirect) {
return (<Redirect to={{ pathname: "/@" + this.props.username }} />);
}

Expand Down Expand Up @@ -74,13 +78,27 @@ export class AccountEdit extends React.Component<IAccountEditProps, IAccountEdit
).then((response: IEditUserResult) => {
if (!response.success) {
alert("Error editing: " + response.error);
} else {
this.setState({ redirect: true });
}
})
.catch(this.handleUpdateError);
}

public handleUserInfo(details: IUserDetails) {
let isPrivate = false;
if ("value" in details.private) {
isPrivate = details.private.value!;
}

this.setState({
bio: details.bio,
displayName: details.display_name,
privateAccount: isPrivate,
});
}

private form() {
// TODO: It would be nice to fill in the current user details here
return (
<div>
<form
Expand Down Expand Up @@ -192,7 +210,7 @@ export class AccountEdit extends React.Component<IAccountEditProps, IAccountEdit

private handleCancel(event: React.FormEvent<HTMLButtonElement>) {
event.preventDefault();
this.setState({ cancel: true });
this.setState({ redirect: true });
}

private handleNewPassword(event: React.ChangeEvent<HTMLInputElement>) {
Expand Down Expand Up @@ -245,7 +263,11 @@ export class AccountEdit extends React.Component<IAccountEditProps, IAccountEdit
});
}

private handleUpdateError() {
alert("Error attempting to update.");
private handleUpdateError(e: any) {
alert("Error attempting to update: " + e.toString());
}

private handleGetError(e: any) {
alert("Error getting user details: " + e.toString());
}
}
39 changes: 38 additions & 1 deletion chump/src/models/edit_user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,41 @@ export interface IEditUserResult {
success: boolean;
}

export interface IUserDetails {
handle: string;
host: string;
global_id: number;
bio: string;
private: {
value?: boolean;
};
display_name: string;
}

export function GetUserInfo() {
const url = "/c2s/details/user";
return new Promise<IUserDetails>((resolve, reject) => {
superagent
.post(url)
.set("Content-Type", "application/json")
.set("Accept", "application/json")
.retry(2)
.end((error, res) => {
if (error) {
reject(error);
return;
}

const details = res!.body;
if (details === null) {
reject("could not get current user details");
return;
}
resolve(details);
});
});
}

export function EditUserPromise(
bio: string, displayName: string,
currentPassword: string, newPassword: string,
Expand All @@ -20,7 +55,9 @@ export function EditUserPromise(
new_password: newPassword,
post_body_css: postBodyCss,
post_title_css: postTitleCss,
private: privateAccount,
private: {
value: privateAccount,
},
};
return new Promise<IEditUserResult>((resolve, reject) => {
superagent
Expand Down
5 changes: 3 additions & 2 deletions services/activities/create/receive_create_servicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ def _check_follow(self, foreign_id, local_user_id):

return True

def _add_to_posts_db(self, author_handle, req):
def _add_to_posts_db(self, author_handle, author_id, req):
self._logger.debug("Calling article service with new foreign article")
# set flag in article service that is foreign (so no need to create service)
# TODO(sailslick) Unstring foreign_id when pr #159 is merged
na = article_pb2.NewArticle(
author=author_handle,
author_id=author_id,
title=req.title,
body=req.content,
creation_datetime=req.published,
Expand Down Expand Up @@ -98,7 +99,7 @@ def ReceiveCreate(self, req, context):
return resp

# add to article db
added_flag = self._add_to_posts_db(author_handle, req)
added_flag = self._add_to_posts_db(author_handle, author_id, req)
if added_flag == False:
resp.result_type = create_pb2.CreateResponse.ERROR
return resp
Expand Down
16 changes: 10 additions & 6 deletions services/article/new_article_servicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,18 @@ def index(self, post_entry):
return resp.result_type == search_pb2.IndexResponse.ResultType.Value("OK")

def send_insert_request(self, req):
author = self._users_util.get_user_from_db(handle=req.author,
host=None)
if author is None:
self._logger.error('Could not find user in db: ' + str(req.author))
return database_pb2.PostsResponse.error, None
global_id = req.author_id
if not req.foreign:
author = self._users_util.get_user_from_db(handle=req.author,
host=None)
if author is None:
self._logger.error('Could not find user in db: ' + str(req.author))
return database_pb2.PostsResponse.error, None
global_id = author.global_id

html_body = self.get_html_body(req.body)
pe = database_pb2.PostsEntry(
author_id=author.global_id,
author_id=global_id,
title=req.title,
body=html_body,
md_body=req.body,
Expand Down
82 changes: 82 additions & 0 deletions services/database/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,55 @@ def test_not_equivalent_filter_defaults(self):
self.assertIn("Despacito", vals)
self.assertIn("cool", vals)

def test_equivalent_filter_deferred(self):
entry = database_pb2.PostsEntry(
title="Despacito",
body="alexa",
)

d = {'body': lambda entry: True}

clause, vals = util.equivalent_filter(entry, deferred=d)
self.assertIn("title = ?", clause)
self.assertIn("AND", clause)
self.assertIn("body = ?", clause)
self.assertIn("Despacito", vals)
self.assertIn(True, vals)
self.assertNotIn("alexa", vals)

def test_equivalent_filter_deferred_default_unset(self):
entry = database_pb2.PostsEntry(
title="Despacito",
)

d = {'body': lambda entry: True}
de = [('body', False)]

clause, vals = util.equivalent_filter(entry, defaults=de, deferred=d)
self.assertIn("title = ?", clause)
self.assertIn("AND", clause)
self.assertIn("body = ?", clause)
self.assertIn("Despacito", vals)
self.assertIn(False, vals)

def test_equivalent_filter_deferred_default_set(self):
entry = database_pb2.PostsEntry(
title="Despacito",
body="nice"
)

d = {'body': lambda entry: True}
de = [('body', False)]

clause, vals = util.equivalent_filter(entry, defaults=de, deferred=d)
self.assertIn("title = ?", clause)
self.assertIn("AND", clause)
self.assertIn("body = ?", clause)
self.assertIn("Despacito", vals)
self.assertIn(True, vals)
self.assertNotIn(False, vals)
self.assertNotIn("nice", vals)

def test_update_filter(self):
entry = database_pb2.PostsEntry(
title="Megolavania",
Expand All @@ -60,3 +109,36 @@ def test_update_filter(self):
self.assertIn('body = ?', clause)
self.assertIn("Megolavania", vals)
self.assertIn("sans is angry", vals)

def test_update_filter_deferred_none_return(self):
entry = database_pb2.PostsEntry(
title="Megolavania",
body="sans is angry",
)
d = {'body': lambda entry: None}
clause, vals = util.entry_to_update(entry, deferred=d)
self.assertIn('title = ?', clause)
self.assertNotIn('body = ?', clause)
self.assertNotIn("sans is angry", vals)

def test_update_filter_deferred_value_set(self):
entry = database_pb2.PostsEntry(
title="Megolavania",
body="sans is angry",
)
d = {'body': lambda entry: True}
clause, vals = util.entry_to_update(entry, deferred=d)
self.assertIn('title = ?', clause)
self.assertIn(', ', clause)
self.assertIn('body = ?', clause)
self.assertIn("Megolavania", vals)
self.assertIn(True, vals)

def test_update_filter_deferred_unset(self):
entry = database_pb2.PostsEntry(
title="Megolavania",
)
d = {'body': lambda entry: True}
clause, vals = util.entry_to_update(entry, deferred=d)
self.assertIn('title = ?', clause)
self.assertIn("Megolavania", vals)
36 changes: 24 additions & 12 deletions services/database/users_servicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ def __init__(self, db, logger):
database_pb2.UsersRequest.FIND_NOT: self._users_handle_find_not,
database_pb2.UsersRequest.UPDATE: self._users_handle_update,
}
self._filter_defer = {
'private': self._private_to_filter
}

def _private_to_filter(self, entry):
if not entry.HasField("private"):
return None
return entry.private.value

def _db_tuple_to_entry(self, tup, entry):
if len(tup) != 11:
Expand All @@ -45,7 +53,7 @@ def _db_tuple_to_entry(self, tup, entry):
entry.password = tup[4]
entry.bio = tup[5]
entry.rss = tup[6]
entry.private = tup[7]
entry.private.value = tup[7]
entry.is_followed = tup[8]
entry.post_title_css = tup[9]
entry.post_body_css = tup[10]
Expand Down Expand Up @@ -170,7 +178,7 @@ def _users_handle_insert(self, req, resp):
req.entry.password,
req.entry.bio,
req.entry.rss,
req.entry.private,
req.entry.private.value,
commit=False)
res = self._db.execute(
'SELECT last_insert_rowid() FROM users LIMIT 1')
Expand All @@ -190,8 +198,10 @@ def _users_handle_insert(self, req, resp):
resp.global_id = res[0][0]

def _users_handle_update(self, req, resp):
update_clause, u_values = util.entry_to_update(req.entry)
filter_clause, f_values = util.equivalent_filter(req.match)
update_clause, u_values = util.entry_to_update(
req.entry, deferred = self._filter_defer)
filter_clause, f_values = util.equivalent_filter(
req.match, deferred = self._filter_defer)
values = u_values + f_values

if not filter_clause or not update_clause:
Expand Down Expand Up @@ -221,19 +231,21 @@ def _users_handle_update(self, req, resp):

resp.result_type = database_pb2.UsersResponse.OK

def _users_handle_find_not(self, req, resp):
def _get_uid(self, req):
user_id = -1
if req.HasField("user_global_id"):
user_id = req.user_global_id.value
filter_clause, values = util.not_equivalent_filter(req.match)
self._user_find_op(resp, filter_clause, [], user_id)
return user_id

def _users_handle_find_not(self, req, resp):
filter_clause, values = util.not_equivalent_filter(
req.match, deferred = self._filter_defer)
self._user_find_op(resp, filter_clause, [], self._get_uid(req))

def _users_handle_find(self, req, resp):
user_id = -1
if req.HasField("user_global_id"):
user_id = req.user_global_id.value
filter_clause, values = util.equivalent_filter(req.match)
self._user_find_op(resp, filter_clause, values, user_id)
filter_clause, values = util.equivalent_filter(
req.match, deferred = self._filter_defer)
self._user_find_op(resp, filter_clause, values, self._get_uid(req))

def _user_find_op(self, resp, filter_clause, values, user_id):
try:
Expand Down
Loading

0 comments on commit d1cfe3b

Please sign in to comment.