Implement the poll feature

Closes #164
This commit is contained in:
syuilo 2017-02-14 13:59:26 +09:00
parent 35cf9f5d04
commit 2b4c5ecff4
18 changed files with 448 additions and 56 deletions

View file

@ -93,6 +93,7 @@ export default [
{ name: 'posts/likes/delete', shouldBeSignin: true, limitDuration: hour, limitMax: 100, kind: 'like-write' }, { name: 'posts/likes/delete', shouldBeSignin: true, limitDuration: hour, limitMax: 100, kind: 'like-write' },
{ name: 'posts/favorites/create', shouldBeSignin: true, limitDuration: hour, limitMax: 100, kind: 'favorite-write' }, { name: 'posts/favorites/create', shouldBeSignin: true, limitDuration: hour, limitMax: 100, kind: 'favorite-write' },
{ name: 'posts/favorites/delete', shouldBeSignin: true, limitDuration: hour, limitMax: 100, kind: 'favorite-write' }, { name: 'posts/favorites/delete', shouldBeSignin: true, limitDuration: hour, limitMax: 100, kind: 'favorite-write' },
{ name: 'posts/polls/vote', shouldBeSignin: true, limitDuration: hour, limitMax: 100, kind: 'vote-write' },
{ name: 'messaging/history', shouldBeSignin: true, kind: 'messaging-read' }, { name: 'messaging/history', shouldBeSignin: true, kind: 'messaging-read' },
{ name: 'messaging/unread', shouldBeSignin: true, kind: 'messaging-read' }, { name: 'messaging/unread', shouldBeSignin: true, kind: 'messaging-read' },

View file

@ -161,9 +161,59 @@ module.exports = (params, user, app) =>
replyTo = null; replyTo = null;
} }
// テキストが無いかつ添付ファイルが無いかつRepostも無かったらエラー // Get 'poll' parameter
if (text === null && files === null && repost === null) { let poll = params.poll;
return rej('text, media_ids or repost_id is required'); if (poll !== undefined && poll !== null) {
// 選択肢が無かったらエラー
if (poll.choices == null) {
return rej('poll choices is required');
}
// 選択肢が配列でなかったらエラー
if (!Array.isArray(poll.choices)) {
return rej('poll choices must be an array');
}
// Validate each choices
const shouldReject = poll.choices.some(choice => {
if (typeof choice !== 'string') return true;
if (choice.trim().length === 0) return true;
if (choice.trim().length > 100) return true;
});
if (shouldReject) {
return rej('invalid poll choices');
}
// Trim choices
poll.choices = poll.choices.map(choice => choice.trim());
// Drop duplicates
poll.choices = poll.choices.filter((x, i, s) => s.indexOf(x) == i);
// 選択肢がひとつならエラー
if (poll.choices.length == 1) {
return rej('poll choices must be ひとつ以上');
}
// 選択肢が多すぎてもエラー
if (poll.choices.length > 10) {
return rej('many poll choices');
}
// serialize
poll.choices = poll.choices.map((choice, i) => ({
id: i, // IDを付与
text: choice,
votes: 0
}));
} else {
poll = null;
}
// テキストが無いかつ添付ファイルが無いかつRepostも無いかつ投票も無かったらエラー
if (text === null && files === null && repost === null && poll === null) {
return rej('text, media_ids, repost_id or poll is required');
} }
// 投稿を作成 // 投稿を作成
@ -172,6 +222,7 @@ module.exports = (params, user, app) =>
media_ids: media ? files.map(file => file._id) : undefined, media_ids: media ? files.map(file => file._id) : undefined,
reply_to_id: replyTo ? replyTo._id : undefined, reply_to_id: replyTo ? replyTo._id : undefined,
repost_id: repost ? repost._id : undefined, repost_id: repost ? repost._id : undefined,
poll: poll ? poll : undefined,
text: text, text: text,
user_id: user._id, user_id: user._id,
app_id: app ? app._id : null app_id: app ? app._id : null

View file

