aboutsummaryrefslogtreecommitdiff
path: root/src/modules
diff options
context:
space:
mode:
Diffstat (limited to 'src/modules')
-rw-r--r--src/modules/statuses.js52
-rw-r--r--src/modules/users.js88
2 files changed, 89 insertions, 51 deletions
diff --git a/src/modules/statuses.js b/src/modules/statuses.js
index 491d0024..e4528520 100644
--- a/src/modules/statuses.js
+++ b/src/modules/statuses.js
@@ -4,45 +4,58 @@ import apiService from '../services/api/api.service.js'
export const defaultState = {
allStatuses: [],
+ allStatusesObject: {},
maxId: 0,
notifications: [],
favorites: new Set(),
timelines: {
mentions: {
statuses: [],
+ statusesObject: {},
faves: [],
visibleStatuses: [],
+ visibleStatusesObject: {},
newStatusCount: 0,
maxId: 0,
minVisibleId: 0,
- loading: false
+ loading: false,
+ error: false
},
public: {
statuses: [],
+ statusesObject: {},
faves: [],
visibleStatuses: [],
+ visibleStatusesObject: {},
newStatusCount: 0,
maxId: 0,
minVisibleId: 0,
- loading: false
+ loading: false,
+ error: false
},
publicAndExternal: {
statuses: [],
+ statusesObject: {},
faves: [],
visibleStatuses: [],
+ visibleStatusesObject: {},
newStatusCount: 0,
maxId: 0,
minVisibleId: 0,
- loading: false
+ loading: false,
+ error: false
},
friends: {
statuses: [],
+ statusesObject: {},
faves: [],
visibleStatuses: [],
+ visibleStatusesObject: {},
newStatusCount: 0,
maxId: 0,
minVisibleId: 0,
- loading: false
+ loading: false,
+ error: false
}
}
}
@@ -87,8 +100,9 @@ export const findMaxId = (...args) => {
return (maxBy(flatten(args), 'id') || {}).id
}
-const mergeOrAdd = (arr, item) => {
- const oldItem = find(arr, {id: item.id})
+const mergeOrAdd = (arr, obj, item) => {
+ const oldItem = obj[item.id]
+
if (oldItem) {
// We already have this, so only merge the new info.
merge(oldItem, item)
@@ -99,6 +113,7 @@ const mergeOrAdd = (arr, item) => {
// This is a new item, prepare it
prepareStatus(item)
arr.push(item)
+ obj[item.id] = item
return {item, new: true}
}
}
@@ -118,6 +133,7 @@ const addNewStatuses = (state, { statuses, showImmediately = false, timeline, us
}
const allStatuses = state.allStatuses
+ const allStatusesObject = state.allStatusesObject
const timelineObject = state.timelines[timeline]
// Set the maxId to the new id if it's larger.
@@ -127,7 +143,7 @@ const addNewStatuses = (state, { statuses, showImmediately = false, timeline, us
}
const addStatus = (status, showImmediately, addToTimeline = true) => {
- const result = mergeOrAdd(allStatuses, status)
+ const result = mergeOrAdd(allStatuses, allStatusesObject, status)
status = result.item
if (result.new) {
@@ -143,7 +159,7 @@ const addNewStatuses = (state, { statuses, showImmediately = false, timeline, us
// Add the mention to the mentions timeline
if (timelineObject !== mentions) {
- mergeOrAdd(mentions.statuses, status)
+ mergeOrAdd(mentions.statuses, mentions.statusesObject, status)
mentions.newStatusCount += 1
sortTimeline(mentions)
@@ -157,13 +173,13 @@ const addNewStatuses = (state, { statuses, showImmediately = false, timeline, us
let resultForCurrentTimeline
// Some statuses should only be added to the global status repository.
if (timeline && addToTimeline) {
- resultForCurrentTimeline = mergeOrAdd(timelineObject.statuses, status)
+ resultForCurrentTimeline = mergeOrAdd(timelineObject.statuses, timelineObject.statusesObject, status)
}
if (timeline && showImmediately) {
// Add it directly to the visibleStatuses, don't change
// newStatusCount
- mergeOrAdd(timelineObject.visibleStatuses, status)
+ mergeOrAdd(timelineObject.visibleStatuses, timelineObject.visibleStatusesObject, status)
} else if (timeline && addToTimeline && resultForCurrentTimeline.new) {
// Just change newStatuscount
timelineObject.newStatusCount += 1
@@ -260,26 +276,31 @@ export const mutations = {
oldTimeline.newStatusCount = 0
oldTimeline.visibleStatuses = slice(oldTimeline.statuses, 0, 50)
+ oldTimeline.visibleStatusesObject = {}
+ each(oldTimeline.visibleStatuses, (status) => { oldTimeline.visibleStatusesObject[status.id] = status })
},
setFavorited (state, { status, value }) {
- const newStatus = find(state.allStatuses, status)
+ const newStatus = state.allStatusesObject[status.id]
newStatus.favorited = value
},
setRetweeted (state, { status, value }) {
- const newStatus = find(state.allStatuses, status)
+ const newStatus = state.allStatusesObject[status.id]
newStatus.repeated = value
},
setDeleted (state, { status }) {
- const newStatus = find(state.allStatuses, status)
+ const newStatus = state.allStatusesObject[status.id]
newStatus.deleted = true
},
setLoading (state, { timeline, value }) {
state.timelines[timeline].loading = value
},
setNsfw (state, { id, nsfw }) {
- const newStatus = find(state.allStatuses, { id })
+ const newStatus = state.allStatusesObject[id]
newStatus.nsfw = nsfw
},
+ setError (state, { timeline, value }) {
+ state.timelines[timeline].error = value
+ },
markNotificationsAsSeen (state, notifications) {
each(notifications, (notification) => {
notification.seen = true
@@ -293,6 +314,9 @@ const statuses = {
addNewStatuses ({ rootState, commit }, { statuses, showImmediately = false, timeline = false, noIdUpdate = false }) {
commit('addNewStatuses', { statuses, showImmediately, timeline, noIdUpdate, user: rootState.users.currentUser })
},
+ setError ({ rootState, commit }, { timeline, value }) {
+ commit('setError', { timeline, value })
+ },
deleteStatus ({ rootState, commit }, status) {
commit('setDeleted', { status })
apiService.deleteStatus({ id: status.id, credentials: rootState.users.currentUser.credentials })
diff --git a/src/modules/users.js b/src/modules/users.js
index 31731880..22e0133c 100644
--- a/src/modules/users.js
+++ b/src/modules/users.js
@@ -1,11 +1,11 @@
import backendInteractorService from '../services/backend_interactor_service/backend_interactor_service.js'
-import { compact, map, each, find, merge } from 'lodash'
+import { compact, map, each, merge } from 'lodash'
import { set } from 'vue'
// TODO: Unify with mergeOrAdd in statuses.js
-export const mergeOrAdd = (arr, item) => {
+export const mergeOrAdd = (arr, obj, item) => {
if (!item) { return false }
- const oldItem = find(arr, {id: item.id})
+ const oldItem = obj[item.id]
if (oldItem) {
// We already have this, so only merge the new info.
merge(oldItem, item)
@@ -13,13 +13,14 @@ export const mergeOrAdd = (arr, item) => {
} else {
// This is a new item, prepare it
arr.push(item)
+ obj[item.id] = item
return {item, new: true}
}
}
export const mutations = {
setMuted (state, { user: {id}, muted }) {
- const user = find(state.users, {id})
+ const user = state.usersObject[id]
set(user, 'muted', muted)
},
setCurrentUser (state, user) {
@@ -32,17 +33,18 @@ export const mutations = {
state.loggingIn = false
},
addNewUsers (state, users) {
- each(users, (user) => mergeOrAdd(state.users, user))
+ each(users, (user) => mergeOrAdd(state.users, state.usersObject, user))
},
setUserForStatus (state, status) {
- status.user = find(state.users, status.user)
+ status.user = state.usersObject[status.user.id]
}
}
export const defaultState = {
currentUser: false,
loggingIn: false,
- users: []
+ users: [],
+ usersObject: {}
}
const users = {
@@ -65,40 +67,52 @@ const users = {
})
},
loginUser (store, userCredentials) {
- const commit = store.commit
- commit('beginLogin')
- store.rootState.api.backendInteractor.verifyCredentials(userCredentials)
- .then((response) => {
- if (response.ok) {
- response.json()
- .then((user) => {
- user.credentials = userCredentials
- commit('setCurrentUser', user)
- commit('addNewUsers', [user])
+ return new Promise((resolve, reject) => {
+ const commit = store.commit
+ commit('beginLogin')
+ store.rootState.api.backendInteractor.verifyCredentials(userCredentials)
+ .then((response) => {
+ if (response.ok) {
+ response.json()
+ .then((user) => {
+ user.credentials = userCredentials
+ commit('setCurrentUser', user)
+ commit('addNewUsers', [user])
- // Set our new backend interactor
- commit('setBackendInteractor', backendInteractorService(userCredentials))
+ // Set our new backend interactor
+ commit('setBackendInteractor', backendInteractorService(userCredentials))
- // Start getting fresh tweets.
- store.dispatch('startFetching', 'friends')
+ // Start getting fresh tweets.
+ store.dispatch('startFetching', 'friends')
- // Get user mutes and follower info
- store.rootState.api.backendInteractor.fetchMutes().then((mutedUsers) => {
- each(mutedUsers, (user) => { user.muted = true })
- store.commit('addNewUsers', mutedUsers)
- })
+ // Get user mutes and follower info
+ store.rootState.api.backendInteractor.fetchMutes().then((mutedUsers) => {
+ each(mutedUsers, (user) => { user.muted = true })
+ store.commit('addNewUsers', mutedUsers)
+ })
- // Fetch our friends
- store.rootState.api.backendInteractor.fetchFriends()
- .then((friends) => commit('addNewUsers', friends))
- })
- }
- commit('endLogin')
- })
- .catch((error) => {
- console.log(error)
- commit('endLogin')
- })
+ // Fetch our friends
+ store.rootState.api.backendInteractor.fetchFriends()
+ .then((friends) => commit('addNewUsers', friends))
+ })
+ } else {
+ // Authentication failed
+ commit('endLogin')
+ if (response.status === 401) {
+ reject('Wrong username or password')
+ } else {
+ reject('An error occurred, please try again')
+ }
+ }
+ commit('endLogin')
+ resolve()
+ })
+ .catch((error) => {
+ console.log(error)
+ commit('endLogin')
+ reject('Failed to connect to server, try again')
+ })
+ })
}
}
}