feed update; other

This commit is contained in:
tmshv committed 2016-03-24 23:52:04 +03:00
1 parent 70707a2244
commit 6655a5fca8
8 files changed
+146 -66

No files matched your search

+1
View File
@@ -36,6 +36,7 @@
"koa-static": "^2.0.0",
"mongodb": "^2.1.11",
"mz": "^2.3.1",
"request-promise": "^2.0.1",
"sitemap": "^1.5.0",
"socket.io": "^1.4.5",
"thunkify": "^2.1.2"
+9
View File
@@ -1,17 +1,26 @@
import {put} from 'koa-route';
import compose from 'koa-compose';
import {ensureServiceAuth} from '../middlewares/ensureServiceAuth';
import {ejson} from '../middlewares/ejson';
import {queryValue, booleanify} from '../middlewares/queryValue';
import data from '../data';
export function add() {
return put('/api/v1/feed', compose([
ensureServiceAuth(),
ejson(),
queryValue({
save: booleanify()
}),
async(ctx) => {
let doSave = ctx.request.query.save || true;
let post = ctx.request.body;
try {
if(!doSave) data.feed.disableSaving();
ctx.body = await data.feed.add(post);
if(!doSave) data.feed.enableSaving();
} catch (e) {
ctx.status = 400;
ctx.body = e;
+57 -48
View File
@@ -1,33 +1,58 @@
import template from '../../templates/components/timeline.html';
function personByInstagram(list, instagram){
list = list.filter(function (member) {
try {
return member.contacts.instagram === instagram;
} catch (error) {
return false;
}
});
import {createInstance} from '../../models/TimelinePostInstagram';
if(list.length){
return list[0].id;
}else{
return null;
}
}
// function personByInstagram(list, instagram){
// list = list.filter(function (member) {
// try {
// return member.contacts.instagram === instagram;
// } catch (error) {
// return false;
// }
// });
//
// if(list.length){
// return list[0].id;
// }else{
// return null;
// }
// }
function postText(post){
try{
return post.data.caption.text.replace(/#[\w_]+/, '')
}catch(error){
return "";
}
}
// function postText(post){
// try{
// return post.data.caption.text.replace(/#[\w_]+/, '')
// }catch(error){
// return "";
// }
// }
export default function(app) {
// function toInstagram(post){
// //var author_id = personByInstagram(team.team, post.data.author);
// //var name = author_id ? team.short(author_id) : post.data.author;
// let name = 'hudozka';
//
// return {
// date: post.date,
// image: post.data.image.standard_resolution.url,
// url: post.data.url,
// username: name,
// userpic: 'https://igcdn-photos-a-a.akamaihd.net/hphotos-ak-xaf1/t51.2885-19/11357496_464997000344800_2124591831_a.jpg',
// text: post.body,
// //text: postText(post),
// type: post.type
// };
// }
const createPost = post => [createInstance]
.reduce((post, map) => {
let result = map(post);
return result ? result : post
}, post);
export default function (app) {
app.component('timeline', {
template: template,
controller: function($timeout, usSpinnerService, api, io){
controller: function ($timeout, usSpinnerService, api, io) {
let skip = 0;
let limit = 10;
this.feed = [];
@@ -35,23 +60,6 @@ export default function(app) {
this.timelineUpdating = false;
usSpinnerService.stop('timelineMore');
function toInstagram(post){
//var author_id = personByInstagram(team.team, post.data.author);
//var name = author_id ? team.short(author_id) : post.data.author;
let name = 'hudozka';
return {
date: post.date,
image: post.data.image.standard_resolution.url,
url: post.data.url,
username: name,
userpic: 'https://igcdn-photos-a-a.akamaihd.net/hphotos-ak-xaf1/t51.2885-19/11357496_464997000344800_2124591831_a.jpg',
text: post.body,
//text: postText(post),
type: post.type
};
}
this.loadNext = () => {
this.timelineUpdating = true;
usSpinnerService.spin('timelineMore');
@@ -62,13 +70,14 @@ export default function(app) {
this.timelineUpdating = false;
usSpinnerService.stop('timelineMore');
let feed2 = feed.map(post => {
if(post.type == 'instagram') return toInstagram(post);
else return post;
});
// let feed2 = feed.map(post => {
// if (post.type == 'instagram') return toInstagram(post);
// else return post;
// });
feed = feed.map(createPost);
skip += limit;
this.feed = this.feed.concat(feed2);
this.feed = this.feed.concat(feed);
this.showFooter = this.feed && this.feed.length;
});
};
@@ -76,12 +85,12 @@ export default function(app) {
this.loadNext();
io.on('feed', feed => {
// console.log(feed);
console.log(feed);
feed = feed instanceof Array ? feed : [feed];
feed = feed.map(i => i.type === 'instagram' ? toInstagram(i) : i);
feed = feed.map(createPost);
$timeout(()=>{
$timeout(()=> {
this.feed = feed.concat(this.feed);
}, 0);
});
+12 -7
View File
@@ -6,21 +6,26 @@ export default class Feed extends Data {
}
add(post) {
let add = async(post) => {
let savePost = async(post) => {
const id = post.id;
const type = post.type;
const query = {id: id, type: type};
await this.store.update(query, post, {upsert: true});
let document = await this.store.find(query).limit(1).toArray();
document = document[0];
// post._id = `schachlo-${Date.now()}`;
// let document = post;
return document[0];
};
let add = async(post) => {
let document = post;
if(this.save){
document = await savePost(post);
}
this.update(document);
return document;
};
return add(post);
}
+12 -8
View File
@@ -4,21 +4,25 @@ import data from './data';
export default function(server) {
let io = socketIO(server);
io.on('connection', socket => {
console.log('a user connected');
// console.log('a user connected');
socket.emit('feed', {
type: 'test',
id: 1,
message: 'hello world'
});
// socket.emit('feed', {
// type: 'test',
// id: 1,
// message: 'hello world'
// });
// socket.on('my other event', function (data) {
// console.log(data);
// });
});
let transfer = name => i => io.emit(name, i);
let feed = transfer('feed');
data.feed.on('update', feed);
data.feed.on('update', i => {
console.log(`io feed update ${JSON.stringify(i, null, '\t')}`);
io.emit('feed', i);
console.log(`io update ${JSON.stringify(i, null, '\t')}`);
});
}
+52
View File
@@ -0,0 +1,52 @@
export default class TimelinePostInstagram {
constructor(date, image, url, username, userpic, text) {
this.type = 'instagram';
this.date = date;
this.image = image;
this.url = url;
this.username = username;
this.userpic = userpic;
this.text = text;
}
}
export function fromInstagram1(post) {
//var author_id = personByInstagram(team.team, post.data.author);
//var name = author_id ? team.short(author_id) : post.data.author;
const name = post.data.author || 'hudozka';
const userpic = 'https://igcdn-photos-a-a.akamaihd.net/hphotos-ak-xaf1/t51.2885-19/11357496_464997000344800_2124591831_a.jpg';
return new TimelinePostInstagram(
post.date,
post.data.image.standard_resolution.url,
post.data.url,
name,
userpic,
post.body
);
}
export function fromInstagram2(post) {
return new TimelinePostInstagram(
post.date,
post.data.images.standard_resolution.url,
post.data.link,
post.data.user.username,
post.data.user.profile_picture,
post.data.caption.text
);
}
export const types = {
'instagram-1': fromInstagram1,
'instagram-2': fromInstagram2
};
export function createInstance(post) {
if(post.type in types) {
let fn = types[post.type];
return fn(post);
}
return null;
}
+2 -2
View File
@@ -4,9 +4,9 @@ import {accepts} from './';
import {c} from '../core/db';
import {sortBy} from '../utils/sort';
const timestamp = i => i.getTime();
const sortNewsByDate = sortBy(
i => (new Date(i.date))
.getTime()
i => timestamp(new Date(i.date))
);
const redirect = async ctx => ctx.redirect('/');
+1 -1
View File
@@ -45,4 +45,4 @@
<!--</div>-->
<timeline></timeline>
</div>
</div>