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