Skip to content

Commit

Permalink
Merge pull request oxen-io#485 from BeaudanBrown/complete-curlys
Browse files Browse the repository at this point in the history
Enforce curlies completely and lint with new settings
  • Loading branch information
Beaudan Campbell-Brown committed Sep 5, 2019
2 parents f8cf1cb + 9beea2c commit 1cf6cca
Show file tree
Hide file tree
Showing 22 changed files with 184 additions and 64 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ module.exports = {
],

// Enforce curlies always
curly: 'error',
curly: ['error', 'all'],
'brace-style': ['error', '1tbs'],

// prevents us from accidentally checking in exclusive tests (`.only`):
'mocha/no-exclusive-tests': 'error',
Expand Down
4 changes: 3 additions & 1 deletion app/profile_images.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ const hasImage = pubKey => fs.existsSync(getImagePath(pubKey));
const getImagePath = pubKey => `${PATH}/${pubKey}.png`;
const getOrCreateImagePath = pubKey => {
// If the image doesn't exist then create it
if (!hasImage(pubKey)) return generateImage(pubKey);
if (!hasImage(pubKey)) {
return generateImage(pubKey);
}

return getImagePath(pubKey);
};
Expand Down
4 changes: 3 additions & 1 deletion js/blocked_number_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
storage.addBlockedNumber(number);

// Make sure we don't add duplicates
if (blockedNumbers.getModel(number)) return;
if (blockedNumbers.getModel(number)) {
return;
}

blockedNumbers.add({ number });
},
Expand Down
88 changes: 66 additions & 22 deletions js/models/conversations.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,9 @@
},

async updateProfileAvatar() {
if (this.isRss()) return;
if (this.isRss()) {
return;
}
const path = profileImages.getOrCreateImagePath(this.id);
await this.setProfileAvatar(path);
},
Expand Down Expand Up @@ -358,7 +360,9 @@

