From 6655a5fca8d4568bb2501e2c145721084f0067dc Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Thu, 24 Mar 2016 23:52:04 +0300 Subject: [PATCH] feed update; other --- package.json | 1 + src/api/feed.js | 9 ++ src/client/components/Timeline.js | 105 ++++++++++++---------- src/data/Feed.js | 19 ++-- src/io.js | 20 +++-- src/models/TimelinePostInstagram.js | 52 +++++++++++ src/routes/news.js | 4 +- src/templates/components/page-school.html | 2 +- 8 files changed, 146 insertions(+), 66 deletions(-) create mode 100644 src/models/TimelinePostInstagram.js diff --git a/package.json b/package.json index 1d83037c..82c1edf9 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/api/feed.js b/src/api/feed.js index f309f2ce..4e964b7a 100644 --- a/src/api/feed.js +++ b/src/api/feed.js @@ -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; diff --git a/src/client/components/Timeline.js b/src/client/components/Timeline.js index 391d55cc..0f508fb8 100644 --- a/src/client/components/Timeline.js +++ b/src/client/components/Timeline.js @@ -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); }); diff --git a/src/data/Feed.js b/src/data/Feed.js index e13ad962..608fca24 100644 --- a/src/data/Feed.js +++ b/src/data/Feed.js @@ -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); } diff --git a/src/io.js b/src/io.js index fcede465..f2b578eb 100644 --- a/src/io.js +++ b/src/io.js @@ -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')}`); }); } diff --git a/src/models/TimelinePostInstagram.js b/src/models/TimelinePostInstagram.js new file mode 100644 index 00000000..4e3cea29 --- /dev/null +++ b/src/models/TimelinePostInstagram.js @@ -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; +} \ No newline at end of file diff --git a/src/routes/news.js b/src/routes/news.js index 3b8e31c3..907a8f3c 100644 --- a/src/routes/news.js +++ b/src/routes/news.js @@ -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('/'); diff --git a/src/templates/components/page-school.html b/src/templates/components/page-school.html index 253fb8ad..0cb64321 100644 --- a/src/templates/components/page-school.html +++ b/src/templates/components/page-school.html @@ -45,4 +45,4 @@ - \ No newline at end of file +