Merge pull request #876 from h3poteto/iss-850

refs #850 Replace Contents with typescript
This commit is contained in:
AkiraFukushima 2019-04-14 17:32:15 +09:00 committed by GitHub
commit 9c5edbfc04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 1116 additions and 892 deletions

View File

@ -2,7 +2,7 @@ import { Response, Status, Account, Application } from 'megalodon'
import mockedMegalodon from '~/spec/mock/megalodon'
import { createLocalVue } from '@vue/test-utils'
import Vuex from 'vuex'
import Home from '~/src/renderer/store/TimelineSpace/Contents/Home'
import Home, { HomeState } from '@/store/TimelineSpace/Contents/Home'
jest.mock('megalodon')
@ -88,7 +88,7 @@ const status2: Status = {
pinned: null
}
let state: any = () => {
let state = (): HomeState => {
return {
lazyLoading: false,
heading: true,
@ -165,12 +165,6 @@ describe('Home', () => {
})
describe('lazyFetchTimeline', () => {
describe('last is null', () => {
it('should not be updated', async () => {
const result = await store.dispatch('Home/lazyFetchTimeline', null)
expect(result).toEqual(null)
})
})
describe('success', () => {
beforeAll(() => {
state = () => {

View File

@ -169,13 +169,6 @@ describe('Mentions', () => {
})
describe('lazyFetchMentions', () => {
describe('last is null', () => {
it('should not be updated', async () => {
const result = await store.dispatch('Mentions/lazyFetchMentions', null)
expect(result).toEqual(null)
})
})
describe('loading', () => {
beforeAll(() => {
state = () => {

View File

@ -1,8 +1,91 @@
import Home from '@/store/TimelineSpace/Contents/Home'
import { Account, Status, Application } from 'megalodon'
import Home, { HomeState, MUTATION_TYPES } from '@/store/TimelineSpace/Contents/Home'
const account: Account = {
id: 1,
username: 'h3poteto',
acct: 'h3poteto@pleroma.io',
display_name: 'h3poteto',
locked: false,
created_at: '2019-03-26T21:30:32',
followers_count: 10,
following_count: 10,
statuses_count: 100,
note: 'engineer',
url: 'https://pleroma.io',
avatar: '',
avatar_static: '',
header: '',
header_static: '',
emojis: [],
moved: null,
fields: null,
bot: false
}
const status1: Status = {
id: 1,
uri: 'http://example.com',
url: 'http://example.com',
account: account,
in_reply_to_id: null,
in_reply_to_account_id: null,
reblog: null,
content: 'hoge',
created_at: '2019-03-26T21:40:32',
emojis: [],
replies_count: 0,
reblogs_count: 0,
favourites_count: 0,
reblogged: null,
favourited: null,
muted: null,
sensitive: false,
spoiler_text: '',
visibility: 'public',
media_attachments: [],
mentions: [],
tags: [],
card: null,
application: {
name: 'Web'
} as Application,
language: null,
pinned: null
}
const status2: Status = {
id: 2,
uri: 'http://example.com',
url: 'http://example.com',
account: account,
in_reply_to_id: null,
in_reply_to_account_id: null,
reblog: null,
content: 'fuga',
created_at: '2019-03-26T21:40:32',
emojis: [],
replies_count: 0,
reblogs_count: 0,
favourites_count: 0,
reblogged: null,
favourited: null,
muted: null,
sensitive: false,
spoiler_text: '',
visibility: 'public',
media_attachments: [],
mentions: [],
tags: [],
card: null,
application: {
name: 'Web'
} as Application,
language: null,
pinned: null
}
describe('TimelineSpace/Contents/Home', () => {
describe('mutations', () => {
let state
let state: HomeState
beforeEach(() => {
state = {
lazyLoading: false,
@ -17,14 +100,14 @@ describe('TimelineSpace/Contents/Home', () => {
describe('changeLazyLoading', () => {
it('should be change', () => {
Home.mutations.changeLazyLoading(state, true)
Home.mutations![MUTATION_TYPES.CHANGE_LAZY_LOADING](state, true)
expect(state.lazyLoading).toEqual(true)
})
})
describe('changeHeading', () => {
it('should be change', () => {
Home.mutations.changeHeading(state, false)
Home.mutations![MUTATION_TYPES.CHANGE_HEADING](state, false)
expect(state.heading).toEqual(false)
})
})
@ -35,7 +118,7 @@ describe('TimelineSpace/Contents/Home', () => {
state = {
lazyLoading: false,
heading: true,
timeline: [5, 4, 3, 2, 1, 0],
timeline: [status1],
unreadTimeline: [],
filter: '',
showReblogs: true,
@ -43,8 +126,8 @@ describe('TimelineSpace/Contents/Home', () => {
}
})
it('should update timeline', () => {
Home.mutations.appendTimeline(state, 6)
expect(state.timeline).toEqual([6, 5, 4, 3, 2, 1, 0])
Home.mutations![MUTATION_TYPES.APPEND_TIMELINE](state, status2)
expect(state.timeline).toEqual([status2, status1])
expect(state.unreadTimeline).toEqual([])
})
})
@ -54,7 +137,7 @@ describe('TimelineSpace/Contents/Home', () => {
state = {
lazyLoading: false,
heading: false,
timeline: [5, 4, 3, 2, 1, 0],
timeline: [status1],
unreadTimeline: [],
filter: '',
showReblogs: true,
@ -62,9 +145,9 @@ describe('TimelineSpace/Contents/Home', () => {
}
})
it('should update unreadTimeline', () => {
Home.mutations.appendTimeline(state, 6)
expect(state.timeline).toEqual([5, 4, 3, 2, 1, 0])
expect(state.unreadTimeline).toEqual([6])
Home.mutations![MUTATION_TYPES.APPEND_TIMELINE](state, status2)
expect(state.timeline).toEqual([status1])
expect(state.unreadTimeline).toEqual([status2])
})
})
})
@ -74,16 +157,16 @@ describe('TimelineSpace/Contents/Home', () => {
state = {
lazyLoading: false,
heading: true,
timeline: [5, 4, 3, 2, 1, 0],
unreadTimeline: [8, 7, 6],
timeline: [status1],
unreadTimeline: [status2],
filter: '',
showReblogs: true,
showReplies: true
}
})
it('should be merged', () => {
Home.mutations.mergeTimeline(state)
expect(state.timeline).toEqual([8, 7, 6, 5, 4, 3, 2, 1, 0])
Home.mutations![MUTATION_TYPES.MERGE_TIMELINE](state, null)
expect(state.timeline).toEqual([status2, status1])
expect(state.unreadTimeline).toEqual([])
})
})
@ -93,7 +176,7 @@ describe('TimelineSpace/Contents/Home', () => {
state = {
lazyLoading: false,
heading: true,
timeline: [5, 4, 3, 2, 1, 0],
timeline: [status1],
unreadTimeline: [],
filter: '',
showReblogs: true,
@ -101,8 +184,8 @@ describe('TimelineSpace/Contents/Home', () => {
}
})
it('should be inserted', () => {
Home.mutations.insertTimeline(state, [-1, -2, -3, -4])
expect(state.timeline).toEqual([5, 4, 3, 2, 1, 0, -1, -2, -3, -4])
Home.mutations![MUTATION_TYPES.INSERT_TIMELINE](state, [status2])
expect(state.timeline).toEqual([status1, status2])
})
})
@ -112,78 +195,60 @@ describe('TimelineSpace/Contents/Home', () => {
state = {
lazyLoading: false,
heading: true,
timeline: [
{
id: 3,
reblog: null,
text: '3rd'
},
{
id: 2,
reblog: null,
text: '2nd'
},
{
id: 1,
reblog: null,
text: '1st'
},
{
id: 0,
reblog: null,
text: 'zero'
}
],
timeline: [status1, status2],
unreadTimeline: [],
filter: '',
showReblogs: true,
showReplies: true
}
})
const favouritedStatus: Status = Object.assign(status1, {
favourited: true
})
it('should be updated', () => {
Home.mutations.updateToot(state, {
id: 2,
reblog: null,
text: 'second'
})
expect(state.timeline[1]).toEqual({
id: 2,
reblog: null,
text: 'second'
})
Home.mutations![MUTATION_TYPES.UPDATE_TOOT](state, favouritedStatus)
expect(state.timeline).toEqual([favouritedStatus, status2])
})
})
describe('message is reblogged', () => {
const rebloggedStatus: Status = {
id: 3,
uri: 'http://example.com',
url: 'http://example.com',
account: account,
in_reply_to_id: null,
in_reply_to_account_id: null,
reblog: status1,
content: '',
created_at: '2019-03-31T21:40:32',
emojis: [],
replies_count: 0,
reblogs_count: 0,
favourites_count: 0,
reblogged: null,
favourited: null,
muted: null,
sensitive: false,
spoiler_text: '',
visibility: 'public',
media_attachments: [],
mentions: [],
tags: [],
card: null,
application: {
name: 'Web'
} as Application,
language: null,
pinned: null
}
const favouritedStatus: Status = Object.assign(status1, {
favourited: true
})
beforeEach(() => {
state = {
lazyLoading: false,
heading: true,
timeline: [
{
id: 3,
reblog: null,
text: '3rd'
},
{
id: 2,
reblog: null,
text: '2nd'
},
{
id: 1,
reblog: {
id: -1,
reblog: null,
text: 'reblogged message'
},
text: null
},
{
id: 0,
reblog: null,
text: 'zero'
}
],
timeline: [rebloggedStatus, status2],
unreadTimeline: [],
filter: '',
showReblogs: true,
@ -191,20 +256,9 @@ describe('TimelineSpace/Contents/Home', () => {
}
})
it('should be updated', () => {
Home.mutations.updateToot(state, {
id: -1,
reblog: null,
text: 'negative id'
})
expect(state.timeline[2]).toEqual({
id: 1,
reblog: {
id: -1,
reblog: null,
text: 'negative id'
},
text: null
})
Home.mutations![MUTATION_TYPES.UPDATE_TOOT](state, favouritedStatus)
expect(state.timeline[0].reblog).not.toBeNull()
expect(state.timeline[0].reblog!.favourited).toEqual(true)
})
})
})
@ -215,28 +269,7 @@ describe('TimelineSpace/Contents/Home', () => {
state = {
lazyLoading: false,
heading: true,
timeline: [
{
id: 3,
reblog: null,
text: '3rd'
},
{
id: 2,
reblog: null,
text: '2nd'
},
{
id: 1,
reblog: null,
text: 'first'
},
{
id: 0,
reblog: null,
text: 'zero'
}
],
timeline: [status1, status2],
unreadTimeline: [],
filter: '',
showReblogs: true,
@ -244,62 +277,47 @@ describe('TimelineSpace/Contents/Home', () => {
}
})
it('should be deleted', () => {
Home.mutations.deleteToot(state, {
id: 0,
reblog: null,
text: 'zero'
})
expect(state.timeline).toEqual([
{
id: 3,
reblog: null,
text: '3rd'
},
{
id: 2,
reblog: null,
text: '2nd'
},
{
id: 1,
reblog: null,
text: 'first'
}
])
Home.mutations![MUTATION_TYPES.DELETE_TOOT](state, status1)
expect(state.timeline).toEqual([status2])
})
})
describe('message is reblogged', () => {
beforeEach(() => {
const rebloggedStatus: Status = {
id: 3,
uri: 'http://example.com',
url: 'http://example.com',
account: account,
in_reply_to_id: null,
in_reply_to_account_id: null,
reblog: status1,
content: '',
created_at: '2019-03-31T21:40:32',
emojis: [],
replies_count: 0,
reblogs_count: 0,
favourites_count: 0,
reblogged: null,
favourited: null,
muted: null,
sensitive: false,
spoiler_text: '',
visibility: 'public',
media_attachments: [],
mentions: [],
tags: [],
card: null,
application: {
name: 'Web'
} as Application,
language: null,
pinned: null
}
state = {
lazyLoading: false,
heading: true,
timeline: [
{
id: 3,
reblog: null,
text: '3rd'
},
{
id: 2,
reblog: null,
text: '2nd'
},
{
id: 1,
reblog: {
id: -1,
reblog: null,
text: 'reblogged toot'
},
text: 'first'
},
{
id: 0,
reblog: null,
text: 'zero'
}
],
timeline: [rebloggedStatus, status2],
unreadTimeline: [],
filter: '',
showReblogs: true,
@ -307,28 +325,8 @@ describe('TimelineSpace/Contents/Home', () => {
}
})
it('should be deleted', () => {
Home.mutations.deleteToot(state, {
id: -1,
reblog: null,
text: 'reblogged toot'
})
expect(state.timeline).toEqual([
{
id: 3,
reblog: null,
text: '3rd'
},
{
id: 2,
reblog: null,
text: '2nd'
},
{
id: 0,
reblog: null,
text: 'zero'
}
])
Home.mutations![MUTATION_TYPES.DELETE_TOOT](state, status1)
expect(state.timeline).toEqual([status2])
})
})
})

View File

@ -1,8 +1,100 @@
import Mentions from '@/store/TimelineSpace/Contents/Mentions'
import { Account, Notification, Status, Application } from 'megalodon'
import Mentions, { MentionsState, MUTATION_TYPES } from '@/store/TimelineSpace/Contents/Mentions'
const account1: Account = {
id: 1,
username: 'h3poteto',
acct: 'h3poteto@pleroma.io',
display_name: 'h3poteto',
locked: false,
created_at: '2019-03-26T21:30:32',
followers_count: 10,
following_count: 10,
statuses_count: 100,
note: 'engineer',
url: 'https://pleroma.io',
avatar: '',
avatar_static: '',
header: '',
header_static: '',
emojis: [],
moved: null,
fields: null,
bot: false
}
const account2: Account = {
id: 2,
username: 'h3poteto',
acct: 'h3poteto@mstdn.io',
display_name: 'h3poteto',
locked: false,
created_at: '2019-03-26T21:30:32',
followers_count: 10,
following_count: 10,
statuses_count: 100,
note: 'engineer',
url: 'https://mstdn.io',
avatar: '',
avatar_static: '',
header: '',
header_static: '',
emojis: [],
moved: null,
fields: null,
bot: false
}
const status: Status = {
id: 1,
uri: 'http://example.com',
url: 'http://example.com',
account: account1,
in_reply_to_id: null,
in_reply_to_account_id: null,
reblog: null,
content: 'hoge',
created_at: '2019-03-26T21:40:32',
emojis: [],
replies_count: 0,
reblogs_count: 0,
favourites_count: 0,
reblogged: null,
favourited: null,
muted: null,
sensitive: false,
spoiler_text: '',
visibility: 'public',
media_attachments: [],
mentions: [],
tags: [],
card: null,
application: {
name: 'Web'
} as Application,
language: null,
pinned: null
}
const notification1: Notification = {
id: 1,
account: account2,
status: status,
type: 'favourite',
created_at: '2019-04-01T17:01:32'
}
const notification2: Notification = {
id: 2,
account: account2,
status: status,
type: 'reblog',
created_at: '2019-04-01T17:01:32'
}
describe('TimelineSpace/Contents/Mentions', () => {
describe('mutations', () => {
let state
let state: MentionsState
beforeEach(() => {
state = {
lazyLoading: false,
@ -19,14 +111,14 @@ describe('TimelineSpace/Contents/Mentions', () => {
state = {
lazyLoading: false,
heading: true,
mentions: [5, 4, 3, 2, 1],
mentions: [notification1],
unreadMentions: [],
filter: ''
}
})
it('should update mentions', () => {
Mentions.mutations.appendMentions(state, 6)
expect(state.mentions).toEqual([6, 5, 4, 3, 2, 1])
Mentions.mutations![MUTATION_TYPES.APPEND_MENTIONS](state, notification2)
expect(state.mentions).toEqual([notification2, notification1])
expect(state.unreadMentions).toEqual([])
})
})
@ -35,15 +127,15 @@ describe('TimelineSpace/Contents/Mentions', () => {
state = {
lazyLoading: false,
heading: false,
mentions: [5, 4, 3, 2, 1],
mentions: [notification1],
unreadMentions: [],
filter: ''
}
})
it('should update mentions', () => {
Mentions.mutations.appendMentions(state, 6)
expect(state.mentions).toEqual([5, 4, 3, 2, 1])
expect(state.unreadMentions).toEqual([6])
Mentions.mutations![MUTATION_TYPES.APPEND_MENTIONS](state, notification2)
expect(state.mentions).toEqual([notification1])
expect(state.unreadMentions).toEqual([notification2])
})
})
})
@ -53,14 +145,14 @@ describe('TimelineSpace/Contents/Mentions', () => {
state = {
lazyLoading: false,
heading: false,
mentions: [5, 4, 3, 2, 1],
unreadMentions: [8, 7, 6],
mentions: [notification1],
unreadMentions: [notification2],
filter: ''
}
})
it('should be merged', () => {
Mentions.mutations.mergeMentions(state)
expect(state.mentions).toEqual([8, 7, 6, 5, 4, 3, 2, 1])
Mentions.mutations![MUTATION_TYPES.MERGE_MENTIONS](state, null)
expect(state.mentions).toEqual([notification2, notification1])
expect(state.unreadMentions).toEqual([])
})
})
@ -70,14 +162,14 @@ describe('TimelineSpace/Contents/Mentions', () => {
state = {
lazyLoading: false,
heading: false,
mentions: [5, 4, 3, 2, 1],
mentions: [notification2],
unreadMentions: [],
filter: ''
}
})
it('should be inserted', () => {
Mentions.mutations.insertMentions(state, [-1, -2, -3, -4])
expect(state.mentions).toEqual([5, 4, 3, 2, 1, -1, -2, -3, -4])
Mentions.mutations![MUTATION_TYPES.INSERT_MENTIONS](state, [notification1])
expect(state.mentions).toEqual([notification2, notification1])
})
})
@ -86,28 +178,18 @@ describe('TimelineSpace/Contents/Mentions', () => {
state = {
lazyLoading: false,
heading: false,
mentions: [
{ type: 'mention', status: { id: 20, favourited: false } },
{ type: 'favourite', status: { id: 19, favourited: false } },
{ type: 'reblog', status: { id: 18, favourited: false } },
{ type: 'follow', status: { id: 17, favourited: false } },
{ type: 'mention', status: { id: 16, favourited: false } }
],
mentions: [notification2, notification1],
unreadMentions: [],
filter: ''
}
})
it('should be updated', () => {
Mentions.mutations.updateToot(state, { id: 20, favourited: true })
expect(state.mentions).toEqual(
[
{ type: 'mention', status: { id: 20, favourited: true } },
{ type: 'favourite', status: { id: 19, favourited: false } },
{ type: 'reblog', status: { id: 18, favourited: false } },
{ type: 'follow', status: { id: 17, favourited: false } },
{ type: 'mention', status: { id: 16, favourited: false } }
]
)
const favourited: Status = Object.assign(status, {
favourited: true
})
Mentions.mutations![MUTATION_TYPES.UPDATE_TOOT](state, favourited)
expect(state.mentions[0].status!.favourited).toEqual(true)
expect(state.mentions[1].status!.favourited).toEqual(true)
})
})
})

View File

@ -1,21 +1,27 @@
import SideBar, { SideBarModuleState } from './Contents/SideBar'
import Home from './Contents/Home'
import Notifications from './Contents/Notifications'
import Home, { HomeState } from './Contents/Home'
import Notifications, { NotificationsState } from './Contents/Notifications'
import Favourites from './Contents/Favourites'
import Local from './Contents/Local'
import Public from './Contents/Public'
import Local, { LocalState } from './Contents/Local'
import Public, { PublicState } from './Contents/Public'
import Search from './Contents/Search'
import Lists from './Contents/Lists'
import Hashtag from './Contents/Hashtag'
import DirectMessages from './Contents/DirectMessages'
import Mentions from './Contents/Mentions'
import DirectMessages, { DirectMessagesState } from './Contents/DirectMessages'
import Mentions, { MentionsState } from './Contents/Mentions'
import { Module } from 'vuex'
import { RootState } from '@/store'
export interface ContentsState {}
export interface ContentsModuleState extends ContentsState {
SideBar: SideBarModuleState
SideBar: SideBarModuleState,
Home: HomeState,
Notifications: NotificationsState,
Mentions: MentionsState,
DirectMessages: DirectMessagesState,
Local: LocalState,
Public: PublicState
}
const state = (): ContentsState => ({})

View File

@ -1,111 +0,0 @@
import Mastodon from 'megalodon'
const DirectMessages = {
namespaced: true,
state: {
lazyLoading: false,
heading: true,
timeline: [],
unreadTimeline: [],
filter: ''
},
mutations: {
changeLazyLoading (state, value) {
state.lazyLoading = value
},
changeHeading (state, value) {
state.heading = value
},
appendTimeline (state, update) {
if (state.heading) {
state.timeline = [update].concat(state.timeline)
} else {
state.unreadTimeline = [update].concat(state.unreadTimeline)
}
},
updateTimeline (state, messages) {
state.timeline = messages
},
mergeTimeline (state) {
state.timeline = state.unreadTimeline.slice(0, 80).concat(state.timeline)
state.unreadTimeline = []
},
insertTimeline (state, messages) {
state.timeline = state.timeline.concat(messages)
},
archiveTimeline (state) {
state.timeline = state.timeline.slice(0, 40)
},
clearTimeline (state) {
state.timeline = []
state.unreadTimeline = []
},
updateToot (state, message) {
// Replace target message in DirectMessagesTimeline and notifications
state.timeline = state.timeline.map((toot) => {
if (toot.id === message.id) {
return message
} else if (toot.reblog !== null && toot.reblog.id === message.id) {
// When user reblog/favourite a reblogged toot, target message is a original toot.
// So, a message which is received now is original toot.
const reblog = {
reblog: message
}
return Object.assign(toot, reblog)
} else {
return toot
}
})
},
deleteToot (state, message) {
state.timeline = state.timeline.filter((toot) => {
if (toot.reblog !== null && toot.reblog.id === message.id) {
return false
} else {
return toot.id !== message.id
}
})
},
changeFilter (state, filter) {
state.filter = filter
}
},
actions: {
fetchTimeline ({ commit, rootState }) {
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.get('/timelines/direct', { limit: 40 })
.then(res => {
commit('updateTimeline', res.data)
return res.data
})
},
lazyFetchTimeline ({ state, commit, rootState }, last) {
if (last === undefined || last === null) {
return Promise.resolve(null)
}
if (state.lazyLoading) {
return Promise.resolve(null)
}
commit('changeLazyLoading', true)
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.get('/timelines/direct', { max_id: last.id, limit: 40 })
.then(res => {
commit('changeLazyLoading', false)
commit('insertTimeline', res.data)
return res.data
})
.catch(err => {
commit('changeLazyLoading', false)
throw err
})
}
}
}
export default DirectMessages

View File

@ -0,0 +1,135 @@
import Mastodon, { Status, Response } from 'megalodon'
import { Module, MutationTree, ActionTree } from 'vuex'
import { RootState } from '@/store'
export interface DirectMessagesState {
lazyLoading: boolean,
heading: boolean,
timeline: Array<Status>,
unreadTimeline: Array<Status>,
filter: string
}
const state = (): DirectMessagesState => ({
lazyLoading: false,
heading: true,
timeline: [],
unreadTimeline: [],
filter: ''
})
export const MUTATION_TYPES = {
CHANGE_LAZY_LOADING: 'changeLazyLoading',
CHANGE_HEADING: 'changeHeading',
APPEND_TIMELINE: 'appendTimeline',
UPDATE_TIMELINE: 'updateTimeline',
MERGE_TIMELINE: 'mergeTimeline',
INSERT_TIMELINE: 'insertTimeline',
ARCHIVE_TIMELINE: 'archiveTimeline',
CLEAR_TIMELINE: 'clearTimeline',
UPDATE_TOOT: 'updateToot',
DELETE_TOOT: 'deleteToot',
CHANGE_FILTER: 'changeFilter'
}
const mutations: MutationTree<DirectMessagesState> = {
[MUTATION_TYPES.CHANGE_LAZY_LOADING]: (state, value: boolean) => {
state.lazyLoading = value
},
[MUTATION_TYPES.CHANGE_HEADING]: (state, value: boolean) => {
state.heading = value
},
[MUTATION_TYPES.APPEND_TIMELINE]: (state, update: Status) => {
if (state.heading) {
state.timeline = [update].concat(state.timeline)
} else {
state.unreadTimeline = [update].concat(state.unreadTimeline)
}
},
[MUTATION_TYPES.UPDATE_TIMELINE]: (state, messages: Array<Status>) => {
state.timeline = messages
},
[MUTATION_TYPES.MERGE_TIMELINE]: (state) => {
state.timeline = state.unreadTimeline.slice(0, 80).concat(state.timeline)
state.unreadTimeline = []
},
[MUTATION_TYPES.INSERT_TIMELINE]: (state, messages: Array<Status>) => {
state.timeline = state.timeline.concat(messages)
},
[MUTATION_TYPES.ARCHIVE_TIMELINE]: (state) => {
state.timeline = state.timeline.slice(0, 40)
},
[MUTATION_TYPES.CLEAR_TIMELINE]: (state) => {
state.timeline = []
state.unreadTimeline = []
},
[MUTATION_TYPES.UPDATE_TOOT]: (state, message: Status) => {
// Replace target message in DirectMessagesTimeline and notifications
state.timeline = state.timeline.map((toot) => {
if (toot.id === message.id) {
return message
} else if (toot.reblog !== null && toot.reblog.id === message.id) {
// When user reblog/favourite a reblogged toot, target message is a original toot.
// So, a message which is received now is original toot.
const reblog = {
reblog: message
}
return Object.assign(toot, reblog)
} else {
return toot
}
})
},
[MUTATION_TYPES.DELETE_TOOT]: (state, message: Status) => {
state.timeline = state.timeline.filter((toot) => {
if (toot.reblog !== null && toot.reblog.id === message.id) {
return false
} else {
return toot.id !== message.id
}
})
},
[MUTATION_TYPES.CHANGE_FILTER]: (state, filter: string) => {
state.filter = filter
}
}
const actions: ActionTree<DirectMessagesState, RootState> = {
fetchTimeline: async ({ commit, rootState }): Promise<Array<Status>> => {
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken!,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
const res: Response<Array<Status>> = await client.get<Array<Status>>('/timelines/direct', { limit: 40 })
commit(MUTATION_TYPES.UPDATE_TIMELINE, res.data)
return res.data
},
lazyFetchTimeline: ({ state, commit, rootState }, lastStatus: Status): Promise<Array<Status> | null> => {
if (state.lazyLoading) {
return Promise.resolve(null)
}
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, true)
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken!,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.get<Array<Status>>('/timelines/direct', { max_id: lastStatus.id, limit: 40 })
.then(res => {
commit(MUTATION_TYPES.INSERT_TIMELINE, res.data)
return res.data
})
.finally(() => {
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, false)
})
}
}
const DirectMessages: Module<DirectMessagesState, RootState> = {
namespaced: true,
state: state,
mutations: mutations,
actions: actions
}
export default DirectMessages

View File

@ -1,117 +0,0 @@
import Mastodon from 'megalodon'
const Home = {
namespaced: true,
state: {
lazyLoading: false,
heading: true,
timeline: [],
unreadTimeline: [],
filter: '',
showReblogs: true,
showReplies: true
},
mutations: {
changeLazyLoading (state, value) {
state.lazyLoading = value
},
changeHeading (state, value) {
state.heading = value
},
appendTimeline (state, update) {
if (state.heading) {
state.timeline = [update].concat(state.timeline)
} else {
state.unreadTimeline = [update].concat(state.unreadTimeline)
}
},
updateTimeline (state, messages) {
state.timeline = messages
},
mergeTimeline (state) {
state.timeline = state.unreadTimeline.slice(0, 80).concat(state.timeline)
state.unreadTimeline = []
},
insertTimeline (state, messages) {
state.timeline = state.timeline.concat(messages)
},
archiveTimeline (state) {
state.timeline = state.timeline.slice(0, 40)
},
clearTimeline (state) {
state.timeline = []
state.unreadTimeline = []
},
updateToot (state, message) {
// Replace target message in homeTimeline and notifications
state.timeline = state.timeline.map((toot) => {
if (toot.id === message.id) {
return message
} else if (toot.reblog !== null && toot.reblog.id === message.id) {
// When user reblog/favourite a reblogged toot, target message is a original toot.
// So, a message which is received now is original toot.
const reblog = {
reblog: message
}
return Object.assign(toot, reblog)
} else {
return toot
}
})
},
deleteToot (state, message) {
state.timeline = state.timeline.filter((toot) => {
if (toot.reblog !== null && toot.reblog.id === message.id) {
return false
} else {
return toot.id !== message.id
}
})
},
changeFilter (state, filter) {
state.filter = filter
},
showReblogs (state, visible) {
state.showReblogs = visible
},
showReplies (state, visible) {
state.showReplies = visible
}
},
actions: {
fetchTimeline ({ commit, rootState }) {
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.get('/timelines/home', { limit: 40 })
.then(res => {
commit('updateTimeline', res.data)
return res.data
})
},
lazyFetchTimeline ({ state, commit, rootState }, last) {
if (last === undefined || last === null) {
return Promise.resolve(null)
}
if (state.lazyLoading) {
return Promise.resolve(null)
}
commit('changeLazyLoading', true)
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.get('/timelines/home', { max_id: last.id, limit: 40 })
.then(res => {
commit('insertTimeline', res.data)
return res.data
})
.finally(() => {
commit('changeLazyLoading', false)
})
}
}
}
export default Home

View File

@ -0,0 +1,146 @@
import Mastodon, { Status, Response } from 'megalodon'
import { Module, MutationTree, ActionTree } from 'vuex'
import { RootState } from '@/store'
export interface HomeState {
lazyLoading: boolean,
heading: boolean,
showReblogs: boolean,
showReplies: boolean,
timeline: Array<Status>,
unreadTimeline: Array<Status>,
filter: string
}
const state = (): HomeState => ({
lazyLoading: false,
heading: true,
timeline: [],
unreadTimeline: [],
filter: '',
showReblogs: true,
showReplies: true
})
export const MUTATION_TYPES = {
CHANGE_LAZY_LOADING: 'changeLazyLoading',
CHANGE_HEADING: 'changeHeading',
APPEND_TIMELINE: 'appendTimeline',
UPDATE_TIMELINE: 'updateTimeline',
MERGE_TIMELINE: 'mergeTimeline',
INSERT_TIMELINE: 'insertTimeline',
ARCHIVE_TIMELINE: 'archiveTimeline',
CLEAR_TIMELINE: 'clearTimeline',
UPDATE_TOOT: 'updateToot',
DELETE_TOOT: 'deleteToot',
CHANGE_FILTER: 'changeFilter',
SHOW_REBLOGS: 'showReblogs',
SHOW_REPLIES: 'showReplies'
}
const mutations: MutationTree<HomeState> = {
[MUTATION_TYPES.CHANGE_LAZY_LOADING]: (state, value: boolean) => {
state.lazyLoading = value
},
[MUTATION_TYPES.CHANGE_HEADING]: (state, value: boolean) => {
state.heading = value
},
[MUTATION_TYPES.APPEND_TIMELINE]: (state, update: Status) => {
if (state.heading) {
state.timeline = [update].concat(state.timeline)
} else {
state.unreadTimeline = [update].concat(state.unreadTimeline)
}
},
[MUTATION_TYPES.UPDATE_TIMELINE]: (state, messages: Array<Status>) => {
state.timeline = messages
},
[MUTATION_TYPES.MERGE_TIMELINE]: (state) => {
state.timeline = state.unreadTimeline.slice(0, 80).concat(state.timeline)
state.unreadTimeline = []
},
[MUTATION_TYPES.INSERT_TIMELINE]: (state, messages: Array<Status>) => {
state.timeline = state.timeline.concat(messages)
},
[MUTATION_TYPES.ARCHIVE_TIMELINE]: (state) => {
state.timeline = state.timeline.slice(0, 40)
},
[MUTATION_TYPES.CLEAR_TIMELINE]: (state) => {
state.timeline = []
state.unreadTimeline = []
},
[MUTATION_TYPES.UPDATE_TOOT]: (state, message: Status) => {
// Replace target message in homeTimeline and notifications
state.timeline = state.timeline.map((toot) => {
if (toot.id === message.id) {
return message
} else if (toot.reblog !== null && toot.reblog.id === message.id) {
// When user reblog/favourite a reblogged toot, target message is a original toot.
// So, a message which is received now is original toot.
const reblog = {
reblog: message
}
return Object.assign(toot, reblog)
} else {
return toot
}
})
},
[MUTATION_TYPES.DELETE_TOOT]: (state, message: Status) => {
state.timeline = state.timeline.filter((toot) => {
if (toot.reblog !== null && toot.reblog.id === message.id) {
return false
} else {
return toot.id !== message.id
}
})
},
[MUTATION_TYPES.CHANGE_FILTER]: (state, filter: string) => {
state.filter = filter
},
[MUTATION_TYPES.SHOW_REBLOGS]: (state, visible: boolean) => {
state.showReblogs = visible
},
[MUTATION_TYPES.SHOW_REPLIES]: (state, visible: boolean) => {
state.showReplies = visible
}
}
const actions: ActionTree<HomeState, RootState> = {
fetchTimeline: async ({ commit, rootState }) => {
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken!,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
const res: Response<Array<Status>> = await client.get<Array<Status>>('/timelines/home', { limit: 40 })
commit(MUTATION_TYPES.UPDATE_TIMELINE, res.data)
return res.data
},
lazyFetchTimeline: async ({ state, commit, rootState }, lastStatus: Status): Promise<Array<Status> | null> => {
if (state.lazyLoading) {
return Promise.resolve(null)
}
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, true)
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken!,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.get<Array<Status>>('/timelines/home', { max_id: lastStatus.id, limit: 40 })
.then(res => {
commit(MUTATION_TYPES.INSERT_TIMELINE, res.data)
return res.data
})
.finally(() => {
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, false)
})
}
}
const Home: Module<HomeState, RootState> = {
namespaced: true,
state: state,
mutations: mutations,
actions: actions
}
export default Home

View File

@ -1,110 +0,0 @@
import Mastodon from 'megalodon'
const Local = {
namespaced: true,
state: {
timeline: [],
unreadTimeline: [],
lazyLoading: false,
heading: true,
filter: ''
},
mutations: {
changeHeading (state, value) {
state.heading = value
},
appendTimeline (state, update) {
if (state.heading) {
state.timeline = [update].concat(state.timeline)
} else {
state.unreadTimeline = [update].concat(state.unreadTimeline)
}
},
updateTimeline (state, messages) {
state.timeline = messages
},
mergeTimeline (state) {
state.timeline = state.unreadTimeline.slice(0, 80).concat(state.timeline)
state.unreadTimeline = []
},
insertTimeline (state, messages) {
state.timeline = state.timeline.concat(messages)
},
archiveTimeline (state) {
state.timeline = state.timeline.slice(0, 40)
},
clearTimeline (state) {
state.timeline = []
state.unreadTimeline = []
},
updateToot (state, message) {
state.timeline = state.timeline.map((toot) => {
if (toot.id === message.id) {
return message
} else if (toot.reblog !== null && toot.reblog.id === message.id) {
// When user reblog/favourite a reblogged toot, target message is a original toot.
// So, a message which is received now is original toot.
const reblog = {
reblog: message
}
return Object.assign(toot, reblog)
} else {
return toot
}
})
},
deleteToot (state, message) {
state.timeline = state.timeline.filter((toot) => {
if (toot.reblog !== null && toot.reblog.id === message.id) {
return false
} else {
return toot.id !== message.id
}
})
},
changeLazyLoading (state, value) {
state.lazyLoading = value
},
changeFilter (state, filter) {
state.filter = filter
}
},
actions: {
fetchLocalTimeline ({ commit, rootState }) {
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.get('/timelines/public', { limit: 40, local: true })
.then(res => {
commit('updateTimeline', res.data)
return res.data
})
},
lazyFetchTimeline ({ state, commit, rootState }, last) {
if (last === undefined || last === null) {
return Promise.resolve(null)
}
if (state.lazyLoading) {
return Promise.resolve(null)
}
commit('changeLazyLoading', true)
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.get('/timelines/public', { max_id: last.id, limit: 40, local: true })
.then(res => {
commit('changeLazyLoading', false)
commit('insertTimeline', res.data)
return res.data
})
.catch(err => {
commit('changeLazyLoading', false)
throw err
})
}
}
}
export default Local

View File

@ -0,0 +1,133 @@
import Mastodon, { Status, Response } from 'megalodon'
import { Module, MutationTree, ActionTree } from 'vuex'
import { RootState } from '@/store'
export interface LocalState {
timeline: Array<Status>,
unreadTimeline: Array<Status>,
lazyLoading: boolean,
heading: boolean,
filter: string
}
const state = (): LocalState => ({
timeline: [],
unreadTimeline: [],
lazyLoading: false,
heading: true,
filter: ''
})
export const MUTATION_TYPES = {
CHANGE_HEADING: 'changeHeading',
APPEND_TIMELINE: 'appendTimeline',
UPDATE_TIMELINE: 'updateTimeline',
MERGE_TIMELINE: 'mergeTimeline',
INSERT_TIMELINE: 'insertTimeline',
ARCHIVE_TIMELINE: 'archiveTimeline',
CLEAR_TIMELINE: 'clearTimeline',
UPDATE_TOOT: 'updateToot',
DELETE_TOOT: 'deleteToot',
CHANGE_LAZY_LOADING: 'changeLazyLoading',
CHANGE_FILTER: 'changeFilter'
}
const mutations: MutationTree<LocalState> = {
[MUTATION_TYPES.CHANGE_HEADING]: (state, value: boolean) => {
state.heading = value
},
[MUTATION_TYPES.APPEND_TIMELINE]: (state, update: Status) => {
if (state.heading) {
state.timeline = [update].concat(state.timeline)
} else {
state.unreadTimeline = [update].concat(state.unreadTimeline)
}
},
[MUTATION_TYPES.UPDATE_TIMELINE]: (state, messages: Array<Status>) => {
state.timeline = messages
},
[MUTATION_TYPES.MERGE_TIMELINE]: (state) => {
state.timeline = state.unreadTimeline.slice(0, 80).concat(state.timeline)
state.unreadTimeline = []
},
[MUTATION_TYPES.INSERT_TIMELINE]: (state, messages: Array<Status>) => {
state.timeline = state.timeline.concat(messages)
},
[MUTATION_TYPES.ARCHIVE_TIMELINE]: (state) => {
state.timeline = state.timeline.slice(0, 40)
},
[MUTATION_TYPES.CLEAR_TIMELINE]: (state) => {
state.timeline = []
state.unreadTimeline = []
},
[MUTATION_TYPES.UPDATE_TOOT]: (state, message: Status) => {
state.timeline = state.timeline.map((toot) => {
if (toot.id === message.id) {
return message
} else if (toot.reblog !== null && toot.reblog.id === message.id) {
// When user reblog/favourite a reblogged toot, target message is a original toot.
// So, a message which is received now is original toot.
const reblog = {
reblog: message
}
return Object.assign(toot, reblog)
} else {
return toot
}
})
},
[MUTATION_TYPES.DELETE_TOOT]: (state, message: Status) => {
state.timeline = state.timeline.filter((toot) => {
if (toot.reblog !== null && toot.reblog.id === message.id) {
return false
} else {
return toot.id !== message.id
}
})
},
[MUTATION_TYPES.CHANGE_LAZY_LOADING]: (state, value: boolean) => {
state.lazyLoading = value
},
[MUTATION_TYPES.CHANGE_FILTER]: (state, filter: string) => {
state.filter = filter
}
}
const actions: ActionTree<LocalState, RootState> = {
fetchLocalTimeline: async ({ commit, rootState }) => {
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken!,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
const res: Response<Array<Status>> = await client.get<Array<Status>>('/timelines/public', { limit: 40, local: true })
commit(MUTATION_TYPES.UPDATE_TIMELINE, res.data)
return res.data
},
lazyFetchTimeline: async ({ state, commit, rootState }, lastStatus: Status): Promise<Array<Status> | null> => {
if (state.lazyLoading) {
return Promise.resolve(null)
}
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, true)
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken!,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.get<Array<Status>>('/timelines/public', { max_id: lastStatus.id, limit: 40, local: true })
.then(res => {
commit(MUTATION_TYPES.INSERT_TIMELINE, res.data)
return res.data
})
.finally(() => {
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, false)
})
}
}
const Local: Module<LocalState, RootState> = {
namespaced: true,
state: state,
mutations: mutations,
actions: actions
}
export default Local

View File

@ -1,100 +0,0 @@
import Mastodon from 'megalodon'
const Mentions = {
namespaced: true,
state: {
lazyLoading: false,
heading: true,
mentions: [],
unreadMentions: [],
filter: ''
},
mutations: {
changeLazyLoading (state, value) {
state.lazyLoading = value
},
changeHeading (state, value) {
state.heading = value
},
appendMentions (state, update) {
if (state.heading) {
state.mentions = [update].concat(state.mentions)
} else {
state.unreadMentions = [update].concat(state.unreadMentions)
}
},
updateMentions (state, messages) {
state.mentions = messages
},
mergeMentions (state) {
state.mentions = state.unreadMentions.slice(0, 80).concat(state.mentions)
state.unreadMentions = []
},
insertMentions (state, messages) {
state.mentions = state.mentions.concat(messages)
},
archiveMentions (state) {
state.mentions = state.mentions.slice(0, 40)
},
clearMentions (state) {
state.mentions = []
state.unreadMentions = []
},
updateToot (state, message) {
state.mentions = state.mentions.map((mention) => {
if (mention.type === 'mention' && mention.status.id === message.id) {
const status = {
status: message
}
return Object.assign(mention, status)
} else {
return mention
}
})
},
changeFilter (state, filter) {
state.filter = filter
}
},
actions: {
fetchMentions ({ commit, rootState }) {
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.get('/notifications', { limit: 30, exclude_types: ['follow', 'favourite', 'reblog'] })
.then(res => {
commit('updateMentions', res.data)
return res.data
})
},
lazyFetchMentions ({ state, commit, rootState }, last) {
if (last === undefined || last === null) {
return Promise.resolve(null)
}
if (state.lazyLoading) {
return Promise.resolve(null)
}
commit('changeLazyLoading', true)
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.get('/notifications', { max_id: last.id, limit: 30, exclude_types: ['follow', 'favourite', 'reblog'] })
.then(res => {
commit('insertMentions', res.data)
return res.data
})
.finally(() => {
commit('changeLazyLoading', false)
})
}
},
getters: {
mentions (state) {
return state.mentions.filter(mention => mention.type === 'mention')
}
}
}
export default Mentions

View File

@ -0,0 +1,126 @@
import Mastodon, { Notification, Response } from 'megalodon'
import { Module, MutationTree, ActionTree, GetterTree } from 'vuex'
import { RootState } from '@/store'
export interface MentionsState {
lazyLoading: boolean,
heading: boolean,
mentions: Array<Notification>,
unreadMentions: Array<Notification>,
filter: string
}
const state = (): MentionsState => ({
lazyLoading: false,
heading: true,
mentions: [],
unreadMentions: [],
filter: ''
})
export const MUTATION_TYPES = {
CHANGE_LAZY_LOADING: 'changeLazyLoading',
CHANGE_HEADING: 'changeHeading',
APPEND_MENTIONS: 'appendMentions',
UPDATE_MENTIONS: 'updateMentions',
MERGE_MENTIONS: 'mergeMentions',
INSERT_MENTIONS: 'insertMentions',
ARCHIVE_MENTIONS: 'archiveMentions',
CLEAR_MENTIONS: 'clearMentions',
UPDATE_TOOT: 'updateToot',
CHANGE_FILTER: 'changeFilter'
}
const mutations: MutationTree<MentionsState> = {
[MUTATION_TYPES.CHANGE_LAZY_LOADING]: (state, value: boolean) => {
state.lazyLoading = value
},
[MUTATION_TYPES.CHANGE_HEADING]: (state, value: boolean) => {
state.heading = value
},
[MUTATION_TYPES.APPEND_MENTIONS]: (state, update: Notification) => {
if (state.heading) {
state.mentions = [update].concat(state.mentions)
} else {
state.unreadMentions = [update].concat(state.unreadMentions)
}
},
[MUTATION_TYPES.UPDATE_MENTIONS]: (state, messages: Array<Notification>) => {
state.mentions = messages
},
[MUTATION_TYPES.MERGE_MENTIONS]: (state) => {
state.mentions = state.unreadMentions.slice(0, 80).concat(state.mentions)
state.unreadMentions = []
},
[MUTATION_TYPES.INSERT_MENTIONS]: (state, messages: Array<Notification>) => {
state.mentions = state.mentions.concat(messages)
},
[MUTATION_TYPES.ARCHIVE_MENTIONS]: (state) => {
state.mentions = state.mentions.slice(0, 40)
},
[MUTATION_TYPES.CLEAR_MENTIONS]: (state) => {
state.mentions = []
state.unreadMentions = []
},
[MUTATION_TYPES.UPDATE_TOOT]: (state, message: Notification) => {
state.mentions = state.mentions.map((mention) => {
if (mention.status !== null && mention.status.id === message.id) {
const status = {
status: message
}
return Object.assign(mention, status)
} else {
return mention
}
})
},
[MUTATION_TYPES.CHANGE_FILTER]: (state, filter: string) => {
state.filter = filter
}
}
const actions: ActionTree<MentionsState, RootState> = {
fetchMentions: async ({ commit, rootState }): Promise<Array<Notification>> => {
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken!,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
const res: Response<Array<Notification>> = await client.get<Array<Notification>>('/notifications', { limit: 30, exclude_types: ['follow', 'favourite', 'reblog'] })
commit(MUTATION_TYPES.UPDATE_MENTIONS, res.data)
return res.data
},
lazyFetchMentions: async ({ state, commit, rootState }, lastMention: Notification): Promise<Array<Notification> | null> => {
if (state.lazyLoading) {
return Promise.resolve(null)
}
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, true)
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken!,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.get<Array<Notification>>('/notifications', { max_id: lastMention.id, limit: 30, exclude_types: ['follow', 'favourite', 'reblog'] })
.then(res => {
commit(MUTATION_TYPES.INSERT_MENTIONS, res.data)
return res.data
})
.finally(() => {
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, false)
})
}
}
const getters: GetterTree<MentionsState, RootState> = {
mentions: (state) => {
return state.mentions.filter(mention => mention.type === 'mention')
}
}
const Mentions: Module<MentionsState, RootState> = {
namespaced: true,
state: state,
mutations: mutations,
actions: actions,
getters: getters
}
export default Mentions

View File

@ -1,102 +0,0 @@
import { ipcRenderer } from 'electron'
import Mastodon from 'megalodon'
const Notifications = {
namespaced: true,
state: {
lazyLoading: false,
heading: true,
notifications: [],
unreadNotifications: [],
filter: ''
},
mutations: {
changeLazyLoading (state, value) {
state.lazyLoading = value
},
changeHeading (state, value) {
state.heading = value
},
appendNotifications (state, notification) {
if (state.heading) {
state.notifications = [notification].concat(state.notifications)
} else {
state.unreadNotifications = [notification].concat(state.unreadNotifications)
}
},
updateNotifications (state, notifications) {
state.notifications = notifications
},
mergeNotifications (state) {
state.notifications = state.unreadNotifications.slice(0, 80).concat(state.notifications)
state.unreadNotifications = []
},
insertNotifications (state, notifications) {
state.notifications = state.notifications.concat(notifications)
},
updateToot (state, message) {
state.notifications = state.notifications.map((notification) => {
// I want to update toot only mention.
// Because Toot component don't use status information when other patterns.
if (notification.type === 'mention' && notification.status.id === message.id) {
const status = {
status: message
}
return Object.assign(notification, status)
} else {
return notification
}
})
},
clearNotifications (state) {
state.notifications = []
},
archiveNotifications (state) {
state.notifications = state.notifications.slice(0, 30)
},
changeFilter (state, filter) {
state.filter = filter
}
},
actions: {
fetchNotifications ({ commit, rootState }) {
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.get('/notifications', { limit: 30 })
.then(res => {
commit('updateNotifications', res.data)
return res.data
})
},
lazyFetchNotifications ({ state, commit, rootState }, last) {
if (last === undefined || last === null) {
return Promise.resolve(null)
}
if (state.lazyLoading) {
return Promise.resolve(null)
}
commit('changeLazyLoading', true)
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.get('/notifications', { max_id: last.id, limit: 30 })
.then(res => {
commit('changeLazyLoading', false)
commit('insertNotifications', res.data)
return res.data
})
.catch(err => {
commit('changeLazyLoading', false)
throw err
})
},
resetBadge () {
ipcRenderer.send('reset-badge')
}
}
}
export default Notifications

View File

@ -0,0 +1,125 @@
import { ipcRenderer } from 'electron'
import Mastodon, { Notification, Status, Response } from 'megalodon'
import { Module, MutationTree, ActionTree } from 'vuex'
import { RootState } from '@/store'
export interface NotificationsState {
lazyLoading: boolean,
heading: boolean,
notifications: Array<Notification>,
unreadNotifications: Array<Notification>,
filter: string
}
const state = (): NotificationsState => ({
lazyLoading: false,
heading: true,
notifications: [],
unreadNotifications: [],
filter: ''
})
export const MUTATION_TYPES = {
CHANGE_LAZY_LOADING: 'changeLazyLoading',
CHANGE_HEADING: 'changeHeading',
APPEND_NOTIFICATIONS: 'appendNotifications',
UPDATE_NOTIFICATIONS: 'updateNotifications',
MERGE_NOTIFICATIONS: 'mergeNotifications',
INSERT_NOTIFICATIONS: 'insertNotifications',
UPDATE_TOOT: 'updateToot',
CLEAR_NOTIFICATIONS: 'clearNotifications',
ARCHIVE_NOTIFICATIONS: 'archiveNotifications',
CHANGE_FILTER: 'changeFilter'
}
const mutations: MutationTree<NotificationsState> = {
[MUTATION_TYPES.CHANGE_LAZY_LOADING]: (state, value: boolean) => {
state.lazyLoading = value
},
[MUTATION_TYPES.CHANGE_HEADING]: (state, value: boolean) => {
state.heading = value
},
[MUTATION_TYPES.APPEND_NOTIFICATIONS]: (state, notification: Notification) => {
if (state.heading) {
state.notifications = [notification].concat(state.notifications)
} else {
state.unreadNotifications = [notification].concat(state.unreadNotifications)
}
},
[MUTATION_TYPES.UPDATE_NOTIFICATIONS]: (state, notifications: Array<Notification>) => {
state.notifications = notifications
},
[MUTATION_TYPES.MERGE_NOTIFICATIONS]: (state) => {
state.notifications = state.unreadNotifications.slice(0, 80).concat(state.notifications)
state.unreadNotifications = []
},
[MUTATION_TYPES.INSERT_NOTIFICATIONS]: (state, notifications: Array<Notification>) => {
state.notifications = state.notifications.concat(notifications)
},
[MUTATION_TYPES.UPDATE_TOOT]: (state, message: Status) => {
state.notifications = state.notifications.map((notification) => {
// I want to update toot only mention.
// Because Toot component don't use status information when other patterns.
if (notification.status !== null && notification.status.id === message.id) {
const status = {
status: message
}
return Object.assign(notification, status)
} else {
return notification
}
})
},
[MUTATION_TYPES.CLEAR_NOTIFICATIONS]: (state) => {
state.notifications = []
},
[MUTATION_TYPES.ARCHIVE_NOTIFICATIONS]: (state) => {
state.notifications = state.notifications.slice(0, 30)
},
[MUTATION_TYPES.CHANGE_FILTER]: (state, filter: string) => {
state.filter = filter
}
}
const actions: ActionTree<NotificationsState, RootState> = {
fetchNotifications: async ({ commit, rootState }): Promise<Array<Notification>> => {
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken!,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
const res: Response<Array<Notification>> = await client.get<Array<Notification>>('/notifications', { limit: 30 })
commit(MUTATION_TYPES.UPDATE_NOTIFICATIONS, res.data)
return res.data
},
lazyFetchNotifications: ({ state, commit, rootState }, lastNotification: Notification): Promise<Array<Notification> | null> => {
if (state.lazyLoading) {
return Promise.resolve(null)
}
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, true)
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken!,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.get<Array<Notification>>('/notifications', { max_id: lastNotification.id, limit: 30 })
.then(res => {
commit(MUTATION_TYPES.INSERT_NOTIFICATIONS, res.data)
return res.data
})
.finally(() => {
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, false)
})
},
resetBadge: () => {
ipcRenderer.send('reset-badge')
}
}
const Notifications: Module<NotificationsState, RootState> = {
namespaced: true,
state: state,
mutations: mutations,
actions: actions
}
export default Notifications

View File

@ -1,109 +0,0 @@
import Mastodon from 'megalodon'
const Public = {
namespaced: true,
state: {
timeline: [],
unreadTimeline: [],
lazyLoading: false,
heading: true,
filter: ''
},
mutations: {
changeHeading (state, value) {
state.heading = value
},
appendTimeline (state, update) {
if (state.heading) {
state.timeline = [update].concat(state.timeline)
} else {
state.unreadTimeline = [update].concat(state.unreadTimeline)
}
},
updateTimeline (state, messages) {
state.timeline = messages
},
mergeTimeline (state) {
state.timeline = state.unreadTimeline.slice(0, 80).concat(state.timeline)
state.unreadTimeline = []
},
insertTimeline (state, messages) {
state.timeline = state.timeline.concat(messages)
},
archiveTimeline (state) {
state.timeline = state.timeline.slice(0, 40)
},
clearTimeline (state) {
state.timeline = []
state.unreadTimeline = []
},
updateToot (state, message) {
state.timeline = state.timeline.map((toot) => {
if (toot.id === message.id) {
return message
} else if (toot.reblog !== null && toot.reblog.id === message.id) {
// When user reblog/favourite a reblogged toot, target message is a original toot.
// So, a message which is received now is original toot.
const reblog = {
reblog: message
}
return Object.assign(toot, reblog)
} else {
return toot
}
})
},
deleteToot (state, message) {
state.timeline = state.timeline.filter((toot) => {
if (toot.reblog !== null && toot.reblog.id === message.id) {
return false
} else {
return toot.id !== message.id
}
})
},
changeLazyLoading (state, value) {
state.lazyLoading = value
},
changeFilter (state, filter) {
state.filter = filter
}
},
actions: {
fetchPublicTimeline ({ commit, rootState }) {
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.get('/timelines/public', { limit: 40 })
.then(res => {
commit('updateTimeline', res.data)
})
},
lazyFetchTimeline ({ state, commit, rootState }, last) {
if (last === undefined || last === null) {
return Promise.resolve(null)
}
if (state.lazyLoading) {
return Promise.resolve(null)
}
commit('changeLazyLoading', true)
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.get('/timelines/public', { max_id: last.id, limit: 40 })
.then(res => {
commit('insertTimeline', res.data)
commit('changeLazyLoading', false)
return res.data
})
.catch(err => {
commit('changeLazyLoading', false)
throw err
})
}
}
}
export default Public

View File

@ -0,0 +1,132 @@
import Mastodon, { Status, Response } from 'megalodon'
import { Module, MutationTree, ActionTree } from 'vuex'
import { RootState } from '@/store'
export interface PublicState {
timeline: Array<Status>,
unreadTimeline: Array<Status>,
lazyLoading: boolean,
heading: boolean,
filter: string
}
const state = (): PublicState => ({
timeline: [],
unreadTimeline: [],
lazyLoading: false,
heading: true,
filter: ''
})
export const MUTATION_TYPES = {
CHANGE_HEADING: 'changeHeading',
APPEND_TIMELINE: 'appendTimeline',
UPDATE_TIMELINE: 'updateTimeline',
MERGE_TIMELINE: 'mergeTimeline',
INSERT_TIMELINE: 'insertTimeline',
ARCHIVE_TIMELINE: 'archiveTimeine',
CLEAR_TIMELINE: 'clearTimeline',
UPDATE_TOOT: 'updateToot',
DELETE_TOOT: 'deleteToot',
CHANGE_LAZY_LOADING: 'changeLazyLoading',
CHANGE_FILTER: 'changeFilter'
}
const mutations: MutationTree<PublicState> = {
[MUTATION_TYPES.CHANGE_HEADING]: (state, value: boolean) => {
state.heading = value
},
[MUTATION_TYPES.APPEND_TIMELINE]: (state, update: Status) => {
if (state.heading) {
state.timeline = [update].concat(state.timeline)
} else {
state.unreadTimeline = [update].concat(state.unreadTimeline)
}
},
[MUTATION_TYPES.UPDATE_TIMELINE]: (state, messages: Array<Status>) => {
state.timeline = messages
},
[MUTATION_TYPES.MERGE_TIMELINE]: (state) => {
state.timeline = state.unreadTimeline.slice(0, 80).concat(state.timeline)
state.unreadTimeline = []
},
[MUTATION_TYPES.INSERT_TIMELINE]: (state, messages: Array<Status>) => {
state.timeline = state.timeline.concat(messages)
},
[MUTATION_TYPES.ARCHIVE_TIMELINE]: (state) => {
state.timeline = state.timeline.slice(0, 40)
},
[MUTATION_TYPES.CLEAR_TIMELINE]: (state) => {
state.timeline = []
state.unreadTimeline = []
},
[MUTATION_TYPES.UPDATE_TOOT]: (state, message: Status) => {
state.timeline = state.timeline.map((toot) => {
if (toot.id === message.id) {
return message
} else if (toot.reblog !== null && toot.reblog.id === message.id) {
// When user reblog/favourite a reblogged toot, target message is a original toot.
// So, a message which is received now is original toot.
const reblog = {
reblog: message
}
return Object.assign(toot, reblog)
} else {
return toot
}
})
},
[MUTATION_TYPES.DELETE_TOOT]: (state, message: Status) => {
state.timeline = state.timeline.filter((toot) => {
if (toot.reblog !== null && toot.reblog.id === message.id) {
return false
} else {
return toot.id !== message.id
}
})
},
[MUTATION_TYPES.CHANGE_LAZY_LOADING]: (state, value: boolean) => {
state.lazyLoading = value
},
[MUTATION_TYPES.CHANGE_FILTER]: (state, filter: string) => {
state.filter = filter
}
}
const actions: ActionTree<PublicState, RootState> = {
fetchPublicTimeline: async ({ commit, rootState }) => {
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken!,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
const res: Response<Array<Status>> = await client.get<Array<Status>>('/timelines/public', { limit: 40 })
commit(MUTATION_TYPES.UPDATE_TIMELINE, res.data)
},
lazyFetchTimeline: ({ state, commit, rootState }, lastStatus: Status): Promise<Array<Status> | null> => {
if (state.lazyLoading) {
return Promise.resolve(null)
}
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, true)
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken!,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.get<Array<Status>>('/timelines/public', { max_id: lastStatus.id, limit: 40 })
.then(res => {
commit(MUTATION_TYPES.INSERT_TIMELINE, res.data)
return res.data
})
.finally(() => {
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, false)
})
}
}
const Public: Module<PublicState, RootState> = {
namespaced: true,
state: state,
mutations: mutations,
actions: actions
}
export default Public

View File

@ -1,6 +1,7 @@
import Mastodon, { Status, Response } from 'megalodon'
import { Module, MutationTree, ActionTree } from 'vuex'
import { RootState } from '@/store'
import { LoadPosition } from '~src/types/load_position'
export interface TimelineState {
timeline: Array<Status>,
@ -95,11 +96,7 @@ const actions: ActionTree<TimelineState, RootState> = {
commit(MUTATION_TYPES.UPDATE_TIMELINE, res.data)
return res.data
},
lazyFetchTimeline: async ({ state, commit, rootState }, info: any): Promise<null> => {
const last = info.last
if (last === undefined || last === null) {
return Promise.resolve(null)
}
lazyFetchTimeline: async ({ state, commit, rootState }, loadPosition: LoadPosition): Promise<null> => {
if (state.lazyLoading) {
return Promise.resolve(null)
}
@ -109,7 +106,7 @@ const actions: ActionTree<TimelineState, RootState> = {
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
try {
const res: Response<Array<Status>> = await client.get<Array<Status>>(`/accounts/${info.account.id}/statuses`, { max_id: last.id, limit: 40 })
const res: Response<Array<Status>> = await client.get<Array<Status>>(`/accounts/${loadPosition.account.id}/statuses`, { max_id: loadPosition.status.id, limit: 40 })
commit(MUTATION_TYPES.INSERT_TIMELINE, res.data)
} finally {
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, false)

View File

@ -0,0 +1,6 @@
import { Status, Account } from 'megalodon'
export interface LoadPosition {
status: Status,
account: Account
}