Add prelude function for URL Query (#4135)

* Update string.ts

* Refactor

* Update string.ts

* Update wrap-url.ts

* Update string.ts

* Update get-static-image-url.ts

* Use querystring.stringify

* Update outbox.ts

* Back to the urlQuery

* Update followers.ts

* Update following.ts

* Update outbox.ts

* Update string.ts

* Update get-static-image-url.ts

* Update string.ts

* Update string.ts

* Separate prelude files
This commit is contained in:
Acid Chicken (硫酸鶏) 2019-02-13 23:45:35 +09:00 committed by GitHub
parent 3548290ff2
commit 4b6c113251
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 14 deletions

View file

@ -1,9 +1,11 @@
import { url as instanceUrl } from '../../config'; import { url as instanceUrl } from '../../config';
import * as url from '../../../../prelude/url';
export function getStaticImageUrl(url: string): string { export function getStaticImageUrl(baseUrl: string): string {
const u = new URL(url); const u = new URL(baseUrl);
const dummy = `${u.host}${u.pathname}`; // 拡張子がないとキャッシュしてくれないCDNがあるので const dummy = `${u.host}${u.pathname}`; // 拡張子がないとキャッシュしてくれないCDNがあるので
let result = `${instanceUrl}/proxy/${dummy}?url=${encodeURIComponent(u.href)}`; return `${instanceUrl}/proxy/${dummy}?${url.query({
result += '&static=1'; url: u.href,
return result; static: '1'
})}`;
} }

7
src/prelude/url.ts Normal file
View file

@ -0,0 +1,7 @@
import { stringify } from 'querystring';
export function query(obj: {}): string {
return stringify(Object.entries(obj)
.filter(([, v]) => Array.isArray(v) ? v.length : v !== undefined)
.reduce((a, [k, v]) => (a[k] = v, a), {} as Record<string, any>));
}

View file

@ -5,6 +5,7 @@ import $ from 'cafy';
import ID, { transform } from '../../misc/cafy-id'; import ID, { transform } from '../../misc/cafy-id';
import User from '../../models/user'; import User from '../../models/user';
import Following from '../../models/following'; import Following from '../../models/following';
import * as url from '../../prelude/url';
import { renderActivity } from '../../remote/activitypub/renderer'; import { renderActivity } from '../../remote/activitypub/renderer';
import renderOrderedCollection from '../../remote/activitypub/renderer/ordered-collection'; import renderOrderedCollection from '../../remote/activitypub/renderer/ordered-collection';
import renderOrderedCollectionPage from '../../remote/activitypub/renderer/ordered-collection-page'; import renderOrderedCollectionPage from '../../remote/activitypub/renderer/ordered-collection-page';
@ -20,7 +21,7 @@ export default async (ctx: Router.IRouterContext) => {
const userId = new ObjectID(ctx.params.user); const userId = new ObjectID(ctx.params.user);
// Get 'cursor' parameter // Get 'cursor' parameter
const [cursor = null, cursorErr] = $.optional.type(ID).get(ctx.request.query.cursor); const [cursor, cursorErr] = $.optional.type(ID).get(ctx.request.query.cursor);
// Get 'page' parameter // Get 'page' parameter
const pageErr = !$.optional.str.or(['true', 'false']).ok(ctx.request.query.page); const pageErr = !$.optional.str.or(['true', 'false']).ok(ctx.request.query.page);
@ -72,10 +73,16 @@ export default async (ctx: Router.IRouterContext) => {
const renderedFollowers = await Promise.all(followings.map(following => renderFollowUser(following.followerId))); const renderedFollowers = await Promise.all(followings.map(following => renderFollowUser(following.followerId)));
const rendered = renderOrderedCollectionPage( const rendered = renderOrderedCollectionPage(
`${partOf}?page=true${cursor ? `&cursor=${cursor}` : ''}`, `${partOf}?${url.query({
page: 'true',
cursor
})}`,
user.followersCount, renderedFollowers, partOf, user.followersCount, renderedFollowers, partOf,
null, null,
inStock ? `${partOf}?page=true&cursor=${followings[followings.length - 1]._id}` : null inStock ? `${partOf}?${url.query({
page: 'true',
cursor: followings[followings.length - 1]._id.toHexString()
})}` : null
); );
ctx.body = renderActivity(rendered); ctx.body = renderActivity(rendered);

View file

@ -5,6 +5,7 @@ import $ from 'cafy';
import ID, { transform } from '../../misc/cafy-id'; import ID, { transform } from '../../misc/cafy-id';
import User from '../../models/user'; import User from '../../models/user';
import Following from '../../models/following'; import Following from '../../models/following';
import * as url from '../../prelude/url';
import { renderActivity } from '../../remote/activitypub/renderer'; import { renderActivity } from '../../remote/activitypub/renderer';
import renderOrderedCollection from '../../remote/activitypub/renderer/ordered-collection'; import renderOrderedCollection from '../../remote/activitypub/renderer/ordered-collection';
import renderOrderedCollectionPage from '../../remote/activitypub/renderer/ordered-collection-page'; import renderOrderedCollectionPage from '../../remote/activitypub/renderer/ordered-collection-page';
@ -20,7 +21,7 @@ export default async (ctx: Router.IRouterContext) => {
const userId = new ObjectID(ctx.params.user); const userId = new ObjectID(ctx.params.user);
// Get 'cursor' parameter // Get 'cursor' parameter
const [cursor = null, cursorErr] = $.optional.type(ID).get(ctx.request.query.cursor); const [cursor, cursorErr] = $.optional.type(ID).get(ctx.request.query.cursor);
// Get 'page' parameter // Get 'page' parameter
const pageErr = !$.optional.str.or(['true', 'false']).ok(ctx.request.query.page); const pageErr = !$.optional.str.or(['true', 'false']).ok(ctx.request.query.page);
@ -72,10 +73,16 @@ export default async (ctx: Router.IRouterContext) => {
const renderedFollowees = await Promise.all(followings.map(following => renderFollowUser(following.followeeId))); const renderedFollowees = await Promise.all(followings.map(following => renderFollowUser(following.followeeId)));
const rendered = renderOrderedCollectionPage( const rendered = renderOrderedCollectionPage(
`${partOf}?page=true${cursor ? `&cursor=${cursor}` : ''}`, `${partOf}?${url.query({
page: 'true',
cursor
})}`,
user.followingCount, renderedFollowees, partOf, user.followingCount, renderedFollowees, partOf,
null, null,
inStock ? `${partOf}?page=true&cursor=${followings[followings.length - 1]._id}` : null inStock ? `${partOf}?${url.query({
page: 'true',
cursor: followings[followings.length - 1]._id.toHexString()
})}` : null
); );
ctx.body = renderActivity(rendered); ctx.body = renderActivity(rendered);

View file

@ -14,6 +14,7 @@ import renderNote from '../../remote/activitypub/renderer/note';
import renderCreate from '../../remote/activitypub/renderer/create'; import renderCreate from '../../remote/activitypub/renderer/create';
import renderAnnounce from '../../remote/activitypub/renderer/announce'; import renderAnnounce from '../../remote/activitypub/renderer/announce';
import { countIf } from '../../prelude/array'; import { countIf } from '../../prelude/array';
import * as url from '../../prelude/url';
export default async (ctx: Router.IRouterContext) => { export default async (ctx: Router.IRouterContext) => {
if (!ObjectID.isValid(ctx.params.user)) { if (!ObjectID.isValid(ctx.params.user)) {
@ -88,10 +89,20 @@ export default async (ctx: Router.IRouterContext) => {
const activities = await Promise.all(notes.map(note => packActivity(note))); const activities = await Promise.all(notes.map(note => packActivity(note)));
const rendered = renderOrderedCollectionPage( const rendered = renderOrderedCollectionPage(
`${partOf}?page=true${sinceId ? `&since_id=${sinceId}` : ''}${untilId ? `&until_id=${untilId}` : ''}`, `${partOf}?${url.query({
page: 'true',
since_id: sinceId,
until_id: untilId
})}`,
user.notesCount, activities, partOf, user.notesCount, activities, partOf,
notes.length > 0 ? `${partOf}?page=true&since_id=${notes[0]._id}` : null, notes.length ? `${partOf}?${url.query({
notes.length > 0 ? `${partOf}?page=true&until_id=${notes[notes.length - 1]._id}` : null page: 'true',
since_id: notes[0]._id.toHexString()
})}` : null,
notes.length ? `${partOf}?${url.query({
page: 'true',
until_id: notes[notes.length - 1]._id.toHexString()
})}` : null
); );
ctx.body = renderActivity(rendered); ctx.body = renderActivity(rendered);