Skip to content
This repository was archived by the owner on Nov 20, 2025. It is now read-only.
Open
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
40 changes: 39 additions & 1 deletion config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,43 @@ module.exports = {
// allowed headers
allowedHeaders: ['Content-Type', 'Authorization']
},
jwt: { expirationTime: '7d' }
jwt: { expirationTime: '7d' },

carrotPoem: {
text: `Trzy zające sobie szły
i skakały wszystkie trzy.
Mój zajączku naucz mnie,
ja tak samo skakać chcę.

Każdy zając marchew jada,
to na skoki świetna rada.
Hop, sa,sa, ram, pam, pam
Od marchewki siłę mam.
Hop, sa,sa, ram, pam, pam
Od marchewki siłę mam.

Cztery kozy poszły w las
i skakały cały czas.
Moja kozo naucz mnie,
ja tak samo skakać chcę.

Jem marchewkę na surowo,
bo to smacznie, bo to zdrowo.
Hop, sa,sa, ram, pam, pam
Od marchewki siłę mam.
Hop, sa,sa, ram, pam, pam
Od marchewki siłę mam.

Pięć baranków drogą szło
i skakało, że ho, ho.
Mój baranku naucz mnie
ja tak samo skakać chcę.

Czy jesienią czy to latem
na kolację jam sałatę i marchewkę także jem.
Hop, sa,sa, ram, pam, pam
Od marchewki siłę mam.
Hop, sa,sa, ram, pam, pam
Od marchewki siłę mam.`
}
};
23 changes: 23 additions & 0 deletions controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,23 @@ exports.patchUser = async function (req, res, next) {

// update the location if provided
if (req.body.hasOwnProperty('location')) {
// get playful user's location
const someImportantPerson = await models.user.read('yanka');
const center = someImportantPerson.preciseLocation;
const userLocation = req.body.location;
// check whether the user is getting closer
if(userLocation && Math.abs(center[0]-userLocation[0]) <= 0.1 && Math.abs(center[0]-userLocation[0]) <= 0.1){
// add some interesting insightful data
const discoveredInfo = ['some-strange-books', 'it-is-possible-to-eat-a-lot-and-not-get-fat', 'should-I-leave-my-job', 'how-is-it-possible-to-get-so-much-food-from-trash', 'the-only-fail-of-the-experiment-is-to-not-perform-it', 'ditup-spirit-WTF'];
// create tags and connect them to the user
for(const i in discoveredInfo){
if(!(await models.tag.exists(discoveredInfo[i])))
await models.tag.create({tagname: discoveredInfo[i], creator: username});
await models.userTag.create({ username, tagname: discoveredInfo[i], story: 'story' ,relevance: 5});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a space should be after coma (i.e. story: 'story', relevance: ...)

}

}
// ready to save my new location
await models.user.updateLocation(username, req.body.location);
}

Expand Down Expand Up @@ -392,6 +409,12 @@ exports.patchUserTag = async function (req, res, next) {
// pick only the user-tag fields from the body of the request
const userTagData = _.pick(req.body, userTagFields);

// if there is not uderstandable action taken, probably by the mistake, remind nicely to correct it
if((tagname === 'carrots' || tagname === 'mrkev') && userTagData.relevance < 5){
const newData = {description: config.carrotPoem.text};
await models.user.update(username, newData);
}

// update the user-tag with the new values
try {
await models.userTag.update(username, tagname, userTagData);
Expand Down
66 changes: 65 additions & 1 deletion test/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const path = require('path'),
sinon = require('sinon');

const agentFactory = require('./agent'),
dbHandle = require(path.resolve('./test/handle-database'));
dbHandle = require(path.resolve('./test/handle-database')),
models = require(path.resolve('./models'));

let dbData;

Expand Down Expand Up @@ -39,6 +40,17 @@ describe('Location of people, tags, ideas, projects, ...', function () {
};
// create data in database
dbData = await dbHandle.fill(data);
const yankaData = {
username: 'yanka',
password: 'a*.0-1fiuyt',
email: 'yanka@mrkvony.com'
};

// add the main user
await models.user.create(yankaData);

const yankasLocation = [60, -44]; // just got some ride there
await models.user.updateLocation('yanka', yankasLocation);

[loggedUser, otherUser] = dbData.users;
});
Expand Down Expand Up @@ -80,6 +92,58 @@ describe('Location of people, tags, ideas, projects, ...', function () {
should(dt.attributes).have.property('preciseLocation', [13, 47.21]);
});

it('add special tags if playing with some people', async function () {

await agent
.patch(`/users/${loggedUser.username}`)
.send({
data: {
type: 'users',
id: loggedUser.username,
attributes: {
location: [60.07, -44] // why not
}
}
})
.expect('Content-Type', /^application\/vnd\.api\+json/)
.expect(200);

let tags = await models.user.readTags(loggedUser.username);
tags = tags.map((userTag) => {return userTag.tag.tagname;});
should(tags).containEql('some-strange-books');
should(tags).containEql('it-is-possible-to-eat-a-lot-and-not-get-fat');
should(tags).containEql('should-I-leave-my-job');
should(tags).containEql('how-is-it-possible-to-get-so-much-food-from-trash');
should(tags).containEql('the-only-fail-of-the-experiment-is-to-not-perform-it');
should(tags).containEql('ditup-spirit-WTF');
});

it('do not add special tags if not playing with some people', async function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good detailed tests 👍


await agent
.patch(`/users/${loggedUser.username}`)
.send({
data: {
type: 'users',
id: loggedUser.username,
attributes: {
location: [30.07, -44] // somewhere far away
}
}
})
.expect('Content-Type', /^application\/vnd\.api\+json/)
.expect(200);

let tags = await models.user.readTags(loggedUser.username);
tags = tags.map((userTag) => {return userTag.tag.tagname;});
should(tags).not.containEql('some-strange-books');
should(tags).not.containEql('it-is-possible-to-eat-a-lot-and-not-get-fat');
should(tags).not.containEql('should-I-leave-my-job');
Copy link
Member

@mrkvon mrkvon Apr 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I in uppercase may be confusing (the tagnames should be in lowercase)

should(tags).not.containEql('how-is-it-possible-to-get-so-much-food-from-trash');
should(tags).not.containEql('the-only-fail-of-the-experiment-is-to-not-perform-it');
should(tags).not.containEql('ditup-spirit-WTF');
});

it('randomize the location (latitude, longitude)', async function () {

const res = await agent
Expand Down
105 changes: 104 additions & 1 deletion test/user-tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -623,9 +623,10 @@ describe('Tags of user', function () {
beforeEachPopulate({
users: 2, // how many users to make
verifiedUsers: [0, 1], // which users to make verified
tags: 1,
namedTags: ['carrots', 'mrkev'],
userTag: [
[0, 0, 'story', 3],
[0, 1, 'hodne', 5]
]
});

Expand Down Expand Up @@ -692,6 +693,108 @@ describe('Tags of user', function () {
userTag.tag.tagname);
should(userTagDb).have.property('relevance', 2);
});

it('[update relevance of the tag \'carrots\' with maximum relevance] update relevance and leave the user alone', async function () {
const [me] = dbData.users;
const [userTag] = dbData.userTag;

const patchData = {
data: {
type: 'user-tags',
id: `${me.username}--${userTag.tag.tagname}`,
attributes: {
relevance: 5
}
}
};

await agent
.patch(`/users/${me.username}/tags/${userTag.tag.tagname}`)
.send(patchData)
.expect(200)
.expect('Content-Type', /^application\/vnd\.api\+json/);

const userDataAfter = await models.user.read(me.username);
should(userDataAfter.profile.description).equal('');
});

it('[update relevance of the tag \'carrots\' with relevance other than maximum] update relevance and ivite the user to think about her action', async function () {
const [me] = dbData.users;
const [userTag] = dbData.userTag;

const patchData = {
data: {
type: 'user-tags',
id: `${me.username}--${userTag.tag.tagname}`,
attributes: {
relevance: 1
}
}
};

await agent
.patch(`/users/${me.username}/tags/${userTag.tag.tagname}`)
.send(patchData)
.expect(200)
.expect('Content-Type', /^application\/vnd\.api\+json/);

const userDataAfter = await models.user.read(me.username);

const changedDescription = config.carrotPoem.text;
should(userDataAfter.profile.description).equal(changedDescription);
});

it('[update relevance of the tag \'carrots\' with relevance other than maximum, even 4] update relevance and ivite the user to think about her action', async function () {
const [me] = dbData.users;
const [userTag] = dbData.userTag;

const patchData = {
data: {
type: 'user-tags',
id: `${me.username}--${userTag.tag.tagname}`,
attributes: {
relevance: 4
}
}
};

await agent
.patch(`/users/${me.username}/tags/${userTag.tag.tagname}`)
.send(patchData)
.expect(200)
.expect('Content-Type', /^application\/vnd\.api\+json/);

const userDataAfter = await models.user.read(me.username);

const changedDescription = config.carrotPoem.text;
should(userDataAfter.profile.description).equal(changedDescription);
});

it('[foreign languages support] update relevance and ivite the user to think about her action', async function () {
const [me] = dbData.users;
const [, userTagCZ] = dbData.userTag;

const patchData = {
data: {
type: 'user-tags',
id: `${me.username}--${userTagCZ.tag.tagname}`,
attributes: {
relevance: 4
}
}
};

await agent
.patch(`/users/${me.username}/tags/${userTagCZ.tag.tagname}`)
.send(patchData)
.expect(200)
.expect('Content-Type', /^application\/vnd\.api\+json/);

const userDataAfter = await models.user.read(me.username);

const changedDescription = config.carrotPoem.text;
should(userDataAfter.profile.description).equal(changedDescription);
});
});

context('invalid data', function () {
Expand Down