// Get messages with the given timestamp
_getMessagesWithTimestamp(pubKey, timestamp) {
if (this.id !== pubKey) return [];
if (this.id !== pubKey) {
return [];
}

// Go through our messages and find the one that we need to update
return this.messageCollection.models.filter(
Expand Down Expand Up @@ -414,7 +418,9 @@
// Get the pending friend requests that match the direction
// If no direction is supplied then return all pending friend requests
return messages.models.filter(m => {
if (!status.includes(m.get('friendStatus'))) return false;
if (!status.includes(m.get('friendStatus'))) {
return false;
}
return direction === null || m.get('direction') === direction;
});
},
Expand All @@ -424,7 +430,9 @@

addSingleMessage(message, setToExpire = true) {
const model = this.messageCollection.add(message, { merge: true });
if (setToExpire) model.setToExpire();
if (setToExpire) {
model.setToExpire();
}
return model;
},
format() {
Expand Down Expand Up @@ -680,7 +688,9 @@
},
async setFriendRequestStatus(newStatus) {
// Ensure that the new status is a valid FriendStatusEnum value
if (!(newStatus in Object.values(FriendRequestStatusEnum))) return;
if (!(newStatus in Object.values(FriendRequestStatusEnum))) {
return;
}
if (
this.ourNumber === this.id &&
newStatus !== FriendRequestStatusEnum.friends
Expand All @@ -698,11 +708,15 @@
async respondToAllFriendRequests(options) {
const { response, status, direction = null } = options;
// Ignore if no response supplied
if (!response) return;
if (!response) {
return;
}
const pending = await this.getFriendRequests(direction, status);
await Promise.all(
pending.map(async request => {
if (request.hasErrors()) return;
if (request.hasErrors()) {
return;
}

request.set({ friendStatus: response });
await window.Signal.Data.saveMessage(request.attributes, {
Expand Down Expand Up @@ -736,7 +750,9 @@
},
// We have accepted an incoming friend request
async onAcceptFriendRequest() {
if (this.unlockTimer) clearTimeout(this.unlockTimer);
if (this.unlockTimer) {
clearTimeout(this.unlockTimer);
}
if (this.hasReceivedFriendRequest()) {
this.setFriendRequestStatus(FriendRequestStatusEnum.friends);
await this.respondToAllFriendRequests({
Expand All @@ -752,7 +768,9 @@
if (this.isFriend()) {
return false;
}
if (this.unlockTimer) clearTimeout(this.unlockTimer);
if (this.unlockTimer) {
clearTimeout(this.unlockTimer);
}
if (this.hasSentFriendRequest()) {
this.setFriendRequestStatus(FriendRequestStatusEnum.friends);
await this.respondToAllFriendRequests({
Expand All @@ -766,9 +784,13 @@
},
async onFriendRequestTimeout() {
// Unset the timer
if (this.unlockTimer) clearTimeout(this.unlockTimer);
if (this.unlockTimer) {
clearTimeout(this.unlockTimer);
}
this.unlockTimer = null;
if (this.isFriend()) return;
if (this.isFriend()) {
return;
}

// Set the unlock timestamp to null
if (this.get('unlockTimestamp')) {
Expand Down Expand Up @@ -822,7 +844,9 @@
await this.setFriendRequestStatus(FriendRequestStatusEnum.requestSent);
},
setFriendRequestExpiryTimeout() {
if (this.isFriend()) return;
if (this.isFriend()) {
return;
}
const unlockTimestamp = this.get('unlockTimestamp');
if (unlockTimestamp && !this.unlockTimer) {
const delta = Math.max(unlockTimestamp - Date.now(), 0);
Expand Down Expand Up @@ -1080,12 +1104,18 @@
},

validateNumber() {
if (!this.id) return 'Invalid ID';
if (!this.isPrivate()) return null;
if (!this.id) {
return 'Invalid ID';
}
if (!this.isPrivate()) {
return null;
}

// Check if it's hex
const isHex = this.id.replace(/[\s]*/g, '').match(/^[0-9a-fA-F]+$/);
if (!isHex) return 'Invalid Hex ID';
if (!isHex) {
return 'Invalid Hex ID';
}

// Check if the pubkey length is 33 and leading with 05 or of length 32
const len = this.id.length;
Expand Down Expand Up @@ -1216,7 +1246,9 @@

async sendMessage(body, attachments, quote, preview) {
// Input should be blocked if there is a pending friend request
if (this.isPendingFriendRequest()) return;
if (this.isPendingFriendRequest()) {
return;
}

this.clearTypingTimers();

Expand Down Expand Up @@ -1277,7 +1309,9 @@

// If the requests didn't error then don't add a new friend request
// because one of them was sent successfully
if (friendRequestSent) return null;
if (friendRequestSent) {
return null;
}
}
await this.setFriendRequestStatus(
FriendRequestStatusEnum.pendingSend
Expand Down Expand Up @@ -1762,7 +1796,9 @@
},
async setSessionResetStatus(newStatus) {
// Ensure that the new status is a valid SessionResetEnum value
if (!(newStatus in Object.values(SessionResetEnum))) return;
if (!(newStatus in Object.values(SessionResetEnum))) {
return;
}
if (this.get('sessionResetStatus') !== newStatus) {
this.set({ sessionResetStatus: newStatus });
await window.Signal.Data.updateConversation(this.id, this.attributes, {
Expand Down Expand Up @@ -2014,7 +2050,9 @@

async setNickname(nickname) {
const trimmed = nickname && nickname.trim();
if (this.get('nickname') === trimmed) return;
if (this.get('nickname') === trimmed) {
return;
}

this.set({ nickname: trimmed });
await window.Signal.Data.updateConversation(this.id, this.attributes, {
Expand Down Expand Up @@ -2467,7 +2505,9 @@
const avatar = this.get('avatar') || this.get('profileAvatar');

if (avatar) {
if (avatar.path) return getAbsoluteAttachmentPath(avatar.path);
if (avatar.path) {
return getAbsoluteAttachmentPath(avatar.path);
}
return avatar;
}

Expand Down Expand Up @@ -2511,7 +2551,9 @@
}
return this.notifyFriendRequest(message.get('source'), 'requested');
}
if (!message.isIncoming()) return Promise.resolve();
if (!message.isIncoming()) {
return Promise.resolve();
}
const conversationId = this.id;

return ConversationController.getOrCreateAndWait(
Expand Down Expand Up @@ -2544,7 +2586,9 @@
// Notification for friend request received
async notifyFriendRequest(source, type) {
// Data validation
if (!source) throw new Error('Invalid source');
if (!source) {
throw new Error('Invalid source');
}
if (!['accepted', 'requested'].includes(type)) {
throw new Error('Type must be accepted or requested.');
}
Expand Down
36 changes: 27 additions & 9 deletions js/models/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,9 @@
getNotificationText() {
const description = this.getDescription();
if (description) {
if (this.isFriendRequest()) return `Friend Request: ${description}`;
if (this.isFriendRequest()) {
return `Friend Request: ${description}`;
}
return description;
}
if (this.get('attachments').length > 0) {
Expand Down Expand Up @@ -432,7 +434,9 @@
},

async acceptFriendRequest() {
if (this.get('friendStatus') !== 'pending') return;
if (this.get('friendStatus') !== 'pending') {
return;
}
const conversation = this.getConversation();

this.set({ friendStatus: 'accepted' });
Expand All @@ -442,7 +446,9 @@
conversation.onAcceptFriendRequest();
},
async declineFriendRequest() {
if (this.get('friendStatus') !== 'pending') return;
if (this.get('friendStatus') !== 'pending') {
return;
}
const conversation = this.getConversation();

this.set({ friendStatus: 'declined' });
Expand Down Expand Up @@ -591,7 +597,9 @@
return 'sent';
}
const calculatingPoW = this.get('calculatingPoW');
if (calculatingPoW) return 'pow';
if (calculatingPoW) {
return 'pow';
}

return 'sending';
},
Expand Down Expand Up @@ -1235,7 +1243,9 @@
return null;
},
async setCalculatingPoW() {
if (this.calculatingPoW) return;
if (this.calculatingPoW) {
return;
}

this.set({
calculatingPoW: true,
Expand All @@ -1246,7 +1256,9 @@
});
},
async setIsP2p(isP2p) {
if (_.isEqual(this.get('isP2p'), isP2p)) return;
if (_.isEqual(this.get('isP2p'), isP2p)) {
return;
}

this.set({
isP2p: !!isP2p,
Expand All @@ -1260,7 +1272,9 @@
return this.get('serverId');
},
async setServerId(serverId) {
if (_.isEqual(this.get('serverId'), serverId)) return;
if (_.isEqual(this.get('serverId'), serverId)) {
return;
}

this.set({
serverId,
Expand All @@ -1271,7 +1285,9 @@
});
},
async setIsPublic(isPublic) {
if (_.isEqual(this.get('isPublic'), isPublic)) return;
if (_.isEqual(this.get('isPublic'), isPublic)) {
return;
}

this.set({
isPublic: !!isPublic,
Expand Down Expand Up @@ -2098,7 +2114,9 @@
// Need to do this here because the conversation has already changed states
if (autoAccept) {
await conversation.notifyFriendRequest(source, 'accepted');
} else await conversation.notify(message);
} else {
await conversation.notify(message);
}
}

confirm();
Expand Down
4 changes: 3 additions & 1 deletion js/modules/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,9 @@ async function getAllSessions(id) {
// Conversation

function setifyProperty(data, propertyName) {
if (!data) return data;
if (!data) {
return data;
}
const returnData = { ...data };
if (Array.isArray(returnData[propertyName])) {
returnData[propertyName] = new Set(returnData[propertyName]);
Expand Down
4 changes: 3 additions & 1 deletion js/modules/loki_p2p_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ class LokiP2pAPI extends EventEmitter {
}

getContactP2pDetails(pubKey) {
if (!this.contactP2pDetails[pubKey]) return null;
if (!this.contactP2pDetails[pubKey]) {
return null;
}
return { ...this.contactP2pDetails[pubKey] };
}

Expand Down
4 changes: 3 additions & 1 deletion js/modules/loki_public_chat_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,9 @@ class LokiPublicChannelAPI {

// if any problems, abort out
if (res.err || !res.response) {
if (res.err) log.error(`Error ${res.err}`);
if (res.err) {
log.error(`Error ${res.err}`);
}
break;
}

Expand Down
4 changes: 3 additions & 1 deletion js/modules/loki_rss_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ class LokiRssAPI extends EventEmitter {
log.error('xmlerror', e);
success = false;
}
if (!success) return;
if (!success) {
return;
}
const feedObj = xml2json(feedDOM);
let receivedAt = new Date().getTime();

Expand Down
Loading

0 comments on commit 1cf6cca

Please sign in to comment.