Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add capability to read custom nicknames from new thread data. #150

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions scripts/messenger.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@
const request = require('request'); // For making HTTP requests
const Settings = require('./settings');

function getThreadName(thread, participant) {
if (!Settings.properties.useCustomNicknames) {
return participant.name;
}

const nicknames = thread['custom_nickname'];
return (nicknames && nicknames[participant['fbid']]) || participant.name;
}

class Messenger {

Expand Down Expand Up @@ -64,6 +56,23 @@ class Messenger {
}
}

getUserDisplayName(userId) {
if (!Settings.properties.useCustomNicknames) {
return this.users[userId].name;
}

return this.users[userId].custom_nickname
|| this.users[userId].name;
}


saveParticipantCustomizations(customizations) {
customizations
.map(({ participant_id: id, nickname }) =>
[ id, id !== this.userId ? nickname : 'Me' ])
.map(args => this.setCustomNickname(...args));
}

// Parses a list of conversation participants into users
// Useful for adding users that are not "friends" to our database
saveParticipantsAsFriends(participants) {
Expand Down Expand Up @@ -385,6 +394,7 @@ class Messenger {
// Get name from convo participants
const participants = thread.all_participants.nodes;
const otherParticipants = participants.filter(participant => { return participant.messaging_actor.id !== this.userId;});
// TODO: Use nicknames or short names?
let threadName = otherParticipants.reduce((reducer, participant) => {
return `${reducer}${participant.messaging_actor.short_name}, `;
}, '');
Expand All @@ -411,8 +421,9 @@ class Messenger {
} else {
// No nicknames for now
for (const participant of thread.all_participants.nodes) {
if (participant.messaging_actor.id === id)
name = participant.messaging_actor.name;
if (participant.messaging_actor.id === id) {
name = this.getUserDisplayName(id);
}
}
}

Expand Down Expand Up @@ -476,6 +487,11 @@ class Messenger {
}

for (const thread of threads) {
if (thread.customization_info &&
thread.customization_info.participant_customizations) {
this.saveParticipantCustomizations(
thread.customization_info.participant_customizations);
}
this.saveParticipantsAsFriends(thread.all_participants);
}

Expand Down