@ -0,0 +1,101 @@
'use strict';
/**
* Module dependencies
*/
import * as mongo from 'mongodb';
import Vote from '../../../models/poll-vote';
import Post from '../../../models/post';
import notify from '../../../common/notify';
/**
* Vote poll of a post
*
* @param {Object} params
* @param {Object} user
* @return {Promise<object>}
*/
module.exports = (params, user) =>
new Promise(async (res, rej) =>
{
// Get 'post_id' parameter
const postId = params.post_id;
if (postId === undefined || postId === null) {
return rej('post_id is required');
}
// Validate id
if (!mongo.ObjectID.isValid(postId)) {
return rej('incorrect post_id');
}
// Get votee
const post = await Post.findOne({
_id: new mongo.ObjectID(postId)
});
if (post === null) {
return rej('post not found');
}
if (post.poll == null) {
return rej('poll not found');
}
// Get 'choice' parameter
const choice = params.choice;
if (choice == null) {
return rej('choice is required');
}
// Validate choice
if (!post.poll.choices.some(x => x.id == choice)) {
return rej('invalid choice');
}
// Check arleady voted
const exist = await Vote.findOne({
post_id: post._id,
user_id: user._id
});
if (exist !== null) {
return rej('already voted');
}
// Create vote
await Vote.insert({
created_at: new Date(),
post_id: post._id,
user_id: user._id,
choice: choice
});
// Send response
res();
const inc = {};
inc[`poll.choices.${ findWithAttr(post.poll.choices, 'id', choice) }.votes`] = 1;
console.log(inc);
// Increment likes count
Post.update({ _id: post._id }, {
$inc: inc
});
// Notify
notify(post.user_id, user._id, 'poll_vote', {
post_id: post._id,
choice: choice
});
});
function findWithAttr(array, attr, value) {
for (let i = 0; i < array.length; i += 1) {
if(array[i][attr] === value) {
return i;
}
}
return -1;
}

View file

@ -39,7 +39,6 @@ module.exports = (params, user) =>
// Serialize // Serialize
res(await serialize(post, user, { res(await serialize(post, user, {
serializeReplyTo: true, detail: true
includeIsLiked: true
})); }));
}); });

View file

@ -0,0 +1,3 @@
import db from '../../db/mongodb';
export default db.get('poll_votes') as any; // fuck type definition

View file

