Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

397 #469

Merged
merged 8 commits into from
Mar 16, 2017
Merged

397 #469

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ If there are not many items, just list them sequentially.

* Display End of Feed UI for feeds (#389)
* Update mobx-react from 4.1.0 to 4.1.3.
* Explore Nearby: Separate API for Bot List loading (#397)
* Add geosearch for bots API.


# 1.25.3 - 2017 March 13
Expand Down
1 change: 1 addition & 0 deletions src/components/BotDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ export default class extends React.Component {
const coef = bot.image && bot.image.width ? (width-34*k)/bot.image.width : 0;
const profile = bot.owner;
if (!profile || !bot.location){
console.log("No profile or not bot location", JSON.stringify(bot));
return <Screen/>
}
const source = bot.image && bot.image.source;
Expand Down
14 changes: 11 additions & 3 deletions src/components/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import model from '../model/model';
import statem from '../../gen/state';
import {MessageBar, MessageBarManager} from 'react-native-message-bar';
import TransparentGradient from './TransparentGradient';
import botStore from '../store/botStore';

class OwnMessageBar extends MessageBar {
componentWillReceiveProps(nextProps) {
Expand Down Expand Up @@ -53,7 +54,8 @@ export default class Map extends React.Component {

constructor(props){
super(props);
this.state = {selectedBot : this.props.selectedBot, followUser: this.props.followUser}
const list = this.props.bot ? [this.props.bot] : [];
this.state = {selectedBot : this.props.selectedBot, followUser: this.props.followUser, list}
}

setCenterCoordinate(latitude, longitude, animated = true, callback) {
Expand All @@ -75,6 +77,12 @@ export default class Map extends React.Component {
}
}

async onRegionDidChange({latitude, longitude, zoomLevel, direction, pitch, animated}) {
MessageBarManager.hideAlert();
console.log("onRegionDidChange", latitude, longitude, zoomLevel, direction, pitch, animated);
const list = await botStore.geosearch({latitude, longitude});
this.setState({list});
}

followUser() {
if (!this.handler) {
Expand Down Expand Up @@ -135,7 +143,7 @@ export default class Map extends React.Component {
return;
}
this.setState({selectedBot : annotation.id});
const bot: Bot = model.followingBots.list.find(bot=>bot.id === annotation.id);
const bot: Bot = this.state.list.find(bot=>bot.id === annotation.id);
if (!bot){
alert("Cannot find bot with id: " + annotation.id);
return;
Expand Down Expand Up @@ -176,7 +184,7 @@ export default class Map extends React.Component {
const isDay = location.isDay;
const current = location.location;
const coords = this.state.followUser ? location.location : this.props.location;
const list = this.props.bot && !model.followingBots.get(this.props.bot.id) ? [...model.followingBots.list, this.props.bot] : model.followingBots.list;
const list = this.state.list.filter(bot=>bot.loaded);
const annotations = list.filter(bot=>!this.props.showOnlyBot || this.props.bot.id === bot.id ).map(bot => {return {
coordinates: [bot.location.latitude, bot.location.longitude],
type: 'point',
Expand Down
20 changes: 12 additions & 8 deletions src/model/Bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@ export default class Bot {
@observable shareMode;
coverColor: integer;

constructor({id, fullId, server, type, ...data}){
constructor({id, fullId, server, type, loaded = false, ...data}){
console.log("CREATE BOT", fullId, id, server, type);
this.id = id;
this.server = server;
this.loaded = loaded;
if (id && server){
this.fullId = `${id}/${server}`;
this.isNew = false;
Expand All @@ -103,20 +104,23 @@ export default class Bot {
this.server = fullId.split('/')[1];
this.isNew = false;
}
if (!type && this.server){
if (!loaded && !type && this.server){
// bot is not loaded yet, lets load it
autorun(async () => {
if (model.connected && !this.loaded){
console.log("DOWNLOAD BOT", this.id);
try {
const d = await bot.load({id: this.id, server: this.server});
console.log("BOT LOADED:", this.id, JSON.stringify(d));
this.load(d);
this.loaded = true;

console.log("DOWNLOAD BOT", this.id);
const d = await bot.load({id: this.id, server: this.server});
console.log("BOT LOADED:", this.id, JSON.stringify(d));
this.load(d);
this.loaded = true;
} catch (e){
console.log("BOT ERROR:", this.id, e);
console.log("BOT LOAD ERROR", e);
}

}

});
} else {
this.type = type;
Expand Down
14 changes: 13 additions & 1 deletion src/store/botStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,19 @@ class BotStore {
}
}
}

async geosearch({latitude, longitude}){
const list = await xmpp.geosearch({latitude, longitude, server: model.server});
const res = [];
for (const botData of list) {
// if we have necessary data, no need to do additional fetch for each bot
if (botData.owner && botData.location) {
res.push(botFactory.create({loaded: true, ...botData}));
} else {
res.push(botFactory.create(botData));
}
}
return res;
}
async loadImages(before) {
try {
const images = await xmpp.imageItems({id: this.bot.id, server: this.bot.server}, before);
Expand Down
27 changes: 25 additions & 2 deletions src/store/xmpp/botService.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class BotService {

convert(data){
return data.field.reduce((total: Bot, current: Bot)=>{
if (current.var === 'followers+size') {
if (current.var === 'subscribers+size'){
total.followersSize = parseInt(current.value);
} else if (current.type === 'geoloc'){
total[current.var] = {latitude: parseFloat(current.geoloc.lat), longitude: parseFloat(current.geoloc.lon)};
Expand Down Expand Up @@ -161,7 +161,7 @@ class BotService {
.c('bot', {xmlns: NS, node:`bot/${id}`});
//console.log("LOAD BOT:", iq.toString());
const data = await xmpp.sendIQ(iq);
//console.log("BOT RES:", data);
console.log("BOT RES:", data);
if (data.error){
throw data.error;
}
Expand Down Expand Up @@ -196,6 +196,29 @@ class BotService {
xmpp.sendStanza(msg);
}

async geosearch({server, latitude, longitude}) {
const iq = $iq({type: 'get', to: server})
.c('bots', {xmlns: NS, lat:latitude, lon:longitude})

const data = await xmpp.sendIQ(iq);

if (data.error){
throw data.error;
}
const res = [];
let bots = data.bots.bot;
if (!bots){
bots = [];
}
if (!Array.isArray(bots)){
bots = [bots];
}
for (let item of bots){
res.push(this.convert(item))
}
return res;
}

async list(user, server, before, limit = 10){
assert(user, 'bot.list: user is not defined!');
assert(server, 'bot.list: server is not defined!');
Expand Down
21 changes: 18 additions & 3 deletions test/bot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ let botData;
let user, password, server, botId;
let friend;
describe("bot", function() {
step("geoseach test", async function(done){
try {
await profile.connect('d6976ac8-5a3a-11e6-8008-0e2ac49618c7', '$T$ajO219JxmSkwOy/7qlqD1/24uab1EU7QIra3CBi11XU=', 'staging.dev.tinyrobot.com');
const list = await botStore.geosearch({latitude:11.0, longitude:12.5});
console.log("GEOSEARCH", list);
await profile.logout();
done();
} catch (e){
done(e);
}
})
step("generate id", async function(done){
try {
const data = testDataNew(11);
Expand All @@ -42,7 +53,7 @@ describe("bot", function() {
done()
}
});

step("register/login friend", async function(done){
const data = testDataNew(12);
const {user, password, server} = await xmpp.register(data.resource, data.provider_data);
Expand Down Expand Up @@ -98,7 +109,7 @@ describe("bot", function() {

botStore.create({type:'location', title:'Bot title', radius:10, shortname, description,
location: {latitude:11.1, longitude:12.5, accuracy:2}, image, visibility: VISIBILITY_FRIENDS, newAffiliates:[{user:friend}]});

when (()=>botStore.bot.id, async ()=>{
try {
expect(botStore.bot.id).to.be.not.undefined;
Expand All @@ -114,7 +125,7 @@ describe("bot", function() {
botData = res;
await xmpp.disconnect(null);
done();

} catch (e){
done(e)
}
Expand Down Expand Up @@ -231,6 +242,10 @@ describe("bot", function() {
await xmpp.disconnect(null);
done();
});




// step("test workflow", async function(done) {
// try {
// statem.start();
Expand Down