-
Notifications
You must be signed in to change notification settings - Fork 30
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 CoC update message... command #89
Open
technetos
wants to merge
3
commits into
master
Choose a base branch
from
coc-update
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,8 @@ pub(crate) fn post_message(args: Args) -> Result<(), Error> { | |
|
||
let white_check_mark = ReactionType::from("✅"); | ||
message.react(args.cx, white_check_mark)?; | ||
|
||
cache_welcome_message(args.cx, message)?; | ||
} | ||
Ok(()) | ||
} | ||
|
@@ -126,6 +128,90 @@ pub(crate) fn assign_talk_role(cx: &Context, reaction: &Reaction) -> Result<(), | |
Ok(()) | ||
} | ||
|
||
pub(crate) fn update_welcome_message(args: Args) -> Result<(), Error> { | ||
use std::str::FromStr; | ||
|
||
let new_message = &args | ||
.params | ||
.get("message") | ||
.ok_or("unable to retrieve message param")?; | ||
|
||
let welcome_message_is_not_cached = { | ||
let data = args.cx.data.read(); | ||
!data.contains::<CachedWelcomeMessage>() | ||
}; | ||
|
||
if welcome_message_is_not_cached { | ||
info!("Welcome message not cached, caching"); | ||
let conn = DB.get()?; | ||
|
||
let res = conn | ||
.build_transaction() | ||
.read_only() | ||
.run::<_, Box<dyn std::error::Error>, _>(|| { | ||
let res: Option<_> = messages::table | ||
.filter(messages::name.eq("welcome")) | ||
.first::<(i32, String, String, String)>(&conn) | ||
.optional()?; | ||
|
||
Ok(res) | ||
})?; | ||
|
||
if let Some((_, _, message_id, channel_id)) = res { | ||
let message = ChannelId::from(u64::from_str(&channel_id)?) | ||
.message(args.cx, u64::from_str(&message_id)?)?; | ||
|
||
cache_welcome_message(args.cx, message)?; | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of failing, maybe we should somehow have it post the message? Might be tricky, ergonomically speaking, because of the lack of awareness of which channel to post in. |
||
api::send_reply( | ||
&args, | ||
"No welcome message found, please post the welcome message", | ||
)?; | ||
return Ok(()); | ||
} | ||
} | ||
|
||
let mut data = args.cx.data.write(); | ||
let welcome_message = data.get_mut::<CachedWelcomeMessage>().unwrap(); | ||
welcome_message.edit(args.cx, |m| m.content(new_message))?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub(crate) struct CachedWelcomeMessage; | ||
|
||
impl TypeMapKey for CachedWelcomeMessage { | ||
type Value = Message; | ||
} | ||
|
||
pub(crate) fn cache_welcome_message(cx: &Context, message: Message) -> Result<(), Error> { | ||
let mut data = cx.data.write(); | ||
data.insert::<CachedWelcomeMessage>(message); | ||
|
||
Ok(()) | ||
} | ||
|
||
pub(crate) fn update_help(args: Args) -> Result<(), Error> { | ||
let help_string = format!( | ||
" | ||
Update the existing welcome message content | ||
``` | ||
{command} | ||
``` | ||
**Example:** | ||
``` | ||
?CoC update my new message | ||
|
||
``` | ||
will update the welcome message to \"my new message\" | ||
", | ||
command = "?CoC update message..." | ||
); | ||
|
||
api::send_reply(&args, &help_string)?; | ||
Ok(()) | ||
} | ||
|
||
pub(crate) fn help(args: Args) -> Result<(), Error> { | ||
let help_string = format!( | ||
" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Say this function was ran twice in parallel. This check could be hit twice before the message is stored in
data
, which could cause parallel attempts to populate it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are absolutely correct, nice catch, this is a problem and I will address it.