@ -54,6 +54,7 @@ export default (notification: any) => new Promise<Object>(async (resolve, reject
case 'repost': case 'repost':
case 'quote': case 'quote':
case 'like': case 'like':
case 'poll_vote':
// Populate post // Populate post
_notification.post = await serializePost(_notification.post_id, me); _notification.post = await serializePost(_notification.post_id, me);
break; break;

View file

@ -6,6 +6,7 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
import Post from '../models/post'; import Post from '../models/post';
import Like from '../models/like'; import Like from '../models/like';
import Vote from '../models/poll-vote';
import serializeApp from './app'; import serializeApp from './app';
import serializeUser from './user'; import serializeUser from './user';
import serializeDriveFile from './drive-file'; import serializeDriveFile from './drive-file';
@ -23,15 +24,11 @@ const self = (
post: any, post: any,
me?: any, me?: any,
options?: { options?: {
serializeReplyTo: boolean, detail: boolean
serializeRepost: boolean,
includeIsLiked: boolean
} }
) => new Promise<Object>(async (resolve, reject) => { ) => new Promise<Object>(async (resolve, reject) => {
const opts = options || { const opts = options || {
serializeReplyTo: true, detail: true,
serializeRepost: true,
includeIsLiked: true
}; };
let _post: any; let _post: any;
@ -72,26 +69,35 @@ const self = (
)); ));
} }
if (_post.reply_to_id && opts.serializeReplyTo) { if (_post.reply_to_id && opts.detail) {
// Populate reply to post // Populate reply to post
_post.reply_to = await self(_post.reply_to_id, me, { _post.reply_to = await self(_post.reply_to_id, me, {
serializeReplyTo: false, detail: false
serializeRepost: false,
includeIsLiked: false
}); });
} }
if (_post.repost_id && opts.serializeRepost) { if (_post.repost_id && opts.detail) {
// Populate repost // Populate repost
_post.repost = await self(_post.repost_id, me, { _post.repost = await self(_post.repost_id, me, {
serializeReplyTo: _post.text == null, detail: _post.text == null
serializeRepost: _post.text == null,
includeIsLiked: _post.text == null
}); });
} }
// Poll
if (me && _post.poll && opts.detail) {
const vote = await Vote
.findOne({
user_id: me._id,
post_id: id
});
if (vote != null) {
_post.poll.choices.filter(c => c.id == vote.choice)[0].is_voted = true;
}
}
// Check if it is liked // Check if it is liked
if (me && opts.includeIsLiked) { if (me && opts.detail) {
const liked = await Like const liked = await Like
.count({ .count({
user_id: me._id, user_id: me._id,

View file

@ -18,3 +18,5 @@ require('./signin-history.tag');
require('./api-info.tag'); require('./api-info.tag');
require('./twitter-setting.tag'); require('./twitter-setting.tag');
require('./authorized-apps.tag'); require('./authorized-apps.tag');
require('./poll.tag');
require('./poll-editor.tag');

View file

@ -0,0 +1,47 @@
<mk-poll-editor>
<ul>
<li each={ choice, i in choices }>
<input value={ choice } oninput={ oninput.bind(null, i) }>
<button onclick={ remove.bind(null, i) }>削除</button>
</li>
</ul>
<button onclick={ add }>選択肢を追加</button>
<style type="stylus">
:scope
display block
> ul
display block
margin 0
padding 0
list-style none
> li
display block
margin 4px
padding 8px 12px
width 100%
</style>
<script>
@choices = ['', '']
@oninput = (i, e) ~>
@choices[i] = e.target.value
@add = ~>
@choices.push ''
@update!
@remove = (i) ~>
console.log i
console.log @choices.filter((_, _i) -> _i != i)
@choices = @choices.filter((_, _i) -> _i != i)
@update!
@get = ~>
return {
choices: @choices.filter (choice) -> choice != ''
}
</script>
</mk-poll-editor>

View file

@ -0,0 +1,73 @@
<mk-poll>
<ul>
<li each={ poll.choices } onclick={ vote.bind(null, id) } class={ voted: voted }>
<div class="backdrop" if={ parent.result } style={ 'width:' + (votes / parent.total * 100) + '%' }></div>
<span>
<i class="fa fa-check" if={ is_voted }></i>
{ text }
<span class="votes" if={ parent.result }>({ votes }票)</span>
</span>
</li>
</ul>
<p>{ total }人が投票</p>
<style type="stylus">
:scope
display block
> ul
display block
margin 0
padding 0
list-style none
> li
display block
margin 4px
padding 4px 8px
width 100%
border-radius 4px
overflow hidden
cursor pointer
&:hover
background rgba(0, 0, 0, 0.05)
&:active
background rgba(0, 0, 0, 0.1)
> .backdrop
position absolute
top 0
left 0
height 100%
background $theme-color
> .votes
margin-left 4px
</style>
<script>
@mixin \api
@post = @opts.post
@poll = @post.poll
@total = @poll.choices.reduce ((a, b) -> a + b.votes), 0
@result = @poll.choices.some (c) -> c.is_voted
@vote = (id) ~>
if (@poll.choices.some (c) -> c.is_voted) then return
@api \posts/polls/vote do
post_id: @post.id
choice: id
.then ~>
@poll.choices.for-each (c) ->
if c.id == id
c.votes++
c.is_voted = true
@update do
poll: @poll
result: true
total: @total + 1
</script>
</mk-poll>

View file

@ -39,6 +39,12 @@
<p><i class="fa fa-at"></i><a href={ CONFIG.url + '/' + notification.post.user.username } data-user-preview={ notification.post.user_id }>{ notification.post.user.name }</a></p><a class="post-preview" href={ CONFIG.url + '/' + notification.post.user.username + '/' + notification.post.id }>{ getPostSummary(notification.post) }</a> <p><i class="fa fa-at"></i><a href={ CONFIG.url + '/' + notification.post.user.username } data-user-preview={ notification.post.user_id }>{ notification.post.user.name }</a></p><a class="post-preview" href={ CONFIG.url + '/' + notification.post.user.username + '/' + notification.post.id }>{ getPostSummary(notification.post) }</a>
</div> </div>
</virtual> </virtual>
<virtual if={ notification.type == 'poll_vote' }>
<a class="avatar-anchor" href={ CONFIG.url + '/' + notification.user.username } data-user-preview={ notification.user.id }><img class="avatar" src={ notification.user.avatar_url + '?thumbnail&size=48' } alt="avatar"/></a>
<div class="text">
<p><i class="fa fa-pie-chart"></i><a href={ CONFIG.url + '/' + notification.user.username } data-user-preview={ notification.user.id }>{ notification.user.name }</a></p><a class="post-ref" href={ CONFIG.url + '/' + notification.post.user.username + '/' + notification.post.id }>{ getPostSummary(notification.post) }</a>
</div>
</virtual>
</div> </div>
<p class="date" if={ i != notifications.length - 1 && notification._date != notifications[i + 1]._date }><span><i class="fa fa-angle-up"></i>{ notification._datetext }</span><span><i class="fa fa-angle-down"></i>{ notifications[i + 1]._datetext }</span></p> <p class="date" if={ i != notifications.length - 1 && notification._date != notifications[i + 1]._date }><span><i class="fa fa-angle-up"></i>{ notification._datetext }</span><span><i class="fa fa-angle-down"></i>{ notifications[i + 1]._datetext }</span></p>
</virtual> </virtual>

View file

@ -10,9 +10,11 @@
<p class="remain">残り{ 4 - files.length }</p> <p class="remain">残り{ 4 - files.length }</p>
</div> </div>
<mk-uploader ref="uploader"></mk-uploader> <mk-uploader ref="uploader"></mk-uploader>
<div ref="pollZone"></div>
<button ref="upload" title="PCからファイルを添付" onclick={ selectFile }><i class="fa fa-upload"></i></button> <button ref="upload" title="PCからファイルを添付" onclick={ selectFile }><i class="fa fa-upload"></i></button>
<button ref="drive" title="ドライブからファイルを添付" onclick={ selectFileFromDrive }><i class="fa fa-cloud"></i></button> <button ref="drive" title="ドライブからファイルを添付" onclick={ selectFileFromDrive }><i class="fa fa-cloud"></i></button>
<button class="cat" title="Insert The Cat" onclick={ cat }><i class="fa fa-smile-o"></i></button> <button class="cat" title="Insert The Cat" onclick={ cat }><i class="fa fa-smile-o"></i></button>
<button class="poll" title="投票を作成" onclick={ addPoll }><i class="fa fa-pie-chart"></i></button>
<p class="text-count { over: refs.text.value.length > 1000 }">のこり{ 1000 - refs.text.value.length }文字</p> <p class="text-count { over: refs.text.value.length > 1000 }">のこり{ 1000 - refs.text.value.length }文字</p>
<button class={ wait: wait } ref="submit" disabled={ wait || (refs.text.value.length == 0 && files.length == 0) } onclick={ post }>{ wait ? '投稿中' : opts.reply ? '返信' : '投稿' } <button class={ wait: wait } ref="submit" disabled={ wait || (refs.text.value.length == 0 && files.length == 0) } onclick={ post }>{ wait ? '投稿中' : opts.reply ? '返信' : '投稿' }
<mk-ellipsis if={ wait }></mk-ellipsis> <mk-ellipsis if={ wait }></mk-ellipsis>
@ -239,6 +241,7 @@
[ref='upload'] [ref='upload']
[ref='drive'] [ref='drive']
.cat .cat
.poll
display inline-block display inline-block
cursor pointer cursor pointer
padding 0 padding 0
@ -295,6 +298,7 @@
@uploadings = [] @uploadings = []
@files = [] @files = []
@autocomplete = null @autocomplete = null
@poll = null
@in-reply-to-post = @opts.reply @in-reply-to-post = @opts.reply
@ -409,6 +413,13 @@
@trigger \change-files @files @trigger \change-files @files
@update! @update!
@add-poll = ~>
if @poll?
@poll.unmount!
@poll = null
return
@poll = riot.mount(@refs.pollZone.append-child document.create-element \mk-poll-editor).0
@post = (e) ~> @post = (e) ~>
@wait = true @wait = true
@ -420,6 +431,7 @@
text: @refs.text.value text: @refs.text.value
media_ids: files media_ids: files
reply_to_id: if @in-reply-to-post? then @in-reply-to-post.id else undefined reply_to_id: if @in-reply-to-post? then @in-reply-to-post.id else undefined
poll: if @poll? then @poll.get! else undefined
.then (data) ~> .then (data) ~>
@trigger \post @trigger \post
@notify if @in-reply-to-post? then '返信しました!' else '投稿しました!' @notify if @in-reply-to-post? then '返信しました!' else '投稿しました!'

View file

@ -40,6 +40,7 @@
<div class="media" if={ p.media }> <div class="media" if={ p.media }>
<mk-images-viewer images={ p.media }></mk-images-viewer> <mk-images-viewer images={ p.media }></mk-images-viewer>
</div> </div>
<mk-poll if={ p.poll } post={ p }></mk-poll>
<div class="repost" if={ p.repost }><i class="fa fa-quote-right fa-flip-horizontal"></i> <div class="repost" if={ p.repost }><i class="fa fa-quote-right fa-flip-horizontal"></i>
<mk-post-preview class="repost" post={ p.repost }></mk-post-preview> <mk-post-preview class="repost" post={ p.repost }></mk-post-preview>
</div> </div>
@ -258,6 +259,9 @@
display block display block
max-width 100% max-width 100%
> mk-poll
font-size 80%
> .repost > .repost
margin 8px 0 margin 8px 0

View file

@ -1,48 +1,52 @@
<mk-notification-preview class={ notification.type }> <mk-notification-preview class={ notification.type }>
<div class="main" if={ notification.type == 'like' }><img class="avatar" src={ notification.user.avatar_url + '?thumbnail&size=64' } alt="avatar"/> <virtual if={ notification.type == 'like' }><img class="avatar" src={ notification.user.avatar_url + '?thumbnail&size=64' } alt="avatar"/>
<div class="text"> <div class="text">
<p><i class="fa fa-thumbs-o-up"></i>{ notification.user.name }</p> <p><i class="fa fa-thumbs-o-up"></i>{ notification.user.name }</p>
<p class="post-ref">{ getPostSummary(notification.post) }</p> <p class="post-ref">{ getPostSummary(notification.post) }</p>
</div> </div>
</div> </virtual>
<div class="main" if={ notification.type == 'repost' }><img class="avatar" src={ notification.post.user.avatar_url + '?thumbnail&size=64' } alt="avatar"/> <virtual if={ notification.type == 'repost' }><img class="avatar" src={ notification.post.user.avatar_url + '?thumbnail&size=64' } alt="avatar"/>
<div class="text"> <div class="text">
<p><i class="fa fa-retweet"></i>{ notification.post.user.name }</p> <p><i class="fa fa-retweet"></i>{ notification.post.user.name }</p>
<p class="post-ref">{ getPostSummary(notification.post.repost) }</p> <p class="post-ref">{ getPostSummary(notification.post.repost) }</p>
</div> </div>
</div> </virtual>
<div class="main" if={ notification.type == 'quote' }><img class="avatar" src={ notification.post.user.avatar_url + '?thumbnail&size=64' } alt="avatar"/> <virtual if={ notification.type == 'quote' }><img class="avatar" src={ notification.post.user.avatar_url + '?thumbnail&size=64' } alt="avatar"/>
<div class="text"> <div class="text">
<p><i class="fa fa-quote-left"></i>{ notification.post.user.name }</p> <p><i class="fa fa-quote-left"></i>{ notification.post.user.name }</p>
<p class="post-preview">{ getPostSummary(notification.post) }</p> <p class="post-preview">{ getPostSummary(notification.post) }</p>
</div> </div>
</div> </virtual>
<div class="main" if={ notification.type == 'follow' }><img class="avatar" src={ notification.user.avatar_url + '?thumbnail&size=64' } alt="avatar"/> <virtual if={ notification.type == 'follow' }><img class="avatar" src={ notification.user.avatar_url + '?thumbnail&size=64' } alt="avatar"/>
<div class="text"> <div class="text">
<p><i class="fa fa-user-plus"></i>{ notification.user.name }</p> <p><i class="fa fa-user-plus"></i>{ notification.user.name }</p>
</div> </div>
</div> </virtual>
<div class="main" if={ notification.type == 'reply' }><img class="avatar" src={ notification.post.user.avatar_url + '?thumbnail&size=64' } alt="avatar"/> <virtual if={ notification.type == 'reply' }><img class="avatar" src={ notification.post.user.avatar_url + '?thumbnail&size=64' } alt="avatar"/>
<div class="text"> <div class="text">
<p><i class="fa fa-reply"></i>{ notification.post.user.name }</p> <p><i class="fa fa-reply"></i>{ notification.post.user.name }</p>
<p class="post-preview">{ getPostSummary(notification.post) }</p> <p class="post-preview">{ getPostSummary(notification.post) }</p>
</div> </div>
</div> </virtual>
<div class="main" if={ notification.type == 'mention' }><img class="avatar" src={ notification.post.user.avatar_url + '?thumbnail&size=64' } alt="avatar"/> <virtual if={ notification.type == 'mention' }><img class="avatar" src={ notification.post.user.avatar_url + '?thumbnail&size=64' } alt="avatar"/>
<div class="text"> <div class="text">
<p><i class="fa fa-at"></i>{ notification.post.user.name }</p> <p><i class="fa fa-at"></i>{ notification.post.user.name }</p>
<p class="post-preview">{ getPostSummary(notification.post) }</p> <p class="post-preview">{ getPostSummary(notification.post) }</p>
</div> </div>
</div> </virtual>
<virtual if={ notification.type == 'poll_vote' }><img class="avatar" src={ notification.user.avatar_url + '?thumbnail&size=64' } alt="avatar"/>
<div class="text">
<p><i class="fa fa-pie-chart"></i>{ notification.user.name }</p>
<p class="post-ref">{ getPostSummary(notification.post) }</p>
</div>
</virtual>
<style type="stylus"> <style type="stylus">
:scope :scope
display block display block
margin 0 margin 0
padding 8px padding 8px
color #fff color #fff
overflow-wrap break-word
> .main
overflow-wrap break-word
&:after &:after
content "" content ""

View file

@ -1,40 +1,94 @@
<mk-notification class={ notification.type }> <mk-notification class={ notification.type }>
<mk-time time={ notification.created_at }></mk-time> <mk-time time={ notification.created_at }></mk-time>
<div class="main" if={ notification.type == 'like' }><a class="avatar-anchor" href={ CONFIG.url + '/' + notification.user.username }><img class="avatar" src={ notification.user.avatar_url + '?thumbnail&size=64' } alt="avatar"/></a> <virtual if={ notification.type == 'like' }>
<a class="avatar-anchor" href={ CONFIG.url + '/' + notification.user.username }>
<img class="avatar" src={ notification.user.avatar_url + '?thumbnail&size=64' } alt="avatar"/>
</a>
<div class="text"> <div class="text">
<p><i class="fa fa-thumbs-o-up"></i><a href={ CONFIG.url + '/' + notification.user.username }>{ notification.user.name }</a></p><a class="post-ref" href={ CONFIG.url + '/' + notification.post.user.username + '/' + notification.post.id }>{ getPostSummary(notification.post) }</a> <p>
<i class="fa fa-thumbs-o-up"></i>
<a href={ CONFIG.url + '/' + notification.user.username }>{ notification.user.name }</a>
</p>
<a class="post-ref" href={ CONFIG.url + '/' + notification.post.user.username + '/' + notification.post.id }>{ getPostSummary(notification.post) }</a>
</div> </div>
</div> </virtual>
<div class="main" if={ notification.type == 'repost' }><a class="avatar-anchor" href={ CONFIG.url + '/' + notification.post.user.username }><img class="avatar" src={ notification.post.user.avatar_url + '?thumbnail&size=64' } alt="avatar"/></a> <virtual if={ notification.type == 'repost' }>
<a class="avatar-anchor" href={ CONFIG.url + '/' + notification.post.user.username }>
<img class="avatar" src={ notification.post.user.avatar_url + '?thumbnail&size=64' } alt="avatar"/>
</a>
<div class="text"> <div class="text">
<p><i class="fa fa-retweet"></i><a href={ CONFIG.url + '/' + notification.post.user.username }>{ notification.post.user.name }</a></p><a class="post-ref" href={ CONFIG.url + '/' + notification.post.user.username + '/' + notification.post.id }>{ getPostSummary(notification.post.repost) }</a> <p>
<i class="fa fa-retweet"></i>
<a href={ CONFIG.url + '/' + notification.post.user.username }>{ notification.post.user.name }</a>
</p>
<a class="post-ref" href={ CONFIG.url + '/' + notification.post.user.username + '/' + notification.post.id }>{ getPostSummary(notification.post.repost) }</a>
</div> </div>
</div> </virtual>
<div class="main" if={ notification.type == 'quote' }><a class="avatar-anchor" href={ CONFIG.url + '/' + notification.post.user.username }><img class="avatar" src={ notification.post.user.avatar_url + '?thumbnail&size=64' } alt="avatar"/></a> <virtual if={ notification.type == 'quote' }>
<a class="avatar-anchor" href={ CONFIG.url + '/' + notification.post.user.username }>
<img class="avatar" src={ notification.post.user.avatar_url + '?thumbnail&size=64' } alt="avatar"/>
</a>
<div class="text"> <div class="text">
<p><i class="fa fa-quote-left"></i><a href={ CONFIG.url + '/' + notification.post.user.username }>{ notification.post.user.name }</a></p><a class="post-preview" href={ CONFIG.url + '/' + notification.post.user.username + '/' + notification.post.id }>{ getPostSummary(notification.post) }</a> <p>
<i class="fa fa-quote-left"></i>
<a href={ CONFIG.url + '/' + notification.post.user.username }>{ notification.post.user.name }</a>
</p>
<a class="post-preview" href={ CONFIG.url + '/' + notification.post.user.username + '/' + notification.post.id }>{ getPostSummary(notification.post) }</a>
</div> </div>
</div> </virtual>
<div class="main" if={ notification.type == 'follow' }><a class="avatar-anchor" href={ CONFIG.url + '/' + notification.user.username }><img class="avatar" src={ notification.user.avatar_url + '?thumbnail&size=64' } alt="avatar"/></a> <virtual if={ notification.type == 'follow' }>
<a class="avatar-anchor" href={ CONFIG.url + '/' + notification.user.username }>
<img class="avatar" src={ notification.user.avatar_url + '?thumbnail&size=64' } alt="avatar"/>
</a>
<div class="text"> <div class="text">
<p><i class="fa fa-user-plus"></i><a href={ CONFIG.url + '/' + notification.user.username }>{ notification.user.name }</a></p> <p>
<i class="fa fa-user-plus"></i>
<a href={ CONFIG.url + '/' + notification.user.username }>{ notification.user.name }</a>
</p>
</div> </div>
</div> </virtual>
<div class="main" if={ notification.type == 'reply' }><a class="avatar-anchor" href={ CONFIG.url + '/' + notification.post.user.username }><img class="avatar" src={ notification.post.user.avatar_url + '?thumbnail&size=64' } alt="avatar"/></a> <virtual if={ notification.type == 'reply' }>
<a class="avatar-anchor" href={ CONFIG.url + '/' + notification.post.user.username }>
<img class="avatar" src={ notification.post.user.avatar_url + '?thumbnail&size=64' } alt="avatar"/>
</a>
<div class="text"> <div class="text">
<p><i class="fa fa-reply"></i><a href={ CONFIG.url + '/' + notification.post.user.username }>{ notification.post.user.name }</a></p><a class="post-preview" href={ CONFIG.url + '/' + notification.post.user.username + '/' + notification.post.id }>{ getPostSummary(notification.post) }</a> <p>
<i class="fa fa-reply"></i>
<a href={ CONFIG.url + '/' + notification.post.user.username }>{ notification.post.user.name }</a>
</p>
<a class="post-preview" href={ CONFIG.url + '/' + notification.post.user.username + '/' + notification.post.id }>{ getPostSummary(notification.post) }</a>
</div> </div>
</div> </virtual>
<div class="main" if={ notification.type == 'mention' }><a class="avatar-anchor" href={ CONFIG.url + '/' + notification.post.user.username }><img class="avatar" src={ notification.post.user.avatar_url + '?thumbnail&size=64' } alt="avatar"/></a> <virtual if={ notification.type == 'mention' }>
<a class="avatar-anchor" href={ CONFIG.url + '/' + notification.post.user.username }>
<img class="avatar" src={ notification.post.user.avatar_url + '?thumbnail&size=64' } alt="avatar"/>
</a>
<div class="text"> <div class="text">
<p><i class="fa fa-at"></i><a href={ CONFIG.url + '/' + notification.post.user.username }>{ notification.post.user.name }</a></p><a class="post-preview" href={ CONFIG.url + '/' + notification.post.user.username + '/' + notification.post.id }>{ getPostSummary(notification.post) }</a> <p>
<i class="fa fa-at"></i>
<a href={ CONFIG.url + '/' + notification.post.user.username }>{ notification.post.user.name }</a>
</p>
<a class="post-preview" href={ CONFIG.url + '/' + notification.post.user.username + '/' + notification.post.id }>{ getPostSummary(notification.post) }</a>
</div> </div>
</div> </virtual>
<virtual if={ notification.type == 'poll_vote' }>
<a class="avatar-anchor" href={ CONFIG.url + '/' + notification.user.username }>
<img class="avatar" src={ notification.user.avatar_url + '?thumbnail&size=64' } alt="avatar"/>
</a>
<div class="text">
<p>
<i class="fa fa-pie-shart"></i>
<a href={ CONFIG.url + '/' + notification.user.username }>{ notification.user.name }</a>
</p>
<a class="post-ref" href={ CONFIG.url + '/' + notification.post.user.username + '/' + notification.post.id }>{ getPostSummary(notification.post) }</a>
</div>
</virtual>
<style type="stylus"> <style type="stylus">
:scope :scope
display block display block
margin 0 margin 0
padding 16px padding 16px
overflow-wrap break-word
> mk-time > mk-time
display inline display inline
@ -45,9 +99,6 @@
color rgba(0, 0, 0, 0.6) color rgba(0, 0, 0, 0.6)
font-size 12px font-size 12px
> .main
overflow-wrap break-word
&:after &:after
content "" content ""
display block display block

View file

@ -19,9 +19,11 @@
</ul> </ul>
</div> </div>
<mk-uploader ref="uploader"></mk-uploader> <mk-uploader ref="uploader"></mk-uploader>
<div ref="pollZone"></div>
<button ref="upload" onclick={ selectFile }><i class="fa fa-upload"></i></button> <button ref="upload" onclick={ selectFile }><i class="fa fa-upload"></i></button>
<button ref="drive" onclick={ selectFileFromDrive }><i class="fa fa-cloud"></i></button> <button ref="drive" onclick={ selectFileFromDrive }><i class="fa fa-cloud"></i></button>
<button class="cat" onclick={ cat }><i class="fa fa-smile-o"></i></button> <button class="cat" onclick={ cat }><i class="fa fa-smile-o"></i></button>
<button class="poll" onclick={ addPoll }><i class="fa fa-pie-chart"></i></button>
<input ref="file" type="file" accept="image/*" multiple="multiple" onchange={ changeFile }/> <input ref="file" type="file" accept="image/*" multiple="multiple" onchange={ changeFile }/>
</div> </div>
<style type="stylus"> <style type="stylus">
@ -163,6 +165,7 @@
> [ref='upload'] > [ref='upload']
> [ref='drive'] > [ref='drive']
.cat .cat
.poll
display inline-block display inline-block
padding 0 padding 0
margin 0 margin 0
@ -185,6 +188,7 @@
@wait = false @wait = false
@uploadings = [] @uploadings = []
@files = [] @files = []
@poll = null
@on \mount ~> @on \mount ~>
@refs.uploader.on \uploaded (file) ~> @refs.uploader.on \uploaded (file) ~>
@ -241,6 +245,13 @@
@trigger \change-files @files @trigger \change-files @files
@update! @update!
@add-poll = ~>
if @poll?
@poll.unmount!
@poll = null
return
@poll = riot.mount(@refs.pollZone.append-child document.create-element \mk-poll-editor).0
@post = ~> @post = ~>
@wait = true @wait = true
@ -252,6 +263,7 @@
text: @refs.text.value text: @refs.text.value
media_ids: files media_ids: files
reply_to_id: if @opts.reply? then @opts.reply.id else undefined reply_to_id: if @opts.reply? then @opts.reply.id else undefined
poll: if @poll? then @poll.get! else undefined
.then (data) ~> .then (data) ~>
@trigger \post @trigger \post
@unmount! @unmount!

View file

@ -27,6 +27,7 @@
<div class="media" if={ p.media }> <div class="media" if={ p.media }>
<mk-images-viewer images={ p.media }></mk-images-viewer> <mk-images-viewer images={ p.media }></mk-images-viewer>
</div> </div>
<mk-poll if={ p.poll } post={ p }></mk-poll>
<span class="app" if={ p.app }>via <b>{ p.app.name }</b></span> <span class="app" if={ p.app }>via <b>{ p.app.name }</b></span>
<div class="repost" if={ p.repost }><i class="fa fa-quote-right fa-flip-horizontal"></i> <div class="repost" if={ p.repost }><i class="fa fa-quote-right fa-flip-horizontal"></i>
<mk-post-preview class="repost" post={ p.repost }></mk-post-preview> <mk-post-preview class="repost" post={ p.repost }></mk-post-preview>
@ -242,6 +243,9 @@
font-size 12px font-size 12px
color #ccc color #ccc
> mk-poll
font-size 80%
> .repost > .repost
margin 8px 0 margin 8px 0

View file

@ -408,6 +408,21 @@ describe('API', () => {
done(); done();
}); });
})); }));
it('投票を添付できる', () => new Promise(async (done) => {
const me = await insertSakurako();
request('/posts/create', {
text: 'インデントするなら?',
poll: {
choices: ['スペース', 'タブ']
}
}, me).then(res => {
res.should.have.status(200);
res.body.should.be.a('object');
res.body.should.have.property('poll');
done();
});
}));
}); });
describe('posts/show', () => { describe('posts/show', () => {