Skip to content

Commit

Permalink
Merge and fix conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
brianlovin committed Jan 15, 2018
2 parents 40d08fe + 6cd8e12 commit 1f271e8
Show file tree
Hide file tree
Showing 64 changed files with 10,638 additions and 2,112 deletions.
Empty file added .circleci/config.yml
Empty file.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"node": true
},
"parser": "babel-eslint",
"extends": ["eslint:recommended", "plugin:promise/recommended", "plugin:flowtype/recommended"],
"extends": ["eslint:recommended", "plugin:promise/recommended", "plugin:flowtype/recommended", "plugin:react/recommended"],
"parserOptions": {
"ecmaVersion": 2017,
"ecmaFeatures": {
Expand Down Expand Up @@ -44,4 +44,4 @@
"onlyFilesWithFlowAnnotation": false
}
}
}
}
1 change: 1 addition & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.*/node_modules/.*
.*/build.*
.*/*.test.js
.*/.expo

[include]

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ queries-by-time.js
test-extend.js
stats.json
iris/.env
.expo
3 changes: 3 additions & 0 deletions flow-typed/react-native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'react-native' {
declare module.exports: any;
}
33 changes: 33 additions & 0 deletions introspection-query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const fetch = require('node-fetch');
const fs = require('fs');

fetch(`http://localhost:3001/api`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
query: `
{
__schema {
types {
kind
name
possibleTypes {
name
}
}
}
}
`,
}),
})
.then(result => result.json())
.then(result => {
fs.writeFile(
'./fragmentTypes.json',
JSON.stringify(result.data, null, 2),
err => {
if (err) console.error('Error writing fragmentTypes file', err);
console.log('Fragment types successfully extracted!');
}
);
});
89 changes: 84 additions & 5 deletions iris/migrations/seed/default.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { fromPlainText, toJSON } = require('../../../shared/draft-utils');

// 2017/01/01
// 2017 / 01 / 01;
const DATE = 1483225200000;

const MAX_ID = 'gVk5mYwccUOEKiN5vtOouqroGKo1';
Expand Down Expand Up @@ -208,19 +208,35 @@ const DEFAULT_USERS_THREADS = [

const DEFAULT_DIRECT_MESSAGE_THREADS = [
{
id: 'first-dm-thread-asfd123',
id: 'first-dm-thread-asdf123',
createdAt: new Date(DATE),
name: null,
threadLastActive: new Date(),
threadLastActive: new Date(DATE),
},
];

const DEFAULT_USERS_DIRECT_MESSAGE_THREADS = [
{
id: '1f462515-e1e7-4aff-be48-adda326df133',
createdAt: new Date(DATE),
userId: '01p2A7kDCWUjGj6zQLlMQUOSQL42',
threadId: 'ce2b4488-4c75-47e0-8ebc-2539c1e6a191',
userId: BRIAN_ID,
threadId: 'first-dm-thread-asdf123',
lastActive: new Date(DATE),
lastSeen: new Date(DATE),
receiveNotifications: true,
},
{
createdAt: new Date(DATE),
userId: BRYN_ID,
threadId: 'first-dm-thread-asdf123',
lastActive: new Date(DATE),
lastSeen: new Date(DATE),
receiveNotifications: true,
},
{
createdAt: new Date(DATE),
userId: MAX_ID,
threadId: 'first-dm-thread-asdf123',
lastActive: new Date(DATE),
lastSeen: new Date(DATE),
receiveNotifications: true,
Expand Down Expand Up @@ -356,6 +372,69 @@ const DEFAULT_MESSAGES = [
senderId: BRIAN_ID,
timestamp: new Date(DATE + 3),
},
// DM Thread
{
threadId: 'first-dm-thread-asdf123',
threadType: 'directMessageThread',
id: 'b546f27e-d842-4afe-900d-ad26ee60ca03',
attachments: [],
content: {
body: JSON.stringify(
toJSON(fromPlainText('Direct message thread message!'))
),
},
messageType: 'draftjs',
senderId: MAX_ID,
timestamp: new Date(DATE),
},
{
threadId: 'first-dm-thread-asdf123',
threadType: 'directMessageThread',
id: '2017f510-fe85-4de2-b5b2-6324567a6871',
attachments: [],
content: {
body: JSON.stringify(toJSON(fromPlainText('A second one'))),
},
messageType: 'draftjs',
senderId: BRYN_ID,
timestamp: new Date(DATE + 50000),
},
{
threadId: 'first-dm-thread-asdf123',
threadType: 'directMessageThread',
id: '965830bb-e79d-4cec-acfa-91f67b590865',
attachments: [],
content: {
body: JSON.stringify(toJSON(fromPlainText('A third one'))),
},
messageType: 'draftjs',
senderId: BRIAN_ID,
timestamp: new Date(DATE + 100000),
},
{
threadId: 'first-dm-thread-asdf123',
threadType: 'directMessageThread',
id: '77e9619d-bea2-443a-bb9c-4b2957980644',
attachments: [],
content: {
body: JSON.stringify(toJSON(fromPlainText('A fourth one'))),
},
messageType: 'draftjs',
senderId: MAX_ID,
timestamp: new Date(DATE + 200000),
},
{
threadId: 'first-dm-thread-asdf123',
threadType: 'directMessageThread',
id: '9356f7f1-4be0-4230-8f79-71a9f97f7a1f',
attachments: [],
content: {
body: JSON.stringify(toJSON(fromPlainText('A fifth one'))),
},
messageType: 'draftjs',
senderId: BRYN_ID,
timestamp: new Date(DATE + 300000),
},
];

const DEFAULT_NOTIFICATIONS = [];
Expand Down
62 changes: 46 additions & 16 deletions iris/models/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,59 @@ export const getMessage = (messageId: string): Promise<Message> => {
});
};

export const getMessages = (
type BackwardsPaginationOptions = { last?: number, before?: number | Date };

const getBackwardsMessages = (
threadId: string,
{
first = 999999,
after,
reverse = false,
}: { first?: number, after?: number, reverse?: boolean }
): Promise<Array<Message>> => {
const order = reverse
? db.desc('threadIdAndTimestamp')
: 'threadIdAndTimestamp';
{ last, before }: BackwardsPaginationOptions
) => {
return db
.table('messages')
.between([threadId, db.minval], [threadId, db.maxval], {
index: 'threadIdAndTimestamp',
})
.orderBy({ index: order })
.between(
[threadId, db.minval],
[threadId, before ? new Date(before) : db.maxval],
{ index: 'threadIdAndTimestamp' }
)
.orderBy({ index: db.desc('threadIdAndTimestamp') })
.filter(db.row.hasFields('deletedAt').not())
.skip(after || 0)
.limit(first)
.limit(last || 0)
.run();
};

type ForwardsPaginationOptions = { first?: number, after?: number | Date };

const getForwardMessages = (
threadId: string,
{ first, after }: ForwardsPaginationOptions
) => {
return db
.table('messages')
.between(
[threadId, after ? new Date(after) : db.minval],
[threadId, db.maxval],
{ index: 'threadIdAndTimestamp', leftBound: 'open', rightBound: 'closed' }
)
.orderBy({ index: 'threadIdAndTimestamp' })
.filter(db.row.hasFields('deletedAt').not())
.limit(first || 0)
.run();
};

export const getMessages = (
threadId: string,
{
first,
after,
last,
before,
}: { ...BackwardsPaginationOptions, ...ForwardsPaginationOptions }
): Promise<Array<Message>> => {
// $FlowIssue
if (last || before) return getBackwardsMessages(threadId, { last, before });
// $FlowIssue
return getForwardMessages(threadId, { first, after });
};

export const getLastMessage = (threadId: string): Promise<Message> => {
return db
.table('messages')
Expand Down
12 changes: 7 additions & 5 deletions iris/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"dependencies": {
"algoliasearch": "^3.24.7",
"apollo-engine": "^0.8.3",
"apollo-local-query": "^0.3.0",
"apollo-upload-client": "^5.1.0",
"apollo-upload-server": "^2.0.4",
Expand Down Expand Up @@ -39,11 +40,12 @@
"faker": "^4.1.0",
"find-with-regex": "^1.0.2",
"flow-typed": "^2.1.5",
"graphql": "0.10",
"graphql-date": "^1.0.3",
"graphql-log": "0.1.2",
"graphql-server-express": "0.8.x",
"graphql-server-express": "1.3.0",
"graphql-subscriptions": "0.4.x",
"graphql-tools": "^0.11.0",
"graphql-tools": "1.2.3",
"highlight.js": "^9.10.0",
"history": "^4.6.1",
"hoist-non-react-statics": "^2.3.1",
Expand All @@ -61,6 +63,7 @@
"moment": "^2.18.1",
"node-env-file": "^0.1.8",
"node-localstorage": "^1.3.0",
"now-env": "^3.0.1",
"offline-plugin": "^4.8.4",
"optics-agent": "^1.1.2",
"passport": "^0.3.2",
Expand Down Expand Up @@ -108,12 +111,11 @@
"stripe": "^4.15.0",
"striptags": "2.x",
"styled-components": "2.1.2",
"subscriptions-transport-ws": "^0.7.0",
"subscriptions-transport-ws": "0.8",
"sw-precache-webpack-plugin": "^0.11.4",
"then-queue": "^1.3.0",
"validator": "^9.0.0",
"web-push": "^3.2.2",
"now-env": "^3.0.1"
"web-push": "^3.2.2"
},
"scripts": {
"start": "NODE_ENV=production node main.js"
Expand Down
25 changes: 14 additions & 11 deletions iris/queries/directMessageThread.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,32 +54,35 @@ module.exports = {
DirectMessageThread: {
messageConnection: async (
{ id }: { id: string },
{ first = 30, after }: PaginationOptions,
{ first, after }: PaginationOptions,
{ user, loaders }: GraphQLContext
) => {
if (!user || !user.id) return null;

const canViewThread = await canViewDMThread(id, user.id, { loaders });
if (!canViewThread) return null;

const cursor = decode(after);
// Get the index from the encoded cursor, asdf234gsdf-2 => ["-2", "2"]
const lastDigits = cursor.match(/-(\d+)$/);
const lastMessageIndex =
lastDigits && lastDigits.length > 0 && parseInt(lastDigits[1], 10);
// $FlowFixMe
const cursor = parseInt(decode(after), 10);
const messages = await getMessages(id, {
first,
after: lastMessageIndex,
reverse: true,
// NOTE(@mxstbr): We used to use first/after for reverse DM pagination
// so we have to keep it that way for backwards compat, but really this
// should be last/before since it's in the other way of time
last: first || 30,
before: cursor,
});

return {
pageInfo: {
hasNextPage: messages && messages.length >= first,
// NOTE(@mxstbr): For DM threads we just assume there to be a previous page
// if the user provided a cursor and there were at least some messages
// That way they might get a false positive here if they request the messages before the last message
// but since that query returns no messages this will be false and all will be well
// (so it essentially just takes 1 “unnecessary” request to figure out whether or not there is a previous page)
hasPreviousPage: messages && messages.length > 0 && !!cursor,
},
edges: messages.map((message, index) => ({
cursor: encode(`${message.id}-${lastMessageIndex + index + 1}`),
cursor: encode(message.timestamp.getTime().toString()),
node: message,
})),
};
Expand Down
Loading

0 comments on commit 1f271e8

Please sign in to comment.